Significant speed improvements by removing linear scans in arrays.\nEspecially effect...
[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
11 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
12 /* private */ var $mImageLinks;
13 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
14
15 /* private */ function getKey( $title ) {
16 global $wgDBname;
17 return "$wgDBname:lc:title:$title";
18 }
19
20 function LinkCache()
21 {
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();
29 }
30
31 function getGoodLinkID( $title )
32 {
33 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
34 return $this->mGoodLinks[$title];
35 } else {
36 return 0;
37 }
38 }
39
40 function isBadLink( $title )
41 {
42 return array_key_exists( $title, $this->mBadLinks );
43 }
44
45 function addGoodLink( $id, $title )
46 {
47 if ( $this->mActive ) {
48 $this->mGoodLinks[$title] = $id;
49 }
50 }
51
52 function addBadLink( $title )
53 {
54 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
55 $this->mBadLinks[$title] = 1;
56 }
57 }
58
59 function addImageLink( $title )
60 {
61 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
62 }
63
64 function addImageLinkObj( $nt )
65 {
66 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
67 }
68
69 function clearBadLink( $title )
70 {
71 unset( $this->mBadLinks[$title] );
72 $this->clearLink( $title );
73 }
74
75 function clearLink( $title )
76 {
77 global $wgMemc, $wgLinkCacheMemcached;
78 if( $wgLinkCacheMemcached )
79 $wgMemc->delete( $this->getKey( $title ) );
80 }
81
82 function suspend() { $this->mActive = false; }
83 function resume() { $this->mActive = true; }
84 function getGoodLinks() { return $this->mGoodLinks; }
85 function getBadLinks() { return array_keys( $this->mBadLinks ); }
86 function getImageLinks() { return $this->mImageLinks; }
87
88 function addLink( $title )
89 {
90 $nt = Title::newFromDBkey( $title );
91 if( $nt ) {
92 return $this->addLinkObj( $nt );
93 } else {
94 return 0;
95 }
96 }
97
98 function addLinkObj( &$nt )
99 {
100 $title = $nt->getPrefixedDBkey();
101 if ( $this->isBadLink( $title ) ) { return 0; }
102 $id = $this->getGoodLinkID( $title );
103 if ( 0 != $id ) { return $id; }
104
105 global $wgMemc;
106 $fname = "LinkCache::addLinkObj";
107 wfProfileIn( $fname );
108
109 $ns = $nt->getNamespace();
110 $t = $nt->getDBkey();
111
112 if ( "" == $title ) {
113 wfProfileOut( $fname );
114 return 0;
115 }
116
117 $id = FALSE;
118 if( $wgLinkCacheMemcached )
119 $id = $wgMemc->get( $key = $this->getKey( $title ) );
120 if( $id === FALSE ) {
121 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
122 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
123 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
124
125 if ( 0 == wfNumRows( $res ) ) {
126 $id = 0;
127 } else {
128 $s = wfFetchObject( $res );
129 $id = $s->cur_id;
130 }
131 if( $wgLinkCacheMemcached )
132 $wgMemc->add( $key, $id, time()+3600 );
133 }
134
135 if ( 0 == $id ) { $this->addBadLink( $title ); }
136 else { $this->addGoodLink( $id, $title ); }
137 wfProfileOut( $fname );
138 return $id;
139 }
140
141 function preFill( &$fromtitle )
142 {
143 global $wgEnablePersistentLC;
144
145 $fname = "LinkCache::preFill";
146 wfProfileIn( $fname );
147 # Note -- $fromtitle is a Title *object*
148 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
149
150 if ( $wgEnablePersistentLC ) {
151 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
152 DB_READ);
153 $row = wfFetchObject( $res );
154 if( $row != FALSE){
155 $cacheobj = gzuncompress( $row->lcc_cacheobj );
156 $cc = unserialize( $cacheobj );
157 $this->mGoodLinks = $cc->mGoodLinks;
158 $this->mBadLinks = $cc->mBadLinks;
159 $this->mPreFilled = true;
160 wfProfileOut( $fname );
161 return;
162 }
163 }
164
165
166 $sql = "SELECT cur_id,cur_namespace,cur_title
167 FROM cur,links
168 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
169 $res = wfQuery( $sql, DB_READ, $fname );
170 while( $s = wfFetchObject( $res ) ) {
171 $this->addGoodLink( $s->cur_id,
172 Title::makeName( $s->cur_namespace, $s->cur_title )
173 );
174 }
175
176 $this->suspend();
177 $id = $fromtitle->getArticleID();
178 $this->resume();
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 // put fetched link data into cache
194 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
195 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
196 DB_WRITE);
197 }
198
199 wfProfileOut( $fname );
200 }
201
202 function getGoodAdditions()
203 {
204 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
205 }
206
207 function getBadAdditions()
208 {
209 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
210 }
211
212 function getImageAdditions()
213 {
214 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
215 }
216
217 function getGoodDeletions()
218 {
219 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
220 }
221
222 function getBadDeletions()
223 {
224 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
225 }
226
227 function getImageDeletions()
228 {
229 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
230 }
231
232 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
233 # the incremental update arrays which will be filled. Returns whether or not it's
234 # worth doing the incremental version. For example, if [[List of mathematical topics]]
235 # was blanked, it would take a long, long time to do incrementally.
236 function incrementalSetup( $which, &$del, &$add )
237 {
238 if ( ! $this->mPreFilled ) {
239 return false;
240 }
241
242 switch ( $which ) {
243 case LINKCACHE_GOOD:
244 $old =& $this->mOldGoodLinks;
245 $cur =& $this->mGoodLinks;
246 $del = $this->getGoodDeletions();
247 $add = $this->getGoodAdditions();
248 break;
249 case LINKCACHE_BAD:
250 $old =& $this->mOldBadLinks;
251 $cur =& $this->mBadLinks;
252 $del = $this->getBadDeletions();
253 $add = $this->getBadAdditions();
254 break;
255 default: # LINKCACHE_IMAGE
256 return false;
257 }
258
259 return true;
260 }
261
262 # Clears cache but leaves old preFill copies alone
263 function clear()
264 {
265 $this->mGoodLinks = array();
266 $this->mBadLinks = array();
267 $this->mImageLinks = array();
268 }
269
270 }
271 ?>