3 * Cache for article titles (prefixed DB keys) and ids linked from one source
9 # These are used in incrementalSetup()
10 define ('LINKCACHE_GOOD', 0);
11 define ('LINKCACHE_BAD', 1);
12 define ('LINKCACHE_IMAGE', 2);
18 // Increment $mClassVer whenever old serialized versions of this class
19 // becomes incompatible with the new version.
20 /* private */ var $mClassVer = 2;
22 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
23 /* private */ var $mImageLinks, $mCategoryLinks;
24 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
25 /* private */ var $mForUpdate;
27 /* private */ function getKey( $title ) {
29 return $wgDBname.':lc:title:'.$title;
32 function LinkCache() {
33 $this->mActive
= true;
34 $this->mPreFilled
= false;
35 $this->mForUpdate
= false;
36 $this->mGoodLinks
= array();
37 $this->mBadLinks
= array();
38 $this->mImageLinks
= array();
39 $this->mCategoryLinks
= array();
40 $this->mOldGoodLinks
= array();
41 $this->mOldBadLinks
= array();
45 * General accessor to get/set whether SELECT FOR UPDATE should be used
47 function forUpdate( $update = NULL ) {
48 return wfSetVar( $this->mForUpdate
, $update );
51 function getGoodLinkID( $title ) {
52 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
53 return $this->mGoodLinks
[$title];
59 function isBadLink( $title ) {
60 return array_key_exists( $title, $this->mBadLinks
);
63 function addGoodLink( $id, $title ) {
64 if ( $this->mActive
) {
65 $this->mGoodLinks
[$title] = $id;
69 function addBadLink( $title ) {
70 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
71 $this->mBadLinks
[$title] = 1;
75 function addImageLink( $title ) {
76 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
79 function addImageLinkObj( $nt ) {
80 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
83 function addCategoryLink( $title, $sortkey ) {
84 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
87 function addCategoryLinkObj( &$nt, $sortkey ) {
88 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
91 function clearBadLink( $title ) {
92 unset( $this->mBadLinks
[$title] );
93 $this->clearLink( $title );
96 function clearLink( $title ) {
97 global $wgMemc, $wgLinkCacheMemcached;
98 if( $wgLinkCacheMemcached )
99 $wgMemc->delete( $this->getKey( $title ) );
102 function suspend() { $this->mActive
= false; }
103 function resume() { $this->mActive
= true; }
104 function getGoodLinks() { return $this->mGoodLinks
; }
105 function getBadLinks() { return array_keys( $this->mBadLinks
); }
106 function getImageLinks() { return $this->mImageLinks
; }
107 function getCategoryLinks() { return $this->mCategoryLinks
; }
109 function addLink( $title ) {
110 $nt = Title
::newFromDBkey( $title );
112 return $this->addLinkObj( $nt );
118 function addLinkObj( &$nt ) {
119 global $wgMemc, $wgLinkCacheMemcached;
120 $title = $nt->getPrefixedDBkey();
121 if ( $this->isBadLink( $title ) ) { return 0; }
122 $id = $this->getGoodLinkID( $title );
123 if ( 0 != $id ) { return $id; }
125 $fname = 'LinkCache::addLinkObj';
126 wfProfileIn( $fname );
128 $ns = $nt->getNamespace();
129 $t = $nt->getDBkey();
131 if ( '' == $title ) {
132 wfProfileOut( $fname );
137 if( $wgLinkCacheMemcached )
138 $id = $wgMemc->get( $key = $this->getKey( $title ) );
139 if( ! is_integer( $id ) ) {
140 if ( $this->mForUpdate
) {
141 $db =& wfGetDB( DB_MASTER
);
142 $options = array( 'FOR UPDATE' );
144 $db =& wfGetDB( DB_SLAVE
);
148 $id = $db->getField( 'cur', 'cur_id', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname, $options );
152 if( $wgLinkCacheMemcached )
153 $wgMemc->add( $key, $id, 3600*24 );
156 if ( 0 == $id ) { $this->addBadLink( $title ); }
157 else { $this->addGoodLink( $id, $title ); }
158 wfProfileOut( $fname );
162 function preFill( &$fromtitle ) {
163 global $wgEnablePersistentLC;
165 $fname = 'LinkCache::preFill';
166 wfProfileIn( $fname );
167 # Note -- $fromtitle is a Title *object*
170 $id = $fromtitle->getArticleID();
174 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
175 wfProfileOut( $fname );
179 if ( $wgEnablePersistentLC ) {
180 if( $this->fillFromLinkscc( $id ) ){
181 wfProfileOut( $fname );
185 if ( $this->mForUpdate
) {
186 $db =& wfGetDB( DB_MASTER
);
187 $options = 'FOR UPDATE';
189 $db =& wfGetDB( DB_SLAVE
);
193 $cur = $db->tableName( 'cur' );
194 $links = $db->tableName( 'links' );
196 $sql = "SELECT cur_id,cur_namespace,cur_title
198 WHERE cur_id=l_to AND l_from=$id $options";
199 $res = $db->query( $sql, $fname );
200 while( $s = $db->fetchObject( $res ) ) {
201 $this->addGoodLink( $s->cur_id
,
202 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
206 $res = $db->select( 'brokenlinks', array( 'bl_to' ), array( 'bl_from' => $id ), $fname, array( $options ) );
207 while( $s = $db->fetchObject( $res ) ) {
208 $this->addBadLink( $s->bl_to
);
211 $this->mOldBadLinks
= $this->mBadLinks
;
212 $this->mOldGoodLinks
= $this->mGoodLinks
;
213 $this->mPreFilled
= true;
215 if ( $wgEnablePersistentLC ) {
216 $this->saveToLinkscc( $id );
218 wfProfileOut( $fname );
221 function getGoodAdditions() {
222 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
225 function getBadAdditions() {
226 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
227 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
228 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
231 function getImageAdditions() {
232 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
235 function getGoodDeletions() {
236 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
239 function getBadDeletions() {
240 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
243 function getImageDeletions() {
244 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
249 * @param $which is one of the LINKCACHE_xxx constants
250 * @param $del,$add are the incremental update arrays which will be filled.
252 * @return Returns whether or not it's worth doing the incremental version.
254 * For example, if [[List of mathematical topics]] was blanked,
255 * it would take a long, long time to do incrementally.
257 function incrementalSetup( $which, &$del, &$add ) {
258 if ( ! $this->mPreFilled
) {
264 $old =& $this->mOldGoodLinks
;
265 $cur =& $this->mGoodLinks
;
266 $del = $this->getGoodDeletions();
267 $add = $this->getGoodAdditions();
270 $old =& $this->mOldBadLinks
;
271 $cur =& $this->mBadLinks
;
272 $del = $this->getBadDeletions();
273 $add = $this->getBadAdditions();
275 default: # LINKCACHE_IMAGE
283 * Clears cache but leaves old preFill copies alone
286 $this->mGoodLinks
= array();
287 $this->mBadLinks
= array();
288 $this->mImageLinks
= array();
294 function fillFromLinkscc( $id ){
295 $fname = 'LinkCache::fillFromLinkscc';
298 if ( $this->mForUpdate
) {
299 $db =& wfGetDB( DB_MASTER
);
300 $options = 'FOR UPDATE';
302 $db =& wfGetDB( DB_SLAVE
);
305 $raw = $db->getField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
306 if ( $raw === false ) {
311 if( function_exists( 'gzuncompress' ) )
312 $cacheobj = @gzuncompress
( $raw );
314 if($cacheobj == FALSE){
317 $cc = @unserialize
( $cacheobj );
318 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){
319 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
320 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
321 $this->mPreFilled
= true;
332 function saveToLinkscc( $pid ){
333 global $wgCompressedPersistentLC;
334 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
335 $ser = gzcompress( serialize( $this ), 3 );
337 $ser = serialize( $this );
339 $db =& wfGetDB( DB_MASTER
);
340 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
344 * Delete linkscc rows which link to here
345 * @param $pid is a page id
348 function linksccClearLinksTo( $pid ){
349 global $wgEnablePersistentLC;
350 if ( $wgEnablePersistentLC ) {
351 $fname = 'LinkCache::linksccClearLinksTo';
352 $pid = intval( $pid );
353 $dbw =& wfGetDB( DB_MASTER
);
354 # Delete linkscc rows which link to here
355 $dbw->deleteJoin( 'linkscc', 'links', 'lcc_pageid', 'l_from', array( 'l_to' => $pid ), $fname );
356 # Delete linkscc row representing this page
357 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
363 * Delete linkscc rows with broken links to here
364 * @param $title is a prefixed db title for example like Title->getPrefixedDBkey() returns.
367 function linksccClearBrokenLinksTo( $title ){
368 global $wgEnablePersistentLC;
369 $fname = 'LinkCache::linksccClearBrokenLinksTo';
371 if ( $wgEnablePersistentLC ) {
372 $dbw =& wfGetDB( DB_MASTER
);
373 $dbw->deleteJoin( 'linkscc', 'brokenlinks', 'lcc_pageid', 'bl_from', array( 'bl_to' => $title ), $fname );
378 * @param $pid is a page id
381 function linksccClearPage( $pid ){
382 global $wgEnablePersistentLC;
383 if ( $wgEnablePersistentLC ) {
384 $pid = intval( $pid );
385 $dbw =& wfGetDB( DB_MASTER
);
386 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );