Merge maintenance-work branch (now with less errors!):
[lhc/web/wiklou.git] / maintenance / refreshLinks.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @ingroup Maintenance
19 */
20
21 require_once( "Maintenance.php" );
22
23 class RefreshLinks extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->mDescription = "Refresh link tables";
27 $this->addOption( 'dfn-only', 'Delete links from nonexistent articles only' );
28 $this->addOption( 'new-only', 'Only affect articles with just a single edit' );
29 $this->addOption( 'redirects-only', 'Only fix redirects, not all links' );
30 $this->addOption( 'old-redirects-only', 'Only fix redirects with no redirect table entry' );
31 $this->addOption( 'm', 'Maximum replication lag', false, true );
32 $this->addOption( 'e', 'Last page id to refresh', false, true );
33 $this->addArgs( array( 'start' => true ) );
34 $this->setBatchSize( 100 );
35 }
36
37 public function execute() {
38 if( !$this->hasOption( 'dfn-only' ) ) {
39 $start = $this->getArg( 0, 1 );
40 $new = $this->getOption( 'new-only', false );
41 $max = $this->getOption( 'm', false );
42 $end = $this->getOption( 'e', 0 );
43 $redir = $this->getOption( 'redirects-only', false );
44 $oldRedir = $this->getOption( 'old-redirects-only', false );
45 $this->doRefreshLinks( $start, $new, $max, $end, $redir, $oldRedir );
46 }
47 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
48 }
49
50 /**
51 * Do the actual link refreshing.
52 * @param $start int Page_id to start from
53 * @param $newOnly bool Only do pages with 1 edit
54 * @param $maxLag int Max DB replication lag
55 * @param $end int Page_id to stop at
56 * @param $redirectsOnly bool Only fix redirects
57 * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
58 */
59 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
60 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
61 global $wgUser, $wgParser, $wgUseTidy;
62
63 $reportingInterval = 100;
64 $dbr = wfGetDB( DB_SLAVE );
65 $start = intval( $start );
66
67 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
68 $wgUser->setOption('math', MW_MATH_SOURCE);
69
70 # Don't generate extension images (e.g. Timeline)
71 if( method_exists( $wgParser, "clearTagHooks" ) ) {
72 $wgParser->clearTagHooks();
73 }
74
75 # Don't use HTML tidy
76 $wgUseTidy = false;
77
78 $what = $redirectsOnly ? "redirects" : "links";
79
80 if( $oldRedirectsOnly ) {
81 # This entire code path is cut-and-pasted from below. Hurrah.
82 $res = $dbr->query(
83 "SELECT page_id ".
84 "FROM page ".
85 "LEFT JOIN redirect ON page_id=rd_from ".
86 "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
87 ($end == 0 ? "page_id >= $start"
88 : "page_id BETWEEN $start AND $end"),
89 __METHOD__
90 );
91 $num = $dbr->numRows( $res );
92 $this->output( "Refreshing $num old redirects from $start...\n" );
93
94 while( $row = $dbr->fetchObject( $res ) ) {
95 if ( !( ++$i % $reportingInterval ) ) {
96 $this->output( "$i\n" );
97 wfWaitForSlaves( $maxLag );
98 }
99 $this->fixRedirect( $row->page_id );
100 }
101 } elseif( $newOnly ) {
102 $this->output( "Refreshing $what from " );
103 $res = $dbr->select( 'page',
104 array( 'page_id' ),
105 array(
106 'page_is_new' => 1,
107 "page_id >= $start" ),
108 __METHOD__
109 );
110 $num = $dbr->numRows( $res );
111 $this->output( "$num new articles...\n" );
112
113 $i = 0;
114 while ( $row = $dbr->fetchObject( $res ) ) {
115 if ( !( ++$i % $reportingInterval ) ) {
116 $this->output( "$i\n" );
117 wfWaitForSlaves( $maxLag );
118 }
119 if($redirectsOnly)
120 $this->fixRedirect( $row->page_id );
121 else
122 $this->fixLinksFromArticle( $row->page_id );
123 }
124 } else {
125 $this->output( "Refreshing $what table.\n" );
126 if ( !$end ) {
127 $end = $dbr->selectField( 'page', 'max(page_id)', false );
128 }
129 $this->output( "Starting from page_id $start of $end.\n" );
130
131 for ($id = $start; $id <= $end; $id++) {
132
133 if ( !($id % $reportingInterval) ) {
134 $this->output( "$id\n" );
135 wfWaitForSlaves( $maxLag );
136 }
137 if($redirectsOnly)
138 $this->fixRedirect( $id );
139 else
140 $this->fixLinksFromArticle( $id );
141 }
142 }
143 }
144
145 /**
146 * Update the redirect entry for a given page
147 * @param $id int The page_id of the redirect
148 */
149 private function fixRedirect( $id ){
150 global $wgTitle, $wgArticle;
151
152 $wgTitle = Title::newFromID( $id );
153 $dbw = wfGetDB( DB_MASTER );
154
155 if ( is_null( $wgTitle ) ) {
156 return;
157 }
158 $wgArticle = new Article($wgTitle);
159
160 $rt = $wgArticle->followRedirect();
161
162 if($rt == false || !is_object($rt))
163 return;
164
165 $wgArticle->updateRedirectOn($dbw,$rt);
166 }
167
168 /**
169 * Run LinksUpdate for all links on a given page_id
170 * @param $id int The page_id
171 */
172 private function fixLinksFromArticle( $id ) {
173 global $wgTitle, $wgParser;
174
175 $wgTitle = Title::newFromID( $id );
176 $dbw = wfGetDB( DB_MASTER );
177
178 $linkCache =& LinkCache::singleton();
179 $linkCache->clear();
180
181 if ( is_null( $wgTitle ) ) {
182 return;
183 }
184 $dbw->begin();
185
186 $revision = Revision::newFromTitle( $wgTitle );
187 if ( !$revision ) {
188 return;
189 }
190
191 $options = new ParserOptions;
192 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
193 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
194 $update->doUpdate();
195 $dbw->immediateCommit();
196 }
197
198 /*
199 * Removes non-existing links from pages from pagelinks, imagelinks,
200 * categorylinks, templatelinks and externallinks tables.
201 *
202 * @param $maxLag
203 * @param $batchSize The size of deletion batches
204 *
205 * @author Merlijn van Deen <valhallasw@arctus.nl>
206 */
207 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
208 wfWaitForSlaves( $maxLag );
209
210 $dbw = wfGetDB( DB_MASTER );
211
212 $lb = wfGetLBFactory()->newMainLB();
213 $dbr = $lb->getConnection( DB_SLAVE );
214 $dbr->bufferResults( false );
215
216 $linksTables = array( // table name => page_id field
217 'pagelinks' => 'pl_from',
218 'imagelinks' => 'il_from',
219 'categorylinks' => 'cl_from',
220 'templatelinks' => 'tl_from',
221 'externallinks' => 'el_from',
222 );
223
224 foreach ( $linksTables as $table => $field ) {
225 $this->output( "Retrieving illegal entries from $table... " );
226
227 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
228 $results = $dbr->select( array( $table, 'page' ),
229 $field,
230 array('page_id' => null ),
231 __METHOD__,
232 'DISTINCT',
233 array( 'page' => array( 'LEFT JOIN', "$field=page_id"))
234 );
235
236 $counter = 0;
237 $list = array();
238 $this->output( "0.." );
239
240 foreach( $results as $row ) {
241 $counter++;
242 $list[] = $row->$field;
243 if ( ( $counter % $batchSize ) == 0 ) {
244 wfWaitForSlaves(5);
245 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
246
247 $this->output( $counter . ".." );
248 $list = array();
249 }
250 }
251 $this->output( $counter );
252 if (count($list) > 0) {
253 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
254 }
255 $this->output( "\n" );
256 }
257 $lb->closeAll();
258 }
259 }
260
261 $maintClass = 'RefreshLinks';
262 require_once( DO_MAINTENANCE );