Nov. branch merge. Various features backported from stable, various bug fixes.
[mediawiki.git] / includes / LinkCache.php
blob212b214aefb29da7a53ed8d289ae8faaa7ffb8a9
1 <?
2 # Cache for article titles and ids linked from one source
4 # These are used in incrementalSetup()
5 define ('LINKCACHE_GOOD', 0);
6 define ('LINKCACHE_BAD', 1);
7 define ('LINKCACHE_IMAGE', 2);
9 class LinkCache {
11 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
12 /* private */ var $mImageLinks;
13 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
15 /* private */ function getKey( $title ) {
16 global $wgDBname;
17 return "$wgDBname:lc:title:$title";
20 function LinkCache()
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();
31 function getGoodLinkID( $title )
33 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
34 return $this->mGoodLinks[$title];
35 } else {
36 return 0;
40 function isBadLink( $title )
42 return in_array( $title, $this->mBadLinks );
45 function addGoodLink( $id, $title )
47 if ( $this->mActive ) {
48 $this->mGoodLinks[$title] = $id;
52 function addBadLink( $title )
54 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
55 array_push( $this->mBadLinks, $title );
59 function addImageLink( $title )
61 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
64 function addImageLinkObj( $nt )
66 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
69 function clearBadLink( $title )
71 $index = array_search( $title, $this->mBadLinks );
72 if ( isset( $index ) ) {
73 unset( $this->mBadLinks[$index] );
75 $this->clearLink( $title );
78 function clearLink( $title ) {
79 global $wgMemc;
80 $wgMemc->delete( $this->getKey( $title ) );
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; }
89 function addLink( $title )
91 $nt = Title::newFromDBkey( $title );
92 if( $nt ) {
93 return $this->addLinkObj( $nt );
94 } else {
95 return 0;
99 function addLinkObj( &$nt )
101 $title = $nt->getPrefixedDBkey();
102 if ( $this->isBadLink( $title ) ) { return 0; }
103 $id = $this->getGoodLinkID( $title );
104 if ( 0 != $id ) { return $id; }
106 global $wgMemc;
107 $fname = "LinkCache::addLinkObj";
108 wfProfileIn( $fname );
110 $ns = $nt->getNamespace();
111 $t = $nt->getDBkey();
113 if ( "" == $title ) {
114 wfProfileOut( $fname );
115 return 0;
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" );
124 if ( 0 == wfNumRows( $res ) ) {
125 $id = 0;
126 } else {
127 $s = wfFetchObject( $res );
128 $id = $s->cur_id;
130 $wgMemc->add( $key, $id, time()+3600 );
132 if ( 0 == $id ) { $this->addBadLink( $title ); }
133 else { $this->addGoodLink( $id, $title ); }
134 wfProfileOut( $fname );
135 return $id;
138 function preFill( &$fromtitle )
140 global $wgEnablePersistentLC;
142 $fname = "LinkCache::preFill";
143 wfProfileIn( $fname );
144 # Note -- $fromtitle is a Title *object*
145 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
147 if ( $wgEnablePersistentLC ) {
148 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
149 DB_READ);
150 $row = wfFetchObject( $res );
151 if( $row != FALSE){
152 $cacheobj = gzuncompress( $row->lcc_cacheobj );
153 $cc = unserialize( $cacheobj );
154 $this->mGoodLinks = $cc->mGoodLinks;
155 $this->mBadLinks = $cc->mBadLinks;
156 $this->mPreFilled = true;
157 wfProfileOut( $fname );
158 return;
163 $sql = "SELECT cur_id,cur_namespace,cur_title
164 FROM cur,links
165 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
166 $res = wfQuery( $sql, DB_READ, $fname );
167 while( $s = wfFetchObject( $res ) ) {
168 $this->addGoodLink( $s->cur_id,
169 Title::makeName( $s->cur_namespace, $s->cur_title )
173 $this->suspend();
174 $id = $fromtitle->getArticleID();
175 $this->resume();
177 $sql = "SELECT bl_to
178 FROM brokenlinks
179 WHERE bl_from='{$id}'";
180 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
181 while( $s = wfFetchObject( $res ) ) {
182 $this->addBadLink( $s->bl_to );
185 $this->mOldBadLinks = $this->mBadLinks;
186 $this->mOldGoodLinks = $this->mGoodLinks;
187 $this->mPreFilled = true;
189 if ( $wgEnablePersistentLC ) {
190 // put fetched link data into cache
191 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
192 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
193 DB_WRITE);
196 wfProfileOut( $fname );
199 function getGoodAdditions()
201 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
204 function getBadAdditions()
206 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
209 function getImageAdditions()
211 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
214 function getGoodDeletions()
216 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
219 function getBadDeletions()
221 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
224 function getImageDeletions()
226 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
229 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
230 # the incremental update arrays which will be filled. Returns whether or not it's
231 # worth doing the incremental version. For example, if [[List of mathematical topics]]
232 # was blanked, it would take a long, long time to do incrementally.
233 function incrementalSetup( $which, &$del, &$add )
235 if ( ! $this->mPreFilled ) {
236 return false;
239 switch ( $which ) {
240 case LINKCACHE_GOOD:
241 $old =& $this->mOldGoodLinks;
242 $cur =& $this->mGoodLinks;
243 $del = $this->getGoodDeletions();
244 $add = $this->getGoodAdditions();
245 break;
246 case LINKCACHE_BAD:
247 $old =& $this->mOldBadLinks;
248 $cur =& $this->mBadLinks;
249 $del = $this->getBadDeletions();
250 $add = $this->getBadAdditions();
251 break;
252 default: # LINKCACHE_IMAGE
253 return false;
256 return true;
259 # Clears cache but leaves old preFill copies alone
260 function clear()
262 $this->mGoodLinks = array();
263 $this->mBadLinks = array();
264 $this->mImageLinks = array();