0c50c7d2f797eda9fb8a3b013479bffedbe16660
[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 = 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 $title = $nt->getPrefixedDBkey();
105 if ( $this->isBadLink( $title ) ) { return 0; }
106 $id = $this->getGoodLinkID( $title );
107 if ( 0 != $id ) { return $id; }
108
109 $fname = "LinkCache::addLinkObj";
110 wfProfileIn( $fname );
111
112 $ns = $nt->getNamespace();
113 $t = $nt->getDBkey();
114
115 if ( "" == $title ) {
116 wfProfileOut( $fname );
117 return 0;
118 }
119
120 $id = NULL;
121 if( $wgLinkCacheMemcached )
122 $id = $wgMemc->get( $key = $this->getKey( $title ) );
123 if( ! is_integer( $id ) ) {
124 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
125 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
126 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
127
128 if ( 0 == wfNumRows( $res ) ) {
129 $id = 0;
130 } else {
131 $s = wfFetchObject( $res );
132 $id = $s->cur_id;
133 }
134 if( $wgLinkCacheMemcached )
135 $wgMemc->add( $key, $id, 3600*24 );
136 }
137
138 if ( 0 == $id ) { $this->addBadLink( $title ); }
139 else { $this->addGoodLink( $id, $title ); }
140 wfProfileOut( $fname );
141 return $id;
142 }
143
144 function preFill( &$fromtitle )
145 {
146 global $wgEnablePersistentLC;
147
148 $fname = "LinkCache::preFill";
149 wfProfileIn( $fname );
150 # Note -- $fromtitle is a Title *object*
151
152 $this->suspend();
153 $id = $fromtitle->getArticleID();
154 $this->resume();
155
156 if( $id == 0 ) {
157 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
158 wfProfileOut( $fname );
159 return;
160 }
161
162 if ( $wgEnablePersistentLC ) {
163 if( $this->fillFromLinkscc( $id ) ){
164 return;
165 }
166 }
167
168 $sql = "SELECT cur_id,cur_namespace,cur_title
169 FROM cur,links
170 WHERE cur_id=l_to AND l_from=$id";
171 $res = wfQuery( $sql, DB_READ, $fname );
172 while( $s = wfFetchObject( $res ) ) {
173 $this->addGoodLink( $s->cur_id,
174 Title::makeName( $s->cur_namespace, $s->cur_title )
175 );
176 }
177
178 $sql = "SELECT bl_to
179 FROM brokenlinks
180 WHERE bl_from='{$id}'";
181 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
182 while( $s = wfFetchObject( $res ) ) {
183 $this->addBadLink( $s->bl_to );
184 }
185
186 $this->mOldBadLinks = $this->mBadLinks;
187 $this->mOldGoodLinks = $this->mGoodLinks;
188 $this->mPreFilled = true;
189
190 if ( $wgEnablePersistentLC ) {
191 $this->saveToLinkscc( $id );
192 }
193 wfProfileOut( $fname );
194 }
195
196 function getGoodAdditions()
197 {
198 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
199 }
200
201 function getBadAdditions()
202 {
203 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
204 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
205 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
206 }
207
208 function getImageAdditions()
209 {
210 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
211 }
212
213 function getGoodDeletions()
214 {
215 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
216 }
217
218 function getBadDeletions()
219 {
220 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
221 }
222
223 function getImageDeletions()
224 {
225 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
226 }
227
228 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
229 # the incremental update arrays which will be filled. Returns whether or not it's
230 # worth doing the incremental version. For example, if [[List of mathematical topics]]
231 # was blanked, it would take a long, long time to do incrementally.
232 function incrementalSetup( $which, &$del, &$add )
233 {
234 if ( ! $this->mPreFilled ) {
235 return false;
236 }
237
238 switch ( $which ) {
239 case LINKCACHE_GOOD:
240 $old =& $this->mOldGoodLinks;
241 $cur =& $this->mGoodLinks;
242 $del = $this->getGoodDeletions();
243 $add = $this->getGoodAdditions();
244 break;
245 case LINKCACHE_BAD:
246 $old =& $this->mOldBadLinks;
247 $cur =& $this->mBadLinks;
248 $del = $this->getBadDeletions();
249 $add = $this->getBadAdditions();
250 break;
251 default: # LINKCACHE_IMAGE
252 return false;
253 }
254
255 return true;
256 }
257
258 # Clears cache but leaves old preFill copies alone
259 function clear()
260 {
261 $this->mGoodLinks = array();
262 $this->mBadLinks = array();
263 $this->mImageLinks = array();
264 }
265
266 /* private */ function fillFromLinkscc( $id ){
267 $id = IntVal( $id );
268 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_pageid = $id",
269 DB_READ);
270 $row = wfFetchObject( $res );
271 if( $row == FALSE)
272 return false;
273
274 $cacheobj = false;
275 if( function_exists( "gzuncompress" ) )
276 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
277
278 if($cacheobj == FALSE){
279 $cacheobj = $row->lcc_cacheobj;
280 }
281 $cc = @unserialize( $cacheobj );
282 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
283 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
284 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
285 $this->mPreFilled = true;
286 return TRUE;
287 } else {
288 return FALSE;
289 }
290
291 }
292
293 /* private */ function saveToLinkscc( $pid ){
294 global $wgCompressedPersistentLC;
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_cacheobj) " .
301 "VALUES({$pid}, '{$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_pageid=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 ?>