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
30 * Initialize private variables
32 * @param string $title
34 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
35 global $wgAntiLockFlags;
37 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
38 $this->mOptions
= array();
40 $this->mOptions
= array( 'FOR UPDATE' );
42 $this->mDb
=& wfGetDB( DB_MASTER
);
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" );
48 $this->mTitle
= $title;
49 $this->mId
= $title->getArticleID();
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;
61 * Update link tables with outgoing links from an updated article
64 global $wgUseDumbLinkUpdate;
65 if ( $wgUseDumbLinkUpdate ) {
66 $this->doDumbUpdate();
68 $this->doIncrementalUpdate();
72 function doIncrementalUpdate() {
73 $fname = 'LinksUpdate::doIncrementalUpdate';
74 wfProfileIn( $fname );
77 $existing = $this->getExistingLinks();
78 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
79 $this->getLinkInsertions( $existing ) );
82 $existing = $this->getExistingImages();
83 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
84 $this->getImageInsertions( $existing ) );
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 );
91 $existing = $this->getExistingExternals();
92 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
93 $this->getExternalInsertions( $existing ) );
96 $existing = $this->getExistingTemplates();
97 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
98 $this->getTemplateInsertions( $existing ) );
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 );
110 $existing = $this->getExistingCategories();
111 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
112 $this->getCategoryInsertions( $existing ) );
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 );
118 wfProfileOut( $fname );
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
126 function doDumbUpdate() {
127 $fname = 'LinksUpdate::doDumbUpdate';
128 wfProfileIn( $fname );
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 );
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 );
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' );
151 # Update the cache of all the category pages and image description pages which were changed
152 $this->invalidateCategories( $categoryUpdates );
153 $this->invalidateImageDescriptions( $imageUpdates );
155 wfProfileOut( $fname );
158 function invalidateCategories( $cats ) {
159 $fname = 'LinksUpdate::invalidateCategories';
160 if ( count( $cats ) ) {
161 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
163 'page_namespace' => NS_CATEGORY
,
164 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
170 function invalidateImageDescriptions( $images ) {
171 $fname = 'LinksUpdate::invalidateImageDescriptions';
172 if ( count( $images ) ) {
173 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
175 'page_namespace' => NS_IMAGE
,
176 'page_title IN (' . $this->mDb
->makeList( array_keys( $images ) ) . ')'
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' ) );
194 * Make a WHERE clause from a 2-d NS/dbkey array
196 * @param array $arr 2-d array indexed by namespace and DB key
197 * @param string $prefix Field name prefix, without the underscore
199 function makeWhereFrom2d( &$arr, $prefix ) {
201 $lb->setArray( $arr );
202 return $lb->constructSet( $prefix, $this->mDb
);
206 * Update a table by doing a delete query then an insert query
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 );
220 if ( count( $deletions ) ) {
221 $where[] = "{$prefix}_to IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
227 $this->mDb
->delete( $table, $where, $fname );
229 if ( count( $insertions ) ) {
230 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
236 * Get an array of pagelinks insertions for passing to the DB
237 * Skips the titles specified by the 2-D array $existing
240 function getLinkInsertions( $existing = 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 ) {
248 'pl_from' => $this->mId
,
249 'pl_namespace' => $ns,
258 * Get an array of template insertions. Like getLinkInsertions()
261 function getTemplateInsertions( $existing = 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 ) {
267 'tl_from' => $this->mId
,
268 'tl_namespace' => $ns,
277 * Get an array of image insertions
278 * Skips the names specified in $existing
281 function getImageInsertions( $existing = array() ) {
283 $diffs = array_diff_key( $this->mImages
, $existing );
284 foreach( $diffs as $iname => $dummy ) {
286 'il_from' => $this->mId
,
294 * Get an array of externallinks insertions. Skips the names specified in $existing
297 function getExternalInsertions( $existing = array() ) {
299 $diffs = array_diff_key( $this->mExternals
, $existing );
300 foreach( $diffs as $url => $dummy ) {
302 'el_from' => $this->mId
,
304 'el_index' => wfMakeUrlIndex( $url ),
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
316 function getCategoryInsertions( $existing = array() ) {
317 $diffs = array_diff_assoc( $this->mCategories
, $existing );
319 foreach ( $diffs as $name => $sortkey ) {
321 'cl_from' => $this->mId
,
323 'cl_sortkey' => $sortkey,
324 'cl_timestamp' => $this->mDb
->timestamp()
331 * Given an array of existing links, returns those links which are not in $this
332 * and thus should be deleted.
335 function getLinkDeletions( $existing ) {
337 foreach ( $existing as $ns => $dbkeys ) {
338 if ( isset( $this->mLinks
[$ns] ) ) {
339 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
341 $del[$ns] = $existing[$ns];
348 * Given an array of existing templates, returns those templates which are not in $this
349 * and thus should be deleted.
352 function getTemplateDeletions( $existing ) {
354 foreach ( $existing as $ns => $dbkeys ) {
355 if ( isset( $this->mTemplates
[$ns] ) ) {
356 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
358 $del[$ns] = $existing[$ns];
365 * Given an array of existing images, returns those images which are not in $this
366 * and thus should be deleted.
369 function getImageDeletions( $existing ) {
370 return array_diff_key( $existing, $this->mImages
);
374 * Given an array of existing external links, returns those links which are not
375 * in $this and thus should be deleted.
378 function getExternalDeletions( $existing ) {
379 return array_diff_key( $existing, $this->mExternals
);
383 * Given an array of existing categories, returns those categories which are not in $this
384 * and thus should be deleted.
387 function getCategoryDeletions( $existing ) {
388 return array_diff_assoc( $existing, $this->mCategories
);
392 * Get an array of existing links, as a 2-D array
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
);
400 while ( $row = $this->mDb
->fetchObject( $res ) ) {
401 if ( !isset( $arr[$row->pl_namespace
] ) ) {
402 $arr[$row->pl_namespace
] = array();
404 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
406 $this->mDb
->freeResult( $res );
411 * Get an array of existing templates, as a 2-D array
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
);
419 while ( $row = $this->mDb
->fetchObject( $res ) ) {
420 if ( !isset( $arr[$row->tl_namespace
] ) ) {
421 $arr[$row->tl_namespace
] = array();
423 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
425 $this->mDb
->freeResult( $res );
430 * Get an array of existing images, image names in the keys
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
);
438 while ( $row = $this->mDb
->fetchObject( $res ) ) {
439 $arr[$row->il_to
] = 1;
441 $this->mDb
->freeResult( $res );
446 * Get an array of existing external links, URLs in the keys
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
);
454 while ( $row = $this->mDb
->fetchObject( $res ) ) {
455 $arr[$row->el_to
] = 1;
457 $this->mDb
->freeResult( $res );
462 * Get an array of existing categories, with the name in the key and sort key in the value.
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
);
470 while ( $row = $this->mDb
->fetchObject( $res ) ) {
471 $arr[$row->cl_to
] = $row->cl_sortkey
;
473 $this->mDb
->freeResult( $res );