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