Revert "merged master"
[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 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 /**
27 * Maintenance script to refresh link tables.
28 *
29 * @ingroup Maintenance
30 */
31 class RefreshLinks extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Refresh link tables";
35 $this->addOption( 'dfn-only', 'Delete links from nonexistent articles only' );
36 $this->addOption( 'new-only', 'Only affect articles with just a single edit' );
37 $this->addOption( 'redirects-only', 'Only fix redirects, not all links' );
38 $this->addOption( 'old-redirects-only', 'Only fix redirects with no redirect table entry' );
39 $this->addOption( 'm', 'Maximum replication lag', false, true );
40 $this->addOption( 'e', 'Last page id to refresh', false, true );
41 $this->addArg( 'start', 'Page_id to start from, default 1', false );
42 $this->setBatchSize( 100 );
43 }
44
45 public function execute() {
46 $max = $this->getOption( 'm', 0 );
47 if ( !$this->hasOption( 'dfn-only' ) ) {
48 $start = $this->getArg( 0, 1 );
49 $new = $this->getOption( 'new-only', false );
50 $end = $this->getOption( 'e', 0 );
51 $redir = $this->getOption( 'redirects-only', false );
52 $oldRedir = $this->getOption( 'old-redirects-only', false );
53 $this->doRefreshLinks( $start, $new, $max, $end, $redir, $oldRedir );
54 }
55 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
56 }
57
58 /**
59 * Do the actual link refreshing.
60 * @param $start int Page_id to start from
61 * @param $newOnly bool Only do pages with 1 edit
62 * @param $maxLag int Max DB replication lag
63 * @param $end int Page_id to stop at
64 * @param $redirectsOnly bool Only fix redirects
65 * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
66 */
67 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
68 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
69 global $wgParser, $wgUseTidy;
70
71 $reportingInterval = 100;
72 $dbr = wfGetDB( DB_SLAVE );
73 $start = intval( $start );
74
75 // Give extensions a chance to optimize settings
76 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
77
78 # Don't generate extension images (e.g. Timeline)
79 $wgParser->clearTagHooks();
80
81 # Don't use HTML tidy
82 $wgUseTidy = false;
83
84 $what = $redirectsOnly ? "redirects" : "links";
85
86 if ( $oldRedirectsOnly ) {
87 # This entire code path is cut-and-pasted from below. Hurrah.
88
89 $conds = array(
90 "page_is_redirect=1",
91 "rd_from IS NULL"
92 );
93
94 if ( $end == 0 ) {
95 $conds[] = "page_id >= $start";
96 } else {
97 $conds[] = "page_id BETWEEN $start AND $end";
98 }
99
100 $res = $dbr->select(
101 array( 'page', 'redirect' ),
102 'page_id',
103 $conds,
104 __METHOD__,
105 array(),
106 array( 'redirect' => array( "LEFT JOIN", "page_id=rd_from" ) )
107 );
108 $num = $dbr->numRows( $res );
109 $this->output( "Refreshing $num old redirects from $start...\n" );
110
111 $i = 0;
112
113 foreach ( $res as $row ) {
114 if ( !( ++$i % $reportingInterval ) ) {
115 $this->output( "$i\n" );
116 wfWaitForSlaves();
117 }
118 $this->fixRedirect( $row->page_id );
119 }
120 } elseif ( $newOnly ) {
121 $this->output( "Refreshing $what from " );
122 $res = $dbr->select( 'page',
123 array( 'page_id' ),
124 array(
125 'page_is_new' => 1,
126 "page_id >= $start" ),
127 __METHOD__
128 );
129 $num = $dbr->numRows( $res );
130 $this->output( "$num new articles...\n" );
131
132 $i = 0;
133 foreach ( $res as $row ) {
134 if ( !( ++$i % $reportingInterval ) ) {
135 $this->output( "$i\n" );
136 wfWaitForSlaves();
137 }
138 if ( $redirectsOnly ) {
139 $this->fixRedirect( $row->page_id );
140 } else {
141 self::fixLinksFromArticle( $row->page_id );
142 }
143 }
144 } else {
145 if ( !$end ) {
146 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
147 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
148 $end = max( $maxPage, $maxRD );
149 }
150 $this->output( "Refreshing redirects table.\n" );
151 $this->output( "Starting from page_id $start of $end.\n" );
152
153 for ( $id = $start; $id <= $end; $id++ ) {
154
155 if ( !( $id % $reportingInterval ) ) {
156 $this->output( "$id\n" );
157 wfWaitForSlaves();
158 }
159 $this->fixRedirect( $id );
160 }
161
162 if ( !$redirectsOnly ) {
163 $this->output( "Refreshing links table.\n" );
164 $this->output( "Starting from page_id $start of $end.\n" );
165
166 for ( $id = $start; $id <= $end; $id++ ) {
167
168 if ( !( $id % $reportingInterval ) ) {
169 $this->output( "$id\n" );
170 wfWaitForSlaves();
171 }
172 self::fixLinksFromArticle( $id );
173 }
174 }
175 }
176 }
177
178 /**
179 * Update the redirect entry for a given page
180 * @param $id int The page_id of the redirect
181 */
182 private function fixRedirect( $id ) {
183 $page = WikiPage::newFromID( $id );
184 $dbw = wfGetDB( DB_MASTER );
185
186 if ( $page === null ) {
187 // This page doesn't exist (any more)
188 // Delete any redirect table entry for it
189 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
190 __METHOD__ );
191 return;
192 }
193
194 $rt = $page->getRedirectTarget();
195
196 if ( $rt === null ) {
197 // The page is not a redirect
198 // Delete any redirect table entry for it
199 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
200 __METHOD__ );
201 }
202 }
203
204 /**
205 * Run LinksUpdate for all links on a given page_id
206 * @param $id int The page_id
207 */
208 public static function fixLinksFromArticle( $id ) {
209 global $wgParser, $wgContLang;
210
211 $page = WikiPage::newFromID( $id );
212
213 LinkCache::singleton()->clear();
214
215 if ( $page === null ) {
216 return;
217 }
218
219 $content = $page->getContent( REVISION::RAW );
220 if ( null === false ) {
221 return;
222 }
223
224 $dbw = wfGetDB( DB_MASTER );
225 $dbw->begin( __METHOD__ );
226
227 $updates = $content->getSecondaryDataUpdates( $page->getTitle() );
228 DataUpdate::runUpdates( $updates );
229
230 $dbw->commit( __METHOD__ );
231 }
232
233 /**
234 * Removes non-existing links from pages from pagelinks, imagelinks,
235 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
236 *
237 * @param $maxLag int
238 * @param $batchSize int The size of deletion batches
239 *
240 * @author Merlijn van Deen <valhallasw@arctus.nl>
241 */
242 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
243 wfWaitForSlaves();
244
245 $dbw = wfGetDB( DB_MASTER );
246
247 $lb = wfGetLBFactory()->newMainLB();
248 $dbr = $lb->getConnection( DB_SLAVE );
249 $dbr->bufferResults( false );
250
251 $linksTables = array( // table name => page_id field
252 'pagelinks' => 'pl_from',
253 'imagelinks' => 'il_from',
254 'categorylinks' => 'cl_from',
255 'templatelinks' => 'tl_from',
256 'externallinks' => 'el_from',
257 'iwlinks' => 'iwl_from',
258 'langlinks' => 'll_from',
259 'redirect' => 'rd_from',
260 'page_props' => 'pp_page',
261 );
262
263 foreach ( $linksTables as $table => $field ) {
264 $this->output( "Retrieving illegal entries from $table... " );
265
266 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
267 $results = $dbr->select( array( $table, 'page' ),
268 $field,
269 array( 'page_id' => null ),
270 __METHOD__,
271 'DISTINCT',
272 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
273 );
274
275 $counter = 0;
276 $list = array();
277 $this->output( "0.." );
278 foreach ( $results as $row ) {
279 $counter++;
280 $list[] = $row->$field;
281 if ( ( $counter % $batchSize ) == 0 ) {
282 wfWaitForSlaves();
283 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
284
285 $this->output( $counter . ".." );
286 $list = array();
287 }
288 }
289 $this->output( $counter );
290 if ( count( $list ) > 0 ) {
291 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
292 }
293 $this->output( "\n" );
294 wfWaitForSlaves();
295 }
296 $lb->closeAll();
297 }
298 }
299
300 $maintClass = 'RefreshLinks';
301 require_once( RUN_MAINTENANCE_IF_MAIN );