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 ) );
124 if( $id === FALSE ) {
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, $wgCompressedPersistentLC;
149 $fname = "LinkCache::preFill";
150 wfProfileIn( $fname );
151 # Note -- $fromtitle is a Title *object*
152 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
154 if ( $wgEnablePersistentLC ) {
155 $cc =& $this->getFromLinkscc( $dbkeyfrom );
157 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
158 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
159 $this->mPreFilled
= true;
160 wfProfileOut( $fname );
161 wfDebug( "LinkCache::preFill - got from linkscc\n" );
167 $sql = "SELECT cur_id,cur_namespace,cur_title
169 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
170 $res = wfQuery( $sql, DB_READ
, $fname );
171 while( $s = wfFetchObject( $res ) ) {
172 $this->addGoodLink( $s->cur_id
,
173 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
178 $id = $fromtitle->getArticleID();
183 WHERE bl_from='{$id}'";
184 $res = wfQuery( $sql, DB_READ
, "LinkCache::preFill" );
185 while( $s = wfFetchObject( $res ) ) {
186 $this->addBadLink( $s->bl_to
);
189 $this->mOldBadLinks
= $this->mBadLinks
;
190 $this->mOldGoodLinks
= $this->mGoodLinks
;
191 $this->mPreFilled
= true;
193 if ( $wgEnablePersistentLC ) {
194 // put fetched link data into cache
195 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
196 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
198 $ser = wfStrencode( serialize( $this ) );
200 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$ser}')",
202 wfDebug( "LinkCache::preFill - saved to linkscc\n" );
205 wfProfileOut( $fname );
208 function getGoodAdditions()
210 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
213 function getBadAdditions()
215 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
216 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
217 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
220 function getImageAdditions()
222 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
225 function getGoodDeletions()
227 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
230 function getBadDeletions()
232 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
235 function getImageDeletions()
237 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
240 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
241 # the incremental update arrays which will be filled. Returns whether or not it's
242 # worth doing the incremental version. For example, if [[List of mathematical topics]]
243 # was blanked, it would take a long, long time to do incrementally.
244 function incrementalSetup( $which, &$del, &$add )
246 if ( ! $this->mPreFilled
) {
252 $old =& $this->mOldGoodLinks
;
253 $cur =& $this->mGoodLinks
;
254 $del = $this->getGoodDeletions();
255 $add = $this->getGoodAdditions();
258 $old =& $this->mOldBadLinks
;
259 $cur =& $this->mBadLinks
;
260 $del = $this->getBadDeletions();
261 $add = $this->getBadAdditions();
263 default: # LINKCACHE_IMAGE
270 # Clears cache but leaves old preFill copies alone
273 $this->mGoodLinks
= array();
274 $this->mBadLinks
= array();
275 $this->mImageLinks
= array();
279 function &getFromLinkscc( $dbkeyfrom ){
280 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
282 $row = wfFetchObject( $res );
287 if( function_exists( "gzuncompress" ) )
288 $cacheobj = @gzuncompress
( $row->lcc_cacheobj
);
290 if($cacheobj == FALSE){
291 $cacheobj = $row->lcc_cacheobj
;
293 $cc = @unserialize
( $cacheobj );
294 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){