phpcs: More require/include is not a function
[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 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to refresh link tables.
28 *
29 * @ingroup Maintenance
30 */
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 );
43 }
44
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 );
54 }
55 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
56 }
57
58 /**
59 * Do the actual link refreshing.
60 * @param $start int Page_id to start from
61 * @param $newOnly bool Only do pages with 1 edit
62 * @param $maxLag int Max DB replication lag
63 * @param $end int Page_id to stop at
64 * @param $redirectsOnly bool Only fix redirects
65 * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
66 */
67 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
68 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
69 global $wgParser, $wgUseTidy;
70
71 $reportingInterval = 100;
72 $dbr = wfGetDB( DB_SLAVE );
73 $start = intval( $start );
74
75 // Give extensions a chance to optimize settings
76 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
77
78 # Don't generate extension images (e.g. Timeline)
79 $wgParser->clearTagHooks();
80
81 # Don't use HTML tidy
82 $wgUseTidy = false;
83
84 $what = $redirectsOnly ? "redirects" : "links";
85
86 if ( $oldRedirectsOnly ) {
87 # This entire code path is cut-and-pasted from below. Hurrah.
88
89 $conds = array(
90 "page_is_redirect=1",
91 "rd_from IS NULL"
92 );
93
94 if ( $end == 0 ) {
95 $conds[] = "page_id >= $start";
96 } else {
97 $conds[] = "page_id BETWEEN $start AND $end";
98 }
99
100 $res = $dbr->select(
101 array( 'page', 'redirect' ),
102 'page_id',
103 $conds,
104 __METHOD__,
105 array(),
106 array( 'redirect' => array( "LEFT JOIN", "page_id=rd_from" ) )
107 );
108 $num = $res->numRows();
109 $this->output( "Refreshing $num old redirects from $start...\n" );
110
111 $i = 0;
112
113 foreach ( $res as $row ) {
114 if ( !( ++$i % $reportingInterval ) ) {
115 $this->output( "$i\n" );
116 wfWaitForSlaves();
117 }
118 $this->fixRedirect( $row->page_id );
119 }
120 } elseif ( $newOnly ) {
121 $this->output( "Refreshing $what from " );
122 $res = $dbr->select( 'page',
123 array( 'page_id' ),
124 array(
125 'page_is_new' => 1,
126 "page_id >= $start" ),
127 __METHOD__
128 );
129 $num = $res->numRows();
130 $this->output( "$num new articles...\n" );
131
132 $i = 0;
133 foreach ( $res as $row ) {
134 if ( !( ++$i % $reportingInterval ) ) {
135 $this->output( "$i\n" );
136 wfWaitForSlaves();
137 }
138 if ( $redirectsOnly ) {
139 $this->fixRedirect( $row->page_id );
140 } else {
141 self::fixLinksFromArticle( $row->page_id );
142 }
143 }
144 } else {
145 if ( !$end ) {
146 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
147 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
148 $end = max( $maxPage, $maxRD );
149 }
150 $this->output( "Refreshing redirects table.\n" );
151 $this->output( "Starting from page_id $start of $end.\n" );
152
153 for ( $id = $start; $id <= $end; $id++ ) {
154
155 if ( !( $id % $reportingInterval ) ) {
156 $this->output( "$id\n" );
157 wfWaitForSlaves();
158 }
159 $this->fixRedirect( $id );
160 }
161
162 if ( !$redirectsOnly ) {
163 $this->output( "Refreshing links table.\n" );
164 $this->output( "Starting from page_id $start of $end.\n" );
165
166 for ( $id = $start; $id <= $end; $id++ ) {
167
168 if ( !( $id % $reportingInterval ) ) {
169 $this->output( "$id\n" );
170 wfWaitForSlaves();
171 }
172 self::fixLinksFromArticle( $id );
173 }
174 }
175 }
176 }
177
178 /**
179 * Update the redirect entry for a given page.
180 *
181 * This methods bypasses the "redirect" table to get the redirect target,
182 * and parses the page's content to fetch it. This allows to be sure that
183 * the redirect target is up to date and valid.
184 * This is particularly useful when modifying namespaces to be sure the
185 * entry in the "redirect" table points to the correct page and not to an
186 * invalid one.
187 *
188 * @param $id int The page ID to check
189 */
190 private function fixRedirect( $id ) {
191 $page = WikiPage::newFromID( $id );
192 $dbw = wfGetDB( DB_MASTER );
193
194 if ( $page === null ) {
195 // This page doesn't exist (any more)
196 // Delete any redirect table entry for it
197 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
198 __METHOD__ );
199 return;
200 }
201
202 $rt = null;
203 $content = $page->getContent( Revision::RAW );
204 if ( $content !== null ) {
205 $rt = $content->getUltimateRedirectTarget();
206 }
207
208 if ( $rt === null ) {
209 // The page is not a redirect
210 // Delete any redirect table entry for it
211 $dbw->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
212 $fieldValue = 0;
213 } else {
214 $page->insertRedirectEntry( $rt );
215 $fieldValue = 1;
216 }
217
218 // Update the page table to be sure it is an a consistent state
219 $dbw->update( 'page', array( 'page_is_redirect' => $fieldValue ),
220 array( 'page_id' => $id ), __METHOD__ );
221 }
222
223 /**
224 * Run LinksUpdate for all links on a given page_id
225 * @param $id int The page_id
226 */
227 public static function fixLinksFromArticle( $id ) {
228 $page = WikiPage::newFromID( $id );
229
230 LinkCache::singleton()->clear();
231
232 if ( $page === null ) {
233 return;
234 }
235
236 $content = $page->getContent( Revision::RAW );
237 if ( $content === null ) {
238 return;
239 }
240
241 $dbw = wfGetDB( DB_MASTER );
242 $dbw->begin( __METHOD__ );
243
244 $updates = $content->getSecondaryDataUpdates( $page->getTitle() );
245 DataUpdate::runUpdates( $updates );
246
247 $dbw->commit( __METHOD__ );
248 }
249
250 /**
251 * Removes non-existing links from pages from pagelinks, imagelinks,
252 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
253 *
254 * @param $maxLag int
255 * @param $batchSize int The size of deletion batches
256 *
257 * @author Merlijn van Deen <valhallasw@arctus.nl>
258 */
259 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
260 wfWaitForSlaves();
261
262 $dbw = wfGetDB( DB_MASTER );
263
264 $lb = wfGetLBFactory()->newMainLB();
265 $dbr = $lb->getConnection( DB_SLAVE );
266 $dbr->bufferResults( false );
267
268 $linksTables = array( // table name => page_id field
269 'pagelinks' => 'pl_from',
270 'imagelinks' => 'il_from',
271 'categorylinks' => 'cl_from',
272 'templatelinks' => 'tl_from',
273 'externallinks' => 'el_from',
274 'iwlinks' => 'iwl_from',
275 'langlinks' => 'll_from',
276 'redirect' => 'rd_from',
277 'page_props' => 'pp_page',
278 );
279
280 foreach ( $linksTables as $table => $field ) {
281 $this->output( "Retrieving illegal entries from $table... " );
282
283 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
284 $results = $dbr->select(
285 array( $table, 'page' ),
286 $field,
287 array( 'page_id' => null ),
288 __METHOD__,
289 'DISTINCT',
290 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
291 );
292
293 $counter = 0;
294 $list = array();
295 $this->output( "0.." );
296 foreach ( $results as $row ) {
297 $counter++;
298 $list[] = $row->$field;
299 if ( ( $counter % $batchSize ) == 0 ) {
300 wfWaitForSlaves();
301 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
302
303 $this->output( $counter . ".." );
304 $list = array();
305 }
306 }
307 $this->output( $counter );
308 if ( count( $list ) > 0 ) {
309 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
310 }
311 $this->output( "\n" );
312 wfWaitForSlaves();
313 }
314 $lb->closeAll();
315 }
316 }
317
318 $maintClass = 'RefreshLinks';
319 require_once RUN_MAINTENANCE_IF_MAIN;