add Last-Modified header
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 # Cache for article titles (prefixed DB keys) 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 = 2;
13
14 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
15 /* private */ var $mImageLinks, $mCategoryLinks;
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->mCategoryLinks = array();
31 $this->mOldGoodLinks = array();
32 $this->mOldBadLinks = array();
33 }
34
35 function getGoodLinkID( $title )
36 {
37 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
38 return $this->mGoodLinks[$title];
39 } else {
40 return 0;
41 }
42 }
43
44 function isBadLink( $title )
45 {
46 return array_key_exists( $title, $this->mBadLinks );
47 }
48
49 function addGoodLink( $id, $title )
50 {
51 if ( $this->mActive ) {
52 $this->mGoodLinks[$title] = $id;
53 }
54 }
55
56 function addBadLink( $title )
57 {
58 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
59 $this->mBadLinks[$title] = 1;
60 }
61 }
62
63 function addImageLink( $title )
64 {
65 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
66 }
67
68 function addImageLinkObj( $nt )
69 {
70 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
71 }
72
73 function addCategoryLink( $title, $sortkey ) {
74 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
75 }
76
77 function addCategoryLinkObj( &$nt, $sortkey ) {
78 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
79 }
80
81 function clearBadLink( $title )
82 {
83 unset( $this->mBadLinks[$title] );
84 $this->clearLink( $title );
85 }
86
87 function clearLink( $title )
88 {
89 global $wgMemc, $wgLinkCacheMemcached;
90 if( $wgLinkCacheMemcached )
91 $wgMemc->delete( $this->getKey( $title ) );
92 }
93
94 function suspend() { $this->mActive = false; }
95 function resume() { $this->mActive = true; }
96 function getGoodLinks() { return $this->mGoodLinks; }
97 function getBadLinks() { return array_keys( $this->mBadLinks ); }
98 function getImageLinks() { return $this->mImageLinks; }
99 function getCategoryLinks() { return $this->mCategoryLinks; }
100
101 function addLink( $title )
102 {
103 $nt = Title::newFromDBkey( $title );
104 if( $nt ) {
105 return $this->addLinkObj( $nt );
106 } else {
107 return 0;
108 }
109 }
110
111 function addLinkObj( &$nt )
112 {
113 global $wgMemc, $wgLinkCacheMemcached;
114 $title = $nt->getPrefixedDBkey();
115 if ( $this->isBadLink( $title ) ) { return 0; }
116 $id = $this->getGoodLinkID( $title );
117 if ( 0 != $id ) { return $id; }
118
119 $fname = "LinkCache::addLinkObj";
120 wfProfileIn( $fname );
121
122 $ns = $nt->getNamespace();
123 $t = $nt->getDBkey();
124
125 if ( "" == $title ) {
126 wfProfileOut( $fname );
127 return 0;
128 }
129
130 $id = NULL;
131 if( $wgLinkCacheMemcached )
132 $id = $wgMemc->get( $key = $this->getKey( $title ) );
133 if( ! is_integer( $id ) ) {
134 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
135 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
136 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
137
138 if ( 0 == wfNumRows( $res ) ) {
139 $id = 0;
140 } else {
141 $s = wfFetchObject( $res );
142 $id = $s->cur_id;
143 }
144 if( $wgLinkCacheMemcached )
145 $wgMemc->add( $key, $id, 3600*24 );
146 }
147
148 if ( 0 == $id ) { $this->addBadLink( $title ); }
149 else { $this->addGoodLink( $id, $title ); }
150 wfProfileOut( $fname );
151 return $id;
152 }
153
154 function preFill( &$fromtitle )
155 {
156 global $wgEnablePersistentLC;
157
158 $fname = "LinkCache::preFill";
159 wfProfileIn( $fname );
160 # Note -- $fromtitle is a Title *object*
161
162 $this->suspend();
163 $id = $fromtitle->getArticleID();
164 $this->resume();
165
166 if( $id == 0 ) {
167 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
168 wfProfileOut( $fname );
169 return;
170 }
171
172 if ( $wgEnablePersistentLC ) {
173 if( $this->fillFromLinkscc( $id ) ){
174 wfProfileOut( $fname );
175 return;
176 }
177 }
178
179 $sql = "SELECT cur_id,cur_namespace,cur_title
180 FROM cur,links
181 WHERE cur_id=l_to AND l_from=$id";
182 $res = wfQuery( $sql, DB_READ, $fname );
183 while( $s = wfFetchObject( $res ) ) {
184 $this->addGoodLink( $s->cur_id,
185 Title::makeName( $s->cur_namespace, $s->cur_title )
186 );
187 }
188
189 $sql = "SELECT bl_to
190 FROM brokenlinks
191 WHERE bl_from='{$id}'";
192 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
193 while( $s = wfFetchObject( $res ) ) {
194 $this->addBadLink( $s->bl_to );
195 }
196
197 $this->mOldBadLinks = $this->mBadLinks;
198 $this->mOldGoodLinks = $this->mGoodLinks;
199 $this->mPreFilled = true;
200
201 if ( $wgEnablePersistentLC ) {
202 $this->saveToLinkscc( $id );
203 }
204 wfProfileOut( $fname );
205 }
206
207 function getGoodAdditions()
208 {
209 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
210 }
211
212 function getBadAdditions()
213 {
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 ) ) );
217 }
218
219 function getImageAdditions()
220 {
221 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
222 }
223
224 function getGoodDeletions()
225 {
226 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
227 }
228
229 function getBadDeletions()
230 {
231 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
232 }
233
234 function getImageDeletions()
235 {
236 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
237 }
238
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 )
244 {
245 if ( ! $this->mPreFilled ) {
246 return false;
247 }
248
249 switch ( $which ) {
250 case LINKCACHE_GOOD:
251 $old =& $this->mOldGoodLinks;
252 $cur =& $this->mGoodLinks;
253 $del = $this->getGoodDeletions();
254 $add = $this->getGoodAdditions();
255 break;
256 case LINKCACHE_BAD:
257 $old =& $this->mOldBadLinks;
258 $cur =& $this->mBadLinks;
259 $del = $this->getBadDeletions();
260 $add = $this->getBadAdditions();
261 break;
262 default: # LINKCACHE_IMAGE
263 return false;
264 }
265
266 return true;
267 }
268
269 # Clears cache but leaves old preFill copies alone
270 function clear()
271 {
272 $this->mGoodLinks = array();
273 $this->mBadLinks = array();
274 $this->mImageLinks = array();
275 }
276
277 /* private */ function fillFromLinkscc( $id ){
278 $id = IntVal( $id );
279 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_pageid = $id",
280 DB_READ);
281 $row = wfFetchObject( $res );
282 if( $row == FALSE)
283 return false;
284
285 $cacheobj = false;
286 if( function_exists( "gzuncompress" ) )
287 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
288
289 if($cacheobj == FALSE){
290 $cacheobj = $row->lcc_cacheobj;
291 }
292 $cc = @unserialize( $cacheobj );
293 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
294 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
295 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
296 $this->mPreFilled = true;
297 return TRUE;
298 } else {
299 return FALSE;
300 }
301
302 }
303
304 /* private */ function saveToLinkscc( $pid ){
305 global $wgCompressedPersistentLC, $wgIsMySQL;
306 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
307 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
308 } else {
309 $ser = wfStrencode( serialize( $this ) );
310 }
311 if ($wgIsMySQL) {
312 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_cacheobj) " .
313 "VALUES({$pid}, '{$ser}')", DB_WRITE);
314 } else {
315 wfQuery("DELETE FROM linkscc WHERE lcc_pageid={$pid}",DB_WRITE);
316 wfQuery("INSERT INTO linkscc(lcc_pageid,lcc_cacheobj) " .
317 "VALUES({$pid}, '{$ser}')", DB_WRITE);
318 }
319 }
320
321 # $pid is a page id
322 /* static */ function linksccClearLinksTo( $pid ){
323 global $wgEnablePersistentLC, $wgIsMySQL;
324 if ( $wgEnablePersistentLC ) {
325 $pid = intval( $pid );
326 if ($wgIsMySQL) {
327 wfQuery("DELETE linkscc FROM linkscc,links ".
328 "WHERE lcc_pageid=links.l_from AND l_to={$pid}", DB_WRITE);
329 } else {
330 wfQuery("DELETE FROM linkscc WHERE lcc_pageid IN ".
331 "(SELECT l_from FROM links WHERE l_to={$pid})", DB_WRITE);
332 }
333 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
334 }
335 }
336
337 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
338 /* static */ function linksccClearBrokenLinksTo( $title ){
339 global $wgEnablePersistentLC,$wgIsMySQL;
340 if ( $wgEnablePersistentLC ) {
341 $title = wfStrencode( $title );
342 if ($wgIsMySQL) {
343 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
344 "WHERE lcc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
345 } else {
346 wfQuery("DELETE FROM linkscc WHERE lcc_pageid IN ".
347 "(SELECT bl_from FROM brokenlinks ".
348 "WHERE bl_to='{$title}')",DB_WRITE);
349 }
350 }
351 }
352
353 # $pid is a page id
354 /* static */ function linksccClearPage( $pid ){
355 global $wgEnablePersistentLC;
356 if ( $wgEnablePersistentLC ) {
357 $pid = intval( $pid );
358 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
359 }
360 }
361 }
362 ?>