7332524e8251139b5f12d166b4508ac4fd72d9a8
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 $title = $nt->getPrefixedDBkey();
104 if ( $this->isBadLink( $title ) ) { return 0; }
105 $id = $this->getGoodLinkID( $title );
106 if ( 0 != $id ) { return $id; }
109 $fname = "LinkCache::addLinkObj";
110 wfProfileIn( $fname );
112 $ns = $nt->getNamespace();
113 $t = $nt->getDBkey();
115 if ( "" == $title ) {
116 wfProfileOut( $fname );
121 if( $wgLinkCacheMemcached )
122 $id = $wgMemc->get( $key = $this->getKey( $title ) );
123 if( $id === FALSE ) {
124 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
125 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
126 $res = wfQuery( $sql, DB_READ
, "LinkCache::addLink" );
128 if ( 0 == wfNumRows( $res ) ) {
131 $s = wfFetchObject( $res );
134 if( $wgLinkCacheMemcached )
135 $wgMemc->add( $key, $id, time()+
3600 );
138 if ( 0 == $id ) { $this->addBadLink( $title ); }
139 else { $this->addGoodLink( $id, $title ); }
140 wfProfileOut( $fname );
144 function preFill( &$fromtitle )
146 global $wgEnablePersistentLC, $wgCompressedPersistentLC;
148 $fname = "LinkCache::preFill";
149 wfProfileIn( $fname );
150 # Note -- $fromtitle is a Title *object*
151 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
153 if ( $wgEnablePersistentLC ) {
154 $cc =& $this->getFromLinkscc( $dbkeyfrom );
156 $this->mOldGoodLinks
= $this->mGoodLinks
= $cc->mGoodLinks
;
157 $this->mOldBadLinks
= $this->mBadLinks
= $cc->mBadLinks
;
158 $this->mPreFilled
= true;
159 wfProfileOut( $fname );
160 wfDebug( "LinkCache::preFill - got from linkscc\n" );
166 $sql = "SELECT cur_id,cur_namespace,cur_title
168 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
169 $res = wfQuery( $sql, DB_READ
, $fname );
170 while( $s = wfFetchObject( $res ) ) {
171 $this->addGoodLink( $s->cur_id
,
172 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
177 $id = $fromtitle->getArticleID();
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 // put fetched link data into cache
194 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
195 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
197 $ser = wfStrencode( serialize( $this ) );
199 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$ser}')",
201 wfDebug( "LinkCache::preFill - saved to linkscc\n" );
204 wfProfileOut( $fname );
207 function getGoodAdditions()
209 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
212 function getBadAdditions()
214 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
215 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
216 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
219 function getImageAdditions()
221 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
224 function getGoodDeletions()
226 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
229 function getBadDeletions()
231 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
234 function getImageDeletions()
236 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
239 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
240 # the incremental update arrays which will be filled. Returns whether or not it's
241 # worth doing the incremental version. For example, if [[List of mathematical topics]]
242 # was blanked, it would take a long, long time to do incrementally.
243 function incrementalSetup( $which, &$del, &$add )
245 if ( ! $this->mPreFilled
) {
251 $old =& $this->mOldGoodLinks
;
252 $cur =& $this->mGoodLinks
;
253 $del = $this->getGoodDeletions();
254 $add = $this->getGoodAdditions();
257 $old =& $this->mOldBadLinks
;
258 $cur =& $this->mBadLinks
;
259 $del = $this->getBadDeletions();
260 $add = $this->getBadAdditions();
262 default: # LINKCACHE_IMAGE
269 # Clears cache but leaves old preFill copies alone
272 $this->mGoodLinks
= array();
273 $this->mBadLinks
= array();
274 $this->mImageLinks
= array();
278 function &getFromLinkscc( $dbkeyfrom ){
279 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
281 $row = wfFetchObject( $res );
286 if( function_exists( "gzuncompress" ) )
287 $cacheobj = @gzuncompress
( $row->lcc_cacheobj
);
289 if($cacheobj == FALSE){
290 $cacheobj = $row->lcc_cacheobj
;
292 $cc = @unserialize
( $cacheobj );
293 if( isset( $cc->mClassVer
) and ($cc->mClassVer
== $this->mClassVer
) ){