17 $mId, //!< Page ID of the article linked from
18 $mTitle, //!< Title object of the article linked from
19 $mLinks, //!< Map of title strings to IDs for the links in the document
20 $mImages, //!< DB keys of the images used, in the array key only
21 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
22 $mExternals, //!< URLs of external links, array key only
23 $mCategories, //!< Map of category names to sort keys
24 $mInterlangs, //!< Map of language codes to titles
25 $mDb, //!< Database connection reference
26 $mOptions, //!< SELECT options to be used (array)
27 $mRecursive; //!< Whether to queue jobs for recursive updates
32 * Initialize private variables
33 * @param $title Integer: FIXME
34 * @param $parserOutput FIXME
35 * @param $recursive Boolean: FIXME, default 'true'.
37 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
38 global $wgAntiLockFlags;
40 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
41 $this->mOptions
= array();
43 $this->mOptions
= array( 'FOR UPDATE' );
45 $this->mDb
=& wfGetDB( DB_MASTER
);
47 if ( !is_object( $title ) ) {
48 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
49 "Please see Article::editUpdates() for an invocation example.\n" );
51 $this->mTitle
= $title;
52 $this->mId
= $title->getArticleID();
54 $this->mLinks
= $parserOutput->getLinks();
55 $this->mImages
= $parserOutput->getImages();
56 $this->mTemplates
= $parserOutput->getTemplates();
57 $this->mExternals
= $parserOutput->getExternalLinks();
58 $this->mCategories
= $parserOutput->getCategories();
60 # Convert the format of the interlanguage links
61 # I didn't want to change it in the ParserOutput, because that array is passed all
62 # the way back to the skin, so either a skin API break would be required, or an
63 # inefficient back-conversion.
64 $ill = $parserOutput->getLanguageLinks();
65 $this->mInterlangs
= array();
66 foreach ( $ill as $link ) {
67 list( $key, $title ) = explode( ':', $link, 2 );
68 $this->mInterlangs
[$key] = $title;
71 $this->mRecursive
= $recursive;
75 * Update link tables with outgoing links from an updated article
78 global $wgUseDumbLinkUpdate;
79 if ( $wgUseDumbLinkUpdate ) {
80 $this->doDumbUpdate();
82 $this->doIncrementalUpdate();
86 function doIncrementalUpdate() {
87 $fname = 'LinksUpdate::doIncrementalUpdate';
88 wfProfileIn( $fname );
91 $existing = $this->getExistingLinks();
92 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
93 $this->getLinkInsertions( $existing ) );
96 $existing = $this->getExistingImages();
97 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
98 $this->getImageInsertions( $existing ) );
100 # Invalidate all image description pages which had links added or removed
101 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
102 $this->invalidateImageDescriptions( $imageUpdates );
105 $existing = $this->getExistingExternals();
106 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
107 $this->getExternalInsertions( $existing ) );
110 $existing = $this->getExistingInterlangs();
111 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
112 $this->getInterlangInsertions( $existing ) );
115 $existing = $this->getExistingTemplates();
116 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
117 $this->getTemplateInsertions( $existing ) );
119 # Refresh links of all pages including this page
120 if ( $this->mRecursive
) {
121 $tlto = $this->mTitle
->getTemplateLinksTo();
122 if ( count( $tlto ) ) {
123 require_once( 'JobQueue.php' );
124 Job
::queueLinksJobs( $tlto );
129 $existing = $this->getExistingCategories();
130 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
131 $this->getCategoryInsertions( $existing ) );
133 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
134 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
135 $this->invalidateCategories( $categoryUpdates );
137 wfProfileOut( $fname );
141 * Link update which clears the previous entries and inserts new ones
142 * May be slower or faster depending on level of lock contention and write speed of DB
143 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
145 function doDumbUpdate() {
146 $fname = 'LinksUpdate::doDumbUpdate';
147 wfProfileIn( $fname );
149 # Refresh category pages and image description pages
150 $existing = $this->getExistingCategories();
151 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
152 $existing = $this->getExistingImages();
153 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
155 # Refresh links of all pages including this page
156 if ( $this->mRecursive
) {
157 $tlto = $this->mTitle
->getTemplateLinksTo();
158 if ( count( $tlto ) ) {
159 require_once( 'JobQueue.php' );
160 Job
::queueLinksJobs( $tlto );
164 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
165 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
166 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
167 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
168 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
169 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
171 # Update the cache of all the category pages and image description pages which were changed
172 $this->invalidateCategories( $categoryUpdates );
173 $this->invalidateImageDescriptions( $imageUpdates );
175 wfProfileOut( $fname );
179 * Invalidate the cache of a list of pages from a single namespace
181 * @param integer $namespace
182 * @param array $dbkeys
184 function invalidatePages( $namespace, $dbkeys ) {
185 $fname = 'LinksUpdate::invalidatePages';
187 if ( !count( $dbkeys ) ) {
192 * Determine which pages need to be updated
193 * This is necessary to prevent the job queue from smashing the DB with
194 * large numbers of concurrent invalidations of the same page
196 $now = $this->mDb
->timestamp();
198 $res = $this->mDb
->select( 'page', array( 'page_id' ),
200 'page_namespace' => $namespace,
201 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
202 'page_touched < ' . $this->mDb
->addQuotes( $now )
205 while ( $row = $this->mDb
->fetchObject( $res ) ) {
206 $ids[] = $row->page_id
;
208 if ( !count( $ids ) ) {
214 * We still need the page_touched condition, in case the row has changed since
215 * the non-locking select above.
217 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
219 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
220 'page_touched < ' . $this->mDb
->addQuotes( $now )
225 function invalidateCategories( $cats ) {
226 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
229 function invalidateImageDescriptions( $images ) {
230 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
233 function dumbTableUpdate( $table, $insertions, $fromField ) {
234 $fname = 'LinksUpdate::dumbTableUpdate';
235 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
236 if ( count( $insertions ) ) {
237 # The link array was constructed without FOR UPDATE, so there may be collisions
238 # This may cause minor link table inconsistencies, which is better than
239 # crippling the site with lock contention.
240 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
245 * Make a WHERE clause from a 2-d NS/dbkey array
247 * @param array $arr 2-d array indexed by namespace and DB key
248 * @param string $prefix Field name prefix, without the underscore
250 function makeWhereFrom2d( &$arr, $prefix ) {
252 $lb->setArray( $arr );
253 return $lb->constructSet( $prefix, $this->mDb
);
257 * Update a table by doing a delete query then an insert query
260 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
261 $fname = 'LinksUpdate::incrTableUpdate';
262 $where = array( "{$prefix}_from" => $this->mId
);
263 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
264 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
271 if ( $table == 'langlinks' ) {
272 $toField = 'll_lang';
274 $toField = $prefix . '_to';
276 if ( count( $deletions ) ) {
277 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
283 $this->mDb
->delete( $table, $where, $fname );
285 if ( count( $insertions ) ) {
286 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
292 * Get an array of pagelinks insertions for passing to the DB
293 * Skips the titles specified by the 2-D array $existing
296 function getLinkInsertions( $existing = array() ) {
298 foreach( $this->mLinks
as $ns => $dbkeys ) {
299 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
300 # in GlobalFunctions.php
301 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
302 foreach ( $diffs as $dbk => $id ) {
304 'pl_from' => $this->mId
,
305 'pl_namespace' => $ns,
314 * Get an array of template insertions. Like getLinkInsertions()
317 function getTemplateInsertions( $existing = array() ) {
319 foreach( $this->mTemplates
as $ns => $dbkeys ) {
320 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
321 foreach ( $diffs as $dbk => $id ) {
323 'tl_from' => $this->mId
,
324 'tl_namespace' => $ns,
333 * Get an array of image insertions
334 * Skips the names specified in $existing
337 function getImageInsertions( $existing = array() ) {
339 $diffs = array_diff_key( $this->mImages
, $existing );
340 foreach( $diffs as $iname => $dummy ) {
342 'il_from' => $this->mId
,
350 * Get an array of externallinks insertions. Skips the names specified in $existing
353 function getExternalInsertions( $existing = array() ) {
355 $diffs = array_diff_key( $this->mExternals
, $existing );
356 foreach( $diffs as $url => $dummy ) {
358 'el_from' => $this->mId
,
360 'el_index' => wfMakeUrlIndex( $url ),
367 * Get an array of category insertions
368 * @param array $existing Array mapping existing category names to sort keys. If both
369 * match a link in $this, the link will be omitted from the output
372 function getCategoryInsertions( $existing = array() ) {
373 $diffs = array_diff_assoc( $this->mCategories
, $existing );
375 foreach ( $diffs as $name => $sortkey ) {
377 'cl_from' => $this->mId
,
379 'cl_sortkey' => $sortkey,
380 'cl_timestamp' => $this->mDb
->timestamp()
387 * Get an array of interlanguage link insertions
388 * @param array $existing Array mapping existing language codes to titles
391 function getInterlangInsertions( $existing = array() ) {
392 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
394 foreach( $diffs as $lang => $title ) {
396 'll_from' => $this->mId
,
405 * Given an array of existing links, returns those links which are not in $this
406 * and thus should be deleted.
409 function getLinkDeletions( $existing ) {
411 foreach ( $existing as $ns => $dbkeys ) {
412 if ( isset( $this->mLinks
[$ns] ) ) {
413 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
415 $del[$ns] = $existing[$ns];
422 * Given an array of existing templates, returns those templates which are not in $this
423 * and thus should be deleted.
426 function getTemplateDeletions( $existing ) {
428 foreach ( $existing as $ns => $dbkeys ) {
429 if ( isset( $this->mTemplates
[$ns] ) ) {
430 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
432 $del[$ns] = $existing[$ns];
439 * Given an array of existing images, returns those images which are not in $this
440 * and thus should be deleted.
443 function getImageDeletions( $existing ) {
444 return array_diff_key( $existing, $this->mImages
);
448 * Given an array of existing external links, returns those links which are not
449 * in $this and thus should be deleted.
452 function getExternalDeletions( $existing ) {
453 return array_diff_key( $existing, $this->mExternals
);
457 * Given an array of existing categories, returns those categories which are not in $this
458 * and thus should be deleted.
461 function getCategoryDeletions( $existing ) {
462 return array_diff_assoc( $existing, $this->mCategories
);
466 * Given an array of existing interlanguage links, returns those links which are not
467 * in $this and thus should be deleted.
470 function getInterlangDeletions( $existing ) {
471 return array_diff_assoc( $existing, $this->mInterlangs
);
475 * Get an array of existing links, as a 2-D array
478 function getExistingLinks() {
479 $fname = 'LinksUpdate::getExistingLinks';
480 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
481 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
483 while ( $row = $this->mDb
->fetchObject( $res ) ) {
484 if ( !isset( $arr[$row->pl_namespace
] ) ) {
485 $arr[$row->pl_namespace
] = array();
487 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
489 $this->mDb
->freeResult( $res );
494 * Get an array of existing templates, as a 2-D array
497 function getExistingTemplates() {
498 $fname = 'LinksUpdate::getExistingTemplates';
499 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
500 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
502 while ( $row = $this->mDb
->fetchObject( $res ) ) {
503 if ( !isset( $arr[$row->tl_namespace
] ) ) {
504 $arr[$row->tl_namespace
] = array();
506 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
508 $this->mDb
->freeResult( $res );
513 * Get an array of existing images, image names in the keys
516 function getExistingImages() {
517 $fname = 'LinksUpdate::getExistingImages';
518 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
519 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
521 while ( $row = $this->mDb
->fetchObject( $res ) ) {
522 $arr[$row->il_to
] = 1;
524 $this->mDb
->freeResult( $res );
529 * Get an array of existing external links, URLs in the keys
532 function getExistingExternals() {
533 $fname = 'LinksUpdate::getExistingExternals';
534 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
535 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
537 while ( $row = $this->mDb
->fetchObject( $res ) ) {
538 $arr[$row->el_to
] = 1;
540 $this->mDb
->freeResult( $res );
545 * Get an array of existing categories, with the name in the key and sort key in the value.
548 function getExistingCategories() {
549 $fname = 'LinksUpdate::getExistingCategories';
550 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
551 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
553 while ( $row = $this->mDb
->fetchObject( $res ) ) {
554 $arr[$row->cl_to
] = $row->cl_sortkey
;
556 $this->mDb
->freeResult( $res );
561 * Get an array of existing interlanguage links, with the language code in the key and the
562 * title in the value.
565 function getExistingInterlangs() {
566 $fname = 'LinksUpdate::getExistingInterlangs';
567 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
568 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
570 while ( $row = $this->mDb
->fetchObject( $res ) ) {
571 $arr[$row->ll_lang
] = $row->ll_title
;