2 # Cache for article titles and ids linked from one source
4 # These are used in incrementalSetup()
5 define ('LINKCACHE_GOOD', 0);
6 define ('LINKCACHE_BAD', 1);
7 define ('LINKCACHE_IMAGE', 2);
10 // Increment $mClassVer whenever old serialized versions of this class
11 // becomes incompatible with the new version.
12 /* private */ var $mClassVer = 1;
14 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
15 /* private */ var $mImageLinks;
16 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
18 /* private */ function getKey( $title ) {
20 return "$wgDBname:lc:title:$title";
25 $this->mActive
= true;
26 $this->mPreFilled
= false;
27 $this->mGoodLinks
= array();
28 $this->mBadLinks
= array();
29 $this->mImageLinks
= array();
30 $this->mOldGoodLinks
= array();
31 $this->mOldBadLinks
= array();
34 function getGoodLinkID( $title )
36 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
37 return $this->mGoodLinks
[$title];
43 function isBadLink( $title )
45 return array_key_exists( $title, $this->mBadLinks
);
48 function addGoodLink( $id, $title )
50 if ( $this->mActive
) {
51 $this->mGoodLinks
[$title] = $id;
55 function addBadLink( $title )
57 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
58 $this->mBadLinks
[$title] = 1;
62 function addImageLink( $title )
64 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
67 function addImageLinkObj( $nt )
69 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
72 function clearBadLink( $title )
74 unset( $this->mBadLinks
[$title] );
75 $this->clearLink( $title );
78 function clearLink( $title )
80 global $wgMemc, $wgLinkCacheMemcached;
81 if( $wgLinkCacheMemcached )
82 $wgMemc->delete( $this->getKey( $title ) );
85 function suspend() { $this->mActive
= false; }
86 function resume() { $this->mActive
= true; }
87 function getGoodLinks() { return $this->mGoodLinks
; }
88 function getBadLinks() { return array_keys( $this->mBadLinks
); }
89 function getImageLinks() { return $this->mImageLinks
; }
91 function addLink( $title )
93 $nt = Title
::newFromDBkey( $title );
95 return $this->addLinkObj( $nt );
101 function addLinkObj( &$nt )
103 global $wgMemc, $wgLinkCacheMemcached;
105 $title = $nt->getPrefixedDBkey();
106 if ( $this->isBadLink( $title ) ) { return 0; }
107 $id = $this->getGoodLinkID( $title );
108 if ( 0 != $id ) { return $id; }
110 $fname = "LinkCache::addLinkObj";
111 wfProfileIn( $fname );
113 $ns = $nt->getNamespace();
114 $t = $nt->getDBkey();
116 if ( "" == $title ) {
117 wfProfileOut( $fname );
122 if( $wgLinkCacheMemcached )
123 $id = $wgMemc->get( $key = $this->getKey( $title ) );
125 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
126 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
127 $res = wfQuery( $sql, DB_READ
, "LinkCache::addLink" );
129 if ( 0 == wfNumRows( $res ) ) {
132 $s = wfFetchObject( $res );
135 if( $wgLinkCacheMemcached )
136 $wgMemc->add( $key, $id, time()+
3600 );
139 if ( 0 == $id ) { $this->addBadLink( $title ); }
140 else { $this->addGoodLink( $id, $title ); }
141 wfProfileOut( $fname );
145 function preFill( &$fromtitle )
147 global $wgEnablePersistentLC;
149 $fname = "LinkCache::preFill";
150 wfProfileIn( $fname );
151 # Note -- $fromtitle is a Title *object*
152 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
154 if ( $wgEnablePersistentLC ) {
155 if( $this->fillFromLinkscc( $dbkeyfrom ) ){
160 $sql = "SELECT cur_id,cur_namespace,cur_title
162 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
163 $res = wfQuery( $sql, DB_READ
, $fname );
164 while( $s = wfFetchObject( $res ) ) {
165 $this->addGoodLink( $s->cur_id
,
166 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
171 $id = $fromtitle->getArticleID();
175 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
176 wfProfileOut( $fname );
182 WHERE bl_from='{$id}'";
183 $res = wfQuery( $sql, DB_READ
, "LinkCache::preFill" );
184 while( $s = wfFetchObject( $res ) ) {
185 $this->addBadLink( $s->bl_to
);
188 $this->mOldBadLinks
= $this->mBadLinks
;
189 $this->mOldGoodLinks
= $this->mGoodLinks
;
190 $this->mPreFilled
= true;
192 if ( $wgEnablePersistentLC ) {
193 $this->saveToLinkscc( $id, $dbkeyfrom );
195 wfProfileOut( $fname );
198 function getGoodAdditions()
200 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
203 function getBadAdditions()
205 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
206 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
207 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
210 function getImageAdditions()
212 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
215 function getGoodDeletions()
217 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
220 function getBadDeletions()
222 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
225 function getImageDeletions()
227 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
230 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
231 # the incremental update arrays which will be filled. Returns whether or not it's
232 # worth doing the incremental version. For example, if [[List of mathematical topics]]
233 # was blanked, it would take a long, long time to do incrementally.
234 function incrementalSetup( $which, &$del, &$add )
236 if ( ! $this->mPreFilled
) {
242 $old =& $this->mOldGoodLinks
;
243 $cur =& $this->mGoodLinks
;
244 $del = $this->getGoodDeletions();
245 $add = $this->getGoodAdditions();
248 $old =& $this->mOldBadLinks
;
249 $cur =& $this->mBadLinks
;
250 $del = $this->getBadDeletions();
251 $add = $this->getBadAdditions();
253 default: # LINKCACHE_IMAGE
260 # Clears cache but leaves old preFill copies alone
263 $this->mGoodLinks
= array();
264 $this->mBadLinks
= array();
265 $this->mImageLinks
= array();
268 /* private */ function fillFromLinkscc( $dbkeyfrom ){
269 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
271 $row = wfFetchObject( $res );
276 if( function_exists( "gzuncompress" ) )
277 $cacheobj = @gzuncompress
( $row->lcc_cacheobj
);
279 if($cacheobj == FALSE){
280 $cacheobj = $row->lcc_cacheobj
;
282 $cc = @unserialize
( $cacheobj );
283 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){
284 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
285 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
286 $this->mPreFilled
= true;
294 /* private */ function saveToLinkscc( $pid, $dbkeyfrom ){
295 global $wgCompressedPersistentLC;
296 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
297 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
299 $ser = wfStrencode( serialize( $this ) );
301 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_title,lcc_cacheobj) " .
302 "VALUES({$pid}, '{$dbkeyfrom}', '{$ser}')", DB_WRITE
);
306 /* static */ function linksccClearLinksTo( $pid ){
307 $pid = intval( $pid );
308 wfQuery("DELETE linkscc FROM linkscc,links ".
309 "WHERE lcc_title=links.l_from AND l_to={$pid}", DB_WRITE
);
310 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
313 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
314 /* static */ function linksccClearBrokenLinksTo( $title ){
315 $title = wfStrencode( $title );
316 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
317 "WHERE lcc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE
);
321 /* static */ function linksccClearPage( $pid ){
322 $pid = intval( $pid );
323 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);