3 * Cache for article titles (prefixed DB keys) and ids linked from one source
10 # These are used in incrementalSetup()
11 define ('LINKCACHE_GOOD', 0);
12 define ('LINKCACHE_BAD', 1);
13 define ('LINKCACHE_IMAGE', 2);
20 // Increment $mClassVer whenever old serialized versions of this class
21 // becomes incompatible with the new version.
22 /* private */ var $mClassVer = 2;
24 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
25 /* private */ var $mImageLinks, $mCategoryLinks;
26 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
27 /* private */ var $mForUpdate;
29 /* private */ function getKey( $title ) {
31 return $wgDBname.':lc:title:'.$title;
34 function LinkCache() {
35 $this->mActive
= true;
36 $this->mPreFilled
= false;
37 $this->mForUpdate
= false;
38 $this->mGoodLinks
= array();
39 $this->mBadLinks
= array();
40 $this->mImageLinks
= array();
41 $this->mCategoryLinks
= array();
42 $this->mOldGoodLinks
= array();
43 $this->mOldBadLinks
= array();
47 * General accessor to get/set whether SELECT FOR UPDATE should be used
49 function forUpdate( $update = NULL ) {
50 return wfSetVar( $this->mForUpdate
, $update );
53 function getGoodLinkID( $title ) {
54 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
55 return $this->mGoodLinks
[$title];
61 function isBadLink( $title ) {
62 return array_key_exists( $title, $this->mBadLinks
);
65 function addGoodLink( $id, $title ) {
66 if ( $this->mActive
) {
67 $this->mGoodLinks
[$title] = $id;
71 function addBadLink( $title ) {
72 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
73 $this->mBadLinks
[$title] = 1;
77 function addImageLink( $title ) {
78 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
81 function addImageLinkObj( $nt ) {
82 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
85 function addCategoryLink( $title, $sortkey ) {
86 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
89 function addCategoryLinkObj( &$nt, $sortkey ) {
90 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
93 function clearBadLink( $title ) {
94 unset( $this->mBadLinks
[$title] );
95 $this->clearLink( $title );
98 function clearLink( $title ) {
99 global $wgMemc, $wgLinkCacheMemcached;
100 if( $wgLinkCacheMemcached )
101 $wgMemc->delete( $this->getKey( $title ) );
104 function suspend() { $this->mActive
= false; }
105 function resume() { $this->mActive
= true; }
106 function getGoodLinks() { return $this->mGoodLinks
; }
107 function getBadLinks() { return array_keys( $this->mBadLinks
); }
108 function getImageLinks() { return $this->mImageLinks
; }
109 function getCategoryLinks() { return $this->mCategoryLinks
; }
111 function addLink( $title ) {
112 $nt = Title
::newFromDBkey( $title );
114 return $this->addLinkObj( $nt );
120 function addLinkObj( &$nt ) {
121 global $wgMemc, $wgLinkCacheMemcached;
122 $title = $nt->getPrefixedDBkey();
123 if ( $this->isBadLink( $title ) ) { return 0; }
124 $id = $this->getGoodLinkID( $title );
125 if ( 0 != $id ) { return $id; }
127 $fname = 'LinkCache::addLinkObj';
128 wfProfileIn( $fname );
130 $ns = $nt->getNamespace();
131 $t = $nt->getDBkey();
133 if ( '' == $title ) {
134 wfProfileOut( $fname );
139 if( $wgLinkCacheMemcached )
140 $id = $wgMemc->get( $key = $this->getKey( $title ) );
141 if( ! is_integer( $id ) ) {
142 if ( $this->mForUpdate
) {
143 $db =& wfGetDB( DB_MASTER
);
144 $options = array( 'FOR UPDATE' );
146 $db =& wfGetDB( DB_SLAVE
);
150 $id = $db->getField( 'cur', 'cur_id', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname, $options );
154 if( $wgLinkCacheMemcached )
155 $wgMemc->add( $key, $id, 3600*24 );
158 if ( 0 == $id ) { $this->addBadLink( $title ); }
159 else { $this->addGoodLink( $id, $title ); }
160 wfProfileOut( $fname );
164 function preFill( &$fromtitle ) {
165 global $wgEnablePersistentLC;
167 $fname = 'LinkCache::preFill';
168 wfProfileIn( $fname );
169 # Note -- $fromtitle is a Title *object*
172 $id = $fromtitle->getArticleID();
176 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
177 wfProfileOut( $fname );
181 if ( $wgEnablePersistentLC ) {
182 if( $this->fillFromLinkscc( $id ) ){
183 wfProfileOut( $fname );
187 if ( $this->mForUpdate
) {
188 $db =& wfGetDB( DB_MASTER
);
189 $options = 'FOR UPDATE';
191 $db =& wfGetDB( DB_SLAVE
);
195 $cur = $db->tableName( 'cur' );
196 $links = $db->tableName( 'links' );
198 $sql = "SELECT cur_id,cur_namespace,cur_title
200 WHERE cur_id=l_to AND l_from=$id $options";
201 $res = $db->query( $sql, $fname );
202 while( $s = $db->fetchObject( $res ) ) {
203 $this->addGoodLink( $s->cur_id
,
204 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
208 $res = $db->select( 'brokenlinks', array( 'bl_to' ), array( 'bl_from' => $id ), $fname, array( $options ) );
209 while( $s = $db->fetchObject( $res ) ) {
210 $this->addBadLink( $s->bl_to
);
213 $this->mOldBadLinks
= $this->mBadLinks
;
214 $this->mOldGoodLinks
= $this->mGoodLinks
;
215 $this->mPreFilled
= true;
217 if ( $wgEnablePersistentLC ) {
218 $this->saveToLinkscc( $id );
220 wfProfileOut( $fname );
223 function getGoodAdditions() {
224 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
227 function getBadAdditions() {
228 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
229 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
230 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
233 function getImageAdditions() {
234 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
237 function getGoodDeletions() {
238 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
241 function getBadDeletions() {
242 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
245 function getImageDeletions() {
246 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
251 * @param $which is one of the LINKCACHE_xxx constants
252 * @param $del,$add are the incremental update arrays which will be filled.
254 * @return Returns whether or not it's worth doing the incremental version.
256 * For example, if [[List of mathematical topics]] was blanked,
257 * it would take a long, long time to do incrementally.
259 function incrementalSetup( $which, &$del, &$add ) {
260 if ( ! $this->mPreFilled
) {
266 $old =& $this->mOldGoodLinks
;
267 $cur =& $this->mGoodLinks
;
268 $del = $this->getGoodDeletions();
269 $add = $this->getGoodAdditions();
272 $old =& $this->mOldBadLinks
;
273 $cur =& $this->mBadLinks
;
274 $del = $this->getBadDeletions();
275 $add = $this->getBadAdditions();
277 default: # LINKCACHE_IMAGE
285 * Clears cache but leaves old preFill copies alone
288 $this->mGoodLinks
= array();
289 $this->mBadLinks
= array();
290 $this->mImageLinks
= array();
296 function fillFromLinkscc( $id ){
297 $fname = 'LinkCache::fillFromLinkscc';
300 if ( $this->mForUpdate
) {
301 $db =& wfGetDB( DB_MASTER
);
302 $options = 'FOR UPDATE';
304 $db =& wfGetDB( DB_SLAVE
);
307 $raw = $db->getField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
308 if ( $raw === false ) {
313 if( function_exists( 'gzuncompress' ) )
314 $cacheobj = @gzuncompress
( $raw );
316 if($cacheobj == FALSE){
319 $cc = @unserialize
( $cacheobj );
320 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){
321 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
322 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
323 $this->mPreFilled
= true;
334 function saveToLinkscc( $pid ){
335 global $wgCompressedPersistentLC;
336 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
337 $ser = gzcompress( serialize( $this ), 3 );
339 $ser = serialize( $this );
341 $db =& wfGetDB( DB_MASTER
);
342 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
346 * Delete linkscc rows which link to here
347 * @param $pid is a page id
350 function linksccClearLinksTo( $pid ){
351 global $wgEnablePersistentLC;
352 if ( $wgEnablePersistentLC ) {
353 $fname = 'LinkCache::linksccClearLinksTo';
354 $pid = intval( $pid );
355 $dbw =& wfGetDB( DB_MASTER
);
356 # Delete linkscc rows which link to here
357 $dbw->deleteJoin( 'linkscc', 'links', 'lcc_pageid', 'l_from', array( 'l_to' => $pid ), $fname );
358 # Delete linkscc row representing this page
359 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
365 * Delete linkscc rows with broken links to here
366 * @param $title is a prefixed db title for example like Title->getPrefixedDBkey() returns.
369 function linksccClearBrokenLinksTo( $title ){
370 global $wgEnablePersistentLC;
371 $fname = 'LinkCache::linksccClearBrokenLinksTo';
373 if ( $wgEnablePersistentLC ) {
374 $dbw =& wfGetDB( DB_MASTER
);
375 $dbw->deleteJoin( 'linkscc', 'brokenlinks', 'lcc_pageid', 'bl_from', array( 'bl_to' => $title ), $fname );
380 * @param $pid is a page id
383 function linksccClearPage( $pid ){
384 global $wgEnablePersistentLC;
385 if ( $wgEnablePersistentLC ) {
386 $pid = intval( $pid );
387 $dbw =& wfGetDB( DB_MASTER
);
388 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );