3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @ingroup Maintenance
21 require_once( dirname(__FILE__
) . '/Maintenance.php' );
23 class RefreshLinks
extends Maintenance
{
24 public function __construct() {
25 parent
::__construct();
26 $this->mDescription
= "Refresh link tables";
27 $this->addOption( 'dfn-only', 'Delete links from nonexistent articles only' );
28 $this->addOption( 'new-only', 'Only affect articles with just a single edit' );
29 $this->addOption( 'redirects-only', 'Only fix redirects, not all links' );
30 $this->addOption( 'old-redirects-only', 'Only fix redirects with no redirect table entry' );
31 $this->addOption( 'm', 'Maximum replication lag', false, true );
32 $this->addOption( 'e', 'Last page id to refresh', false, true );
33 $this->addArg( 'start', 'Page_id to start from, default 1' );
34 $this->setBatchSize( 100 );
37 public function execute() {
38 if( !$this->hasOption( 'dfn-only' ) ) {
39 $start = $this->getArg( 0, 1 );
40 $new = $this->getOption( 'new-only', false );
41 $max = $this->getOption( 'm', false );
42 $end = $this->getOption( 'e', 0 );
43 $redir = $this->getOption( 'redirects-only', false );
44 $oldRedir = $this->getOption( 'old-redirects-only', false );
45 $this->doRefreshLinks( $start, $new, $max, $end, $redir, $oldRedir );
47 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize
);
51 * Do the actual link refreshing.
52 * @param $start int Page_id to start from
53 * @param $newOnly bool Only do pages with 1 edit
54 * @param $maxLag int Max DB replication lag
55 * @param $end int Page_id to stop at
56 * @param $redirectsOnly bool Only fix redirects
57 * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
59 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
60 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
61 global $wgUser, $wgParser, $wgUseTidy;
63 $reportingInterval = 100;
64 $dbr = wfGetDB( DB_SLAVE
);
65 $start = intval( $start );
67 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
68 $wgUser->setOption('math', MW_MATH_SOURCE
);
70 # Don't generate extension images (e.g. Timeline)
71 if( method_exists( $wgParser, "clearTagHooks" ) ) {
72 $wgParser->clearTagHooks();
78 $what = $redirectsOnly ?
"redirects" : "links";
80 if( $oldRedirectsOnly ) {
81 # This entire code path is cut-and-pasted from below. Hurrah.
85 "LEFT JOIN redirect ON page_id=rd_from ".
86 "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
87 ($end == 0 ?
"page_id >= $start"
88 : "page_id BETWEEN $start AND $end"),
91 $num = $dbr->numRows( $res );
92 $this->output( "Refreshing $num old redirects from $start...\n" );
94 foreach( $res as $row ) {
95 if ( !( ++
$i %
$reportingInterval ) ) {
96 $this->output( "$i\n" );
97 wfWaitForSlaves( $maxLag );
99 $this->fixRedirect( $row->page_id
);
101 } elseif( $newOnly ) {
102 $this->output( "Refreshing $what from " );
103 $res = $dbr->select( 'page',
107 "page_id >= $start" ),
110 $num = $dbr->numRows( $res );
111 $this->output( "$num new articles...\n" );
114 foreach ( $res as $row ) {
115 if ( !( ++
$i %
$reportingInterval ) ) {
116 $this->output( "$i\n" );
117 wfWaitForSlaves( $maxLag );
120 $this->fixRedirect( $row->page_id
);
122 $this->fixLinksFromArticle( $row->page_id
);
125 $this->output( "Refreshing $what table.\n" );
127 $end = $dbr->selectField( 'page', 'max(page_id)', false );
129 $this->output( "Starting from page_id $start of $end.\n" );
131 for ($id = $start; $id <= $end; $id++
) {
133 if ( !($id %
$reportingInterval) ) {
134 $this->output( "$id\n" );
135 wfWaitForSlaves( $maxLag );
138 $this->fixRedirect( $id );
140 $this->fixLinksFromArticle( $id );
146 * Update the redirect entry for a given page
147 * @param $id int The page_id of the redirect
149 private function fixRedirect( $id ){
150 global $wgTitle, $wgArticle;
152 $wgTitle = Title
::newFromID( $id );
153 $dbw = wfGetDB( DB_MASTER
);
155 if ( is_null( $wgTitle ) ) {
158 $wgArticle = new Article($wgTitle);
160 $rt = $wgArticle->followRedirect();
162 if($rt == false ||
!is_object($rt))
165 $wgArticle->updateRedirectOn($dbw,$rt);
169 * Run LinksUpdate for all links on a given page_id
170 * @param $id int The page_id
172 private function fixLinksFromArticle( $id ) {
173 global $wgTitle, $wgParser;
175 $wgTitle = Title
::newFromID( $id );
176 $dbw = wfGetDB( DB_MASTER
);
178 $linkCache =& LinkCache
::singleton();
181 if ( is_null( $wgTitle ) ) {
186 $revision = Revision
::newFromTitle( $wgTitle );
191 $options = new ParserOptions
;
192 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
193 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
195 $dbw->immediateCommit();
199 * Removes non-existing links from pages from pagelinks, imagelinks,
200 * categorylinks, templatelinks and externallinks tables.
203 * @param $batchSize The size of deletion batches
205 * @author Merlijn van Deen <valhallasw@arctus.nl>
207 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
208 wfWaitForSlaves( $maxLag );
210 $dbw = wfGetDB( DB_MASTER
);
212 $lb = wfGetLBFactory()->newMainLB();
213 $dbr = $lb->getConnection( DB_SLAVE
);
214 $dbr->bufferResults( false );
216 $linksTables = array( // table name => page_id field
217 'pagelinks' => 'pl_from',
218 'imagelinks' => 'il_from',
219 'categorylinks' => 'cl_from',
220 'templatelinks' => 'tl_from',
221 'externallinks' => 'el_from',
224 foreach ( $linksTables as $table => $field ) {
225 $this->output( "Retrieving illegal entries from $table... " );
227 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
228 $results = $dbr->select( array( $table, 'page' ),
230 array('page_id' => null ),
233 array( 'page' => array( 'LEFT JOIN', "$field=page_id"))
238 $this->output( "0.." );
240 foreach( $results as $row ) {
242 $list[] = $row->$field;
243 if ( ( $counter %
$batchSize ) == 0 ) {
245 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
247 $this->output( $counter . ".." );
251 $this->output( $counter );
252 if (count($list) > 0) {
253 $dbw->delete( $table, array( $field => $list ), __METHOD__
);
255 $this->output( "\n" );
261 $maintClass = 'RefreshLinks';
262 require_once( DO_MAINTENANCE
);