HTMLForm: throw a warning when no callback is set
[mediawiki.git] / maintenance / refreshLinks.php
blob26d7e29f28895c709ba6736d831f52fedb94393b
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 * @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 );
52 /**
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 $wgParser, $wgUseTidy;
65 $reportingInterval = 100;
66 $dbr = wfGetDB( DB_SLAVE );
67 $start = intval( $start );
69 // Give extensions a chance to optimize settings
70 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
72 # Don't generate extension images (e.g. Timeline)
73 $wgParser->clearTagHooks();
75 # Don't use HTML tidy
76 $wgUseTidy = false;
78 $what = $redirectsOnly ? "redirects" : "links";
80 if ( $oldRedirectsOnly ) {
81 # This entire code path is cut-and-pasted from below. Hurrah.
83 $conds = array(
84 "page_is_redirect=1",
85 "rd_from IS NULL"
88 if ( $end == 0 ) {
89 $conds[] = "page_id >= $start";
90 } else {
91 $conds[] = "page_id BETWEEN $start AND $end";
94 $res = $dbr->select(
95 array( 'page', 'redirect' ),
96 'page_id',
97 $conds,
98 __METHOD__,
99 array(),
100 array( 'redirect' => array( "LEFT JOIN", "page_id=rd_from" ) )
102 $num = $dbr->numRows( $res );
103 $this->output( "Refreshing $num old redirects from $start...\n" );
105 $i = 0;
107 foreach ( $res as $row ) {
108 if ( !( ++$i % $reportingInterval ) ) {
109 $this->output( "$i\n" );
110 wfWaitForSlaves();
112 $this->fixRedirect( $row->page_id );
114 } elseif ( $newOnly ) {
115 $this->output( "Refreshing $what from " );
116 $res = $dbr->select( 'page',
117 array( 'page_id' ),
118 array(
119 'page_is_new' => 1,
120 "page_id >= $start" ),
121 __METHOD__
123 $num = $dbr->numRows( $res );
124 $this->output( "$num new articles...\n" );
126 $i = 0;
127 foreach ( $res as $row ) {
128 if ( !( ++$i % $reportingInterval ) ) {
129 $this->output( "$i\n" );
130 wfWaitForSlaves();
132 if ( $redirectsOnly ) {
133 $this->fixRedirect( $row->page_id );
134 } else {
135 self::fixLinksFromArticle( $row->page_id );
138 } else {
139 if ( !$end ) {
140 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
141 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
142 $end = max( $maxPage, $maxRD );
144 $this->output( "Refreshing redirects table.\n" );
145 $this->output( "Starting from page_id $start of $end.\n" );
147 for ( $id = $start; $id <= $end; $id++ ) {
149 if ( !( $id % $reportingInterval ) ) {
150 $this->output( "$id\n" );
151 wfWaitForSlaves();
153 $this->fixRedirect( $id );
156 if ( !$redirectsOnly ) {
157 $this->output( "Refreshing links table.\n" );
158 $this->output( "Starting from page_id $start of $end.\n" );
160 for ( $id = $start; $id <= $end; $id++ ) {
162 if ( !( $id % $reportingInterval ) ) {
163 $this->output( "$id\n" );
164 wfWaitForSlaves();
166 self::fixLinksFromArticle( $id );
173 * Update the redirect entry for a given page
174 * @param $id int The page_id of the redirect
176 private function fixRedirect( $id ) {
177 $page = WikiPage::newFromID( $id );
178 $dbw = wfGetDB( DB_MASTER );
180 if ( $page === null ) {
181 // This page doesn't exist (any more)
182 // Delete any redirect table entry for it
183 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
184 __METHOD__ );
185 return;
188 $rt = $page->getRedirectTarget();
190 if ( $rt === null ) {
191 // The page is not a redirect
192 // Delete any redirect table entry for it
193 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
194 __METHOD__ );
199 * Run LinksUpdate for all links on a given page_id
200 * @param $id int The page_id
202 public static function fixLinksFromArticle( $id ) {
203 global $wgParser, $wgContLang;
205 $page = WikiPage::newFromID( $id );
207 LinkCache::singleton()->clear();
209 if ( $page === null ) {
210 return;
213 $text = $page->getRawText();
214 if ( $text === false ) {
215 return;
218 $dbw = wfGetDB( DB_MASTER );
219 $dbw->begin( __METHOD__ );
221 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
222 $parserOutput = $wgParser->parse( $text, $page->getTitle(), $options, true, true, $page->getLatest() );
223 $update = new LinksUpdate( $page->getTitle(), $parserOutput, false );
224 $update->doUpdate();
226 $dbw->commit( __METHOD__ );
230 * Removes non-existing links from pages from pagelinks, imagelinks,
231 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
233 * @param $maxLag int
234 * @param $batchSize int The size of deletion batches
236 * @author Merlijn van Deen <valhallasw@arctus.nl>
238 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
239 wfWaitForSlaves();
241 $dbw = wfGetDB( DB_MASTER );
243 $lb = wfGetLBFactory()->newMainLB();
244 $dbr = $lb->getConnection( DB_SLAVE );
245 $dbr->bufferResults( false );
247 $linksTables = array( // table name => page_id field
248 'pagelinks' => 'pl_from',
249 'imagelinks' => 'il_from',
250 'categorylinks' => 'cl_from',
251 'templatelinks' => 'tl_from',
252 'externallinks' => 'el_from',
253 'iwlinks' => 'iwl_from',
254 'langlinks' => 'll_from',
255 'redirect' => 'rd_from',
256 'page_props' => 'pp_page',
259 foreach ( $linksTables as $table => $field ) {
260 $this->output( "Retrieving illegal entries from $table... " );
262 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
263 $results = $dbr->select( array( $table, 'page' ),
264 $field,
265 array( 'page_id' => null ),
266 __METHOD__,
267 'DISTINCT',
268 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
271 $counter = 0;
272 $list = array();
273 $this->output( "0.." );
274 foreach ( $results as $row ) {
275 $counter++;
276 $list[] = $row->$field;
277 if ( ( $counter % $batchSize ) == 0 ) {
278 wfWaitForSlaves();
279 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
281 $this->output( $counter . ".." );
282 $list = array();
285 $this->output( $counter );
286 if ( count( $list ) > 0 ) {
287 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
289 $this->output( "\n" );
291 $lb->closeAll();
295 $maintClass = 'RefreshLinks';
296 require_once( RUN_MAINTENANCE_IF_MAIN );