23f5182c025655fe3d1705faa47e01c7ad5a77df
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);
11 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
12 /* private */ var $mImageLinks;
13 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
15 /* private */ function getKey( $title ) {
17 return "$wgDBname:lc:title:$title";
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];
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 ) {
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 );
93 return $this->addLinkObj( $nt );
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; }
107 $fname = "LinkCache::addLinkObj";
108 wfProfileIn( $fname );
110 $ns = $nt->getNamespace();
111 $t = $nt->getDBkey();
113 if ( "" == $title ) {
114 wfProfileOut( $fname );
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 ) ) {
127 $s = wfFetchObject( $res );
130 $wgMemc->add( $key, $id, time()+
3600 );
132 if ( 0 == $id ) { $this->addBadLink( $title ); }
133 else { $this->addGoodLink( $id, $title ); }
134 wfProfileOut( $fname );
138 function preFill( &$fromtitle )
140 $fname = "LinkCache::preFill";
141 wfProfileIn( $fname );
142 # Note -- $fromtitle is a Title *object*
143 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
146 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
148 $row = wfFetchObject( $res );
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 );
160 $sql = "SELECT cur_id,cur_namespace,cur_title
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
)
171 $id = $fromtitle->getArticleID();
176 WHERE bl_from='{$id}'";
177 $res = wfQuery( $sql, DB_READ
, "LinkCache::preFill" );
178 while( $s = wfFetchObject( $res ) ) {
179 $this->addBadLink( $s->bl_to
);
182 $this->mOldBadLinks
= $this->mBadLinks
;
183 $this->mOldGoodLinks
= $this->mGoodLinks
;
184 $this->mPreFilled
= true;
186 // put fetched link data into cache
187 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
188 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
191 wfProfileOut( $fname );
194 function getGoodAdditions()
196 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
199 function getBadAdditions()
201 return array_values( array_diff( $this->mBadLinks
, $this->mOldBadLinks
) );
204 function getImageAdditions()
206 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
209 function getGoodDeletions()
211 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
214 function getBadDeletions()
216 return array_values( array_diff( $this->mOldBadLinks
, $this->mBadLinks
) );
219 function getImageDeletions()
221 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
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 )
230 if ( ! $this->mPreFilled
) {
236 $old =& $this->mOldGoodLinks
;
237 $cur =& $this->mGoodLinks
;
238 $del = $this->getGoodDeletions();
239 $add = $this->getGoodAdditions();
242 $old =& $this->mOldBadLinks
;
243 $cur =& $this->mBadLinks
;
244 $del = $this->getBadDeletions();
245 $add = $this->getBadAdditions();
247 default: # LINKCACHE_IMAGE
254 # Clears cache but leaves old preFill copies alone
257 $this->mGoodLinks
= array();
258 $this->mBadLinks
= array();
259 $this->mImageLinks
= array();