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);
21 // Increment $mClassVer whenever old serialized versions of this class
22 // becomes incompatible with the new version.
23 /* private */ var $mClassVer = 2;
25 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
26 /* private */ var $mImageLinks, $mCategoryLinks;
27 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
28 /* private */ var $mForUpdate;
30 /* private */ function getKey( $title ) {
32 return $wgDBname.':lc:title:'.$title;
35 function LinkCache() {
36 $this->mActive
= true;
37 $this->mPreFilled
= false;
38 $this->mForUpdate
= false;
39 $this->mGoodLinks
= array();
40 $this->mBadLinks
= array();
41 $this->mImageLinks
= array();
42 $this->mCategoryLinks
= array();
43 $this->mOldGoodLinks
= array();
44 $this->mOldBadLinks
= array();
48 * General accessor to get/set whether SELECT FOR UPDATE should be used
50 function forUpdate( $update = NULL ) {
51 return wfSetVar( $this->mForUpdate
, $update );
54 function getGoodLinkID( $title ) {
55 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
56 return $this->mGoodLinks
[$title];
62 function isBadLink( $title ) {
63 return array_key_exists( $title, $this->mBadLinks
);
66 function addGoodLink( $id, $title ) {
67 if ( $this->mActive
) {
68 $this->mGoodLinks
[$title] = $id;
72 function addBadLink( $title ) {
73 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
74 $this->mBadLinks
[$title] = 1;
78 function addImageLink( $title ) {
79 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
82 function addImageLinkObj( $nt ) {
83 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
86 function addCategoryLink( $title, $sortkey ) {
87 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
90 function addCategoryLinkObj( &$nt, $sortkey ) {
91 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
94 function clearBadLink( $title ) {
95 unset( $this->mBadLinks
[$title] );
96 $this->clearLink( $title );
99 function clearLink( $title ) {
100 global $wgMemc, $wgLinkCacheMemcached;
101 if( $wgLinkCacheMemcached )
102 $wgMemc->delete( $this->getKey( $title ) );
105 function suspend() { $this->mActive
= false; }
106 function resume() { $this->mActive
= true; }
107 function getGoodLinks() { return $this->mGoodLinks
; }
108 function getBadLinks() { return array_keys( $this->mBadLinks
); }
109 function getImageLinks() { return $this->mImageLinks
; }
110 function getCategoryLinks() { return $this->mCategoryLinks
; }
112 function addLink( $title ) {
113 $nt = Title
::newFromDBkey( $title );
115 return $this->addLinkObj( $nt );
121 function addLinkObj( &$nt ) {
122 global $wgMemc, $wgLinkCacheMemcached;
123 $title = $nt->getPrefixedDBkey();
124 if ( $this->isBadLink( $title ) ) { return 0; }
125 $id = $this->getGoodLinkID( $title );
126 if ( 0 != $id ) { return $id; }
128 $fname = 'LinkCache::addLinkObj';
129 wfProfileIn( $fname );
131 $ns = $nt->getNamespace();
132 $t = $nt->getDBkey();
134 if ( '' == $title ) {
135 wfProfileOut( $fname );
140 if( $wgLinkCacheMemcached )
141 $id = $wgMemc->get( $key = $this->getKey( $title ) );
142 if( ! is_integer( $id ) ) {
143 if ( $this->mForUpdate
) {
144 $db =& wfGetDB( DB_MASTER
);
145 $options = array( 'FOR UPDATE' );
147 $db =& wfGetDB( DB_SLAVE
);
151 $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
155 if( $wgLinkCacheMemcached )
156 $wgMemc->add( $key, $id, 3600*24 );
159 if ( 0 == $id ) { $this->addBadLink( $title ); }
160 else { $this->addGoodLink( $id, $title ); }
161 wfProfileOut( $fname );
165 function preFill( &$fromtitle ) {
166 global $wgEnablePersistentLC;
168 $fname = 'LinkCache::preFill';
169 wfProfileIn( $fname );
170 # Note -- $fromtitle is a Title *object*
173 $id = $fromtitle->getArticleID();
177 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
178 wfProfileOut( $fname );
182 if ( $wgEnablePersistentLC ) {
183 if( $this->fillFromLinkscc( $id ) ){
184 wfProfileOut( $fname );
188 if ( $this->mForUpdate
) {
189 $db =& wfGetDB( DB_MASTER
);
190 $options = 'FOR UPDATE';
192 $db =& wfGetDB( DB_SLAVE
);
196 $page = $db->tableName( 'page' );
197 $links = $db->tableName( 'links' );
199 $sql = "SELECT page_id,page_namespace,page_title
201 WHERE page_id=l_to AND l_from=$id $options";
202 $res = $db->query( $sql, $fname );
203 while( $s = $db->fetchObject( $res ) ) {
204 $this->addGoodLink( $s->page_id
,
205 Title
::makeName( $s->page_namespace
, $s->page_title
)
209 $res = $db->select( 'brokenlinks', array( 'bl_to' ), array( 'bl_from' => $id ), $fname, array( $options ) );
210 while( $s = $db->fetchObject( $res ) ) {
211 $this->addBadLink( $s->bl_to
);
214 $this->mOldBadLinks
= $this->mBadLinks
;
215 $this->mOldGoodLinks
= $this->mGoodLinks
;
216 $this->mPreFilled
= true;
218 if ( $wgEnablePersistentLC ) {
219 $this->saveToLinkscc( $id );
221 wfProfileOut( $fname );
224 function getGoodAdditions() {
225 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
228 function getBadAdditions() {
229 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
230 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
231 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
234 function getImageAdditions() {
235 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
238 function getGoodDeletions() {
239 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
242 function getBadDeletions() {
243 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
246 function getImageDeletions() {
247 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
252 * @param $which is one of the LINKCACHE_xxx constants
253 * @param $del,$add are the incremental update arrays which will be filled.
255 * @return Returns whether or not it's worth doing the incremental version.
257 * For example, if [[List of mathematical topics]] was blanked,
258 * it would take a long, long time to do incrementally.
260 function incrementalSetup( $which, &$del, &$add ) {
261 if ( ! $this->mPreFilled
) {
267 $old =& $this->mOldGoodLinks
;
268 $cur =& $this->mGoodLinks
;
269 $del = $this->getGoodDeletions();
270 $add = $this->getGoodAdditions();
273 $old =& $this->mOldBadLinks
;
274 $cur =& $this->mBadLinks
;
275 $del = $this->getBadDeletions();
276 $add = $this->getBadAdditions();
278 default: # LINKCACHE_IMAGE
286 * Clears cache but leaves old preFill copies alone
289 $this->mGoodLinks
= array();
290 $this->mBadLinks
= array();
291 $this->mImageLinks
= array();
297 function fillFromLinkscc( $id ){
298 $fname = 'LinkCache::fillFromLinkscc';
301 if ( $this->mForUpdate
) {
302 $db =& wfGetDB( DB_MASTER
);
303 $options = 'FOR UPDATE';
305 $db =& wfGetDB( DB_SLAVE
);
308 $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
309 if ( $raw === false ) {
314 if( function_exists( 'gzuncompress' ) )
315 $cacheobj = @gzuncompress
( $raw );
317 if($cacheobj == FALSE){
320 $cc = @unserialize
( $cacheobj );
321 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){
322 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
323 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
324 $this->mPreFilled
= true;
335 function saveToLinkscc( $pid ){
336 global $wgCompressedPersistentLC;
337 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
338 $ser = gzcompress( serialize( $this ), 3 );
340 $ser = serialize( $this );
342 $db =& wfGetDB( DB_MASTER
);
343 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
347 * Delete linkscc rows which link to here
348 * @param $pid is a page id
351 function linksccClearLinksTo( $pid ){
352 global $wgEnablePersistentLC;
353 if ( $wgEnablePersistentLC ) {
354 $fname = 'LinkCache::linksccClearLinksTo';
355 $pid = intval( $pid );
356 $dbw =& wfGetDB( DB_MASTER
);
357 # Delete linkscc rows which link to here
358 $dbw->deleteJoin( 'linkscc', 'links', 'lcc_pageid', 'l_from', array( 'l_to' => $pid ), $fname );
359 # Delete linkscc row representing this page
360 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
366 * Delete linkscc rows with broken links to here
367 * @param $title is a prefixed db title for example like Title->getPrefixedDBkey() returns.
370 function linksccClearBrokenLinksTo( $title ){
371 global $wgEnablePersistentLC;
372 $fname = 'LinkCache::linksccClearBrokenLinksTo';
374 if ( $wgEnablePersistentLC ) {
375 $dbw =& wfGetDB( DB_MASTER
);
376 $dbw->deleteJoin( 'linkscc', 'brokenlinks', 'lcc_pageid', 'bl_from', array( 'bl_to' => $title ), $fname );
381 * @param $pid is a page id
384 function linksccClearPage( $pid ){
385 global $wgEnablePersistentLC;
386 if ( $wgEnablePersistentLC ) {
387 $pid = intval( $pid );
388 $dbw =& wfGetDB( DB_MASTER
);
389 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );
395 * Class representing a list of titles
396 * The execute() method checks them all for existence and adds them to a LinkCache object
400 * 2-d array, first index namespace, second index dbkey, value arbitrary
404 function addObj( $title ) {
405 $this->add( $title->getNamespace(), $title->getDBkey() );
408 function add( $ns, $dbkey ) {
412 if ( !array_key_exists( $ns, $this->data
) ) {
413 $this->data
[$ns] = array();
416 $this->data
[$ns][$dbkey] = 1;
419 function execute( &$cache ) {
420 $fname = 'LinkBatch::execute';
421 $namespaces = array();
423 if ( !count( $this->data
) ) {
427 wfProfileIn( $fname );
430 // This is very similar to Parser::replaceLinkHolders
431 $dbr = wfGetDB( DB_SLAVE
);
432 $page = $dbr->tableName( 'page' );
433 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE ";
436 foreach ( $this->data
as $ns => $dbkeys ) {
437 if ( !count( $dbkeys ) ) {
446 $sql .= "(page_namespace=$ns AND page_title IN (";
449 foreach( $dbkeys as $dbkey => $nothing ) {
455 $sql .= $dbr->addQuotes( $dbkey );
462 $res = $dbr->query( $sql, $fname );
465 // For each returned entry, add it to the list of good links, and remove it from $remaining
467 $remaining = $this->data
;
468 while ( $row = $dbr->fetchObject( $res ) ) {
469 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
470 $cache->addGoodLink( $row->page_id
, $title->getPrefixedDBkey() );
471 unset( $remaining[$row->page_namespace
][$row->page_title
] );
473 $dbr->freeResult( $res );
475 // The remaining links in $data are bad links, register them as such
476 foreach ( $remaining as $ns => $dbkeys ) {
477 foreach ( $dbkeys as $dbkey => $nothing ) {
478 $title = Title
::makeTitle( $ns, $dbkey );
479 $cache->addBadLink( $title->getPrefixedText() );
483 wfProfileOut( $fname );