3 * Cache for article titles (prefixed DB keys) and ids linked from one source
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);
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
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;
32 /* private */ function getKey( $title ) {
34 return $wgDBname.':lc:title:'.$title;
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();
51 * General accessor to get/set whether SELECT FOR UPDATE should be used
53 function forUpdate( $update = NULL ) {
54 return wfSetVar( $this->mForUpdate
, $update );
57 function getGoodLinkID( $title ) {
58 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
59 return $this->mGoodLinks
[$title];
65 function isBadLink( $title ) {
66 return array_key_exists( $title, $this->mBadLinks
);
69 function addGoodLinkObj( $id, $title ) {
70 if ( $this->mActive
) {
71 $dbkey = $title->getPrefixedDbKey();
72 $this->mGoodLinks
[$dbkey] = $id;
73 $this->mPageLinks
[$dbkey] = $title;
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;
85 function addImageLink( $title ) {
86 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
89 function addImageLinkObj( $nt ) {
90 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
93 function addCategoryLink( $title, $sortkey ) {
94 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
97 function addCategoryLinkObj( &$nt, $sortkey ) {
98 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
101 function clearBadLink( $title ) {
102 unset( $this->mBadLinks
[$title] );
103 $this->clearLink( $title );
106 function clearLink( $title ) {
107 global $wgMemc, $wgLinkCacheMemcached;
108 if( $wgLinkCacheMemcached )
109 $wgMemc->delete( $this->getKey( $title ) );
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
; }
120 function addLink( $title ) {
121 $nt = Title
::newFromDBkey( $title );
123 return $this->addLinkObj( $nt );
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; }
136 $fname = 'LinkCache::addLinkObj';
137 wfProfileIn( $fname );
139 $ns = $nt->getNamespace();
140 $t = $nt->getDBkey();
142 if ( '' == $title ) {
143 wfProfileOut( $fname );
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' );
155 $db =& wfGetDB( DB_SLAVE
);
159 $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
163 if( $wgLinkCacheMemcached )
164 $wgMemc->add( $key, $id, 3600*24 );
168 $this->addBadLinkObj( $nt );
170 $this->addGoodLinkObj( $id, $nt );
172 wfProfileOut( $fname );
177 * Bulk-check the pagelinks and page arrays for existence info.
178 * @param Title $fromtitle
180 function preFill( &$fromtitle ) {
181 global $wgEnablePersistentLC;
183 $fname = 'LinkCache::preFill';
184 wfProfileIn( $fname );
187 $id = $fromtitle->getArticleID();
191 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
192 wfProfileOut( $fname );
196 if ( $wgEnablePersistentLC ) {
197 if( $this->fillFromLinkscc( $id ) ){
198 wfProfileOut( $fname );
202 if ( $this->mForUpdate
) {
203 $db =& wfGetDB( DB_MASTER
);
204 $options = 'FOR UPDATE';
206 $db =& wfGetDB( DB_SLAVE
);
210 $page = $db->tableName( 'page' );
211 $pagelinks = $db->tableName( 'pagelinks' );
213 $sql = "SELECT page_id,pl_namespace,pl_title
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
);
222 $this->addGoodLinkObj( $s->page_id
, $title );
224 $this->addBadLinkObj( $title );
227 $this->mOldPageLinks
= $this->mPageLinks
;
228 $this->mOldBadLinks
= $this->mBadLinks
;
229 $this->mOldGoodLinks
= $this->mGoodLinks
;
230 $this->mPreFilled
= true;
232 if ( $wgEnablePersistentLC ) {
233 $this->saveToLinkscc( $id );
235 wfProfileOut( $fname );
238 function getGoodAdditions() {
239 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
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
) ) );
248 function getImageAdditions() {
249 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
252 function getGoodDeletions() {
253 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
256 function getBadDeletions() {
257 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
260 function getImageDeletions() {
261 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
264 function getPageAdditions() {
265 $set = array_diff( array_keys( $this->mPageLinks
), array_keys( $this->mOldPageLinks
) );
267 foreach( $set as $key ) {
268 $out[$key] = $this->mPageLinks
[$key];
273 function getPageDeletions() {
274 $set = array_diff( array_keys( $this->mOldPageLinks
), array_keys( $this->mPageLinks
) );
276 foreach( $set as $key ) {
277 $out[$key] = $this->mOldPageLinks
[$key];
284 * @param $which is one of the LINKCACHE_xxx constants
285 * @param $del,$add are the incremental update arrays which will be filled.
287 * @return Returns whether or not it's worth doing the incremental version.
289 * For example, if [[List of mathematical topics]] was blanked,
290 * it would take a long, long time to do incrementally.
292 function incrementalSetup( $which, &$del, &$add ) {
293 if ( ! $this->mPreFilled
) {
299 $old =& $this->mOldGoodLinks
;
300 $cur =& $this->mGoodLinks
;
301 $del = $this->getGoodDeletions();
302 $add = $this->getGoodAdditions();
305 $old =& $this->mOldBadLinks
;
306 $cur =& $this->mBadLinks
;
307 $del = $this->getBadDeletions();
308 $add = $this->getBadAdditions();
311 $old =& $this->mOldPageLinks
;
312 $cur =& $this->mPageLinks
;
313 $del = $this->getPageDeletions();
314 $add = $this->getPageAdditions();
316 default: # LINKCACHE_IMAGE
324 * Clears cache but leaves old preFill copies alone
327 $this->mPageLinks
= array();
328 $this->mGoodLinks
= array();
329 $this->mBadLinks
= array();
330 $this->mImageLinks
= array();
336 function fillFromLinkscc( $id ){
337 $fname = 'LinkCache::fillFromLinkscc';
340 if ( $this->mForUpdate
) {
341 $db =& wfGetDB( DB_MASTER
);
342 $options = 'FOR UPDATE';
344 $db =& wfGetDB( DB_SLAVE
);
347 $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
348 if ( $raw === false ) {
353 if( function_exists( 'gzuncompress' ) )
354 $cacheobj = @gzuncompress
( $raw );
356 if($cacheobj == FALSE){
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;
375 function saveToLinkscc( $pid ){
376 global $wgCompressedPersistentLC;
377 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
378 $ser = gzcompress( serialize( $this ), 3 );
380 $ser = serialize( $this );
382 $db =& wfGetDB( DB_MASTER
);
383 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
387 * Delete linkscc rows which link to here
388 * @param $title The page linked to
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',
400 'pl_namespace' => $title->getNamespace(),
401 'pl-title' => $title->getDbKey() ),
403 # Delete linkscc row representing this page
404 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
410 * @param $pid is a page id
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 ) );
424 * Class representing a list of titles
425 * The execute() method checks them all for existence and adds them to a LinkCache object
427 * @package MediaWikki
432 * 2-d array, first index namespace, second index dbkey, value arbitrary
436 function LinkBatch( $arr = array() ) {
437 foreach( $arr as $item ) {
438 $this->addObj( $item );
442 function addObj( $title ) {
443 $this->add( $title->getNamespace(), $title->getDBkey() );
446 function add( $ns, $dbkey ) {
450 if ( !array_key_exists( $ns, $this->data
) ) {
451 $this->data
[$ns] = array();
454 $this->data
[$ns][$dbkey] = 1;
457 function execute( &$cache ) {
458 $fname = 'LinkBatch::execute';
459 $namespaces = array();
461 if ( !count( $this->data
) ) {
465 wfProfileIn( $fname );
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 );
475 $res = $dbr->query( $sql, $fname );
478 // For each returned entry, add it to the list of good links, and remove it from $remaining
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
] );
486 $dbr->freeResult( $res );
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 );
496 wfProfileOut( $fname );
500 * Construct a WHERE clause which will match all the given titles.
501 * Give the appropriate table's field name prefix ('page', 'pl', etc).
503 * @param string $prefix
507 function constructSet( $prefix, $db ) {
510 foreach ( $this->data
as $ns => $dbkeys ) {
511 if ( !count( $dbkeys ) ) {
520 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
523 foreach( $dbkeys as $dbkey => $nothing ) {
529 $sql .= $db->addQuotes( $dbkey );