Avoid circular links updates
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**#@+
14 * @access private
15 */
16 var $mId, # Page ID of the article linked from
17 $mTitle, # Title object of the article linked from
18 $mLinks, # Map of title strings to IDs for the links in the document
19 $mImages, # DB keys of the images used, in the array key only
20 $mTemplates, # Map of title strings to IDs for the template references, including broken ones
21 $mExternals, # URLs of external links, array key only
22 $mCategories, # Map of category names to sort keys
23 $mDb, # Database connection reference
24 $mOptions, # SELECT options to be used (array)
25 $mRecursive; # Whether to queue jobs for recursive updates
26 /**#@-*/
27
28 /**
29 * Constructor
30 * Initialize private variables
31 * @param integer $id
32 * @param string $title
33 */
34 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
35 global $wgAntiLockFlags;
36
37 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
38 $this->mOptions = array();
39 } else {
40 $this->mOptions = array( 'FOR UPDATE' );
41 }
42 $this->mDb =& wfGetDB( DB_MASTER );
43
44 if ( !is_object( $title ) ) {
45 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
46 "Please see Article::editUpdates() for an invocation example.\n" );
47 }
48 $this->mTitle = $title;
49 $this->mId = $title->getArticleID();
50
51 $this->mLinks = $parserOutput->getLinks();
52 $this->mImages = $parserOutput->getImages();
53 $this->mTemplates = $parserOutput->getTemplates();
54 $this->mExternals = $parserOutput->getExternalLinks();
55 $this->mCategories = $parserOutput->getCategories();
56 $this->mRecursive = $recursive;
57
58 }
59
60 /**
61 * Update link tables with outgoing links from an updated article
62 */
63 function doUpdate() {
64 global $wgUseDumbLinkUpdate;
65 if ( $wgUseDumbLinkUpdate ) {
66 $this->doDumbUpdate();
67 } else {
68 $this->doIncrementalUpdate();
69 }
70 }
71
72 function doIncrementalUpdate() {
73 $fname = 'LinksUpdate::doIncrementalUpdate';
74 wfProfileIn( $fname );
75
76 # Page links
77 $existing = $this->getExistingLinks();
78 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
79 $this->getLinkInsertions( $existing ) );
80
81 # Image links
82 $existing = $this->getExistingImages();
83 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
84 $this->getImageInsertions( $existing ) );
85
86 # External links
87 $existing = $this->getExistingExternals();
88 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
89 $this->getExternalInsertions( $existing ) );
90
91 # Template links
92 $existing = $this->getExistingTemplates();
93 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
94 $this->getTemplateInsertions( $existing ) );
95
96 # Refresh links of all pages including this page
97 if ( $this->mRecursive ) {
98 $tlto = $this->mTitle->getTemplateLinksTo();
99 if ( count( $tlto ) ) {
100 require_once( 'JobQueue.php' );
101 Job::queueLinksJobs( $tlto );
102 }
103 }
104
105 # Category links
106 $existing = $this->getExistingCategories();
107 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
108 $this->getCategoryInsertions( $existing ) );
109
110 # I think this works out to a set XOR operation, the idea is to invalidate all
111 # categories which were added, deleted or changed
112 # FIXME: surely there's a more appropriate place to put this update?
113 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
114 $this->invalidateCategories( $categoryUpdates );
115
116 wfProfileOut( $fname );
117 }
118
119 /**
120 * Link update which clears the previous entries and inserts new ones
121 * May be slower or faster depending on level of lock contention and write speed of DB
122 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
123 */
124 function doDumbUpdate() {
125 $fname = 'LinksUpdate::doDumbUpdate';
126 wfProfileIn( $fname );
127
128 # Refresh category pages
129 $existing = $this->getExistingCategories();
130 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
131
132 # Refresh links of all pages including this page
133 if ( $this->mRecursive ) {
134 $tlto = $this->mTitle->getTemplateLinksTo();
135 if ( count( $tlto ) ) {
136 require_once( 'JobQueue.php' );
137 Job::queueLinksJobs( $tlto );
138 }
139 }
140
141 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
142 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
143 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
144 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
145 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
146
147 # Update the cache of all the category pages
148 $this->invalidateCategories( $categoryUpdates );
149
150 wfProfileOut( $fname );
151 }
152
153 function invalidateCategories( $cats ) {
154 $fname = 'LinksUpdate::invalidateCategories';
155 if ( count( $cats ) ) {
156 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
157 array(
158 'page_namespace' => NS_CATEGORY,
159 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
160 ), $fname
161 );
162 }
163 }
164
165 function dumbTableUpdate( $table, $insertions, $fromField ) {
166 $fname = 'LinksUpdate::dumbTableUpdate';
167 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
168 if ( count( $insertions ) ) {
169 # The link array was constructed without FOR UPDATE, so there may be collisions
170 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
171 # sure it's better than without IGNORE
172 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
173 }
174 }
175
176 /**
177 * Make a WHERE clause from a 2-d NS/dbkey array
178 *
179 * @param array $arr 2-d array indexed by namespace and DB key
180 * @param string $prefix Field name prefix, without the underscore
181 */
182 function makeWhereFrom2d( &$arr, $prefix ) {
183 $lb = new LinkBatch;
184 $lb->setArray( $arr );
185 return $lb->constructSet( $prefix, $this->mDb );
186 }
187
188 /**
189 * Update a table by doing a delete query then an insert query
190 * @access private
191 */
192 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
193 $fname = 'LinksUpdate::incrTableUpdate';
194 $where = array( "{$prefix}_from" => $this->mId );
195 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
196 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
197 if ( $clause ) {
198 $where[] = $clause;
199 } else {
200 $where = false;
201 }
202 } else {
203 if ( count( $deletions ) ) {
204 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
205 } else {
206 $where = false;
207 }
208 }
209 if ( $where ) {
210 $this->mDb->delete( $table, $where, $fname );
211 }
212 if ( count( $insertions ) ) {
213 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
214 }
215 }
216
217
218 /**
219 * Get an array of pagelinks insertions for passing to the DB
220 * Skips the titles specified by the 2-D array $existing
221 * @access private
222 */
223 function getLinkInsertions( $existing = array() ) {
224 $arr = array();
225 foreach( $this->mLinks as $ns => $dbkeys ) {
226 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
227 # in GlobalFunctions.php
228 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
229 foreach ( $diffs as $dbk => $id ) {
230 $arr[] = array(
231 'pl_from' => $this->mId,
232 'pl_namespace' => $ns,
233 'pl_title' => $dbk
234 );
235 }
236 }
237 return $arr;
238 }
239
240 /**
241 * Get an array of template insertions. Like getLinkInsertions()
242 * @access private
243 */
244 function getTemplateInsertions( $existing = array() ) {
245 $arr = array();
246 foreach( $this->mTemplates as $ns => $dbkeys ) {
247 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
248 foreach ( $diffs as $dbk => $id ) {
249 $arr[] = array(
250 'tl_from' => $this->mId,
251 'tl_namespace' => $ns,
252 'tl_title' => $dbk
253 );
254 }
255 }
256 return $arr;
257 }
258
259 /**
260 * Get an array of image insertions
261 * Skips the names specified in $existing
262 * @access private
263 */
264 function getImageInsertions( $existing = array() ) {
265 $arr = array();
266 $diffs = array_diff_key( $this->mImages, $existing );
267 foreach( $diffs as $iname => $dummy ) {
268 $arr[] = array(
269 'il_from' => $this->mId,
270 'il_to' => $iname
271 );
272 }
273 return $arr;
274 }
275
276 /**
277 * Get an array of externallinks insertions. Skips the names specified in $existing
278 * @access private
279 */
280 function getExternalInsertions( $existing = array() ) {
281 $arr = array();
282 $diffs = array_diff_key( $this->mExternals, $existing );
283 foreach( $diffs as $url => $dummy ) {
284 $arr[] = array(
285 'el_from' => $this->mId,
286 'el_to' => $url,
287 'el_index' => wfMakeUrlIndex( $url ),
288 );
289 }
290 return $arr;
291 }
292
293 /**
294 * Get an array of category insertions
295 * @param array $existing Array mapping existing category names to sort keys. If both
296 * match a link in $this, the link will be omitted from the output
297 * @access private
298 */
299 function getCategoryInsertions( $existing = array() ) {
300 $diffs = array_diff_assoc( $this->mCategories, $existing );
301 $arr = array();
302 foreach ( $diffs as $name => $sortkey ) {
303 $arr[] = array(
304 'cl_from' => $this->mId,
305 'cl_to' => $name,
306 'cl_sortkey' => $sortkey
307 );
308 }
309 return $arr;
310 }
311
312 /**
313 * Given an array of existing links, returns those links which are not in $this
314 * and thus should be deleted.
315 * @access private
316 */
317 function getLinkDeletions( $existing ) {
318 $del = array();
319 foreach ( $existing as $ns => $dbkeys ) {
320 if ( isset( $this->mLinks[$ns] ) ) {
321 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
322 } else {
323 $del[$ns] = $existing[$ns];
324 }
325 }
326 return $del;
327 }
328
329 /**
330 * Given an array of existing templates, returns those templates which are not in $this
331 * and thus should be deleted.
332 * @access private
333 */
334 function getTemplateDeletions( $existing ) {
335 $del = array();
336 foreach ( $existing as $ns => $dbkeys ) {
337 if ( isset( $this->mTemplates[$ns] ) ) {
338 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
339 } else {
340 $del[$ns] = $existing[$ns];
341 }
342 }
343 return $del;
344 }
345
346 /**
347 * Given an array of existing images, returns those images which are not in $this
348 * and thus should be deleted.
349 * @access private
350 */
351 function getImageDeletions( $existing ) {
352 return array_diff_key( $existing, $this->mImages );
353 }
354
355 /**
356 * Given an array of existing external links, returns those links which are not
357 * in $this and thus should be deleted.
358 * @access private
359 */
360 function getExternalDeletions( $existing ) {
361 return array_diff_key( $existing, $this->mExternals );
362 }
363
364 /**
365 * Given an array of existing categories, returns those categories which are not in $this
366 * and thus should be deleted.
367 * @access private
368 */
369 function getCategoryDeletions( $existing ) {
370 return array_diff_assoc( $existing, $this->mCategories );
371 }
372
373 /**
374 * Get an array of existing links, as a 2-D array
375 * @access private
376 */
377 function getExistingLinks() {
378 $fname = 'LinksUpdate::getExistingLinks';
379 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
380 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
381 $arr = array();
382 while ( $row = $this->mDb->fetchObject( $res ) ) {
383 if ( !isset( $arr[$row->pl_namespace] ) ) {
384 $arr[$row->pl_namespace] = array();
385 }
386 $arr[$row->pl_namespace][$row->pl_title] = 1;
387 }
388 $this->mDb->freeResult( $res );
389 return $arr;
390 }
391
392 /**
393 * Get an array of existing templates, as a 2-D array
394 * @access private
395 */
396 function getExistingTemplates() {
397 $fname = 'LinksUpdate::getExistingTemplates';
398 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
399 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
400 $arr = array();
401 while ( $row = $this->mDb->fetchObject( $res ) ) {
402 if ( !isset( $arr[$row->tl_namespace] ) ) {
403 $arr[$row->tl_namespace] = array();
404 }
405 $arr[$row->tl_namespace][$row->tl_title] = 1;
406 }
407 $this->mDb->freeResult( $res );
408 return $arr;
409 }
410
411 /**
412 * Get an array of existing images, image names in the keys
413 * @access private
414 */
415 function getExistingImages() {
416 $fname = 'LinksUpdate::getExistingImages';
417 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
418 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
419 $arr = array();
420 while ( $row = $this->mDb->fetchObject( $res ) ) {
421 $arr[$row->il_to] = 1;
422 }
423 $this->mDb->freeResult( $res );
424 return $arr;
425 }
426
427 /**
428 * Get an array of existing external links, URLs in the keys
429 * @access private
430 */
431 function getExistingExternals() {
432 $fname = 'LinksUpdate::getExistingExternals';
433 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
434 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
435 $arr = array();
436 while ( $row = $this->mDb->fetchObject( $res ) ) {
437 $arr[$row->el_to] = 1;
438 }
439 $this->mDb->freeResult( $res );
440 return $arr;
441 }
442
443 /**
444 * Get an array of existing categories, with the name in the key and sort key in the value.
445 * @access private
446 */
447 function getExistingCategories() {
448 $fname = 'LinksUpdate::getExistingCategories';
449 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
450 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
451 $arr = array();
452 while ( $row = $this->mDb->fetchObject( $res ) ) {
453 $arr[$row->cl_to] = $row->cl_sortkey;
454 }
455 $this->mDb->freeResult( $res );
456 return $arr;
457 }
458 }
459 ?>