Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / refreshLinks.php
blob0e6725cd6083cb5a63aaf14b1c780ba339c5d8da
1 <?php
2 /**
3 * Refresh link tables.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to refresh link tables.
29 * @ingroup Maintenance
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 );
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 );
55 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
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
67 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
68 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
69 global $wgParser, $wgUseTidy;
71 $reportingInterval = 100;
72 $dbr = wfGetDB( DB_SLAVE );
73 $start = intval( $start );
75 // Give extensions a chance to optimize settings
76 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
78 # Don't generate extension images (e.g. Timeline)
79 $wgParser->clearTagHooks();
81 # Don't use HTML tidy
82 $wgUseTidy = false;
84 $what = $redirectsOnly ? "redirects" : "links";
86 if ( $oldRedirectsOnly ) {
87 # This entire code path is cut-and-pasted from below. Hurrah.
89 $conds = array(
90 "page_is_redirect=1",
91 "rd_from IS NULL"
94 if ( $end == 0 ) {
95 $conds[] = "page_id >= $start";
96 } else {
97 $conds[] = "page_id BETWEEN $start AND $end";
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" ) )
108 $num = $res->numRows();
109 $this->output( "Refreshing $num old redirects from $start...\n" );
111 $i = 0;
113 foreach ( $res as $row ) {
114 if ( !( ++$i % $reportingInterval ) ) {
115 $this->output( "$i\n" );
116 wfWaitForSlaves();
118 $this->fixRedirect( $row->page_id );
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__
129 $num = $res->numRows();
130 $this->output( "$num new articles...\n" );
132 $i = 0;
133 foreach ( $res as $row ) {
134 if ( !( ++$i % $reportingInterval ) ) {
135 $this->output( "$i\n" );
136 wfWaitForSlaves();
138 if ( $redirectsOnly ) {
139 $this->fixRedirect( $row->page_id );
140 } else {
141 self::fixLinksFromArticle( $row->page_id );
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 );
150 $this->output( "Refreshing redirects table.\n" );
151 $this->output( "Starting from page_id $start of $end.\n" );
153 for ( $id = $start; $id <= $end; $id++ ) {
155 if ( !( $id % $reportingInterval ) ) {
156 $this->output( "$id\n" );
157 wfWaitForSlaves();
159 $this->fixRedirect( $id );
162 if ( !$redirectsOnly ) {
163 $this->output( "Refreshing links table.\n" );
164 $this->output( "Starting from page_id $start of $end.\n" );
166 for ( $id = $start; $id <= $end; $id++ ) {
168 if ( !( $id % $reportingInterval ) ) {
169 $this->output( "$id\n" );
170 wfWaitForSlaves();
172 self::fixLinksFromArticle( $id );
179 * Update the redirect entry for a given page.
181 * This methods bypasses the "redirect" table to get the redirect target,
182 * and parses the page's content to fetch it. This allows to be sure that
183 * the redirect target is up to date and valid.
184 * This is particularly useful when modifying namespaces to be sure the
185 * entry in the "redirect" table points to the correct page and not to an
186 * invalid one.
188 * @param $id int The page ID to check
190 private function fixRedirect( $id ) {
191 $page = WikiPage::newFromID( $id );
192 $dbw = wfGetDB( DB_MASTER );
194 if ( $page === null ) {
195 // This page doesn't exist (any more)
196 // Delete any redirect table entry for it
197 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
198 __METHOD__ );
199 return;
202 $rt = null;
203 $content = $page->getContent( Revision::RAW );
204 if ( $content !== null ) {
205 $rt = $content->getUltimateRedirectTarget();
208 if ( $rt === null ) {
209 // The page is not a redirect
210 // Delete any redirect table entry for it
211 $dbw->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
212 $fieldValue = 0;
213 } else {
214 $page->insertRedirectEntry( $rt );
215 $fieldValue = 1;
218 // Update the page table to be sure it is an a consistent state
219 $dbw->update( 'page', array( 'page_is_redirect' => $fieldValue ),
220 array( 'page_id' => $id ), __METHOD__ );
224 * Run LinksUpdate for all links on a given page_id
225 * @param $id int The page_id
227 public static function fixLinksFromArticle( $id ) {
228 $page = WikiPage::newFromID( $id );
230 LinkCache::singleton()->clear();
232 if ( $page === null ) {
233 return;
236 $content = $page->getContent( Revision::RAW );
237 if ( $content === null ) {
238 return;
241 $dbw = wfGetDB( DB_MASTER );
242 $dbw->begin( __METHOD__ );
244 $updates = $content->getSecondaryDataUpdates( $page->getTitle() );
245 DataUpdate::runUpdates( $updates );
247 $dbw->commit( __METHOD__ );
251 * Removes non-existing links from pages from pagelinks, imagelinks,
252 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
254 * @param $maxLag int
255 * @param $batchSize int The size of deletion batches
257 * @author Merlijn van Deen <valhallasw@arctus.nl>
259 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
260 wfWaitForSlaves();
262 $dbw = wfGetDB( DB_MASTER );
264 $lb = wfGetLBFactory()->newMainLB();
265 $dbr = $lb->getConnection( DB_SLAVE );
266 $dbr->bufferResults( false );
268 $linksTables = array( // table name => page_id field
269 'pagelinks' => 'pl_from',
270 'imagelinks' => 'il_from',
271 'categorylinks' => 'cl_from',
272 'templatelinks' => 'tl_from',
273 'externallinks' => 'el_from',
274 'iwlinks' => 'iwl_from',
275 'langlinks' => 'll_from',
276 'redirect' => 'rd_from',
277 'page_props' => 'pp_page',
280 foreach ( $linksTables as $table => $field ) {
281 $this->output( "Retrieving illegal entries from $table... " );
283 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
284 $results = $dbr->select(
285 array( $table, 'page' ),
286 $field,
287 array( 'page_id' => null ),
288 __METHOD__,
289 'DISTINCT',
290 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
293 $counter = 0;
294 $list = array();
295 $this->output( "0.." );
296 foreach ( $results as $row ) {
297 $counter++;
298 $list[] = $row->$field;
299 if ( ( $counter % $batchSize ) == 0 ) {
300 wfWaitForSlaves();
301 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
303 $this->output( $counter . ".." );
304 $list = array();
307 $this->output( $counter );
308 if ( count( $list ) > 0 ) {
309 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
311 $this->output( "\n" );
312 wfWaitForSlaves();
314 $lb->closeAll();
318 $maintClass = 'RefreshLinks';
319 require_once RUN_MAINTENANCE_IF_MAIN;