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 ) );
87 $existing = $this->getExistingExternals();
88 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
89 $this->getExternalInsertions( $existing ) );
92 $existing = $this->getExistingTemplates();
93 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
94 $this->getTemplateInsertions( $existing ) );
96 # Refresh links of all pages including this page
97 if ( $this->mRecursive
) {
98 $tlto = $this->mTitle
->getTemplateLinksTo();
99 if ( count( $tlto ) ) {
100 require_once( 'JobQueue.php' );
101 Job
::queueLinksJobs( $tlto );
106 $existing = $this->getExistingCategories();
107 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
108 $this->getCategoryInsertions( $existing ) );
110 # I think this works out to a set XOR operation, the idea is to invalidate all
111 # categories which were added, deleted or changed
112 # FIXME: surely there's a more appropriate place to put this update?
113 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
114 $this->invalidateCategories( $categoryUpdates );
116 wfProfileOut( $fname );
120 * Link update which clears the previous entries and inserts new ones
121 * May be slower or faster depending on level of lock contention and write speed of DB
122 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
124 function doDumbUpdate() {
125 $fname = 'LinksUpdate::doDumbUpdate';
126 wfProfileIn( $fname );
128 # Refresh category pages
129 $existing = $this->getExistingCategories();
130 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
132 # Refresh links of all pages including this page
133 if ( $this->mRecursive
) {
134 $tlto = $this->mTitle
->getTemplateLinksTo();
135 if ( count( $tlto ) ) {
136 require_once( 'JobQueue.php' );
137 Job
::queueLinksJobs( $tlto );
141 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
142 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
143 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
144 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
145 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
147 # Update the cache of all the category pages
148 $this->invalidateCategories( $categoryUpdates );
150 wfProfileOut( $fname );
153 function invalidateCategories( $cats ) {
154 $fname = 'LinksUpdate::invalidateCategories';
155 if ( count( $cats ) ) {
156 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
158 'page_namespace' => NS_CATEGORY
,
159 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
165 function dumbTableUpdate( $table, $insertions, $fromField ) {
166 $fname = 'LinksUpdate::dumbTableUpdate';
167 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
168 if ( count( $insertions ) ) {
169 # The link array was constructed without FOR UPDATE, so there may be collisions
170 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
171 # sure it's better than without IGNORE
172 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
177 * Make a WHERE clause from a 2-d NS/dbkey array
179 * @param array $arr 2-d array indexed by namespace and DB key
180 * @param string $prefix Field name prefix, without the underscore
182 function makeWhereFrom2d( &$arr, $prefix ) {
184 $lb->setArray( $arr );
185 return $lb->constructSet( $prefix, $this->mDb
);
189 * Update a table by doing a delete query then an insert query
192 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
193 $fname = 'LinksUpdate::incrTableUpdate';
194 $where = array( "{$prefix}_from" => $this->mId
);
195 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
196 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
203 if ( count( $deletions ) ) {
204 $where[] = "{$prefix}_to IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
210 $this->mDb
->delete( $table, $where, $fname );
212 if ( count( $insertions ) ) {
213 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
219 * Get an array of pagelinks insertions for passing to the DB
220 * Skips the titles specified by the 2-D array $existing
223 function getLinkInsertions( $existing = array() ) {
225 foreach( $this->mLinks
as $ns => $dbkeys ) {
226 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
227 # in GlobalFunctions.php
228 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
229 foreach ( $diffs as $dbk => $id ) {
231 'pl_from' => $this->mId
,
232 'pl_namespace' => $ns,
241 * Get an array of template insertions. Like getLinkInsertions()
244 function getTemplateInsertions( $existing = array() ) {
246 foreach( $this->mTemplates
as $ns => $dbkeys ) {
247 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
248 foreach ( $diffs as $dbk => $id ) {
250 'tl_from' => $this->mId
,
251 'tl_namespace' => $ns,
260 * Get an array of image insertions
261 * Skips the names specified in $existing
264 function getImageInsertions( $existing = array() ) {
266 $diffs = array_diff_key( $this->mImages
, $existing );
267 foreach( $diffs as $iname => $dummy ) {
269 'il_from' => $this->mId
,
277 * Get an array of externallinks insertions. Skips the names specified in $existing
280 function getExternalInsertions( $existing = array() ) {
282 $diffs = array_diff_key( $this->mExternals
, $existing );
283 foreach( $diffs as $url => $dummy ) {
285 'el_from' => $this->mId
,
287 'el_index' => wfMakeUrlIndex( $url ),
294 * Get an array of category insertions
295 * @param array $existing Array mapping existing category names to sort keys. If both
296 * match a link in $this, the link will be omitted from the output
299 function getCategoryInsertions( $existing = array() ) {
300 $diffs = array_diff_assoc( $this->mCategories
, $existing );
302 foreach ( $diffs as $name => $sortkey ) {
304 'cl_from' => $this->mId
,
306 'cl_sortkey' => $sortkey
313 * Given an array of existing links, returns those links which are not in $this
314 * and thus should be deleted.
317 function getLinkDeletions( $existing ) {
319 foreach ( $existing as $ns => $dbkeys ) {
320 if ( isset( $this->mLinks
[$ns] ) ) {
321 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
323 $del[$ns] = $existing[$ns];
330 * Given an array of existing templates, returns those templates which are not in $this
331 * and thus should be deleted.
334 function getTemplateDeletions( $existing ) {
336 foreach ( $existing as $ns => $dbkeys ) {
337 if ( isset( $this->mTemplates
[$ns] ) ) {
338 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
340 $del[$ns] = $existing[$ns];
347 * Given an array of existing images, returns those images which are not in $this
348 * and thus should be deleted.
351 function getImageDeletions( $existing ) {
352 return array_diff_key( $existing, $this->mImages
);
356 * Given an array of existing external links, returns those links which are not
357 * in $this and thus should be deleted.
360 function getExternalDeletions( $existing ) {
361 return array_diff_key( $existing, $this->mExternals
);
365 * Given an array of existing categories, returns those categories which are not in $this
366 * and thus should be deleted.
369 function getCategoryDeletions( $existing ) {
370 return array_diff_assoc( $existing, $this->mCategories
);
374 * Get an array of existing links, as a 2-D array
377 function getExistingLinks() {
378 $fname = 'LinksUpdate::getExistingLinks';
379 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
380 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
382 while ( $row = $this->mDb
->fetchObject( $res ) ) {
383 if ( !isset( $arr[$row->pl_namespace
] ) ) {
384 $arr[$row->pl_namespace
] = array();
386 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
388 $this->mDb
->freeResult( $res );
393 * Get an array of existing templates, as a 2-D array
396 function getExistingTemplates() {
397 $fname = 'LinksUpdate::getExistingTemplates';
398 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
399 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
401 while ( $row = $this->mDb
->fetchObject( $res ) ) {
402 if ( !isset( $arr[$row->tl_namespace
] ) ) {
403 $arr[$row->tl_namespace
] = array();
405 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
407 $this->mDb
->freeResult( $res );
412 * Get an array of existing images, image names in the keys
415 function getExistingImages() {
416 $fname = 'LinksUpdate::getExistingImages';
417 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
418 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
420 while ( $row = $this->mDb
->fetchObject( $res ) ) {
421 $arr[$row->il_to
] = 1;
423 $this->mDb
->freeResult( $res );
428 * Get an array of existing external links, URLs in the keys
431 function getExistingExternals() {
432 $fname = 'LinksUpdate::getExistingExternals';
433 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
434 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
436 while ( $row = $this->mDb
->fetchObject( $res ) ) {
437 $arr[$row->el_to
] = 1;
439 $this->mDb
->freeResult( $res );
444 * Get an array of existing categories, with the name in the key and sort key in the value.
447 function getExistingCategories() {
448 $fname = 'LinksUpdate::getExistingCategories';
449 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
450 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
452 while ( $row = $this->mDb
->fetchObject( $res ) ) {
453 $arr[$row->cl_to
] = $row->cl_sortkey
;
455 $this->mDb
->freeResult( $res );