Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / maintenance / refreshLinks.php
1 <?php
2 /**
3 * Refresh link tables.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
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 );
37 }
38
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 );
48 }
49 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
50 }
51
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
60 */
61 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
62 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
63 global $wgParser, $wgUseTidy;
64
65 $reportingInterval = 100;
66 $dbr = wfGetDB( DB_SLAVE );
67 $start = intval( $start );
68
69 // Give extensions a chance to optimize settings
70 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
71
72 # Don't generate extension images (e.g. Timeline)
73 $wgParser->clearTagHooks();
74
75 # Don't use HTML tidy
76 $wgUseTidy = false;
77
78 $what = $redirectsOnly ? "redirects" : "links";
79
80 if ( $oldRedirectsOnly ) {
81 # This entire code path is cut-and-pasted from below. Hurrah.
82
83 $conds = array(
84 "page_is_redirect=1",
85 "rd_from IS NULL"
86 );
87
88 if ( $end == 0 ) {
89 $conds[] = "page_id >= $start";
90 } else {
91 $conds[] = "page_id BETWEEN $start AND $end";
92 }
93
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" ) )
101 );
102 $num = $dbr->numRows( $res );
103 $this->output( "Refreshing $num old redirects from $start...\n" );
104
105 $i = 0;
106
107 foreach ( $res as $row ) {
108 if ( !( ++$i % $reportingInterval ) ) {
109 $this->output( "$i\n" );
110 wfWaitForSlaves();
111 }
112 $this->fixRedirect( $row->page_id );
113 }
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__
122 );
123 $num = $dbr->numRows( $res );
124 $this->output( "$num new articles...\n" );
125
126 $i = 0;
127 foreach ( $res as $row ) {
128 if ( !( ++$i % $reportingInterval ) ) {
129 $this->output( "$i\n" );
130 wfWaitForSlaves();
131 }
132 if ( $redirectsOnly ) {
133 $this->fixRedirect( $row->page_id );
134 } else {
135 self::fixLinksFromArticle( $row->page_id );
136 }
137 }
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 );
143 }
144 $this->output( "Refreshing redirects table.\n" );
145 $this->output( "Starting from page_id $start of $end.\n" );
146
147 for ( $id = $start; $id <= $end; $id++ ) {
148
149 if ( !( $id % $reportingInterval ) ) {
150 $this->output( "$id\n" );
151 wfWaitForSlaves();
152 }
153 $this->fixRedirect( $id );
154 }
155
156 if ( !$redirectsOnly ) {
157 $this->output( "Refreshing links table.\n" );
158 $this->output( "Starting from page_id $start of $end.\n" );
159
160 for ( $id = $start; $id <= $end; $id++ ) {
161
162 if ( !( $id % $reportingInterval ) ) {
163 $this->output( "$id\n" );
164 wfWaitForSlaves();
165 }
166 self::fixLinksFromArticle( $id );
167 }
168 }
169 }
170 }
171
172 /**
173 * Update the redirect entry for a given page
174 * @param $id int The page_id of the redirect
175 */
176 private function fixRedirect( $id ) {
177 $page = WikiPage::newFromID( $id );
178 $dbw = wfGetDB( DB_MASTER );
179
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;
186 }
187
188 $rt = $page->getRedirectTarget();
189
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__ );
195 }
196 }
197
198 /**
199 * Run LinksUpdate for all links on a given page_id
200 * @param $id int The page_id
201 */
202 public static function fixLinksFromArticle( $id ) {
203 global $wgParser, $wgContLang;
204
205 $page = WikiPage::newFromID( $id );
206
207 LinkCache::singleton()->clear();
208
209 if ( $page === null ) {
210 return;
211 }
212
213 $content = $page->getContent( REVISION::RAW );
214 if ( null === false ) {
215 return;
216 }
217
218 $dbw = wfGetDB( DB_MASTER );
219 $dbw->begin( __METHOD__ );
220
221 $contentHandler = $content->getContentHandler();
222 $updates = $contentHandler->getSecondaryDataUpdates( $content, $page->getTitle() );
223 DataUpdate::runUpdates( $updates );
224
225 $dbw->commit( __METHOD__ );
226 }
227
228 /**
229 * Removes non-existing links from pages from pagelinks, imagelinks,
230 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
231 *
232 * @param $maxLag int
233 * @param $batchSize int The size of deletion batches
234 *
235 * @author Merlijn van Deen <valhallasw@arctus.nl>
236 */
237 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
238 wfWaitForSlaves();
239
240 $dbw = wfGetDB( DB_MASTER );
241
242 $lb = wfGetLBFactory()->newMainLB();
243 $dbr = $lb->getConnection( DB_SLAVE );
244 $dbr->bufferResults( false );
245
246 $linksTables = array( // table name => page_id field
247 'pagelinks' => 'pl_from',
248 'imagelinks' => 'il_from',
249 'categorylinks' => 'cl_from',
250 'templatelinks' => 'tl_from',
251 'externallinks' => 'el_from',
252 'iwlinks' => 'iwl_from',
253 'langlinks' => 'll_from',
254 'redirect' => 'rd_from',
255 'page_props' => 'pp_page',
256 );
257
258 foreach ( $linksTables as $table => $field ) {
259 $this->output( "Retrieving illegal entries from $table... " );
260
261 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
262 $results = $dbr->select( array( $table, 'page' ),
263 $field,
264 array( 'page_id' => null ),
265 __METHOD__,
266 'DISTINCT',
267 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
268 );
269
270 $counter = 0;
271 $list = array();
272 $this->output( "0.." );
273 foreach ( $results as $row ) {
274 $counter++;
275 $list[] = $row->$field;
276 if ( ( $counter % $batchSize ) == 0 ) {
277 wfWaitForSlaves();
278 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
279
280 $this->output( $counter . ".." );
281 $list = array();
282 }
283 }
284 $this->output( $counter );
285 if ( count( $list ) > 0 ) {
286 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
287 }
288 $this->output( "\n" );
289 wfWaitForSlaves();
290 }
291 $lb->closeAll();
292 }
293 }
294
295 $maintClass = 'RefreshLinks';
296 require_once( RUN_MAINTENANCE_IF_MAIN );