9b0f07757ed67ff43a93c027fb5a56266121de19
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 in_array( $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 array_push( $this->mBadLinks
, $title );
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 $index = array_search( $title, $this->mBadLinks
);
72 if ( isset( $index ) ) {
73 unset( $this->mBadLinks
[$index] );
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 $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 ) );
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 );
138 if ( 0 == $id ) { $this->addBadLink( $title ); }
139 else { $this->addGoodLink( $id, $title ); }
140 wfProfileOut( $fname );
144 function preFill( &$fromtitle )
146 global $wgEnablePersistentLC;
148 $fname = "LinkCache::preFill";
149 wfProfileIn( $fname );
150 # Note -- $fromtitle is a Title *object*
151 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
153 if ( $wgEnablePersistentLC ) {
154 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
156 $row = wfFetchObject( $res );
158 $cacheobj = gzuncompress( $row->lcc_cacheobj
);
159 $cc = unserialize( $cacheobj );
160 $this->mGoodLinks
= $cc->mGoodLinks
;
161 $this->mBadLinks
= $cc->mBadLinks
;
162 $this->mPreFilled
= true;
163 wfProfileOut( $fname );
169 $sql = "SELECT cur_id,cur_namespace,cur_title
171 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
172 $res = wfQuery( $sql, DB_READ
, $fname );
173 while( $s = wfFetchObject( $res ) ) {
174 $this->addGoodLink( $s->cur_id
,
175 Title
::makeName( $s->cur_namespace
, $s->cur_title
)
180 $id = $fromtitle->getArticleID();
185 WHERE bl_from='{$id}'";
186 $res = wfQuery( $sql, DB_READ
, "LinkCache::preFill" );
187 while( $s = wfFetchObject( $res ) ) {
188 $this->addBadLink( $s->bl_to
);
191 $this->mOldBadLinks
= $this->mBadLinks
;
192 $this->mOldGoodLinks
= $this->mGoodLinks
;
193 $this->mPreFilled
= true;
195 if ( $wgEnablePersistentLC ) {
196 // put fetched link data into cache
197 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
198 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
202 wfProfileOut( $fname );
205 function getGoodAdditions()
207 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
210 function getBadAdditions()
212 return array_values( array_diff( $this->mBadLinks
, $this->mOldBadLinks
) );
215 function getImageAdditions()
217 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
220 function getGoodDeletions()
222 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
225 function getBadDeletions()
227 return array_values( array_diff( $this->mOldBadLinks
, $this->mBadLinks
) );
230 function getImageDeletions()
232 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
235 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
236 # the incremental update arrays which will be filled. Returns whether or not it's
237 # worth doing the incremental version. For example, if [[List of mathematical topics]]
238 # was blanked, it would take a long, long time to do incrementally.
239 function incrementalSetup( $which, &$del, &$add )
241 if ( ! $this->mPreFilled
) {
247 $old =& $this->mOldGoodLinks
;
248 $cur =& $this->mGoodLinks
;
249 $del = $this->getGoodDeletions();
250 $add = $this->getGoodAdditions();
253 $old =& $this->mOldBadLinks
;
254 $cur =& $this->mBadLinks
;
255 $del = $this->getBadDeletions();
256 $add = $this->getBadAdditions();
258 default: # LINKCACHE_IMAGE
265 # Clears cache but leaves old preFill copies alone
268 $this->mGoodLinks
= array();
269 $this->mBadLinks
= array();
270 $this->mImageLinks
= array();