forward-ported the changes made in 1.40.2.1
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 # These are used in incrementalSetup()
12 define ('LINKCACHE_GOOD', 0);
13 define ('LINKCACHE_BAD', 1);
14 define ('LINKCACHE_IMAGE', 2);
15 define ('LINKCACHE_PAGE', 3);
16
17 /**
18 * @package MediaWiki
19 * @subpackage Cache
20 */
21 class LinkCache {
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
25
26 /* private */ var $mPageLinks;
27 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
28 /* private */ var $mImageLinks, $mCategoryLinks;
29 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
30 /* private */ var $mForUpdate;
31
32 /* private */ function getKey( $title ) {
33 global $wgDBname;
34 return $wgDBname.':lc:title:'.$title;
35 }
36
37 function LinkCache() {
38 $this->mActive = true;
39 $this->mPreFilled = false;
40 $this->mForUpdate = false;
41 $this->mPageLinks = array();
42 $this->mGoodLinks = array();
43 $this->mBadLinks = array();
44 $this->mImageLinks = array();
45 $this->mCategoryLinks = array();
46 $this->mOldGoodLinks = array();
47 $this->mOldBadLinks = array();
48 $this->mOldPageLinks = array();
49 }
50
51 /**
52 * General accessor to get/set whether SELECT FOR UPDATE should be used
53 */
54 function forUpdate( $update = NULL ) {
55 return wfSetVar( $this->mForUpdate, $update );
56 }
57
58 function getGoodLinkID( $title ) {
59 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
60 return $this->mGoodLinks[$title];
61 } else {
62 return 0;
63 }
64 }
65
66 function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks );
68 }
69
70 function addGoodLinkObj( $id, $title ) {
71 if ( $this->mActive ) {
72 $dbkey = $title->getPrefixedDbKey();
73 $this->mGoodLinks[$dbkey] = $id;
74 $this->mPageLinks[$dbkey] = $title;
75 }
76 }
77
78 function addBadLinkObj( $title ) {
79 $dbkey = $title->getPrefixedDbKey();
80 if ( $this->mActive && ( ! $this->isBadLink( $dbkey ) ) ) {
81 $this->mBadLinks[$dbkey] = 1;
82 $this->mPageLinks[$dbkey] = $title;
83 }
84 }
85
86 function addImageLink( $title ) {
87 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
88 }
89
90 function addImageLinkObj( $nt ) {
91 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
92 }
93
94 function addCategoryLink( $title, $sortkey ) {
95 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
96 }
97
98 function addCategoryLinkObj( &$nt, $sortkey ) {
99 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
100 }
101
102 function clearBadLink( $title ) {
103 unset( $this->mBadLinks[$title] );
104 $this->clearLink( $title );
105 }
106
107 function clearLink( $title ) {
108 global $wgMemc, $wgLinkCacheMemcached;
109 if( $wgLinkCacheMemcached )
110 $wgMemc->delete( $this->getKey( $title ) );
111 }
112
113 function suspend() { $this->mActive = false; }
114 function resume() { $this->mActive = true; }
115 function getPageLinks() { return $this->mPageLinks; }
116 function getGoodLinks() { return $this->mGoodLinks; }
117 function getBadLinks() { return array_keys( $this->mBadLinks ); }
118 function getImageLinks() { return $this->mImageLinks; }
119 function getCategoryLinks() { return $this->mCategoryLinks; }
120
121 function addLink( $title ) {
122 $nt = Title::newFromDBkey( $title );
123 if( $nt ) {
124 return $this->addLinkObj( $nt );
125 } else {
126 return 0;
127 }
128 }
129
130 function addLinkObj( &$nt ) {
131 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
132 $title = $nt->getPrefixedDBkey();
133 if ( $this->isBadLink( $title ) ) { return 0; }
134 $id = $this->getGoodLinkID( $title );
135 if ( 0 != $id ) { return $id; }
136
137 $fname = 'LinkCache::addLinkObj';
138 wfProfileIn( $fname );
139
140 $ns = $nt->getNamespace();
141 $t = $nt->getDBkey();
142
143 if ( '' == $title ) {
144 wfProfileOut( $fname );
145 return 0;
146 }
147
148 $id = NULL;
149 if( $wgLinkCacheMemcached )
150 $id = $wgMemc->get( $key = $this->getKey( $title ) );
151 if( ! is_integer( $id ) ) {
152 if ( $this->mForUpdate ) {
153 $db =& wfGetDB( DB_MASTER );
154 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
155 $options = array( 'FOR UPDATE' );
156 }
157 } else {
158 $db =& wfGetDB( DB_SLAVE );
159 $options = array();
160 }
161
162 $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
163 if ( !$id ) {
164 $id = 0;
165 }
166 if( $wgLinkCacheMemcached )
167 $wgMemc->add( $key, $id, 3600*24 );
168 }
169
170 if( 0 == $id ) {
171 $this->addBadLinkObj( $nt );
172 } else {
173 $this->addGoodLinkObj( $id, $nt );
174 }
175 wfProfileOut( $fname );
176 return $id;
177 }
178
179 /**
180 * Bulk-check the pagelinks and page arrays for existence info.
181 * @param Title $fromtitle
182 */
183 function preFill( &$fromtitle ) {
184 global $wgEnablePersistentLC;
185
186 $fname = 'LinkCache::preFill';
187 wfProfileIn( $fname );
188
189 $this->suspend();
190 $id = $fromtitle->getArticleID();
191 $this->resume();
192
193 if( $id == 0 ) {
194 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
195 wfProfileOut( $fname );
196 return;
197 }
198
199 if ( $wgEnablePersistentLC ) {
200 if( $this->fillFromLinkscc( $id ) ){
201 wfProfileOut( $fname );
202 return;
203 }
204 }
205 if ( $this->mForUpdate ) {
206 $db =& wfGetDB( DB_MASTER );
207 $options = 'FOR UPDATE';
208 } else {
209 $db =& wfGetDB( DB_SLAVE );
210 $options = '';
211 }
212
213 $page = $db->tableName( 'page' );
214 $pagelinks = $db->tableName( 'pagelinks' );
215
216 $sql = "SELECT page_id,pl_namespace,pl_title
217 FROM $pagelinks
218 LEFT JOIN $page
219 ON pl_namespace=page_namespace AND pl_title=page_title
220 WHERE pl_from=$id $options";
221 $res = $db->query( $sql, $fname );
222 while( $s = $db->fetchObject( $res ) ) {
223 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
224 if( $s->page_id ) {
225 $this->addGoodLinkObj( $s->page_id, $title );
226 } else {
227 $this->addBadLinkObj( $title );
228 }
229 }
230 $this->mPreFilled = true;
231
232 if ( $wgEnablePersistentLC ) {
233 $this->saveToLinkscc( $id );
234 }
235 wfProfileOut( $fname );
236 }
237
238 function getGoodAdditions() {
239 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
240 }
241
242 function getBadAdditions() {
243 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
244 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
245 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
246 }
247
248 function getImageAdditions() {
249 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
250 }
251
252 function getGoodDeletions() {
253 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
254 }
255
256 function getBadDeletions() {
257 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
258 }
259
260 function getImageDeletions() {
261 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
262 }
263
264 function getPageAdditions() {
265 $set = array_diff( array_keys( $this->mPageLinks ), array_keys( $this->mOldPageLinks ) );
266 $out = array();
267 foreach( $set as $key ) {
268 $out[$key] = $this->mPageLinks[$key];
269 }
270 return $out;
271 }
272
273 function getPageDeletions() {
274 $set = array_diff( array_keys( $this->mOldPageLinks ), array_keys( $this->mPageLinks ) );
275 $out = array();
276 foreach( $set as $key ) {
277 $out[$key] = $this->mOldPageLinks[$key];
278 }
279 return $out;
280 }
281
282 /**
283 * Parameters:
284 * @param $which is one of the LINKCACHE_xxx constants
285 * @param $del,$add are the incremental update arrays which will be filled.
286 *
287 * @return Returns whether or not it's worth doing the incremental version.
288 *
289 * For example, if [[List of mathematical topics]] was blanked,
290 * it would take a long, long time to do incrementally.
291 */
292 function incrementalSetup( $which, &$del, &$add ) {
293 if ( ! $this->mPreFilled ) {
294 return false;
295 }
296
297 switch ( $which ) {
298 case LINKCACHE_GOOD:
299 $old =& $this->mOldGoodLinks;
300 $cur =& $this->mGoodLinks;
301 $del = $this->getGoodDeletions();
302 $add = $this->getGoodAdditions();
303 break;
304 case LINKCACHE_BAD:
305 $old =& $this->mOldBadLinks;
306 $cur =& $this->mBadLinks;
307 $del = $this->getBadDeletions();
308 $add = $this->getBadAdditions();
309 break;
310 case LINKCACHE_PAGE:
311 $old =& $this->mOldPageLinks;
312 $cur =& $this->mPageLinks;
313 $del = $this->getPageDeletions();
314 $add = $this->getPageAdditions();
315 break;
316 default: # LINKCACHE_IMAGE
317 return false;
318 }
319
320 return true;
321 }
322
323 /**
324 * Clears cache
325 */
326 function clear() {
327 $this->mPageLinks = array();
328 $this->mGoodLinks = array();
329 $this->mBadLinks = array();
330 $this->mImageLinks = array();
331 $this->mCategoryLinks = array();
332 $this->mOldGoodLinks = array();
333 $this->mOldBadLinks = array();
334 $this->mOldPageLinks = array();
335 }
336
337 /**
338 * @access private
339 */
340 function fillFromLinkscc( $id ){
341 $fname = 'LinkCache::fillFromLinkscc';
342
343 $id = IntVal( $id );
344 if ( $this->mForUpdate ) {
345 $db =& wfGetDB( DB_MASTER );
346 $options = 'FOR UPDATE';
347 } else {
348 $db =& wfGetDB( DB_SLAVE );
349 $options = '';
350 }
351 $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
352 if ( $raw === false ) {
353 return false;
354 }
355
356 $cacheobj = false;
357 if( function_exists( 'gzuncompress' ) )
358 $cacheobj = @gzuncompress( $raw );
359
360 if($cacheobj == FALSE){
361 $cacheobj = $raw;
362 }
363 $cc = @unserialize( $cacheobj );
364 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
365 $this->mOldPageLinks = $this->mPageLinks = $cc->mPageLinks;
366 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
367 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
368 $this->mPreFilled = true;
369 return TRUE;
370 } else {
371 return FALSE;
372 }
373
374 }
375
376 /**
377 * @access private
378 */
379 function saveToLinkscc( $pid ){
380 global $wgCompressedPersistentLC;
381 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
382 $ser = gzcompress( serialize( $this ), 3 );
383 } else {
384 $ser = serialize( $this );
385 }
386 $db =& wfGetDB( DB_MASTER );
387 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
388 }
389
390 /**
391 * Delete linkscc rows which link to here
392 * @param $title The page linked to
393 * @static
394 */
395 function linksccClearLinksTo( $title ){
396 global $wgEnablePersistentLC;
397 if ( $wgEnablePersistentLC ) {
398 $fname = 'LinkCache::linksccClearLinksTo';
399 $pid = intval( $pid );
400 $dbw =& wfGetDB( DB_MASTER );
401 # Delete linkscc rows which link to here
402 $dbw->deleteJoin( 'linkscc', 'pagelinks', 'lcc_pageid', 'pl_from',
403 array(
404 'pl_namespace' => $title->getNamespace(),
405 'pl-title' => $title->getDbKey() ),
406 $fname );
407 # Delete linkscc row representing this page
408 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
409 }
410
411 }
412
413 /**
414 * @param $pid is a page id
415 * @static
416 */
417 function linksccClearPage( $pid ){
418 global $wgEnablePersistentLC;
419 if ( $wgEnablePersistentLC ) {
420 $pid = intval( $pid );
421 $dbw =& wfGetDB( DB_MASTER );
422 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );
423 }
424 }
425
426 /**
427 * Swaps old and current link registers
428 */
429 function swapRegisters() {
430 swap( $this->mGoodLinks, $this->mOldGoodLinks );
431 swap( $this->mBadLinks, $this->mOldBadLinks );
432 swap( $this->mImageLinks, $this->mOldImageLinks );
433 }
434 }
435
436 /**
437 * Class representing a list of titles
438 * The execute() method checks them all for existence and adds them to a LinkCache object
439 +
440 * @package MediaWikki
441 * @subpackage Cache
442 */
443 class LinkBatch {
444 /**
445 * 2-d array, first index namespace, second index dbkey, value arbitrary
446 */
447 var $data = array();
448
449 function LinkBatch( $arr = array() ) {
450 foreach( $arr as $item ) {
451 $this->addObj( $item );
452 }
453 }
454
455 function addObj( $title ) {
456 $this->add( $title->getNamespace(), $title->getDBkey() );
457 }
458
459 function add( $ns, $dbkey ) {
460 if ( $ns < 0 ) {
461 return;
462 }
463 if ( !array_key_exists( $ns, $this->data ) ) {
464 $this->data[$ns] = array();
465 }
466
467 $this->data[$ns][$dbkey] = 1;
468 }
469
470 function execute( &$cache ) {
471 $fname = 'LinkBatch::execute';
472 $namespaces = array();
473
474 if ( !count( $this->data ) ) {
475 return;
476 }
477
478 wfProfileIn( $fname );
479
480 // Construct query
481 // This is very similar to Parser::replaceLinkHolders
482 $dbr = wfGetDB( DB_SLAVE );
483 $page = $dbr->tableName( 'page' );
484 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
485 . $this->constructSet( 'page', $dbr );
486
487 // Do query
488 $res = $dbr->query( $sql, $fname );
489
490 // Process results
491 // For each returned entry, add it to the list of good links, and remove it from $remaining
492
493 $remaining = $this->data;
494 while ( $row = $dbr->fetchObject( $res ) ) {
495 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
496 $cache->addGoodLinkObj( $row->page_id, $title );
497 unset( $remaining[$row->page_namespace][$row->page_title] );
498 }
499 $dbr->freeResult( $res );
500
501 // The remaining links in $data are bad links, register them as such
502 foreach ( $remaining as $ns => $dbkeys ) {
503 foreach ( $dbkeys as $dbkey => $nothing ) {
504 $title = Title::makeTitle( $ns, $dbkey );
505 $cache->addBadLinkObj( $title );
506 }
507 }
508
509 wfProfileOut( $fname );
510 }
511
512 /**
513 * Construct a WHERE clause which will match all the given titles.
514 * Give the appropriate table's field name prefix ('page', 'pl', etc).
515 *
516 * @param string $prefix
517 * @return string
518 * @access public
519 */
520 function constructSet( $prefix, $db ) {
521 $first = true;
522 $sql = '';
523 foreach ( $this->data as $ns => $dbkeys ) {
524 if ( !count( $dbkeys ) ) {
525 continue;
526 }
527
528 if ( $first ) {
529 $first = false;
530 } else {
531 $sql .= ' OR ';
532 }
533 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
534
535 $firstTitle = true;
536 foreach( $dbkeys as $dbkey => $nothing ) {
537 if ( $firstTitle ) {
538 $firstTitle = false;
539 } else {
540 $sql .= ',';
541 }
542 $sql .= $db->addQuotes( $dbkey );
543 }
544
545 $sql .= '))';
546 }
547 return $sql;
548 }
549 }
550
551 ?>