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)
29 * Initialize private variables
31 * @param string $title
33 function LinksUpdate( $title, $parserOutput ) {
34 global $wgAntiLockFlags;
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
37 $this->mOptions
= array();
39 $this->mOptions
= array( 'FOR UPDATE' );
41 $this->mDb
=& wfGetDB( DB_MASTER
);
43 if ( !is_object( $title ) ) {
44 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
45 "Please see Article::editUpdates() for an invocation example.\n" );
47 $this->mTitle
= $title;
48 $this->mId
= $title->getArticleID();
50 $this->mLinks
= $parserOutput->getLinks();
51 $this->mImages
= $parserOutput->getImages();
52 $this->mTemplates
= $parserOutput->getTemplates();
53 $this->mExternals
= $parserOutput->getExternalLinks();
54 $this->mCategories
= $parserOutput->getCategories();
59 * Update link tables with outgoing links from an updated article
62 global $wgUseDumbLinkUpdate;
63 if ( $wgUseDumbLinkUpdate ) {
64 $this->doDumbUpdate();
66 $this->doIncrementalUpdate();
70 function doIncrementalUpdate() {
71 $fname = 'LinksUpdate::doIncrementalUpdate';
72 wfProfileIn( $fname );
75 $existing = $this->getExistingLinks();
76 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
77 $this->getLinkInsertions( $existing ) );
80 $existing = $this->getExistingImages();
81 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
82 $this->getImageInsertions( $existing ) );
85 $existing = $this->getExistingExternals();
86 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
87 $this->getExternalInsertions( $existing ) );
90 $existing = $this->getExistingTemplates();
91 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
92 $this->getTemplateInsertions( $existing ) );
94 # Refresh links of all pages including this page
95 $tlto = $this->mTitle
->getTemplateLinksTo();
96 if ( count( $tlto ) ) {
97 require_once( 'JobQueue.php' );
98 Job
::queueLinksJobs( $tlto );
102 $existing = $this->getExistingCategories();
103 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
104 $this->getCategoryInsertions( $existing ) );
106 # I think this works out to a set XOR operation, the idea is to invalidate all
107 # categories which were added, deleted or changed
108 # FIXME: surely there's a more appropriate place to put this update?
109 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
110 $this->invalidateCategories( $categoryUpdates );
112 wfProfileOut( $fname );
116 * Link update which clears the previous entries and inserts new ones
117 * May be slower or faster depending on level of lock contention and write speed of DB
118 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
120 function doDumbUpdate() {
121 $fname = 'LinksUpdate::doDumbUpdate';
122 wfProfileIn( $fname );
124 # Refresh category pages
125 $existing = $this->getExistingCategories();
126 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
128 # Refresh links of all pages including this page
129 $tlto = $this->mTitle
->getTemplateLinksTo();
130 if ( count( $tlto ) ) {
131 require_once( 'JobQueue.php' );
132 Job
::queueLinksJobs( $tlto );
135 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
136 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
137 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
138 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
139 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
141 # Update the cache of all the category pages
142 $this->invalidateCategories( $categoryUpdates );
144 wfProfileOut( $fname );
147 function invalidateCategories( $cats ) {
148 $fname = 'LinksUpdate::invalidateCategories';
149 if ( count( $cats ) ) {
150 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
152 'page_namespace' => NS_CATEGORY
,
153 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
159 function dumbTableUpdate( $table, $insertions, $fromField ) {
160 $fname = 'LinksUpdate::dumbTableUpdate';
161 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
162 if ( count( $insertions ) ) {
163 # The link array was constructed without FOR UPDATE, so there may be collisions
164 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
165 # sure it's better than without IGNORE
166 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
171 * Make a WHERE clause from a 2-d NS/dbkey array
173 * @param array $arr 2-d array indexed by namespace and DB key
174 * @param string $prefix Field name prefix, without the underscore
176 function makeWhereFrom2d( &$arr, $prefix ) {
178 $lb->setArray( $arr );
179 return $lb->constructSet( $prefix, $this->mDb
);
183 * Update a table by doing a delete query then an insert query
186 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
187 $fname = 'LinksUpdate::incrTableUpdate';
188 $where = array( "{$prefix}_from" => $this->mId
);
189 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
190 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
197 if ( count( $deletions ) ) {
198 $where[] = "{$prefix}_to IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
204 $this->mDb
->delete( $table, $where, $fname );
206 if ( count( $insertions ) ) {
207 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
213 * Get an array of pagelinks insertions for passing to the DB
214 * Skips the titles specified by the 2-D array $existing
217 function getLinkInsertions( $existing = array() ) {
219 foreach( $this->mLinks
as $ns => $dbkeys ) {
220 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
221 # in GlobalFunctions.php
222 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
223 foreach ( $diffs as $dbk => $id ) {
225 'pl_from' => $this->mId
,
226 'pl_namespace' => $ns,
235 * Get an array of template insertions. Like getLinkInsertions()
238 function getTemplateInsertions( $existing = array() ) {
240 foreach( $this->mTemplates
as $ns => $dbkeys ) {
241 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
242 foreach ( $diffs as $dbk => $id ) {
244 'tl_from' => $this->mId
,
245 'tl_namespace' => $ns,
254 * Get an array of image insertions
255 * Skips the names specified in $existing
258 function getImageInsertions( $existing = array() ) {
260 $diffs = array_diff_key( $this->mImages
, $existing );
261 foreach( $diffs as $iname => $dummy ) {
263 'il_from' => $this->mId
,
271 * Get an array of externallinks insertions. Skips the names specified in $existing
274 function getExternalInsertions( $existing = array() ) {
276 $diffs = array_diff_key( $this->mExternals
, $existing );
277 foreach( $diffs as $url => $dummy ) {
279 'el_from' => $this->mId
,
281 'el_index' => wfMakeUrlIndex( $url ),
288 * Get an array of category insertions
289 * @param array $existing Array mapping existing category names to sort keys. If both
290 * match a link in $this, the link will be omitted from the output
293 function getCategoryInsertions( $existing = array() ) {
294 $diffs = array_diff_assoc( $this->mCategories
, $existing );
296 foreach ( $diffs as $name => $sortkey ) {
298 'cl_from' => $this->mId
,
300 'cl_sortkey' => $sortkey
307 * Given an array of existing links, returns those links which are not in $this
308 * and thus should be deleted.
311 function getLinkDeletions( $existing ) {
313 foreach ( $existing as $ns => $dbkeys ) {
314 if ( isset( $this->mLinks
[$ns] ) ) {
315 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
317 $del[$ns] = $existing[$ns];
324 * Given an array of existing templates, returns those templates which are not in $this
325 * and thus should be deleted.
328 function getTemplateDeletions( $existing ) {
330 foreach ( $existing as $ns => $dbkeys ) {
331 if ( isset( $this->mTemplates
[$ns] ) ) {
332 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
334 $del[$ns] = $existing[$ns];
341 * Given an array of existing images, returns those images which are not in $this
342 * and thus should be deleted.
345 function getImageDeletions( $existing ) {
346 return array_diff_key( $existing, $this->mImages
);
350 * Given an array of existing external links, returns those links which are not
351 * in $this and thus should be deleted.
354 function getExternalDeletions( $existing ) {
355 return array_diff_key( $existing, $this->mExternals
);
359 * Given an array of existing categories, returns those categories which are not in $this
360 * and thus should be deleted.
363 function getCategoryDeletions( $existing ) {
364 return array_diff_assoc( $existing, $this->mCategories
);
368 * Get an array of existing links, as a 2-D array
371 function getExistingLinks() {
372 $fname = 'LinksUpdate::getExistingLinks';
373 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
374 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
376 while ( $row = $this->mDb
->fetchObject( $res ) ) {
377 if ( !isset( $arr[$row->pl_namespace
] ) ) {
378 $arr[$row->pl_namespace
] = array();
380 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
382 $this->mDb
->freeResult( $res );
387 * Get an array of existing templates, as a 2-D array
390 function getExistingTemplates() {
391 $fname = 'LinksUpdate::getExistingTemplates';
392 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
393 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
395 while ( $row = $this->mDb
->fetchObject( $res ) ) {
396 if ( !isset( $arr[$row->tl_namespace
] ) ) {
397 $arr[$row->tl_namespace
] = array();
399 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
401 $this->mDb
->freeResult( $res );
406 * Get an array of existing images, image names in the keys
409 function getExistingImages() {
410 $fname = 'LinksUpdate::getExistingImages';
411 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
412 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
414 while ( $row = $this->mDb
->fetchObject( $res ) ) {
415 $arr[$row->il_to
] = 1;
417 $this->mDb
->freeResult( $res );
422 * Get an array of existing external links, URLs in the keys
425 function getExistingExternals() {
426 $fname = 'LinksUpdate::getExistingExternals';
427 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
428 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
430 while ( $row = $this->mDb
->fetchObject( $res ) ) {
431 $arr[$row->el_to
] = 1;
433 $this->mDb
->freeResult( $res );
438 * Get an array of existing categories, with the name in the key and sort key in the value.
441 function getExistingCategories() {
442 $fname = 'LinksUpdate::getExistingCategories';
443 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
444 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
446 while ( $row = $this->mDb
->fetchObject( $res ) ) {
447 $arr[$row->cl_to
] = $row->cl_sortkey
;
449 $this->mDb
->freeResult( $res );