compatibility modifications in preparation for the new memcached client
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?
2 # Cache for article titles and ids linked from one source
3
4 # These are used in incrementalSetup()
5 define ('LINKCACHE_GOOD', 0);
6 define ('LINKCACHE_BAD', 1);
7 define ('LINKCACHE_IMAGE', 2);
8
9 class LinkCache {
10 // Increment $mClassVer whenever old serialized versions of this class
11 // becomes incompatible with the new version.
12 /* private */ var $mClassVer = 1;
13
14 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
15 /* private */ var $mImageLinks;
16 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
17
18 /* private */ function getKey( $title ) {
19 global $wgDBname;
20 return "$wgDBname:lc:title:$title";
21 }
22
23 function LinkCache()
24 {
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();
32 }
33
34 function getGoodLinkID( $title )
35 {
36 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
37 return $this->mGoodLinks[$title];
38 } else {
39 return 0;
40 }
41 }
42
43 function isBadLink( $title )
44 {
45 return array_key_exists( $title, $this->mBadLinks );
46 }
47
48 function addGoodLink( $id, $title )
49 {
50 if ( $this->mActive ) {
51 $this->mGoodLinks[$title] = $id;
52 }
53 }
54
55 function addBadLink( $title )
56 {
57 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
58 $this->mBadLinks[$title] = 1;
59 }
60 }
61
62 function addImageLink( $title )
63 {
64 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
65 }
66
67 function addImageLinkObj( $nt )
68 {
69 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
70 }
71
72 function clearBadLink( $title )
73 {
74 unset( $this->mBadLinks[$title] );
75 $this->clearLink( $title );
76 }
77
78 function clearLink( $title )
79 {
80 global $wgMemc, $wgLinkCacheMemcached;
81 if( $wgLinkCacheMemcached )
82 $wgMemc->delete( $this->getKey( $title ) );
83 }
84
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; }
90
91 function addLink( $title )
92 {
93 $nt = Title::newFromDBkey( $title );
94 if( $nt ) {
95 return $this->addLinkObj( $nt );
96 } else {
97 return 0;
98 }
99 }
100
101 function addLinkObj( &$nt )
102 {
103 global $wgMemc, $wgLinkCacheMemcached;
104
105 $title = $nt->getPrefixedDBkey();
106 if ( $this->isBadLink( $title ) ) { return 0; }
107 $id = $this->getGoodLinkID( $title );
108 if ( 0 != $id ) { return $id; }
109
110 $fname = "LinkCache::addLinkObj";
111 wfProfileIn( $fname );
112
113 $ns = $nt->getNamespace();
114 $t = $nt->getDBkey();
115
116 if ( "" == $title ) {
117 wfProfileOut( $fname );
118 return 0;
119 }
120
121 $id = FALSE;
122 if( $wgLinkCacheMemcached )
123 $id = $wgMemc->get( $key = $this->getKey( $title ) );
124 if( ! $id ) {
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" );
128
129 if ( 0 == wfNumRows( $res ) ) {
130 $id = 0;
131 } else {
132 $s = wfFetchObject( $res );
133 $id = $s->cur_id;
134 }
135 if( $wgLinkCacheMemcached )
136 $wgMemc->add( $key, $id, time()+3600 );
137 }
138
139 if ( 0 == $id ) { $this->addBadLink( $title ); }
140 else { $this->addGoodLink( $id, $title ); }
141 wfProfileOut( $fname );
142 return $id;
143 }
144
145 function preFill( &$fromtitle )
146 {
147 global $wgEnablePersistentLC, $wgCompressedPersistentLC;
148
149 $fname = "LinkCache::preFill";
150 wfProfileIn( $fname );
151 # Note -- $fromtitle is a Title *object*
152 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
153
154 if ( $wgEnablePersistentLC ) {
155 if( $this->fillFromLinkscc( $dbkeyfrom ) ){
156 return;
157 }
158 }
159
160 $sql = "SELECT cur_id,cur_namespace,cur_title
161 FROM cur,links
162 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
163 $res = wfQuery( $sql, DB_READ, $fname );
164 while( $s = wfFetchObject( $res ) ) {
165 $this->addGoodLink( $s->cur_id,
166 Title::makeName( $s->cur_namespace, $s->cur_title )
167 );
168 }
169
170 $this->suspend();
171 $id = $fromtitle->getArticleID();
172 $this->resume();
173
174 if( $id == 0 ) {
175 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
176 wfProfileOut( $fname );
177 return;
178 }
179
180 $sql = "SELECT bl_to
181 FROM brokenlinks
182 WHERE bl_from='{$id}'";
183 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
184 while( $s = wfFetchObject( $res ) ) {
185 $this->addBadLink( $s->bl_to );
186 }
187
188 $this->mOldBadLinks = $this->mBadLinks;
189 $this->mOldGoodLinks = $this->mGoodLinks;
190 $this->mPreFilled = true;
191
192 if ( $wgEnablePersistentLC ) {
193 $this->saveToLinkscc( $id, $dbkeyfrom );
194 }
195 wfProfileOut( $fname );
196 }
197
198 function getGoodAdditions()
199 {
200 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
201 }
202
203 function getBadAdditions()
204 {
205 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
206 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
207 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
208 }
209
210 function getImageAdditions()
211 {
212 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
213 }
214
215 function getGoodDeletions()
216 {
217 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
218 }
219
220 function getBadDeletions()
221 {
222 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
223 }
224
225 function getImageDeletions()
226 {
227 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
228 }
229
230 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
231 # the incremental update arrays which will be filled. Returns whether or not it's
232 # worth doing the incremental version. For example, if [[List of mathematical topics]]
233 # was blanked, it would take a long, long time to do incrementally.
234 function incrementalSetup( $which, &$del, &$add )
235 {
236 if ( ! $this->mPreFilled ) {
237 return false;
238 }
239
240 switch ( $which ) {
241 case LINKCACHE_GOOD:
242 $old =& $this->mOldGoodLinks;
243 $cur =& $this->mGoodLinks;
244 $del = $this->getGoodDeletions();
245 $add = $this->getGoodAdditions();
246 break;
247 case LINKCACHE_BAD:
248 $old =& $this->mOldBadLinks;
249 $cur =& $this->mBadLinks;
250 $del = $this->getBadDeletions();
251 $add = $this->getBadAdditions();
252 break;
253 default: # LINKCACHE_IMAGE
254 return false;
255 }
256
257 return true;
258 }
259
260 # Clears cache but leaves old preFill copies alone
261 function clear()
262 {
263 $this->mGoodLinks = array();
264 $this->mBadLinks = array();
265 $this->mImageLinks = array();
266 }
267
268 /* private */ function fillFromLinkscc( $dbkeyfrom ){
269 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
270 DB_READ);
271 $row = wfFetchObject( $res );
272 if( $row == FALSE)
273 return false;
274
275 $cacheobj = false;
276 if( function_exists( "gzuncompress" ) )
277 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
278
279 if($cacheobj == FALSE){
280 $cacheobj = $row->lcc_cacheobj;
281 }
282 $cc = @unserialize( $cacheobj );
283 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
284 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
285 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
286 $this->mPreFilled = true;
287 return TRUE;
288 } else {
289 return FALSE;
290 }
291
292 }
293
294 /* private */ function saveToLinkscc( $pid, $dbkeyfrom ){
295 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
296 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
297 } else {
298 $ser = wfStrencode( serialize( $this ) );
299 }
300 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_title,lcc_cacheobj) " .
301 "VALUES({$pid}, '{$dbkeyfrom}', '{$ser}')", DB_WRITE);
302 }
303
304 # $pid is a page id
305 /* static */ function linksccClearLinksTo( $pid ){
306 $pid = intval( $pid );
307 wfQuery("DELETE linkscc FROM linkscc,links ".
308 "WHERE lcc_title=links.l_from AND l_to={$pid}", DB_WRITE);
309 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
310 }
311
312 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
313 /* static */ function linksccClearBrokenLinksTo( $title ){
314 $title = wfStrencode( $title );
315 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
316 "WHERE lcc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
317 }
318
319 # $pid is a page id
320 /* static */ function linksccClearPage( $pid ){
321 $pid = intval( $pid );
322 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
323 }
324 }
325 ?>