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);
11 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
12 /* private */ var $mImageLinks;
13 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
15 /* private */ function getKey( $title ) {
17 return "$wgDBname:lc:title:$title";
22 $this->mActive
= true;
23 $this->mPreFilled
= false;
24 $this->mGoodLinks
= array();
25 $this->mBadLinks
= array();
26 $this->mImageLinks
= array();
27 $this->mOldGoodLinks
= array();
28 $this->mOldBadLinks
= array();
31 function getGoodLinkID( $title )
33 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
34 return $this->mGoodLinks
[$title];
40 function isBadLink( $title )
42 return array_key_exists( $title, $this->mBadLinks
);
45 function addGoodLink( $id, $title )
47 if ( $this->mActive
) {
48 $this->mGoodLinks
[$title] = $id;
52 function addBadLink( $title )
54 if ( $this->mActive
&& ( ! $this->isBadLink( $title ) ) ) {
55 $this->mBadLinks
[$title] = 1;
59 function addImageLink( $title )
61 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
64 function addImageLinkObj( $nt )
66 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
69 function clearBadLink( $title )
71 unset( $this->mBadLinks
[$title] );
72 $this->clearLink( $title );
75 function clearLink( $title )
77 global $wgMemc, $wgLinkCacheMemcached;
78 if( $wgLinkCacheMemcached )
79 $wgMemc->delete( $this->getKey( $title ) );
82 function suspend() { $this->mActive
= false; }
83 function resume() { $this->mActive
= true; }
84 function getGoodLinks() { return $this->mGoodLinks
; }
85 function getBadLinks() { return array_keys( $this->mBadLinks
); }
86 function getImageLinks() { return $this->mImageLinks
; }
88 function addLink( $title )
90 $nt = Title
::newFromDBkey( $title );
92 return $this->addLinkObj( $nt );
98 function addLinkObj( &$nt )
100 $title = $nt->getPrefixedDBkey();
101 if ( $this->isBadLink( $title ) ) { return 0; }
102 $id = $this->getGoodLinkID( $title );
103 if ( 0 != $id ) { return $id; }
106 $fname = "LinkCache::addLinkObj";
107 wfProfileIn( $fname );
109 $ns = $nt->getNamespace();
110 $t = $nt->getDBkey();
112 if ( "" == $title ) {
113 wfProfileOut( $fname );
118 if( $wgLinkCacheMemcached )
119 $id = $wgMemc->get( $key = $this->getKey( $title ) );
120 if( $id === FALSE ) {
121 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
122 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
123 $res = wfQuery( $sql, DB_READ
, "LinkCache::addLink" );
125 if ( 0 == wfNumRows( $res ) ) {
128 $s = wfFetchObject( $res );
131 if( $wgLinkCacheMemcached )
132 $wgMemc->add( $key, $id, time()+
3600 );
135 if ( 0 == $id ) { $this->addBadLink( $title ); }
136 else { $this->addGoodLink( $id, $title ); }
137 wfProfileOut( $fname );
141 function preFill( &$fromtitle )
143 global $wgEnablePersistentLC;
145 $fname = "LinkCache::preFill";
146 wfProfileIn( $fname );
147 # Note -- $fromtitle is a Title *object*
148 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
150 if ( $wgEnablePersistentLC ) {
151 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
153 $row = wfFetchObject( $res );
155 $cacheobj = gzuncompress( $row->lcc_cacheobj
);
156 $cc = unserialize( $cacheobj );
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 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
196 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
198 wfDebug( "LinkCache::preFill - saved to linkscc\n" );
201 wfProfileOut( $fname );
204 function getGoodAdditions()
206 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
209 function getBadAdditions()
211 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
212 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
213 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
216 function getImageAdditions()
218 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
221 function getGoodDeletions()
223 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
226 function getBadDeletions()
228 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
231 function getImageDeletions()
233 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
236 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
237 # the incremental update arrays which will be filled. Returns whether or not it's
238 # worth doing the incremental version. For example, if [[List of mathematical topics]]
239 # was blanked, it would take a long, long time to do incrementally.
240 function incrementalSetup( $which, &$del, &$add )
242 if ( ! $this->mPreFilled
) {
248 $old =& $this->mOldGoodLinks
;
249 $cur =& $this->mGoodLinks
;
250 $del = $this->getGoodDeletions();
251 $add = $this->getGoodAdditions();
254 $old =& $this->mOldBadLinks
;
255 $cur =& $this->mBadLinks
;
256 $del = $this->getBadDeletions();
257 $add = $this->getBadAdditions();
259 default: # LINKCACHE_IMAGE
266 # Clears cache but leaves old preFill copies alone
269 $this->mGoodLinks
= array();
270 $this->mBadLinks
= array();
271 $this->mImageLinks
= array();