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 $mInterlangs, //!< Map of language codes to titles
24 $mDb, //!< Database connection reference
25 $mOptions, //!< SELECT options to be used (array)
26 $mRecursive; //!< Whether to queue jobs for recursive updates
31 * Initialize private variables
32 * @param $title Integer: FIXME
33 * @param $parserOutput FIXME
34 * @param $recursive Boolean: FIXME, default 'true'.
36 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
37 global $wgAntiLockFlags;
39 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
40 $this->mOptions
= array();
42 $this->mOptions
= array( 'FOR UPDATE' );
44 $this->mDb
=& wfGetDB( DB_MASTER
);
46 if ( !is_object( $title ) ) {
47 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
48 "Please see Article::editUpdates() for an invocation example.\n" );
50 $this->mTitle
= $title;
51 $this->mId
= $title->getArticleID();
53 $this->mLinks
= $parserOutput->getLinks();
54 $this->mImages
= $parserOutput->getImages();
55 $this->mTemplates
= $parserOutput->getTemplates();
56 $this->mExternals
= $parserOutput->getExternalLinks();
57 $this->mCategories
= $parserOutput->getCategories();
59 # Convert the format of the interlanguage links
60 # I didn't want to change it in the ParserOutput, because that array is passed all
61 # the way back to the skin, so either a skin API break would be required, or an
62 # inefficient back-conversion.
63 $ill = $parserOutput->getLanguageLinks();
64 $this->mInterlangs
= array();
65 foreach ( $ill as $link ) {
66 list( $key, $title ) = explode( ':', $link, 2 );
67 $this->mInterlangs
[$key] = $title;
70 $this->mRecursive
= $recursive;
74 * Update link tables with outgoing links from an updated article
77 global $wgUseDumbLinkUpdate;
78 if ( $wgUseDumbLinkUpdate ) {
79 $this->doDumbUpdate();
81 $this->doIncrementalUpdate();
85 function doIncrementalUpdate() {
86 $fname = 'LinksUpdate::doIncrementalUpdate';
87 wfProfileIn( $fname );
90 $existing = $this->getExistingLinks();
91 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
92 $this->getLinkInsertions( $existing ) );
95 $existing = $this->getExistingImages();
96 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
97 $this->getImageInsertions( $existing ) );
99 # Invalidate all image description pages which had links added or removed
100 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
101 $this->invalidateImageDescriptions( $imageUpdates );
104 $existing = $this->getExistingExternals();
105 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
106 $this->getExternalInsertions( $existing ) );
109 $existing = $this->getExistingInterlangs();
110 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
111 $this->getInterlangInsertions( $existing ) );
114 $existing = $this->getExistingTemplates();
115 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
116 $this->getTemplateInsertions( $existing ) );
118 # Refresh links of all pages including this page
119 if ( $this->mRecursive
) {
120 $tlto = $this->mTitle
->getTemplateLinksTo();
121 if ( count( $tlto ) ) {
122 require_once( 'JobQueue.php' );
123 Job
::queueLinksJobs( $tlto );
128 $existing = $this->getExistingCategories();
129 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
130 $this->getCategoryInsertions( $existing ) );
132 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
133 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
134 $this->invalidateCategories( $categoryUpdates );
136 wfProfileOut( $fname );
140 * Link update which clears the previous entries and inserts new ones
141 * May be slower or faster depending on level of lock contention and write speed of DB
142 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
144 function doDumbUpdate() {
145 $fname = 'LinksUpdate::doDumbUpdate';
146 wfProfileIn( $fname );
148 # Refresh category pages and image description pages
149 $existing = $this->getExistingCategories();
150 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
151 $existing = $this->getExistingImages();
152 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
154 # Refresh links of all pages including this page
155 if ( $this->mRecursive
) {
156 $tlto = $this->mTitle
->getTemplateLinksTo();
157 if ( count( $tlto ) ) {
158 require_once( 'JobQueue.php' );
159 Job
::queueLinksJobs( $tlto );
163 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
164 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
165 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
166 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
167 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
168 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
170 # Update the cache of all the category pages and image description pages which were changed
171 $this->invalidateCategories( $categoryUpdates );
172 $this->invalidateImageDescriptions( $imageUpdates );
174 wfProfileOut( $fname );
178 * Invalidate the cache of a list of pages from a single namespace
180 * @param integer $namespace
181 * @param array $dbkeys
183 function invalidatePages( $namespace, $dbkeys ) {
184 $fname = 'LinksUpdate::invalidatePages';
186 if ( !count( $dbkeys ) ) {
191 * Determine which pages need to be updated
192 * This is necessary to prevent the job queue from smashing the DB with
193 * large numbers of concurrent invalidations of the same page
195 $now = $this->mDb
->timestamp();
197 $res = $this->mDb
->select( 'page', array( 'page_id' ),
199 'page_namespace' => $namespace,
200 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
201 'page_touched < ' . $this->mDb
->addQuotes( $now )
204 while ( $row = $this->mDb
->fetchObject( $res ) ) {
205 $ids[] = $row->page_id
;
207 if ( !count( $ids ) ) {
213 * We still need the page_touched condition, in case the row has changed since
214 * the non-locking select above.
216 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
218 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
219 'page_touched < ' . $this->mDb
->addQuotes( $now )
224 function invalidateCategories( $cats ) {
225 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
228 function invalidateImageDescriptions( $images ) {
229 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
232 function dumbTableUpdate( $table, $insertions, $fromField ) {
233 $fname = 'LinksUpdate::dumbTableUpdate';
234 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
235 if ( count( $insertions ) ) {
236 # The link array was constructed without FOR UPDATE, so there may be collisions
237 # This may cause minor link table inconsistencies, which is better than
238 # crippling the site with lock contention.
239 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
244 * Make a WHERE clause from a 2-d NS/dbkey array
246 * @param array $arr 2-d array indexed by namespace and DB key
247 * @param string $prefix Field name prefix, without the underscore
249 function makeWhereFrom2d( &$arr, $prefix ) {
251 $lb->setArray( $arr );
252 return $lb->constructSet( $prefix, $this->mDb
);
256 * Update a table by doing a delete query then an insert query
259 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
260 $fname = 'LinksUpdate::incrTableUpdate';
261 $where = array( "{$prefix}_from" => $this->mId
);
262 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
263 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
270 if ( $table == 'langlinks' ) {
271 $toField = 'll_lang';
273 $toField = $prefix . '_to';
275 if ( count( $deletions ) ) {
276 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
282 $this->mDb
->delete( $table, $where, $fname );
284 if ( count( $insertions ) ) {
285 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
291 * Get an array of pagelinks insertions for passing to the DB
292 * Skips the titles specified by the 2-D array $existing
295 function getLinkInsertions( $existing = array() ) {
297 foreach( $this->mLinks
as $ns => $dbkeys ) {
298 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
299 # in GlobalFunctions.php
300 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
301 foreach ( $diffs as $dbk => $id ) {
303 'pl_from' => $this->mId
,
304 'pl_namespace' => $ns,
313 * Get an array of template insertions. Like getLinkInsertions()
316 function getTemplateInsertions( $existing = array() ) {
318 foreach( $this->mTemplates
as $ns => $dbkeys ) {
319 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
320 foreach ( $diffs as $dbk => $id ) {
322 'tl_from' => $this->mId
,
323 'tl_namespace' => $ns,
332 * Get an array of image insertions
333 * Skips the names specified in $existing
336 function getImageInsertions( $existing = array() ) {
338 $diffs = array_diff_key( $this->mImages
, $existing );
339 foreach( $diffs as $iname => $dummy ) {
341 'il_from' => $this->mId
,
349 * Get an array of externallinks insertions. Skips the names specified in $existing
352 function getExternalInsertions( $existing = array() ) {
354 $diffs = array_diff_key( $this->mExternals
, $existing );
355 foreach( $diffs as $url => $dummy ) {
357 'el_from' => $this->mId
,
359 'el_index' => wfMakeUrlIndex( $url ),
366 * Get an array of category insertions
367 * @param array $existing Array mapping existing category names to sort keys. If both
368 * match a link in $this, the link will be omitted from the output
371 function getCategoryInsertions( $existing = array() ) {
372 $diffs = array_diff_assoc( $this->mCategories
, $existing );
374 foreach ( $diffs as $name => $sortkey ) {
376 'cl_from' => $this->mId
,
378 'cl_sortkey' => $sortkey,
379 'cl_timestamp' => $this->mDb
->timestamp()
386 * Get an array of interlanguage link insertions
387 * @param array $existing Array mapping existing language codes to titles
390 function getInterlangInsertions( $existing = array() ) {
391 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
393 foreach( $diffs as $lang => $title ) {
395 'll_from' => $this->mId
,
404 * Given an array of existing links, returns those links which are not in $this
405 * and thus should be deleted.
408 function getLinkDeletions( $existing ) {
410 foreach ( $existing as $ns => $dbkeys ) {
411 if ( isset( $this->mLinks
[$ns] ) ) {
412 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
414 $del[$ns] = $existing[$ns];
421 * Given an array of existing templates, returns those templates which are not in $this
422 * and thus should be deleted.
425 function getTemplateDeletions( $existing ) {
427 foreach ( $existing as $ns => $dbkeys ) {
428 if ( isset( $this->mTemplates
[$ns] ) ) {
429 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
431 $del[$ns] = $existing[$ns];
438 * Given an array of existing images, returns those images which are not in $this
439 * and thus should be deleted.
442 function getImageDeletions( $existing ) {
443 return array_diff_key( $existing, $this->mImages
);
447 * Given an array of existing external links, returns those links which are not
448 * in $this and thus should be deleted.
451 function getExternalDeletions( $existing ) {
452 return array_diff_key( $existing, $this->mExternals
);
456 * Given an array of existing categories, returns those categories which are not in $this
457 * and thus should be deleted.
460 function getCategoryDeletions( $existing ) {
461 return array_diff_assoc( $existing, $this->mCategories
);
465 * Given an array of existing interlanguage links, returns those links which are not
466 * in $this and thus should be deleted.
469 function getInterlangDeletions( $existing ) {
470 return array_diff_assoc( $existing, $this->mInterlangs
);
474 * Get an array of existing links, as a 2-D array
477 function getExistingLinks() {
478 $fname = 'LinksUpdate::getExistingLinks';
479 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
480 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
482 while ( $row = $this->mDb
->fetchObject( $res ) ) {
483 if ( !isset( $arr[$row->pl_namespace
] ) ) {
484 $arr[$row->pl_namespace
] = array();
486 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
488 $this->mDb
->freeResult( $res );
493 * Get an array of existing templates, as a 2-D array
496 function getExistingTemplates() {
497 $fname = 'LinksUpdate::getExistingTemplates';
498 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
499 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
501 while ( $row = $this->mDb
->fetchObject( $res ) ) {
502 if ( !isset( $arr[$row->tl_namespace
] ) ) {
503 $arr[$row->tl_namespace
] = array();
505 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
507 $this->mDb
->freeResult( $res );
512 * Get an array of existing images, image names in the keys
515 function getExistingImages() {
516 $fname = 'LinksUpdate::getExistingImages';
517 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
518 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
520 while ( $row = $this->mDb
->fetchObject( $res ) ) {
521 $arr[$row->il_to
] = 1;
523 $this->mDb
->freeResult( $res );
528 * Get an array of existing external links, URLs in the keys
531 function getExistingExternals() {
532 $fname = 'LinksUpdate::getExistingExternals';
533 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
534 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
536 while ( $row = $this->mDb
->fetchObject( $res ) ) {
537 $arr[$row->el_to
] = 1;
539 $this->mDb
->freeResult( $res );
544 * Get an array of existing categories, with the name in the key and sort key in the value.
547 function getExistingCategories() {
548 $fname = 'LinksUpdate::getExistingCategories';
549 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
550 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
552 while ( $row = $this->mDb
->fetchObject( $res ) ) {
553 $arr[$row->cl_to
] = $row->cl_sortkey
;
555 $this->mDb
->freeResult( $res );
560 * Get an array of existing interlanguage links, with the language code in the key and the
561 * title in the value.
564 function getExistingInterlangs() {
565 $fname = 'LinksUpdate::getExistingInterlangs';
566 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
567 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
569 while ( $row = $this->mDb
->fetchObject( $res ) ) {
570 $arr[$row->ll_lang
] = $row->ll_title
;