2 # Cache for article titles (prefixed DB keys) 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 = 2;
14 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
15 /* private */ var $mImageLinks, $mCategoryLinks;
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->mCategoryLinks
= array();
31 $this->mOldGoodLinks
= array();
32 $this->mOldBadLinks
= array();
35 function getGoodLinkID( $title )
37 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
38 return $this->mGoodLinks
[$title];
44 function isBadLink( $title )
46 return array_key_exists( $title, $this->mBadLinks
);
49 function addGoodLink( $id, $title )
51 if ( $this->mActive
) {
52 $this->mGoodLinks
[$title] = $id;
56 function addBadLink( $title )
58 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
59 $this->mBadLinks
[$title] = 1;
63 function addImageLink( $title )
65 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
68 function addImageLinkObj( $nt )
70 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
73 function addCategoryLink( $title, $sortkey ) {
74 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
77 function addCategoryLinkObj( &$nt, $sortkey ) {
78 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
81 function clearBadLink( $title )
83 unset( $this->mBadLinks
[$title] );
84 $this->clearLink( $title );
87 function clearLink( $title )
89 global $wgMemc, $wgLinkCacheMemcached;
90 if( $wgLinkCacheMemcached )
91 $wgMemc->delete( $this->getKey( $title ) );
94 function suspend() { $this->mActive
= false; }
95 function resume() { $this->mActive
= true; }
96 function getGoodLinks() { return $this->mGoodLinks
; }
97 function getBadLinks() { return array_keys( $this->mBadLinks
); }
98 function getImageLinks() { return $this->mImageLinks
; }
99 function getCategoryLinks() { return $this->mCategoryLinks
; }
101 function addLink( $title )
103 $nt = Title
::newFromDBkey( $title );
105 return $this->addLinkObj( $nt );
111 function addLinkObj( &$nt )
113 global $wgMemc, $wgLinkCacheMemcached;
114 $title = $nt->getPrefixedDBkey();
115 if ( $this->isBadLink( $title ) ) { return 0; }
116 $id = $this->getGoodLinkID( $title );
117 if ( 0 != $id ) { return $id; }
119 $fname = "LinkCache::addLinkObj";
120 wfProfileIn( $fname );
122 $ns = $nt->getNamespace();
123 $t = $nt->getDBkey();
125 if ( "" == $title ) {
126 wfProfileOut( $fname );
131 if( $wgLinkCacheMemcached )
132 $id = $wgMemc->get( $key = $this->getKey( $title ) );
133 if( ! is_integer( $id ) ) {
134 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
135 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
136 $res = wfQuery( $sql, DB_READ
, "LinkCache::addLink" );
138 if ( 0 == wfNumRows( $res ) ) {
141 $s = wfFetchObject( $res );
144 if( $wgLinkCacheMemcached )
145 $wgMemc->add( $key, $id, 3600*24 );
148 if ( 0 == $id ) { $this->addBadLink( $title ); }
149 else { $this->addGoodLink( $id, $title ); }
150 wfProfileOut( $fname );
154 function preFill( &$fromtitle )
156 global $wgEnablePersistentLC;
158 $fname = "LinkCache::preFill";
159 wfProfileIn( $fname );
160 # Note -- $fromtitle is a Title *object*
163 $id = $fromtitle->getArticleID();
167 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
168 wfProfileOut( $fname );
172 if ( $wgEnablePersistentLC ) {
173 if( $this->fillFromLinkscc( $id ) ){
178 $sql = "SELECT cur_id,cur_namespace,cur_title
180 WHERE cur_id=l_to AND l_from=$id";
181 $res = wfQuery( $sql, DB_READ
, $fname );
182 while( $s = wfFetchObject( $res ) ) {
183 $this->addGoodLink( $s->cur_id
,
184 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
190 WHERE bl_from='{$id}'";
191 $res = wfQuery( $sql, DB_READ
, "LinkCache::preFill" );
192 while( $s = wfFetchObject( $res ) ) {
193 $this->addBadLink( $s->bl_to
);
196 $this->mOldBadLinks
= $this->mBadLinks
;
197 $this->mOldGoodLinks
= $this->mGoodLinks
;
198 $this->mPreFilled
= true;
200 if ( $wgEnablePersistentLC ) {
201 $this->saveToLinkscc( $id );
203 wfProfileOut( $fname );
206 function getGoodAdditions()
208 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
211 function getBadAdditions()
213 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
214 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
215 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
218 function getImageAdditions()
220 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
223 function getGoodDeletions()
225 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
228 function getBadDeletions()
230 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
233 function getImageDeletions()
235 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
238 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
239 # the incremental update arrays which will be filled. Returns whether or not it's
240 # worth doing the incremental version. For example, if [[List of mathematical topics]]
241 # was blanked, it would take a long, long time to do incrementally.
242 function incrementalSetup( $which, &$del, &$add )
244 if ( ! $this->mPreFilled
) {
250 $old =& $this->mOldGoodLinks
;
251 $cur =& $this->mGoodLinks
;
252 $del = $this->getGoodDeletions();
253 $add = $this->getGoodAdditions();
256 $old =& $this->mOldBadLinks
;
257 $cur =& $this->mBadLinks
;
258 $del = $this->getBadDeletions();
259 $add = $this->getBadAdditions();
261 default: # LINKCACHE_IMAGE
268 # Clears cache but leaves old preFill copies alone
271 $this->mGoodLinks
= array();
272 $this->mBadLinks
= array();
273 $this->mImageLinks
= array();
276 /* private */ function fillFromLinkscc( $id ){
278 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_pageid = $id",
280 $row = wfFetchObject( $res );
285 if( function_exists( "gzuncompress" ) )
286 $cacheobj = @gzuncompress
( $row->lcc_cacheobj
);
288 if($cacheobj == FALSE){
289 $cacheobj = $row->lcc_cacheobj
;
291 $cc = @unserialize
( $cacheobj );
292 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){
293 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
294 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
295 $this->mPreFilled
= true;
303 /* private */ function saveToLinkscc( $pid ){
304 global $wgCompressedPersistentLC;
305 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
306 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
308 $ser = wfStrencode( serialize( $this ) );
310 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_cacheobj) " .
311 "VALUES({$pid}, '{$ser}')", DB_WRITE
);
315 /* static */ function linksccClearLinksTo( $pid ){
316 $pid = intval( $pid );
317 wfQuery("DELETE linkscc FROM linkscc,links ".
318 "WHERE lcc_pageid=links.l_from AND l_to={$pid}", DB_WRITE
);
319 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
322 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
323 /* static */ function linksccClearBrokenLinksTo( $title ){
324 $title = wfStrencode( $title );
325 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
326 "WHERE lcc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE
);
330 /* static */ function linksccClearPage( $pid ){
331 $pid = intval( $pid );
332 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);