(bug 5378) General logs link in Special:Contributions
[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 # Invalidate all image description pages which had links added or removed
87 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
88 $this->invalidateImageDescriptions( $imageUpdates );
89
90 # External links
91 $existing = $this->getExistingExternals();
92 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
93 $this->getExternalInsertions( $existing ) );
94
95 # Template links
96 $existing = $this->getExistingTemplates();
97 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
98 $this->getTemplateInsertions( $existing ) );
99
100 # Refresh links of all pages including this page
101 if ( $this->mRecursive ) {
102 $tlto = $this->mTitle->getTemplateLinksTo();
103 if ( count( $tlto ) ) {
104 require_once( 'JobQueue.php' );
105 Job::queueLinksJobs( $tlto );
106 }
107 }
108
109 # Category links
110 $existing = $this->getExistingCategories();
111 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
112 $this->getCategoryInsertions( $existing ) );
113
114 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
115 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
116 $this->invalidateCategories( $categoryUpdates );
117
118 wfProfileOut( $fname );
119 }
120
121 /**
122 * Link update which clears the previous entries and inserts new ones
123 * May be slower or faster depending on level of lock contention and write speed of DB
124 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
125 */
126 function doDumbUpdate() {
127 $fname = 'LinksUpdate::doDumbUpdate';
128 wfProfileIn( $fname );
129
130 # Refresh category pages and image description pages
131 $existing = $this->getExistingCategories();
132 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
133 $existing = $this->getExistingImages();
134 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
135
136 # Refresh links of all pages including this page
137 if ( $this->mRecursive ) {
138 $tlto = $this->mTitle->getTemplateLinksTo();
139 if ( count( $tlto ) ) {
140 require_once( 'JobQueue.php' );
141 Job::queueLinksJobs( $tlto );
142 }
143 }
144
145 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
146 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
147 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
148 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
149 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
150
151 # Update the cache of all the category pages and image description pages which were changed
152 $this->invalidateCategories( $categoryUpdates );
153 $this->invalidateImageDescriptions( $imageUpdates );
154
155 wfProfileOut( $fname );
156 }
157
158 function invalidateCategories( $cats ) {
159 $fname = 'LinksUpdate::invalidateCategories';
160 if ( count( $cats ) ) {
161 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
162 array(
163 'page_namespace' => NS_CATEGORY,
164 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
165 ), $fname
166 );
167 }
168 }
169
170 function invalidateImageDescriptions( $images ) {
171 $fname = 'LinksUpdate::invalidateImageDescriptions';
172 if ( count( $images ) ) {
173 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
174 array(
175 'page_namespace' => NS_IMAGE,
176 'page_title IN (' . $this->mDb->makeList( array_keys( $images ) ) . ')'
177 ), $fname
178 );
179 }
180 }
181
182 function dumbTableUpdate( $table, $insertions, $fromField ) {
183 $fname = 'LinksUpdate::dumbTableUpdate';
184 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
185 if ( count( $insertions ) ) {
186 # The link array was constructed without FOR UPDATE, so there may be collisions
187 # This may cause minor link table inconsistencies, which is better than
188 # crippling the site with lock contention.
189 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
190 }
191 }
192
193 /**
194 * Make a WHERE clause from a 2-d NS/dbkey array
195 *
196 * @param array $arr 2-d array indexed by namespace and DB key
197 * @param string $prefix Field name prefix, without the underscore
198 */
199 function makeWhereFrom2d( &$arr, $prefix ) {
200 $lb = new LinkBatch;
201 $lb->setArray( $arr );
202 return $lb->constructSet( $prefix, $this->mDb );
203 }
204
205 /**
206 * Update a table by doing a delete query then an insert query
207 * @access private
208 */
209 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
210 $fname = 'LinksUpdate::incrTableUpdate';
211 $where = array( "{$prefix}_from" => $this->mId );
212 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
213 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
214 if ( $clause ) {
215 $where[] = $clause;
216 } else {
217 $where = false;
218 }
219 } else {
220 if ( count( $deletions ) ) {
221 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
222 } else {
223 $where = false;
224 }
225 }
226 if ( $where ) {
227 $this->mDb->delete( $table, $where, $fname );
228 }
229 if ( count( $insertions ) ) {
230 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
231 }
232 }
233
234
235 /**
236 * Get an array of pagelinks insertions for passing to the DB
237 * Skips the titles specified by the 2-D array $existing
238 * @access private
239 */
240 function getLinkInsertions( $existing = array() ) {
241 $arr = array();
242 foreach( $this->mLinks as $ns => $dbkeys ) {
243 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
244 # in GlobalFunctions.php
245 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
246 foreach ( $diffs as $dbk => $id ) {
247 $arr[] = array(
248 'pl_from' => $this->mId,
249 'pl_namespace' => $ns,
250 'pl_title' => $dbk
251 );
252 }
253 }
254 return $arr;
255 }
256
257 /**
258 * Get an array of template insertions. Like getLinkInsertions()
259 * @access private
260 */
261 function getTemplateInsertions( $existing = array() ) {
262 $arr = array();
263 foreach( $this->mTemplates as $ns => $dbkeys ) {
264 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
265 foreach ( $diffs as $dbk => $id ) {
266 $arr[] = array(
267 'tl_from' => $this->mId,
268 'tl_namespace' => $ns,
269 'tl_title' => $dbk
270 );
271 }
272 }
273 return $arr;
274 }
275
276 /**
277 * Get an array of image insertions
278 * Skips the names specified in $existing
279 * @access private
280 */
281 function getImageInsertions( $existing = array() ) {
282 $arr = array();
283 $diffs = array_diff_key( $this->mImages, $existing );
284 foreach( $diffs as $iname => $dummy ) {
285 $arr[] = array(
286 'il_from' => $this->mId,
287 'il_to' => $iname
288 );
289 }
290 return $arr;
291 }
292
293 /**
294 * Get an array of externallinks insertions. Skips the names specified in $existing
295 * @access private
296 */
297 function getExternalInsertions( $existing = array() ) {
298 $arr = array();
299 $diffs = array_diff_key( $this->mExternals, $existing );
300 foreach( $diffs as $url => $dummy ) {
301 $arr[] = array(
302 'el_from' => $this->mId,
303 'el_to' => $url,
304 'el_index' => wfMakeUrlIndex( $url ),
305 );
306 }
307 return $arr;
308 }
309
310 /**
311 * Get an array of category insertions
312 * @param array $existing Array mapping existing category names to sort keys. If both
313 * match a link in $this, the link will be omitted from the output
314 * @access private
315 */
316 function getCategoryInsertions( $existing = array() ) {
317 $diffs = array_diff_assoc( $this->mCategories, $existing );
318 $arr = array();
319 foreach ( $diffs as $name => $sortkey ) {
320 $arr[] = array(
321 'cl_from' => $this->mId,
322 'cl_to' => $name,
323 'cl_sortkey' => $sortkey,
324 'cl_timestamp' => $this->mDb->timestamp()
325 );
326 }
327 return $arr;
328 }
329
330 /**
331 * Given an array of existing links, returns those links which are not in $this
332 * and thus should be deleted.
333 * @access private
334 */
335 function getLinkDeletions( $existing ) {
336 $del = array();
337 foreach ( $existing as $ns => $dbkeys ) {
338 if ( isset( $this->mLinks[$ns] ) ) {
339 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
340 } else {
341 $del[$ns] = $existing[$ns];
342 }
343 }
344 return $del;
345 }
346
347 /**
348 * Given an array of existing templates, returns those templates which are not in $this
349 * and thus should be deleted.
350 * @access private
351 */
352 function getTemplateDeletions( $existing ) {
353 $del = array();
354 foreach ( $existing as $ns => $dbkeys ) {
355 if ( isset( $this->mTemplates[$ns] ) ) {
356 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
357 } else {
358 $del[$ns] = $existing[$ns];
359 }
360 }
361 return $del;
362 }
363
364 /**
365 * Given an array of existing images, returns those images which are not in $this
366 * and thus should be deleted.
367 * @access private
368 */
369 function getImageDeletions( $existing ) {
370 return array_diff_key( $existing, $this->mImages );
371 }
372
373 /**
374 * Given an array of existing external links, returns those links which are not
375 * in $this and thus should be deleted.
376 * @access private
377 */
378 function getExternalDeletions( $existing ) {
379 return array_diff_key( $existing, $this->mExternals );
380 }
381
382 /**
383 * Given an array of existing categories, returns those categories which are not in $this
384 * and thus should be deleted.
385 * @access private
386 */
387 function getCategoryDeletions( $existing ) {
388 return array_diff_assoc( $existing, $this->mCategories );
389 }
390
391 /**
392 * Get an array of existing links, as a 2-D array
393 * @access private
394 */
395 function getExistingLinks() {
396 $fname = 'LinksUpdate::getExistingLinks';
397 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
398 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
399 $arr = array();
400 while ( $row = $this->mDb->fetchObject( $res ) ) {
401 if ( !isset( $arr[$row->pl_namespace] ) ) {
402 $arr[$row->pl_namespace] = array();
403 }
404 $arr[$row->pl_namespace][$row->pl_title] = 1;
405 }
406 $this->mDb->freeResult( $res );
407 return $arr;
408 }
409
410 /**
411 * Get an array of existing templates, as a 2-D array
412 * @access private
413 */
414 function getExistingTemplates() {
415 $fname = 'LinksUpdate::getExistingTemplates';
416 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
417 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
418 $arr = array();
419 while ( $row = $this->mDb->fetchObject( $res ) ) {
420 if ( !isset( $arr[$row->tl_namespace] ) ) {
421 $arr[$row->tl_namespace] = array();
422 }
423 $arr[$row->tl_namespace][$row->tl_title] = 1;
424 }
425 $this->mDb->freeResult( $res );
426 return $arr;
427 }
428
429 /**
430 * Get an array of existing images, image names in the keys
431 * @access private
432 */
433 function getExistingImages() {
434 $fname = 'LinksUpdate::getExistingImages';
435 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
436 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
437 $arr = array();
438 while ( $row = $this->mDb->fetchObject( $res ) ) {
439 $arr[$row->il_to] = 1;
440 }
441 $this->mDb->freeResult( $res );
442 return $arr;
443 }
444
445 /**
446 * Get an array of existing external links, URLs in the keys
447 * @access private
448 */
449 function getExistingExternals() {
450 $fname = 'LinksUpdate::getExistingExternals';
451 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
452 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
453 $arr = array();
454 while ( $row = $this->mDb->fetchObject( $res ) ) {
455 $arr[$row->el_to] = 1;
456 }
457 $this->mDb->freeResult( $res );
458 return $arr;
459 }
460
461 /**
462 * Get an array of existing categories, with the name in the key and sort key in the value.
463 * @access private
464 */
465 function getExistingCategories() {
466 $fname = 'LinksUpdate::getExistingCategories';
467 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
468 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
469 $arr = array();
470 while ( $row = $this->mDb->fetchObject( $res ) ) {
471 $arr[$row->cl_to] = $row->cl_sortkey;
472 }
473 $this->mDb->freeResult( $res );
474 return $arr;
475 }
476 }
477 ?>