* Added templatelinks table. The table currently represents a literal list of templat...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**#@+
14 * @access private
15 */
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 /**#@-*/
26
27 /**
28 * Constructor
29 * Initialize private variables
30 * @param integer $id
31 * @param string $title
32 */
33 function LinksUpdate( $title, $parserOutput ) {
34 global $wgAntiLockFlags;
35
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
37 $this->mOptions = array();
38 } else {
39 $this->mOptions = array( 'FOR UPDATE' );
40 }
41 $this->mDb =& wfGetDB( DB_MASTER );
42
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" );
46 }
47 $this->mTitle = $title;
48 $this->mId = $title->getArticleID();
49 $this->mParserOutput = $parserOutput;
50
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();
56
57 }
58
59 /**
60 * Update link tables with outgoing links from an updated article
61 */
62 function doUpdate() {
63 global $wgUseDumbLinkUpdate;
64 if ( $wgUseDumbLinkUpdate ) {
65 $this->doDumbUpdate();
66 } else {
67 $this->doIncrementalUpdate();
68 }
69 }
70
71 function doIncrementalUpdate() {
72 $fname = 'LinksUpdate::doIncrementalUpdate';
73 wfProfileIn( $fname );
74
75 # Page links
76 $existing = $this->getExistingLinks();
77 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
78 $this->getLinkInsertions( $existing ) );
79
80 # Template links
81 $existing = $this->getExistingTemplates();
82 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
83 $this->getTemplateInsertions( $existing ) );
84
85 # Image links
86 $existing = $this->getExistingImages();
87 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
88 $this->getImageInsertions( $existing ) );
89
90 # Category links
91 $existing = $this->getExistingCategories();
92 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
93 $this->getCategoryInsertions( $existing ) );
94
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 );
100
101 wfProfileOut( $fname );
102 }
103
104 /**
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
108 */
109 function doDumbUpdate() {
110 $fname = 'LinksUpdate::doDumbUpdate';
111 wfProfileIn( $fname );
112
113 $existing = $this->getExistingCategories();
114 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
115
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' );
120
121 # Update the cache of all the category pages
122 $this->invalidateCategories( $categoryUpdates );
123
124 wfProfileOut( $fname );
125 }
126
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
135 );
136 }
137 }
138
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' ) );
147 }
148 }
149
150 /**
151 * Make a WHERE clause from a 2-d NS/dbkey array
152 *
153 * @param array $arr 2-d array indexed by namespace and DB key
154 * @param string $prefix Field name prefix, without the underscore
155 */
156 function makeWhereFrom2d( &$arr, $prefix ) {
157 $lb = new LinkBatch;
158 $lb->setArray( $arr );
159 return $lb->constructSet( $prefix, $this->mDb );
160 }
161
162 /**
163 * Update a table by doing a delete query then an insert query
164 * @private
165 */
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;
175 }
176 } else {
177 if ( count( $deletions ) ) {
178 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
179 } else {
180 $where = false;
181 }
182 }
183 if ( $where ) {
184 $this->mDb->delete( $table, $where, $fname );
185 }
186 if ( count( $insertions ) ) {
187 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
188 }
189 }
190
191
192 /**
193 * Get an array of pagelinks insertions for passing to the DB
194 * Skips the titles specified by the 2-D array $existing
195 * @private
196 */
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
208 );
209 }
210 }
211 return $arr;
212 }
213
214 /**
215 * Get an array of template insertions. Like getLinkInsertions()
216 * @private
217 */
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
227 );
228 }
229 }
230 return $arr;
231 }
232
233 /**
234 * Get an array of image insertions
235 * Skips the names specified in $existing
236 * @private
237 */
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
245 );
246 }
247 return $arr;
248 }
249
250 /**
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 * @private
255 */
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
264 );
265 }
266 return $arr;
267 }
268
269 /**
270 * Given an array of existing links, returns those links which are not in $this
271 * and thus should be deleted.
272 * @private
273 */
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];
281 }
282 }
283 return $del;
284 }
285
286 /**
287 * Given an array of existing templates, returns those templates which are not in $this
288 * and thus should be deleted.
289 * @private
290 */
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];
298 }
299 }
300 return $del;
301 }
302
303 /**
304 * Given an array of existing images, returns those images which are not in $this
305 * and thus should be deleted.
306 * @private
307 */
308 function getImageDeletions( $existing ) {
309 return array_diff_key( $existing, $this->mImages );
310 }
311
312 /**
313 * Given an array of existing categories, returns those categories which are not in $this
314 * and thus should be deleted.
315 * @private
316 */
317 function getCategoryDeletions( $existing ) {
318 return array_diff_assoc( $existing, $this->mCategories );
319 }
320
321 /**
322 * Get an array of existing links, as a 2-D array
323 * @private
324 */
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();
333 }
334 $arr[$row->pl_namespace][$row->pl_title] = 1;
335 }
336 return $arr;
337 }
338
339 /**
340 * Get an array of existing templates, as a 2-D array
341 * @private
342 */
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();
351 }
352 $arr[$row->tl_namespace][$row->tl_title] = 1;
353 }
354 return $arr;
355 }
356
357 /**
358 * Get an array of existing images, image names in the keys
359 * @private
360 */
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;
368 }
369 return $arr;
370 }
371
372 /**
373 * Get an array of existing categories, with the name in the key and sort key in the value.
374 * @private
375 */
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;
383 }
384 return $arr;
385 }
386 }
387 ?>