* links and brokenlinks tables merged to pagelinks; this will reduce pain
[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 }
49
50 /**
51 * General accessor to get/set whether SELECT FOR UPDATE should be used
52 */
53 function forUpdate( $update = NULL ) {
54 return wfSetVar( $this->mForUpdate, $update );
55 }
56
57 function getGoodLinkID( $title ) {
58 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
59 return $this->mGoodLinks[$title];
60 } else {
61 return 0;
62 }
63 }
64
65 function isBadLink( $title ) {
66 return array_key_exists( $title, $this->mBadLinks );
67 }
68
69 function addGoodLinkObj( $id, $title ) {
70 if ( $this->mActive ) {
71 $dbkey = $title->getPrefixedDbKey();
72 $this->mGoodLinks[$dbkey] = $id;
73 $this->mPageLinks[$dbkey] = $title;
74 }
75 }
76
77 function addBadLinkObj( $title ) {
78 $dbkey = $title->getPrefixedDbKey();
79 if ( $this->mActive && ( ! $this->isBadLink( $dbkey ) ) ) {
80 $this->mBadLinks[$dbkey] = 1;
81 $this->mPageLinks[$dbkey] = $title;
82 }
83 }
84
85 function addImageLink( $title ) {
86 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
87 }
88
89 function addImageLinkObj( $nt ) {
90 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
91 }
92
93 function addCategoryLink( $title, $sortkey ) {
94 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
95 }
96
97 function addCategoryLinkObj( &$nt, $sortkey ) {
98 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
99 }
100
101 function clearBadLink( $title ) {
102 unset( $this->mBadLinks[$title] );
103 $this->clearLink( $title );
104 }
105
106 function clearLink( $title ) {
107 global $wgMemc, $wgLinkCacheMemcached;
108 if( $wgLinkCacheMemcached )
109 $wgMemc->delete( $this->getKey( $title ) );
110 }
111
112 function suspend() { $this->mActive = false; }
113 function resume() { $this->mActive = true; }
114 function getPageLinks() { return $this->mPageLinks; }
115 function getGoodLinks() { return $this->mGoodLinks; }
116 function getBadLinks() { return array_keys( $this->mBadLinks ); }
117 function getImageLinks() { return $this->mImageLinks; }
118 function getCategoryLinks() { return $this->mCategoryLinks; }
119
120 function addLink( $title ) {
121 $nt = Title::newFromDBkey( $title );
122 if( $nt ) {
123 return $this->addLinkObj( $nt );
124 } else {
125 return 0;
126 }
127 }
128
129 function addLinkObj( &$nt ) {
130 global $wgMemc, $wgLinkCacheMemcached;
131 $title = $nt->getPrefixedDBkey();
132 if ( $this->isBadLink( $title ) ) { return 0; }
133 $id = $this->getGoodLinkID( $title );
134 if ( 0 != $id ) { return $id; }
135
136 $fname = 'LinkCache::addLinkObj';
137 wfProfileIn( $fname );
138
139 $ns = $nt->getNamespace();
140 $t = $nt->getDBkey();
141
142 if ( '' == $title ) {
143 wfProfileOut( $fname );
144 return 0;
145 }
146
147 $id = NULL;
148 if( $wgLinkCacheMemcached )
149 $id = $wgMemc->get( $key = $this->getKey( $title ) );
150 if( ! is_integer( $id ) ) {
151 if ( $this->mForUpdate ) {
152 $db =& wfGetDB( DB_MASTER );
153 $options = array( 'FOR UPDATE' );
154 } else {
155 $db =& wfGetDB( DB_SLAVE );
156 $options = array();
157 }
158
159 $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
160 if ( !$id ) {
161 $id = 0;
162 }
163 if( $wgLinkCacheMemcached )
164 $wgMemc->add( $key, $id, 3600*24 );
165 }
166
167 if( 0 == $id ) {
168 $this->addBadLinkObj( $nt );
169 } else {
170 $this->addGoodLinkObj( $id, $nt );
171 }
172 wfProfileOut( $fname );
173 return $id;
174 }
175
176 /**
177 * Bulk-check the pagelinks and page arrays for existence info.
178 * @param Title $fromtitle
179 */
180 function preFill( &$fromtitle ) {
181 global $wgEnablePersistentLC;
182
183 $fname = 'LinkCache::preFill';
184 wfProfileIn( $fname );
185
186 $this->suspend();
187 $id = $fromtitle->getArticleID();
188 $this->resume();
189
190 if( $id == 0 ) {
191 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
192 wfProfileOut( $fname );
193 return;
194 }
195
196 if ( $wgEnablePersistentLC ) {
197 if( $this->fillFromLinkscc( $id ) ){
198 wfProfileOut( $fname );
199 return;
200 }
201 }
202 if ( $this->mForUpdate ) {
203 $db =& wfGetDB( DB_MASTER );
204 $options = 'FOR UPDATE';
205 } else {
206 $db =& wfGetDB( DB_SLAVE );
207 $options = '';
208 }
209
210 $page = $db->tableName( 'page' );
211 $pagelinks = $db->tableName( 'pagelinks' );
212
213 $sql = "SELECT page_id,pl_namespace,pl_title
214 FROM $pagelinks
215 LEFT JOIN $page
216 ON pl_namespace=page_namespace AND pl_title=page_title
217 WHERE pl_from=$id $options";
218 $res = $db->query( $sql, $fname );
219 while( $s = $db->fetchObject( $res ) ) {
220 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
221 if( $s->page_id ) {
222 $this->addGoodLinkObj( $s->page_id, $title );
223 } else {
224 $this->addBadLinkObj( $title );
225 }
226 }
227 $this->mOldPageLinks = $this->mPageLinks;
228 $this->mOldBadLinks = $this->mBadLinks;
229 $this->mOldGoodLinks = $this->mGoodLinks;
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 but leaves old preFill copies alone
325 */
326 function clear() {
327 $this->mPageLinks = array();
328 $this->mGoodLinks = array();
329 $this->mBadLinks = array();
330 $this->mImageLinks = array();
331 }
332
333 /**
334 * @access private
335 */
336 function fillFromLinkscc( $id ){
337 $fname = 'LinkCache::fillFromLinkscc';
338
339 $id = IntVal( $id );
340 if ( $this->mForUpdate ) {
341 $db =& wfGetDB( DB_MASTER );
342 $options = 'FOR UPDATE';
343 } else {
344 $db =& wfGetDB( DB_SLAVE );
345 $options = '';
346 }
347 $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
348 if ( $raw === false ) {
349 return false;
350 }
351
352 $cacheobj = false;
353 if( function_exists( 'gzuncompress' ) )
354 $cacheobj = @gzuncompress( $raw );
355
356 if($cacheobj == FALSE){
357 $cacheobj = $raw;
358 }
359 $cc = @unserialize( $cacheobj );
360 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
361 $this->mOldPageLinks = $this->mPageLinks = $cc->mPageLinks;
362 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
363 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
364 $this->mPreFilled = true;
365 return TRUE;
366 } else {
367 return FALSE;
368 }
369
370 }
371
372 /**
373 * @access private
374 */
375 function saveToLinkscc( $pid ){
376 global $wgCompressedPersistentLC;
377 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
378 $ser = gzcompress( serialize( $this ), 3 );
379 } else {
380 $ser = serialize( $this );
381 }
382 $db =& wfGetDB( DB_MASTER );
383 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
384 }
385
386 /**
387 * Delete linkscc rows which link to here
388 * @param $title The page linked to
389 * @static
390 */
391 function linksccClearLinksTo( $title ){
392 global $wgEnablePersistentLC;
393 if ( $wgEnablePersistentLC ) {
394 $fname = 'LinkCache::linksccClearLinksTo';
395 $pid = intval( $pid );
396 $dbw =& wfGetDB( DB_MASTER );
397 # Delete linkscc rows which link to here
398 $dbw->deleteJoin( 'linkscc', 'pagelinks', 'lcc_pageid', 'pl_from',
399 array(
400 'pl_namespace' => $title->getNamespace(),
401 'pl-title' => $title->getDbKey() ),
402 $fname );
403 # Delete linkscc row representing this page
404 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
405 }
406
407 }
408
409 /**
410 * @param $pid is a page id
411 * @static
412 */
413 function linksccClearPage( $pid ){
414 global $wgEnablePersistentLC;
415 if ( $wgEnablePersistentLC ) {
416 $pid = intval( $pid );
417 $dbw =& wfGetDB( DB_MASTER );
418 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );
419 }
420 }
421 }
422
423 /**
424 * Class representing a list of titles
425 * The execute() method checks them all for existence and adds them to a LinkCache object
426 +
427 * @package MediaWikki
428 * @subpackage Cache
429 */
430 class LinkBatch {
431 /**
432 * 2-d array, first index namespace, second index dbkey, value arbitrary
433 */
434 var $data = array();
435
436 function LinkBatch( $arr = array() ) {
437 foreach( $arr as $item ) {
438 $this->addObj( $item );
439 }
440 }
441
442 function addObj( $title ) {
443 $this->add( $title->getNamespace(), $title->getDBkey() );
444 }
445
446 function add( $ns, $dbkey ) {
447 if ( $ns < 0 ) {
448 return;
449 }
450 if ( !array_key_exists( $ns, $this->data ) ) {
451 $this->data[$ns] = array();
452 }
453
454 $this->data[$ns][$dbkey] = 1;
455 }
456
457 function execute( &$cache ) {
458 $fname = 'LinkBatch::execute';
459 $namespaces = array();
460
461 if ( !count( $this->data ) ) {
462 return;
463 }
464
465 wfProfileIn( $fname );
466
467 // Construct query
468 // This is very similar to Parser::replaceLinkHolders
469 $dbr = wfGetDB( DB_SLAVE );
470 $page = $dbr->tableName( 'page' );
471 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
472 . $this->constructSet( 'page', $dbr );
473
474 // Do query
475 $res = $dbr->query( $sql, $fname );
476
477 // Process results
478 // For each returned entry, add it to the list of good links, and remove it from $remaining
479
480 $remaining = $this->data;
481 while ( $row = $dbr->fetchObject( $res ) ) {
482 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
483 $cache->addGoodLinkObj( $row->page_id, $title );
484 unset( $remaining[$row->page_namespace][$row->page_title] );
485 }
486 $dbr->freeResult( $res );
487
488 // The remaining links in $data are bad links, register them as such
489 foreach ( $remaining as $ns => $dbkeys ) {
490 foreach ( $dbkeys as $dbkey => $nothing ) {
491 $title = Title::makeTitle( $ns, $dbkey );
492 $cache->addBadLinkObj( $title );
493 }
494 }
495
496 wfProfileOut( $fname );
497 }
498
499 /**
500 * Construct a WHERE clause which will match all the given titles.
501 * Give the appropriate table's field name prefix ('page', 'pl', etc).
502 *
503 * @param string $prefix
504 * @return string
505 * @access public
506 */
507 function constructSet( $prefix, $db ) {
508 $first = true;
509 $sql = '';
510 foreach ( $this->data as $ns => $dbkeys ) {
511 if ( !count( $dbkeys ) ) {
512 continue;
513 }
514
515 if ( $first ) {
516 $first = false;
517 } else {
518 $sql .= ' OR ';
519 }
520 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
521
522 $firstTitle = true;
523 foreach( $dbkeys as $dbkey => $nothing ) {
524 if ( $firstTitle ) {
525 $firstTitle = false;
526 } else {
527 $sql .= ',';
528 }
529 $sql .= $db->addQuotes( $dbkey );
530 }
531
532 $sql .= '))';
533 }
534 return $sql;
535 }
536 }
537
538 ?>