Add categorylinks table to separately list category relationships. Actual
[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 return;
175 }
176 }
177
178 $sql = "SELECT cur_id,cur_namespace,cur_title
179 FROM cur,links
180 WHERE cur_id=l_to AND l_from=$id";
181 $res = wfQuery( $sql, DB_READ, $fname );
182 while( $s = wfFetchObject( $res ) ) {
183 $this->addGoodLink( $s->cur_id,
184 Title::makeName( $s->cur_namespace, $s->cur_title )
185 );
186 }
187
188 $sql = "SELECT bl_to
189 FROM brokenlinks
190 WHERE bl_from='{$id}'";
191 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
192 while( $s = wfFetchObject( $res ) ) {
193 $this->addBadLink( $s->bl_to );
194 }
195
196 $this->mOldBadLinks = $this->mBadLinks;
197 $this->mOldGoodLinks = $this->mGoodLinks;
198 $this->mPreFilled = true;
199
200 if ( $wgEnablePersistentLC ) {
201 $this->saveToLinkscc( $id );
202 }
203 wfProfileOut( $fname );
204 }
205
206 function getGoodAdditions()
207 {
208 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
209 }
210
211 function getBadAdditions()
212 {
213 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
214 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
215 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
216 }
217
218 function getImageAdditions()
219 {
220 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
221 }
222
223 function getGoodDeletions()
224 {
225 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
226 }
227
228 function getBadDeletions()
229 {
230 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
231 }
232
233 function getImageDeletions()
234 {
235 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
236 }
237
238 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
239 # the incremental update arrays which will be filled. Returns whether or not it's
240 # worth doing the incremental version. For example, if [[List of mathematical topics]]
241 # was blanked, it would take a long, long time to do incrementally.
242 function incrementalSetup( $which, &$del, &$add )
243 {
244 if ( ! $this->mPreFilled ) {
245 return false;
246 }
247
248 switch ( $which ) {
249 case LINKCACHE_GOOD:
250 $old =& $this->mOldGoodLinks;
251 $cur =& $this->mGoodLinks;
252 $del = $this->getGoodDeletions();
253 $add = $this->getGoodAdditions();
254 break;
255 case LINKCACHE_BAD:
256 $old =& $this->mOldBadLinks;
257 $cur =& $this->mBadLinks;
258 $del = $this->getBadDeletions();
259 $add = $this->getBadAdditions();
260 break;
261 default: # LINKCACHE_IMAGE
262 return false;
263 }
264
265 return true;
266 }
267
268 # Clears cache but leaves old preFill copies alone
269 function clear()
270 {
271 $this->mGoodLinks = array();
272 $this->mBadLinks = array();
273 $this->mImageLinks = array();
274 }
275
276 /* private */ function fillFromLinkscc( $id ){
277 $id = IntVal( $id );
278 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_pageid = $id",
279 DB_READ);
280 $row = wfFetchObject( $res );
281 if( $row == FALSE)
282 return false;
283
284 $cacheobj = false;
285 if( function_exists( "gzuncompress" ) )
286 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
287
288 if($cacheobj == FALSE){
289 $cacheobj = $row->lcc_cacheobj;
290 }
291 $cc = @unserialize( $cacheobj );
292 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
293 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
294 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
295 $this->mPreFilled = true;
296 return TRUE;
297 } else {
298 return FALSE;
299 }
300
301 }
302
303 /* private */ function saveToLinkscc( $pid ){
304 global $wgCompressedPersistentLC;
305 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
306 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
307 } else {
308 $ser = wfStrencode( serialize( $this ) );
309 }
310 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_cacheobj) " .
311 "VALUES({$pid}, '{$ser}')", DB_WRITE);
312 }
313
314 # $pid is a page id
315 /* static */ function linksccClearLinksTo( $pid ){
316 $pid = intval( $pid );
317 wfQuery("DELETE linkscc FROM linkscc,links ".
318 "WHERE lcc_pageid=links.l_from AND l_to={$pid}", DB_WRITE);
319 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
320 }
321
322 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
323 /* static */ function linksccClearBrokenLinksTo( $title ){
324 $title = wfStrencode( $title );
325 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
326 "WHERE lcc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
327 }
328
329 # $pid is a page id
330 /* static */ function linksccClearPage( $pid ){
331 $pid = intval( $pid );
332 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
333 }
334 }
335 ?>