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->mGoodLinks
= $cc->mGoodLinks
;
158 $this->mBadLinks
= $cc->mBadLinks
;
159 $this->mPreFilled
= true;
160 wfProfileOut( $fname );
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 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
195 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
199 wfProfileOut( $fname );
202 function getGoodAdditions()
204 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
207 function getBadAdditions()
209 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
212 function getImageAdditions()
214 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
217 function getGoodDeletions()
219 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
222 function getBadDeletions()
224 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
227 function getImageDeletions()
229 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
232 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
233 # the incremental update arrays which will be filled. Returns whether or not it's
234 # worth doing the incremental version. For example, if [[List of mathematical topics]]
235 # was blanked, it would take a long, long time to do incrementally.
236 function incrementalSetup( $which, &$del, &$add )
238 if ( ! $this->mPreFilled
) {
244 $old =& $this->mOldGoodLinks
;
245 $cur =& $this->mGoodLinks
;
246 $del = $this->getGoodDeletions();
247 $add = $this->getGoodAdditions();
250 $old =& $this->mOldBadLinks
;
251 $cur =& $this->mBadLinks
;
252 $del = $this->getBadDeletions();
253 $add = $this->getBadAdditions();
255 default: # LINKCACHE_IMAGE
262 # Clears cache but leaves old preFill copies alone
265 $this->mGoodLinks
= array();
266 $this->mBadLinks
= array();
267 $this->mImageLinks
= array();