revert to Wiki.php 1.28 and index.php 1.130
[mediawiki.git] / includes / LinksUpdate.php
blobbe61ebdd3cdaac6693dda9a8151c9260f2572391
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
7 /**
8 * @todo document
9 * @package MediaWiki
11 class LinksUpdate {
13 /**#@+
14 * @access private
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)
25 /**#@-*/
27 /**
28 * Constructor
29 * Initialize private variables
30 * @param integer $id
31 * @param string $title
33 function LinksUpdate( $title, $parserOutput ) {
34 global $wgAntiLockFlags;
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
37 $this->mOptions = array();
38 } else {
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;
51 // Shortcut aliases
52 $this->mLinks =& $this->mParserOutput->getLinks();
53 $this->mImages =& $this->mParserOutput->getImages();
54 $this->mTemplates =& $this->mParserOutput->getTemplates();
55 $this->mCategories =& $this->mParserOutput->getCategories();
59 /**
60 * Update link tables with outgoing links from an updated article
62 function doUpdate() {
63 global $wgUseDumbLinkUpdate;
64 if ( $wgUseDumbLinkUpdate ) {
65 $this->doDumbUpdate();
66 } else {
67 $this->doIncrementalUpdate();
71 function doIncrementalUpdate() {
72 $fname = 'LinksUpdate::doIncrementalUpdate';
73 wfProfileIn( $fname );
75 # Page links
76 $existing = $this->getExistingLinks();
77 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
78 $this->getLinkInsertions( $existing ) );
80 # Template links
81 $existing = $this->getExistingTemplates();
82 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
83 $this->getTemplateInsertions( $existing ) );
85 # Image links
86 $existing = $this->getExistingImages();
87 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
88 $this->getImageInsertions( $existing ) );
90 # Category links
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() ),
131 array(
132 'page_namespace' => NS_CATEGORY,
133 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
134 ), $fname
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 ) {
157 $lb = new LinkBatch;
158 $lb->setArray( $arr );
159 return $lb->constructSet( $prefix, $this->mDb );
163 * Update a table by doing a delete query then an insert query
164 * @access private
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 );
171 if ( $clause ) {
172 $where[] = $clause;
173 } else {
174 $where = false;
176 } else {
177 if ( count( $deletions ) ) {
178 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
179 } else {
180 $where = false;
183 if ( $where ) {
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
195 * @access private
197 function getLinkInsertions( $existing = array() ) {
198 $arr = 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 ) {
204 $arr[] = array(
205 'pl_from' => $this->mId,
206 'pl_namespace' => $ns,
207 'pl_title' => $dbk
211 return $arr;
215 * Get an array of template insertions. Like getLinkInsertions()
216 * @access private
218 function getTemplateInsertions( $existing = array() ) {
219 $arr = 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 ) {
223 $arr[] = array(
224 'tl_from' => $this->mId,
225 'tl_namespace' => $ns,
226 'tl_title' => $dbk
230 return $arr;
234 * Get an array of image insertions
235 * Skips the names specified in $existing
236 * @access private
238 function getImageInsertions( $existing = array() ) {
239 $arr = array();
240 $diffs = array_diff_key( $this->mImages, $existing );
241 foreach( $diffs as $iname => $val ) {
242 $arr[] = array(
243 'il_from' => $this->mId,
244 'il_to' => $iname
247 return $arr;
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
254 * @access private
256 function getCategoryInsertions( $existing = array() ) {
257 $diffs = array_diff_assoc( $this->mCategories, $existing );
258 $arr = array();
259 foreach ( $diffs as $name => $sortkey ) {
260 $arr[] = array(
261 'cl_from' => $this->mId,
262 'cl_to' => $name,
263 'cl_sortkey' => $sortkey
266 return $arr;
270 * Given an array of existing links, returns those links which are not in $this
271 * and thus should be deleted.
272 * @access private
274 function getLinkDeletions( $existing ) {
275 $del = array();
276 foreach ( $existing as $ns => $dbkeys ) {
277 if ( isset( $this->mLinks[$ns] ) ) {
278 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
279 } else {
280 $del[$ns] = $existing[$ns];
283 return $del;
287 * Given an array of existing templates, returns those templates which are not in $this
288 * and thus should be deleted.
289 * @access private
291 function getTemplateDeletions( $existing ) {
292 $del = array();
293 foreach ( $existing as $ns => $dbkeys ) {
294 if ( isset( $this->mTemplates[$ns] ) ) {
295 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
296 } else {
297 $del[$ns] = $existing[$ns];
300 return $del;
304 * Given an array of existing images, returns those images which are not in $this
305 * and thus should be deleted.
306 * @access private
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.
315 * @access private
317 function getCategoryDeletions( $existing ) {
318 return array_diff_assoc( $existing, $this->mCategories );
322 * Get an array of existing links, as a 2-D array
323 * @access private
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 );
329 $arr = array();
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;
336 return $arr;
340 * Get an array of existing templates, as a 2-D array
341 * @access private
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 );
347 $arr = array();
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;
354 return $arr;
358 * Get an array of existing images, image names in the keys
359 * @access private
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 );
365 $arr = array();
366 while ( $row = $this->mDb->fetchObject( $res ) ) {
367 $arr[$row->il_to] = 1;
369 return $arr;
373 * Get an array of existing categories, with the name in the key and sort key in the value.
374 * @access private
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 );
380 $arr = array();
381 while ( $row = $this->mDb->fetchObject( $res ) ) {
382 $arr[$row->cl_to] = $row->cl_sortkey;
384 return $arr;