Merge branch 'master' 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 $title = Title::newFromID( $id );
178 $dbw = wfGetDB( DB_MASTER );
179
180 if ( is_null( $title ) ) {
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 $page = WikiPage::factory( $title );
189 $rt = $page->getRedirectTarget();
190
191 if ( $rt === null ) {
192 // $title is not a redirect
193 // Delete any redirect table entry for it
194 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
195 __METHOD__ );
196 }
197 }
198
199 /**
200 * Run LinksUpdate for all links on a given page_id
201 * @param $id int The page_id
202 */
203 public static function fixLinksFromArticle( $id ) {
204 global $wgParser;
205
206 $title = Title::newFromID( $id );
207 $dbw = wfGetDB( DB_MASTER );
208
209 LinkCache::singleton()->clear();
210
211 if ( is_null( $title ) ) {
212 return;
213 }
214
215 $revision = Revision::newFromTitle( $title );
216 if ( !$revision ) {
217 return;
218 }
219
220 $dbw->begin( __METHOD__ );
221
222 $options = new ParserOptions;
223 $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
224
225 $updates = $parserOutput->getLinksUpdateAndOtherUpdates( $title, false );
226 SecondaryDataUpdate::runUpdates( $updates );
227
228 $dbw->commit();
229 // TODO: We don't know what happens here.
230 $update = new LinksUpdate( $title, $parserOutput, false );
231 $update->doUpdate();
232 $dbw->commit( __METHOD__ );
233 }
234
235 /**
236 * Removes non-existing links from pages from pagelinks, imagelinks,
237 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
238 *
239 * @param $maxLag int
240 * @param $batchSize int The size of deletion batches
241 *
242 * @author Merlijn van Deen <valhallasw@arctus.nl>
243 */
244 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
245 wfWaitForSlaves();
246
247 $dbw = wfGetDB( DB_MASTER );
248
249 $lb = wfGetLBFactory()->newMainLB();
250 $dbr = $lb->getConnection( DB_SLAVE );
251 $dbr->bufferResults( false );
252
253 $linksTables = array( // table name => page_id field
254 'pagelinks' => 'pl_from',
255 'imagelinks' => 'il_from',
256 'categorylinks' => 'cl_from',
257 'templatelinks' => 'tl_from',
258 'externallinks' => 'el_from',
259 'iwlinks' => 'iwl_from',
260 'langlinks' => 'll_from',
261 'redirect' => 'rd_from',
262 'page_props' => 'pp_page',
263 );
264
265 foreach ( $linksTables as $table => $field ) {
266 $this->output( "Retrieving illegal entries from $table... " );
267
268 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
269 $results = $dbr->select( array( $table, 'page' ),
270 $field,
271 array( 'page_id' => null ),
272 __METHOD__,
273 'DISTINCT',
274 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
275 );
276
277 $counter = 0;
278 $list = array();
279 $this->output( "0.." );
280 foreach ( $results as $row ) {
281 $counter++;
282 $list[] = $row->$field;
283 if ( ( $counter % $batchSize ) == 0 ) {
284 wfWaitForSlaves();
285 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
286
287 $this->output( $counter . ".." );
288 $list = array();
289 }
290 }
291 $this->output( $counter );
292 if ( count( $list ) > 0 ) {
293 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
294 }
295 $this->output( "\n" );
296 }
297 $lb->closeAll();
298 }
299 }
300
301 $maintClass = 'RefreshLinks';
302 require_once( RUN_MAINTENANCE_IF_MAIN );