16 var $mId, # Page ID of the article linked from
17 $mTitle, # Title object of the article linked from
18 $mParserOutput, # Parser output containing the links to be inserted into the database
19 $mLinks, # Map of title strings to IDs for the links in the document
20 $mImages, # DB keys of the images used, in the array key only
21 $mTemplates, # Map of title strings to IDs for the template references, including broken ones
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();
49 $this->mParserOutput
= $parserOutput;
52 $this->mLinks
=& $this->mParserOutput
->getLinks();
53 $this->mImages
=& $this->mParserOutput
->getImages();
54 $this->mTemplates
=& $this->mParserOutput
->getTemplates();
55 $this->mCategories
=& $this->mParserOutput
->getCategories();
60 * Update link tables with outgoing links from an updated article
63 global $wgUseDumbLinkUpdate;
64 if ( $wgUseDumbLinkUpdate ) {
65 $this->doDumbUpdate();
67 $this->doIncrementalUpdate();
71 function doIncrementalUpdate() {
72 $fname = 'LinksUpdate::doIncrementalUpdate';
73 wfProfileIn( $fname );
76 $existing = $this->getExistingLinks();
77 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
78 $this->getLinkInsertions( $existing ) );
81 $existing = $this->getExistingTemplates();
82 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
83 $this->getTemplateInsertions( $existing ) );
86 $existing = $this->getExistingImages();
87 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
88 $this->getImageInsertions( $existing ) );
91 $existing = $this->getExistingCategories();
92 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
93 $this->getCategoryInsertions( $existing ) );
95 # I think this works out to a set XOR operation, the idea is to invalidate all
96 # categories which were added, deleted or changed
97 # FIXME: surely there's a more appropriate place to put this update?
98 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
99 $this->invalidateCategories( $categoryUpdates );
101 wfProfileOut( $fname );
105 * Link update which clears the previous entries and inserts new ones
106 * May be slower or faster depending on level of lock contention and write speed of DB
107 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
109 function doDumbUpdate() {
110 $fname = 'LinksUpdate::doDumbUpdate';
111 wfProfileIn( $fname );
113 $existing = $this->getExistingCategories();
114 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
116 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
117 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
118 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
119 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
121 # Update the cache of all the category pages
122 $this->invalidateCategories( $categoryUpdates );
124 wfProfileOut( $fname );
127 function invalidateCategories( $cats ) {
128 $fname = 'LinksUpdate::invalidateCategories';
129 if ( count( $cats ) ) {
130 $this->mDb
->update( 'page', array( 'page_touched' => $this->mDb
->timestamp() ),
132 'page_namespace' => NS_CATEGORY
,
133 'page_title IN (' . $this->mDb
->makeList( array_keys( $cats ) ) . ')'
139 function dumbTableUpdate( $table, $insertions, $fromField ) {
140 $fname = 'LinksUpdate::dumbTableUpdate';
141 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
142 if ( count( $insertions ) ) {
143 # The link array was constructed without FOR UPDATE, so there may be collisions
144 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
145 # sure it's better than without IGNORE
146 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
151 * Make a WHERE clause from a 2-d NS/dbkey array
153 * @param array $arr 2-d array indexed by namespace and DB key
154 * @param string $prefix Field name prefix, without the underscore
156 function makeWhereFrom2d( &$arr, $prefix ) {
158 $lb->setArray( $arr );
159 return $lb->constructSet( $prefix, $this->mDb
);
163 * Update a table by doing a delete query then an insert query
166 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
167 $fname = 'LinksUpdate::incrTableUpdate';
168 $where = array( "{$prefix}_from" => $this->mId
);
169 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
170 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
177 if ( count( $deletions ) ) {
178 $where[] = "{$prefix}_to IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
184 $this->mDb
->delete( $table, $where, $fname );
186 if ( count( $insertions ) ) {
187 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
193 * Get an array of pagelinks insertions for passing to the DB
194 * Skips the titles specified by the 2-D array $existing
197 function getLinkInsertions( $existing = array() ) {
199 foreach( $this->mLinks
as $ns => $dbkeys ) {
200 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
201 # in GlobalFunctions.php
202 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
203 foreach ( $diffs as $dbk => $id ) {
205 'pl_from' => $this->mId
,
206 'pl_namespace' => $ns,
215 * Get an array of template insertions. Like getLinkInsertions()
218 function getTemplateInsertions( $existing = array() ) {
220 foreach( $this->mTemplates
as $ns => $dbkeys ) {
221 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
222 foreach ( $diffs as $dbk => $id ) {
224 'tl_from' => $this->mId
,
225 'tl_namespace' => $ns,
234 * Get an array of image insertions
235 * Skips the names specified in $existing
238 function getImageInsertions( $existing = array() ) {
240 $diffs = array_diff_key( $this->mImages
, $existing );
241 foreach( $diffs as $iname => $val ) {
243 'il_from' => $this->mId
,
251 * Get an array of category insertions
252 * @param array $existing Array mapping existing category names to sort keys. If both
253 * match a link in $this, the link will be omitted from the output
256 function getCategoryInsertions( $existing = array() ) {
257 $diffs = array_diff_assoc( $this->mCategories
, $existing );
259 foreach ( $diffs as $name => $sortkey ) {
261 'cl_from' => $this->mId
,
263 'cl_sortkey' => $sortkey
270 * Given an array of existing links, returns those links which are not in $this
271 * and thus should be deleted.
274 function getLinkDeletions( $existing ) {
276 foreach ( $existing as $ns => $dbkeys ) {
277 if ( isset( $this->mLinks
[$ns] ) ) {
278 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
280 $del[$ns] = $existing[$ns];
287 * Given an array of existing templates, returns those templates which are not in $this
288 * and thus should be deleted.
291 function getTemplateDeletions( $existing ) {
293 foreach ( $existing as $ns => $dbkeys ) {
294 if ( isset( $this->mTemplates
[$ns] ) ) {
295 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
297 $del[$ns] = $existing[$ns];
304 * Given an array of existing images, returns those images which are not in $this
305 * and thus should be deleted.
308 function getImageDeletions( $existing ) {
309 return array_diff_key( $existing, $this->mImages
);
313 * Given an array of existing categories, returns those categories which are not in $this
314 * and thus should be deleted.
317 function getCategoryDeletions( $existing ) {
318 return array_diff_assoc( $existing, $this->mCategories
);
322 * Get an array of existing links, as a 2-D array
325 function getExistingLinks() {
326 $fname = 'LinksUpdate::getExistingLinks';
327 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
328 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
330 while ( $row = $this->mDb
->fetchObject( $res ) ) {
331 if ( !isset( $arr[$row->pl_namespace
] ) ) {
332 $arr[$row->pl_namespace
] = array();
334 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
340 * Get an array of existing templates, as a 2-D array
343 function getExistingTemplates() {
344 $fname = 'LinksUpdate::getExistingTemplates';
345 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
346 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
348 while ( $row = $this->mDb
->fetchObject( $res ) ) {
349 if ( !isset( $arr[$row->tl_namespace
] ) ) {
350 $arr[$row->tl_namespace
] = array();
352 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
358 * Get an array of existing images, image names in the keys
361 function getExistingImages() {
362 $fname = 'LinksUpdate::getExistingImages';
363 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
364 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
366 while ( $row = $this->mDb
->fetchObject( $res ) ) {
367 $arr[$row->il_to
] = 1;
373 * Get an array of existing categories, with the name in the key and sort key in the value.
376 function getExistingCategories() {
377 $fname = 'LinksUpdate::getExistingCategories';
378 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
379 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
381 while ( $row = $this->mDb
->fetchObject( $res ) ) {
382 $arr[$row->cl_to
] = $row->cl_sortkey
;