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 * @ingroup Maintenance
23 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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 );
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 );
49 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize
);
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
61 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
62 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
63 global $wgUser, $wgParser, $wgUseTidy;
65 $reportingInterval = 100;
66 $dbr = wfGetDB( DB_SLAVE
);
67 $start = intval( $start );
69 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
70 $wgUser->setOption( 'math', MW_MATH_SOURCE
);
72 # Don't generate extension images (e.g. Timeline)
73 if ( method_exists( $wgParser, "clearTagHooks" ) ) {
74 $wgParser->clearTagHooks();
80 $what = $redirectsOnly ?
"redirects" : "links";
82 if ( $oldRedirectsOnly ) {
83 # This entire code path is cut-and-pasted from below. Hurrah.
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" ),
93 $num = $dbr->numRows( $res );
94 $this->output( "Refreshing $num old redirects from $start...\n" );
96 foreach ( $res as $row ) {
97 if ( !( ++
$i %
$reportingInterval ) ) {
98 $this->output( "$i\n" );
99 wfWaitForSlaves( $maxLag );
101 $this->fixRedirect( $row->page_id
);
103 } elseif ( $newOnly ) {
104 $this->output( "Refreshing $what from " );
105 $res = $dbr->select( 'page',
109 "page_id >= $start" ),
112 $num = $dbr->numRows( $res );
113 $this->output( "$num new articles...\n" );
116 foreach ( $res as $row ) {
117 if ( !( ++
$i %
$reportingInterval ) ) {
118 $this->output( "$i\n" );
119 wfWaitForSlaves( $maxLag );
121 if ( $redirectsOnly )
122 $this->fixRedirect( $row->page_id
);
124 self
::fixLinksFromArticle( $row->page_id
);
128 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
129 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
130 $end = max( $maxPage, $maxRD );
132 $this->output( "Refreshing redirects table.\n" );
133 $this->output( "Starting from page_id $start of $end.\n" );
135 for ( $id = $start; $id <= $end; $id++
) {
137 if ( !( $id %
$reportingInterval ) ) {
138 $this->output( "$id\n" );
139 wfWaitForSlaves( $maxLag );
141 $this->fixRedirect( $id );
144 if ( !$redirectsOnly ) {
145 $this->output( "Refreshing links table.\n" );
146 $this->output( "Starting from page_id $start of $end.\n" );
148 for ( $id = $start; $id <= $end; $id++
) {
150 if ( !( $id %
$reportingInterval ) ) {
151 $this->output( "$id\n" );
152 wfWaitForSlaves( $maxLag );
154 self
::fixLinksFromArticle( $id );
161 * Update the redirect entry for a given page
162 * @param $id int The page_id of the redirect
164 private function fixRedirect( $id ) {
165 $title = Title
::newFromID( $id );
166 $dbw = wfGetDB( DB_MASTER
);
168 if ( is_null( $title ) ) {
169 // This page doesn't exist (any more)
170 // Delete any redirect table entry for it
171 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
175 $article = new Article( $title );
177 $rt = $article->followRedirect();
179 if ( !$rt ||
!is_object( $rt ) ) {
180 // $title is not a redirect
181 // Delete any redirect table entry for it
182 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
185 $article->updateRedirectOn( $dbw, $rt );
190 * Run LinksUpdate for all links on a given page_id
191 * @param $id int The page_id
193 public static function fixLinksFromArticle( $id ) {
196 $title = Title
::newFromID( $id );
197 $dbw = wfGetDB( DB_MASTER
);
199 LinkCache
::singleton()->clear();
201 if ( is_null( $title ) ) {
206 $revision = Revision
::newFromTitle( $title );
211 $options = new ParserOptions
;
212 $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
213 $update = new LinksUpdate( $title, $parserOutput, false );
219 * Removes non-existing links from pages from pagelinks, imagelinks,
220 * categorylinks, templatelinks and externallinks tables.
223 * @param $batchSize The size of deletion batches
225 * @author Merlijn van Deen <valhallasw@arctus.nl>
227 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
228 wfWaitForSlaves( $maxLag );
230 $dbw = wfGetDB( DB_MASTER
);
232 $lb = wfGetLBFactory()->newMainLB();
233 $dbr = $lb->getConnection( DB_SLAVE
);
234 $dbr->bufferResults( false );
236 $linksTables = array( // table name => page_id field
237 'pagelinks' => 'pl_from',
238 'imagelinks' => 'il_from',
239 'categorylinks' => 'cl_from',
240 'templatelinks' => 'tl_from',
241 'externallinks' => 'el_from',
244 foreach ( $linksTables as $table => $field ) {
245 $this->output( "Retrieving illegal entries from $table... " );
247 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
248 $results = $dbr->select( array( $table, 'page' ),
250 array( 'page_id' => null ),
253 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
258 $this->output( "0.." );
260 foreach ( $results as $row ) {
262 $list[] = $row->$field;
263 if ( ( $counter %
$batchSize ) == 0 ) {
264 wfWaitForSlaves( 5 );
265 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
267 $this->output( $counter . ".." );
271 $this->output( $counter );
272 if ( count( $list ) > 0 ) {
273 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
275 $this->output( "\n" );
281 $maintClass = 'RefreshLinks';
282 require_once( DO_MAINTENANCE
);