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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
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
);
59 * Do the actual link refreshing.
60 * @param int $start Page_id to start from
61 * @param bool $newOnly Only do pages with 1 edit
62 * @param int $maxLag Max DB replication lag
63 * @param int $end Page_id to stop at
64 * @param bool $redirectsOnly Only fix redirects
65 * @param bool $oldRedirectsOnly Only fix redirects without redirect entries
67 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
68 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false
70 global $wgParser, $wgUseTidy;
72 $reportingInterval = 100;
73 $dbr = wfGetDB( DB_SLAVE
);
74 $start = intval( $start );
76 // Give extensions a chance to optimize settings
77 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
79 # Don't generate extension images (e.g. Timeline)
80 $wgParser->clearTagHooks();
85 $what = $redirectsOnly ?
"redirects" : "links";
87 if ( $oldRedirectsOnly ) {
88 # This entire code path is cut-and-pasted from below. Hurrah.
96 $conds[] = "page_id >= $start";
98 $conds[] = "page_id BETWEEN $start AND $end";
102 array( 'page', 'redirect' ),
107 array( 'redirect' => array( "LEFT JOIN", "page_id=rd_from" ) )
109 $num = $res->numRows();
110 $this->output( "Refreshing $num old redirects from $start...\n" );
114 foreach ( $res as $row ) {
115 if ( !( ++
$i %
$reportingInterval ) ) {
116 $this->output( "$i\n" );
119 $this->fixRedirect( $row->page_id
);
121 } elseif ( $newOnly ) {
122 $this->output( "Refreshing $what from " );
123 $res = $dbr->select( 'page',
127 "page_id >= $start" ),
130 $num = $res->numRows();
131 $this->output( "$num new articles...\n" );
134 foreach ( $res as $row ) {
135 if ( !( ++
$i %
$reportingInterval ) ) {
136 $this->output( "$i\n" );
139 if ( $redirectsOnly ) {
140 $this->fixRedirect( $row->page_id
);
142 self
::fixLinksFromArticle( $row->page_id
);
147 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
148 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
149 $end = max( $maxPage, $maxRD );
151 $this->output( "Refreshing redirects table.\n" );
152 $this->output( "Starting from page_id $start of $end.\n" );
154 for ( $id = $start; $id <= $end; $id++
) {
156 if ( !( $id %
$reportingInterval ) ) {
157 $this->output( "$id\n" );
160 $this->fixRedirect( $id );
163 if ( !$redirectsOnly ) {
164 $this->output( "Refreshing links tables.\n" );
165 $this->output( "Starting from page_id $start of $end.\n" );
167 for ( $id = $start; $id <= $end; $id++
) {
169 if ( !( $id %
$reportingInterval ) ) {
170 $this->output( "$id\n" );
173 self
::fixLinksFromArticle( $id );
180 * Update the redirect entry for a given page.
182 * This methods bypasses the "redirect" table to get the redirect target,
183 * and parses the page's content to fetch it. This allows to be sure that
184 * the redirect target is up to date and valid.
185 * This is particularly useful when modifying namespaces to be sure the
186 * entry in the "redirect" table points to the correct page and not to an
189 * @param int $id The page ID to check
191 private function fixRedirect( $id ) {
192 $page = WikiPage
::newFromID( $id );
193 $dbw = wfGetDB( DB_MASTER
);
195 if ( $page === null ) {
196 // This page doesn't exist (any more)
197 // Delete any redirect table entry for it
198 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
205 $content = $page->getContent( Revision
::RAW
);
206 if ( $content !== null ) {
207 $rt = $content->getUltimateRedirectTarget();
210 if ( $rt === null ) {
211 // The page is not a redirect
212 // Delete any redirect table entry for it
213 $dbw->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__
);
216 $page->insertRedirectEntry( $rt );
220 // Update the page table to be sure it is an a consistent state
221 $dbw->update( 'page', array( 'page_is_redirect' => $fieldValue ),
222 array( 'page_id' => $id ), __METHOD__
);
226 * Run LinksUpdate for all links on a given page_id
227 * @param int $id The page_id
229 public static function fixLinksFromArticle( $id ) {
230 $page = WikiPage
::newFromID( $id );
232 LinkCache
::singleton()->clear();
234 if ( $page === null ) {
238 $content = $page->getContent( Revision
::RAW
);
239 if ( $content === null ) {
243 $dbw = wfGetDB( DB_MASTER
);
244 $dbw->begin( __METHOD__
);
246 $updates = $content->getSecondaryDataUpdates( $page->getTitle() );
247 DataUpdate
::runUpdates( $updates );
249 $dbw->commit( __METHOD__
);
253 * Removes non-existing links from pages from pagelinks, imagelinks,
254 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
257 * @param int $batchSize The size of deletion batches
259 * @author Merlijn van Deen <valhallasw@arctus.nl>
261 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
264 $dbw = wfGetDB( DB_MASTER
);
266 $lb = wfGetLBFactory()->newMainLB();
267 $dbr = $lb->getConnection( DB_SLAVE
);
268 $dbr->bufferResults( false );
270 $linksTables = array( // table name => page_id field
271 'pagelinks' => 'pl_from',
272 'imagelinks' => 'il_from',
273 'categorylinks' => 'cl_from',
274 'templatelinks' => 'tl_from',
275 'externallinks' => 'el_from',
276 'iwlinks' => 'iwl_from',
277 'langlinks' => 'll_from',
278 'redirect' => 'rd_from',
279 'page_props' => 'pp_page',
282 foreach ( $linksTables as $table => $field ) {
283 $this->output( "Retrieving illegal entries from $table... " );
285 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
286 $results = $dbr->select(
287 array( $table, 'page' ),
289 array( 'page_id' => null ),
292 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
297 $this->output( "0.." );
298 foreach ( $results as $row ) {
300 $list[] = $row->$field;
301 if ( ( $counter %
$batchSize ) == 0 ) {
303 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
305 $this->output( $counter . ".." );
309 $this->output( $counter );
310 if ( count( $list ) > 0 ) {
311 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
313 $this->output( "\n" );
320 $maintClass = 'RefreshLinks';
321 require_once RUN_MAINTENANCE_IF_MAIN
;