* Added externallinks table, to track links to arbitrary URLs
[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 $mExternals, # URLs of external links, array key only
23 $mCategories, # Map of category names to sort keys
24 $mDb, # Database connection reference
25 $mOptions; # SELECT options to be used (array)
26 /**#@-*/
27
28 /**
29 * Constructor
30 * Initialize private variables
31 * @param integer $id
32 * @param string $title
33 */
34 function LinksUpdate( $title, $parserOutput ) {
35 global $wgAntiLockFlags;
36
37 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
38 $this->mOptions = array();
39 } else {
40 $this->mOptions = array( 'FOR UPDATE' );
41 }
42 $this->mDb =& wfGetDB( DB_MASTER );
43
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" );
47 }
48 $this->mTitle = $title;
49 $this->mId = $title->getArticleID();
50 $this->mParserOutput = $parserOutput;
51
52 // Shortcut aliases
53 $this->mLinks =& $this->mParserOutput->getLinks();
54 $this->mImages =& $this->mParserOutput->getImages();
55 $this->mTemplates =& $this->mParserOutput->getTemplates();
56 $this->mExternals =& $this->mParserOutput->getExternalLinks();
57 $this->mCategories =& $this->mParserOutput->getCategories();
58
59 }
60
61 /**
62 * Update link tables with outgoing links from an updated article
63 */
64 function doUpdate() {
65 global $wgUseDumbLinkUpdate;
66 if ( $wgUseDumbLinkUpdate ) {
67 $this->doDumbUpdate();
68 } else {
69 $this->doIncrementalUpdate();
70 }
71 }
72
73 function doIncrementalUpdate() {
74 $fname = 'LinksUpdate::doIncrementalUpdate';
75 wfProfileIn( $fname );
76
77 # Page links
78 $existing = $this->getExistingLinks();
79 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
80 $this->getLinkInsertions( $existing ) );
81
82 # Template links
83 $existing = $this->getExistingTemplates();
84 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
85 $this->getTemplateInsertions( $existing ) );
86
87 # Image links
88 $existing = $this->getExistingImages();
89 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
90 $this->getImageInsertions( $existing ) );
91
92 # External links
93 $existing = $this->getExistingExternals();
94 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
95 $this->getExternalInsertions( $existing ) );
96
97 # Category links
98 $existing = $this->getExistingCategories();
99 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
100 $this->getCategoryInsertions( $existing ) );
101
102 # I think this works out to a set XOR operation, the idea is to invalidate all
103 # categories which were added, deleted or changed
104 # FIXME: surely there's a more appropriate place to put this update?
105 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
106 $this->invalidateCategories( $categoryUpdates );
107
108 wfProfileOut( $fname );
109 }
110
111 /**
112 * Link update which clears the previous entries and inserts new ones
113 * May be slower or faster depending on level of lock contention and write speed of DB
114 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
115 */
116 function doDumbUpdate() {
117 $fname = 'LinksUpdate::doDumbUpdate';
118 wfProfileIn( $fname );
119
120 $existing = $this->getExistingCategories();
121 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
122
123 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
124 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
125 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
126 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
127 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
128
129 # Update the cache of all the category pages
130 $this->invalidateCategories( $categoryUpdates );
131
132 wfProfileOut( $fname );
133 }
134
135 function invalidateCategories( $cats ) {
136 $fname = 'LinksUpdate::invalidateCategories';
137 if ( count( $cats ) ) {
138 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
139 array(
140 'page_namespace' => NS_CATEGORY,
141 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
142 ), $fname
143 );
144 }
145 }
146
147 function dumbTableUpdate( $table, $insertions, $fromField ) {
148 $fname = 'LinksUpdate::dumbTableUpdate';
149 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
150 if ( count( $insertions ) ) {
151 # The link array was constructed without FOR UPDATE, so there may be collisions
152 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
153 # sure it's better than without IGNORE
154 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
155 }
156 }
157
158 /**
159 * Make a WHERE clause from a 2-d NS/dbkey array
160 *
161 * @param array $arr 2-d array indexed by namespace and DB key
162 * @param string $prefix Field name prefix, without the underscore
163 */
164 function makeWhereFrom2d( &$arr, $prefix ) {
165 $lb = new LinkBatch;
166 $lb->setArray( $arr );
167 return $lb->constructSet( $prefix, $this->mDb );
168 }
169
170 /**
171 * Update a table by doing a delete query then an insert query
172 * @access private
173 */
174 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
175 $fname = 'LinksUpdate::incrTableUpdate';
176 $where = array( "{$prefix}_from" => $this->mId );
177 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
178 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
179 if ( $clause ) {
180 $where[] = $clause;
181 } else {
182 $where = false;
183 }
184 } else {
185 if ( count( $deletions ) ) {
186 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
187 } else {
188 $where = false;
189 }
190 }
191 if ( $where ) {
192 $this->mDb->delete( $table, $where, $fname );
193 }
194 if ( count( $insertions ) ) {
195 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
196 }
197 }
198
199
200 /**
201 * Get an array of pagelinks insertions for passing to the DB
202 * Skips the titles specified by the 2-D array $existing
203 * @access private
204 */
205 function getLinkInsertions( $existing = array() ) {
206 $arr = array();
207 foreach( $this->mLinks as $ns => $dbkeys ) {
208 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
209 # in GlobalFunctions.php
210 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
211 foreach ( $diffs as $dbk => $id ) {
212 $arr[] = array(
213 'pl_from' => $this->mId,
214 'pl_namespace' => $ns,
215 'pl_title' => $dbk
216 );
217 }
218 }
219 return $arr;
220 }
221
222 /**
223 * Get an array of template insertions. Like getLinkInsertions()
224 * @access private
225 */
226 function getTemplateInsertions( $existing = array() ) {
227 $arr = array();
228 foreach( $this->mTemplates as $ns => $dbkeys ) {
229 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
230 foreach ( $diffs as $dbk => $id ) {
231 $arr[] = array(
232 'tl_from' => $this->mId,
233 'tl_namespace' => $ns,
234 'tl_title' => $dbk
235 );
236 }
237 }
238 return $arr;
239 }
240
241 /**
242 * Get an array of image insertions
243 * Skips the names specified in $existing
244 * @access private
245 */
246 function getImageInsertions( $existing = array() ) {
247 $arr = array();
248 $diffs = array_diff_key( $this->mImages, $existing );
249 foreach( $diffs as $iname => $dummy ) {
250 $arr[] = array(
251 'il_from' => $this->mId,
252 'il_to' => $iname
253 );
254 }
255 return $arr;
256 }
257
258 /**
259 * Get an array of externallinks insertions. Skips the names specified in $existing
260 * @access private
261 */
262 function getExternalInsertions( $existing = array() ) {
263 $arr = array();
264 $diffs = array_diff_key( $this->mExternals, $existing );
265 foreach( $diffs as $url => $dummy ) {
266 $arr[] = array(
267 'el_from' => $this->mId,
268 'el_to' => $url,
269 'el_index' => wfMakeUrlIndex( $url ),
270 );
271 }
272 return $arr;
273 }
274
275 /**
276 * Get an array of category insertions
277 * @param array $existing Array mapping existing category names to sort keys. If both
278 * match a link in $this, the link will be omitted from the output
279 * @access private
280 */
281 function getCategoryInsertions( $existing = array() ) {
282 $diffs = array_diff_assoc( $this->mCategories, $existing );
283 $arr = array();
284 foreach ( $diffs as $name => $sortkey ) {
285 $arr[] = array(
286 'cl_from' => $this->mId,
287 'cl_to' => $name,
288 'cl_sortkey' => $sortkey
289 );
290 }
291 return $arr;
292 }
293
294 /**
295 * Given an array of existing links, returns those links which are not in $this
296 * and thus should be deleted.
297 * @access private
298 */
299 function getLinkDeletions( $existing ) {
300 $del = array();
301 foreach ( $existing as $ns => $dbkeys ) {
302 if ( isset( $this->mLinks[$ns] ) ) {
303 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
304 } else {
305 $del[$ns] = $existing[$ns];
306 }
307 }
308 return $del;
309 }
310
311 /**
312 * Given an array of existing templates, returns those templates which are not in $this
313 * and thus should be deleted.
314 * @access private
315 */
316 function getTemplateDeletions( $existing ) {
317 $del = array();
318 foreach ( $existing as $ns => $dbkeys ) {
319 if ( isset( $this->mTemplates[$ns] ) ) {
320 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
321 } else {
322 $del[$ns] = $existing[$ns];
323 }
324 }
325 return $del;
326 }
327
328 /**
329 * Given an array of existing images, returns those images which are not in $this
330 * and thus should be deleted.
331 * @access private
332 */
333 function getImageDeletions( $existing ) {
334 return array_diff_key( $existing, $this->mImages );
335 }
336
337 /**
338 * Given an array of existing external links, returns those links which are not
339 * in $this and thus should be deleted.
340 * @access private
341 */
342 function getExternalDeletions( $existing ) {
343 return array_diff_key( $existing, $this->mExternals );
344 }
345
346 /**
347 * Given an array of existing categories, returns those categories which are not in $this
348 * and thus should be deleted.
349 * @access private
350 */
351 function getCategoryDeletions( $existing ) {
352 return array_diff_assoc( $existing, $this->mCategories );
353 }
354
355 /**
356 * Get an array of existing links, as a 2-D array
357 * @access private
358 */
359 function getExistingLinks() {
360 $fname = 'LinksUpdate::getExistingLinks';
361 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
362 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
363 $arr = array();
364 while ( $row = $this->mDb->fetchObject( $res ) ) {
365 if ( !isset( $arr[$row->pl_namespace] ) ) {
366 $arr[$row->pl_namespace] = array();
367 }
368 $arr[$row->pl_namespace][$row->pl_title] = 1;
369 }
370 $this->mDb->freeResult( $res );
371 return $arr;
372 }
373
374 /**
375 * Get an array of existing templates, as a 2-D array
376 * @access private
377 */
378 function getExistingTemplates() {
379 $fname = 'LinksUpdate::getExistingTemplates';
380 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
381 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
382 $arr = array();
383 while ( $row = $this->mDb->fetchObject( $res ) ) {
384 if ( !isset( $arr[$row->tl_namespace] ) ) {
385 $arr[$row->tl_namespace] = array();
386 }
387 $arr[$row->tl_namespace][$row->tl_title] = 1;
388 }
389 $this->mDb->freeResult( $res );
390 return $arr;
391 }
392
393 /**
394 * Get an array of existing images, image names in the keys
395 * @access private
396 */
397 function getExistingImages() {
398 $fname = 'LinksUpdate::getExistingImages';
399 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
400 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
401 $arr = array();
402 while ( $row = $this->mDb->fetchObject( $res ) ) {
403 $arr[$row->il_to] = 1;
404 }
405 $this->mDb->freeResult( $res );
406 return $arr;
407 }
408
409 /**
410 * Get an array of existing external links, URLs in the keys
411 * @access private
412 */
413 function getExistingExternals() {
414 $fname = 'LinksUpdate::getExistingExternals';
415 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
416 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
417 $arr = array();
418 while ( $row = $this->mDb->fetchObject( $res ) ) {
419 $arr[$row->el_to] = 1;
420 }
421 $this->mDb->freeResult( $res );
422 return $arr;
423 }
424
425 /**
426 * Get an array of existing categories, with the name in the key and sort key in the value.
427 * @access private
428 */
429 function getExistingCategories() {
430 $fname = 'LinksUpdate::getExistingCategories';
431 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
432 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
433 $arr = array();
434 while ( $row = $this->mDb->fetchObject( $res ) ) {
435 $arr[$row->cl_to] = $row->cl_sortkey;
436 }
437 $this->mDb->freeResult( $res );
438 return $arr;
439 }
440 }
441 ?>