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
33 * @param string $title
35 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
36 global $wgAntiLockFlags;
38 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
39 $this->mOptions
= array();
41 $this->mOptions
= array( 'FOR UPDATE' );
43 $this->mDb
=& wfGetDB( DB_MASTER
);
45 if ( !is_object( $title ) ) {
46 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
47 "Please see Article::editUpdates() for an invocation example.\n" );
49 $this->mTitle
= $title;
50 $this->mId
= $title->getArticleID();
52 $this->mLinks
= $parserOutput->getLinks();
53 $this->mImages
= $parserOutput->getImages();
54 $this->mTemplates
= $parserOutput->getTemplates();
55 $this->mExternals
= $parserOutput->getExternalLinks();
56 $this->mCategories
= $parserOutput->getCategories();
58 # Convert the format of the interlanguage links
59 # I didn't want to change it in the ParserOutput, because that array is passed all
60 # the way back to the skin, so either a skin API break would be required, or an
61 # inefficient back-conversion.
62 $ill = $parserOutput->getLanguageLinks();
63 $this->mInterlangs
= array();
64 foreach ( $ill as $link ) {
65 list( $key, $title ) = explode( ':', $link, 2 );
66 $this->mInterlangs
[$key] = $title;
69 $this->mRecursive
= $recursive;
73 * Update link tables with outgoing links from an updated article
76 global $wgUseDumbLinkUpdate;
77 if ( $wgUseDumbLinkUpdate ) {
78 $this->doDumbUpdate();
80 $this->doIncrementalUpdate();
84 function doIncrementalUpdate() {
85 $fname = 'LinksUpdate::doIncrementalUpdate';
86 wfProfileIn( $fname );
89 $existing = $this->getExistingLinks();
90 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
91 $this->getLinkInsertions( $existing ) );
94 $existing = $this->getExistingImages();
95 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
96 $this->getImageInsertions( $existing ) );
98 # Invalidate all image description pages which had links added or removed
99 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
100 $this->invalidateImageDescriptions( $imageUpdates );
103 $existing = $this->getExistingExternals();
104 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
105 $this->getExternalInsertions( $existing ) );
108 $existing = $this->getExistingInterlangs();
109 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
110 $this->getInterlangInsertions( $existing ) );
113 $existing = $this->getExistingTemplates();
114 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
115 $this->getTemplateInsertions( $existing ) );
117 # Refresh links of all pages including this page
118 if ( $this->mRecursive
) {
119 $tlto = $this->mTitle
->getTemplateLinksTo();
120 if ( count( $tlto ) ) {
121 require_once( 'JobQueue.php' );
122 Job
::queueLinksJobs( $tlto );
127 $existing = $this->getExistingCategories();
128 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
129 $this->getCategoryInsertions( $existing ) );
131 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
132 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
133 $this->invalidateCategories( $categoryUpdates );
135 wfProfileOut( $fname );
139 * Link update which clears the previous entries and inserts new ones
140 * May be slower or faster depending on level of lock contention and write speed of DB
141 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
143 function doDumbUpdate() {
144 $fname = 'LinksUpdate::doDumbUpdate';
145 wfProfileIn( $fname );
147 # Refresh category pages and image description pages
148 $existing = $this->getExistingCategories();
149 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
150 $existing = $this->getExistingImages();
151 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
153 # Refresh links of all pages including this page
154 if ( $this->mRecursive
) {
155 $tlto = $this->mTitle
->getTemplateLinksTo();
156 if ( count( $tlto ) ) {
157 require_once( 'JobQueue.php' );
158 Job
::queueLinksJobs( $tlto );
162 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
163 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
164 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
165 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
166 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
167 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
169 # Update the cache of all the category pages and image description pages which were changed
170 $this->invalidateCategories( $categoryUpdates );
171 $this->invalidateImageDescriptions( $imageUpdates );
173 wfProfileOut( $fname );
176 function invalidateCategories( $cats ) {
177 $fname = 'LinksUpdate::invalidateCategories';
178 if ( count( $cats ) ) {
179 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
181 'page_namespace' => NS_CATEGORY
,
182 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
188 function invalidateImageDescriptions( $images ) {
189 $fname = 'LinksUpdate::invalidateImageDescriptions';
190 if ( count( $images ) ) {
191 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
193 'page_namespace' => NS_IMAGE
,
194 'page_title IN (' . $this->mDb
->makeList( array_keys( $images ) ) . ')'
200 function dumbTableUpdate( $table, $insertions, $fromField ) {
201 $fname = 'LinksUpdate::dumbTableUpdate';
202 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
203 if ( count( $insertions ) ) {
204 # The link array was constructed without FOR UPDATE, so there may be collisions
205 # This may cause minor link table inconsistencies, which is better than
206 # crippling the site with lock contention.
207 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
212 * Make a WHERE clause from a 2-d NS/dbkey array
214 * @param array $arr 2-d array indexed by namespace and DB key
215 * @param string $prefix Field name prefix, without the underscore
217 function makeWhereFrom2d( &$arr, $prefix ) {
219 $lb->setArray( $arr );
220 return $lb->constructSet( $prefix, $this->mDb
);
224 * Update a table by doing a delete query then an insert query
227 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
228 $fname = 'LinksUpdate::incrTableUpdate';
229 $where = array( "{$prefix}_from" => $this->mId
);
230 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
231 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
238 if ( $table == 'langlinks' ) {
239 $toField = 'll_lang';
241 $toField = $prefix . '_to';
243 if ( count( $deletions ) ) {
244 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
250 $this->mDb
->delete( $table, $where, $fname );
252 if ( count( $insertions ) ) {
253 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
259 * Get an array of pagelinks insertions for passing to the DB
260 * Skips the titles specified by the 2-D array $existing
263 function getLinkInsertions( $existing = array() ) {
265 foreach( $this->mLinks
as $ns => $dbkeys ) {
266 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
267 # in GlobalFunctions.php
268 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
269 foreach ( $diffs as $dbk => $id ) {
271 'pl_from' => $this->mId
,
272 'pl_namespace' => $ns,
281 * Get an array of template insertions. Like getLinkInsertions()
284 function getTemplateInsertions( $existing = array() ) {
286 foreach( $this->mTemplates
as $ns => $dbkeys ) {
287 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
288 foreach ( $diffs as $dbk => $id ) {
290 'tl_from' => $this->mId
,
291 'tl_namespace' => $ns,
300 * Get an array of image insertions
301 * Skips the names specified in $existing
304 function getImageInsertions( $existing = array() ) {
306 $diffs = array_diff_key( $this->mImages
, $existing );
307 foreach( $diffs as $iname => $dummy ) {
309 'il_from' => $this->mId
,
317 * Get an array of externallinks insertions. Skips the names specified in $existing
320 function getExternalInsertions( $existing = array() ) {
322 $diffs = array_diff_key( $this->mExternals
, $existing );
323 foreach( $diffs as $url => $dummy ) {
325 'el_from' => $this->mId
,
327 'el_index' => wfMakeUrlIndex( $url ),
334 * Get an array of category insertions
335 * @param array $existing Array mapping existing category names to sort keys. If both
336 * match a link in $this, the link will be omitted from the output
339 function getCategoryInsertions( $existing = array() ) {
340 $diffs = array_diff_assoc( $this->mCategories
, $existing );
342 foreach ( $diffs as $name => $sortkey ) {
344 'cl_from' => $this->mId
,
346 'cl_sortkey' => $sortkey,
347 'cl_timestamp' => $this->mDb
->timestamp()
354 * Get an array of interlanguage link insertions
355 * @param array $existing Array mapping existing language codes to titles
358 function getInterlangInsertions( $existing = array() ) {
359 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
361 foreach( $diffs as $lang => $title ) {
363 'll_from' => $this->mId
,
372 * Given an array of existing links, returns those links which are not in $this
373 * and thus should be deleted.
376 function getLinkDeletions( $existing ) {
378 foreach ( $existing as $ns => $dbkeys ) {
379 if ( isset( $this->mLinks
[$ns] ) ) {
380 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
382 $del[$ns] = $existing[$ns];
389 * Given an array of existing templates, returns those templates which are not in $this
390 * and thus should be deleted.
393 function getTemplateDeletions( $existing ) {
395 foreach ( $existing as $ns => $dbkeys ) {
396 if ( isset( $this->mTemplates
[$ns] ) ) {
397 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
399 $del[$ns] = $existing[$ns];
406 * Given an array of existing images, returns those images which are not in $this
407 * and thus should be deleted.
410 function getImageDeletions( $existing ) {
411 return array_diff_key( $existing, $this->mImages
);
415 * Given an array of existing external links, returns those links which are not
416 * in $this and thus should be deleted.
419 function getExternalDeletions( $existing ) {
420 return array_diff_key( $existing, $this->mExternals
);
424 * Given an array of existing categories, returns those categories which are not in $this
425 * and thus should be deleted.
428 function getCategoryDeletions( $existing ) {
429 return array_diff_assoc( $existing, $this->mCategories
);
433 * Given an array of existing interlanguage links, returns those links which are not
434 * in $this and thus should be deleted.
437 function getInterlangDeletions( $existing ) {
438 return array_diff_assoc( $existing, $this->mInterlangs
);
442 * Get an array of existing links, as a 2-D array
445 function getExistingLinks() {
446 $fname = 'LinksUpdate::getExistingLinks';
447 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
448 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
450 while ( $row = $this->mDb
->fetchObject( $res ) ) {
451 if ( !isset( $arr[$row->pl_namespace
] ) ) {
452 $arr[$row->pl_namespace
] = array();
454 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
456 $this->mDb
->freeResult( $res );
461 * Get an array of existing templates, as a 2-D array
464 function getExistingTemplates() {
465 $fname = 'LinksUpdate::getExistingTemplates';
466 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
467 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
469 while ( $row = $this->mDb
->fetchObject( $res ) ) {
470 if ( !isset( $arr[$row->tl_namespace
] ) ) {
471 $arr[$row->tl_namespace
] = array();
473 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
475 $this->mDb
->freeResult( $res );
480 * Get an array of existing images, image names in the keys
483 function getExistingImages() {
484 $fname = 'LinksUpdate::getExistingImages';
485 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
486 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
488 while ( $row = $this->mDb
->fetchObject( $res ) ) {
489 $arr[$row->il_to
] = 1;
491 $this->mDb
->freeResult( $res );
496 * Get an array of existing external links, URLs in the keys
499 function getExistingExternals() {
500 $fname = 'LinksUpdate::getExistingExternals';
501 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
502 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
504 while ( $row = $this->mDb
->fetchObject( $res ) ) {
505 $arr[$row->el_to
] = 1;
507 $this->mDb
->freeResult( $res );
512 * Get an array of existing categories, with the name in the key and sort key in the value.
515 function getExistingCategories() {
516 $fname = 'LinksUpdate::getExistingCategories';
517 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
518 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
520 while ( $row = $this->mDb
->fetchObject( $res ) ) {
521 $arr[$row->cl_to
] = $row->cl_sortkey
;
523 $this->mDb
->freeResult( $res );
528 * Get an array of existing interlanguage links, with the language code in the key and the
529 * title in the value.
532 function getExistingInterlangs() {
533 $fname = 'LinksUpdate::getExistingInterlangs';
534 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
535 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
537 while ( $row = $this->mDb
->fetchObject( $res ) ) {
538 $arr[$row->ll_lang
] = $row->ll_title
;