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 );
177 function invalidateCategories( $cats ) {
178 $fname = 'LinksUpdate::invalidateCategories';
179 if ( count( $cats ) ) {
180 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
182 'page_namespace' => NS_CATEGORY
,
183 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
189 function invalidateImageDescriptions( $images ) {
190 $fname = 'LinksUpdate::invalidateImageDescriptions';
191 if ( count( $images ) ) {
192 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
194 'page_namespace' => NS_IMAGE
,
195 'page_title IN (' . $this->mDb
->makeList( array_keys( $images ) ) . ')'
201 function dumbTableUpdate( $table, $insertions, $fromField ) {
202 $fname = 'LinksUpdate::dumbTableUpdate';
203 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
204 if ( count( $insertions ) ) {
205 # The link array was constructed without FOR UPDATE, so there may be collisions
206 # This may cause minor link table inconsistencies, which is better than
207 # crippling the site with lock contention.
208 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
213 * Make a WHERE clause from a 2-d NS/dbkey array
215 * @param array $arr 2-d array indexed by namespace and DB key
216 * @param string $prefix Field name prefix, without the underscore
218 function makeWhereFrom2d( &$arr, $prefix ) {
220 $lb->setArray( $arr );
221 return $lb->constructSet( $prefix, $this->mDb
);
225 * Update a table by doing a delete query then an insert query
228 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
229 $fname = 'LinksUpdate::incrTableUpdate';
230 $where = array( "{$prefix}_from" => $this->mId
);
231 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
232 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
239 if ( $table == 'langlinks' ) {
240 $toField = 'll_lang';
242 $toField = $prefix . '_to';
244 if ( count( $deletions ) ) {
245 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
251 $this->mDb
->delete( $table, $where, $fname );
253 if ( count( $insertions ) ) {
254 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
260 * Get an array of pagelinks insertions for passing to the DB
261 * Skips the titles specified by the 2-D array $existing
264 function getLinkInsertions( $existing = array() ) {
266 foreach( $this->mLinks
as $ns => $dbkeys ) {
267 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
268 # in GlobalFunctions.php
269 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
270 foreach ( $diffs as $dbk => $id ) {
272 'pl_from' => $this->mId
,
273 'pl_namespace' => $ns,
282 * Get an array of template insertions. Like getLinkInsertions()
285 function getTemplateInsertions( $existing = array() ) {
287 foreach( $this->mTemplates
as $ns => $dbkeys ) {
288 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
289 foreach ( $diffs as $dbk => $id ) {
291 'tl_from' => $this->mId
,
292 'tl_namespace' => $ns,
301 * Get an array of image insertions
302 * Skips the names specified in $existing
305 function getImageInsertions( $existing = array() ) {
307 $diffs = array_diff_key( $this->mImages
, $existing );
308 foreach( $diffs as $iname => $dummy ) {
310 'il_from' => $this->mId
,
318 * Get an array of externallinks insertions. Skips the names specified in $existing
321 function getExternalInsertions( $existing = array() ) {
323 $diffs = array_diff_key( $this->mExternals
, $existing );
324 foreach( $diffs as $url => $dummy ) {
326 'el_from' => $this->mId
,
328 'el_index' => wfMakeUrlIndex( $url ),
335 * Get an array of category insertions
336 * @param array $existing Array mapping existing category names to sort keys. If both
337 * match a link in $this, the link will be omitted from the output
340 function getCategoryInsertions( $existing = array() ) {
341 $diffs = array_diff_assoc( $this->mCategories
, $existing );
343 foreach ( $diffs as $name => $sortkey ) {
345 'cl_from' => $this->mId
,
347 'cl_sortkey' => $sortkey,
348 'cl_timestamp' => $this->mDb
->timestamp()
355 * Get an array of interlanguage link insertions
356 * @param array $existing Array mapping existing language codes to titles
359 function getInterlangInsertions( $existing = array() ) {
360 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
362 foreach( $diffs as $lang => $title ) {
364 'll_from' => $this->mId
,
373 * Given an array of existing links, returns those links which are not in $this
374 * and thus should be deleted.
377 function getLinkDeletions( $existing ) {
379 foreach ( $existing as $ns => $dbkeys ) {
380 if ( isset( $this->mLinks
[$ns] ) ) {
381 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
383 $del[$ns] = $existing[$ns];
390 * Given an array of existing templates, returns those templates which are not in $this
391 * and thus should be deleted.
394 function getTemplateDeletions( $existing ) {
396 foreach ( $existing as $ns => $dbkeys ) {
397 if ( isset( $this->mTemplates
[$ns] ) ) {
398 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
400 $del[$ns] = $existing[$ns];
407 * Given an array of existing images, returns those images which are not in $this
408 * and thus should be deleted.
411 function getImageDeletions( $existing ) {
412 return array_diff_key( $existing, $this->mImages
);
416 * Given an array of existing external links, returns those links which are not
417 * in $this and thus should be deleted.
420 function getExternalDeletions( $existing ) {
421 return array_diff_key( $existing, $this->mExternals
);
425 * Given an array of existing categories, returns those categories which are not in $this
426 * and thus should be deleted.
429 function getCategoryDeletions( $existing ) {
430 return array_diff_assoc( $existing, $this->mCategories
);
434 * Given an array of existing interlanguage links, returns those links which are not
435 * in $this and thus should be deleted.
438 function getInterlangDeletions( $existing ) {
439 return array_diff_assoc( $existing, $this->mInterlangs
);
443 * Get an array of existing links, as a 2-D array
446 function getExistingLinks() {
447 $fname = 'LinksUpdate::getExistingLinks';
448 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
449 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
451 while ( $row = $this->mDb
->fetchObject( $res ) ) {
452 if ( !isset( $arr[$row->pl_namespace
] ) ) {
453 $arr[$row->pl_namespace
] = array();
455 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
457 $this->mDb
->freeResult( $res );
462 * Get an array of existing templates, as a 2-D array
465 function getExistingTemplates() {
466 $fname = 'LinksUpdate::getExistingTemplates';
467 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
468 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
470 while ( $row = $this->mDb
->fetchObject( $res ) ) {
471 if ( !isset( $arr[$row->tl_namespace
] ) ) {
472 $arr[$row->tl_namespace
] = array();
474 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
476 $this->mDb
->freeResult( $res );
481 * Get an array of existing images, image names in the keys
484 function getExistingImages() {
485 $fname = 'LinksUpdate::getExistingImages';
486 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
487 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
489 while ( $row = $this->mDb
->fetchObject( $res ) ) {
490 $arr[$row->il_to
] = 1;
492 $this->mDb
->freeResult( $res );
497 * Get an array of existing external links, URLs in the keys
500 function getExistingExternals() {
501 $fname = 'LinksUpdate::getExistingExternals';
502 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
503 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
505 while ( $row = $this->mDb
->fetchObject( $res ) ) {
506 $arr[$row->el_to
] = 1;
508 $this->mDb
->freeResult( $res );
513 * Get an array of existing categories, with the name in the key and sort key in the value.
516 function getExistingCategories() {
517 $fname = 'LinksUpdate::getExistingCategories';
518 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
519 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
521 while ( $row = $this->mDb
->fetchObject( $res ) ) {
522 $arr[$row->cl_to
] = $row->cl_sortkey
;
524 $this->mDb
->freeResult( $res );
529 * Get an array of existing interlanguage links, with the language code in the key and the
530 * title in the value.
533 function getExistingInterlangs() {
534 $fname = 'LinksUpdate::getExistingInterlangs';
535 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
536 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
538 while ( $row = $this->mDb
->fetchObject( $res ) ) {
539 $arr[$row->ll_lang
] = $row->ll_title
;