23f5182c025655fe3d1705faa47e01c7ad5a77df
[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 in_array( $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 array_push( $this->mBadLinks, $title );
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 $index = array_search( $title, $this->mBadLinks );
72 if ( isset( $index ) ) {
73 unset( $this->mBadLinks[$index] );
74 }
75 $this->clearLink( $title );
76 }
77
78 function clearLink( $title ) {
79 global $wgMemc;
80 $wgMemc->delete( $this->getKey( $title ) );
81 }
82
83 function suspend() { $this->mActive = false; }
84 function resume() { $this->mActive = true; }
85 function getGoodLinks() { return $this->mGoodLinks; }
86 function getBadLinks() { return $this->mBadLinks; }
87 function getImageLinks() { return $this->mImageLinks; }
88
89 function addLink( $title )
90 {
91 $nt = Title::newFromDBkey( $title );
92 if( $nt ) {
93 return $this->addLinkObj( $nt );
94 } else {
95 return 0;
96 }
97 }
98
99 function addLinkObj( &$nt )
100 {
101 $title = $nt->getPrefixedDBkey();
102 if ( $this->isBadLink( $title ) ) { return 0; }
103 $id = $this->getGoodLinkID( $title );
104 if ( 0 != $id ) { return $id; }
105
106 global $wgMemc;
107 $fname = "LinkCache::addLinkObj";
108 wfProfileIn( $fname );
109
110 $ns = $nt->getNamespace();
111 $t = $nt->getDBkey();
112
113 if ( "" == $title ) {
114 wfProfileOut( $fname );
115 return 0;
116 }
117
118 $id = $wgMemc->get( $key = $this->getKey( $title ) );
119 if( $id === FALSE ) {
120 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
121 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
122 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
123
124 if ( 0 == wfNumRows( $res ) ) {
125 $id = 0;
126 } else {
127 $s = wfFetchObject( $res );
128 $id = $s->cur_id;
129 }
130 $wgMemc->add( $key, $id, time()+3600 );
131 }
132 if ( 0 == $id ) { $this->addBadLink( $title ); }
133 else { $this->addGoodLink( $id, $title ); }
134 wfProfileOut( $fname );
135 return $id;
136 }
137
138 function preFill( &$fromtitle )
139 {
140 $fname = "LinkCache::preFill";
141 wfProfileIn( $fname );
142 # Note -- $fromtitle is a Title *object*
143 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
144
145
146 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
147 DB_READ);
148 $row = wfFetchObject( $res );
149 if( $row != FALSE){
150 $cacheobj = gzuncompress( $row->lcc_cacheobj );
151 $cc = unserialize( $cacheobj );
152 $this->mGoodLinks = $cc->mGoodLinks;
153 $this->mBadLinks = $cc->mBadLinks;
154 $this->mPreFilled = true;
155 wfProfileOut( $fname );
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 $sql = "SELECT bl_to
175 FROM brokenlinks
176 WHERE bl_from='{$id}'";
177 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
178 while( $s = wfFetchObject( $res ) ) {
179 $this->addBadLink( $s->bl_to );
180 }
181
182 $this->mOldBadLinks = $this->mBadLinks;
183 $this->mOldGoodLinks = $this->mGoodLinks;
184 $this->mPreFilled = true;
185
186 // put fetched link data into cache
187 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
188 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
189 DB_WRITE);
190
191 wfProfileOut( $fname );
192 }
193
194 function getGoodAdditions()
195 {
196 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
197 }
198
199 function getBadAdditions()
200 {
201 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
202 }
203
204 function getImageAdditions()
205 {
206 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
207 }
208
209 function getGoodDeletions()
210 {
211 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
212 }
213
214 function getBadDeletions()
215 {
216 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
217 }
218
219 function getImageDeletions()
220 {
221 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
222 }
223
224 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
225 # the incremental update arrays which will be filled. Returns whether or not it's
226 # worth doing the incremental version. For example, if [[List of mathematical topics]]
227 # was blanked, it would take a long, long time to do incrementally.
228 function incrementalSetup( $which, &$del, &$add )
229 {
230 if ( ! $this->mPreFilled ) {
231 return false;
232 }
233
234 switch ( $which ) {
235 case LINKCACHE_GOOD:
236 $old =& $this->mOldGoodLinks;
237 $cur =& $this->mGoodLinks;
238 $del = $this->getGoodDeletions();
239 $add = $this->getGoodAdditions();
240 break;
241 case LINKCACHE_BAD:
242 $old =& $this->mOldBadLinks;
243 $cur =& $this->mBadLinks;
244 $del = $this->getBadDeletions();
245 $add = $this->getBadAdditions();
246 break;
247 default: # LINKCACHE_IMAGE
248 return false;
249 }
250
251 return true;
252 }
253
254 # Clears cache but leaves old preFill copies alone
255 function clear()
256 {
257 $this->mGoodLinks = array();
258 $this->mBadLinks = array();
259 $this->mImageLinks = array();
260 }
261
262 }
263 ?>