Minor link update fix
[mediawiki.git] / includes / LinkCache.php
blobd442920352b6b09aaa0d8979164e001cfcbe7263
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 function LinkCache()
17 $this->mActive = true;
18 $this->mPreFilled = false;
19 $this->mGoodLinks = array();
20 $this->mBadLinks = array();
21 $this->mImageLinks = array();
22 $this->mOldGoodLinks = array();
23 $this->mOldBadLinks = array();
26 function getGoodLinkID( $title )
28 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
29 return $this->mGoodLinks[$title];
30 } else {
31 return 0;
35 function isBadLink( $title )
37 return in_array( $title, $this->mBadLinks );
40 function addGoodLink( $id, $title )
42 if ( $this->mActive ) {
43 $this->mGoodLinks[$title] = $id;
47 function addBadLink( $title )
49 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
50 array_push( $this->mBadLinks, $title );
54 function addImageLink( $title )
56 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
59 function clearBadLink( $title )
61 $index = array_search( $title, $this->mBadLinks );
62 if ( isset( $index ) ) {
63 unset( $this->mBadLinks[$index] );
67 function suspend() { $this->mActive = false; }
68 function resume() { $this->mActive = true; }
69 function getGoodLinks() { return $this->mGoodLinks; }
70 function getBadLinks() { return $this->mBadLinks; }
71 function getImageLinks() { return $this->mImageLinks; }
73 function addLink( $title )
75 if ( $this->isBadLink( $title ) ) { return 0; }
76 $id = $this->getGoodLinkID( $title );
77 if ( 0 != $id ) { return $id; }
79 wfProfileIn( "LinkCache::addLink-checkdatabase" );
81 $nt = Title::newFromDBkey( $title );
82 $ns = $nt->getNamespace();
83 $t = $nt->getDBkey();
85 if ( "" == $t ) { return 0; }
86 $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" .
87 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
88 $res = wfQuery( $sql, "LinkCache::addLink" );
90 if ( 0 == wfNumRows( $res ) ) {
91 $id = 0;
92 } else {
93 $s = wfFetchObject( $res );
94 $id = $s->cur_id;
96 if ( 0 == $id ) { $this->addBadLink( $title ); }
97 else { $this->addGoodLink( $id, $title ); }
98 wfProfileOut();
99 return $id;
102 function preFill( $fromtitle )
104 wfProfileIn( "LinkCache::preFill" );
105 # Note -- $fromtitle is a Title *object*
106 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
107 $sql = "SELECT HIGH_PRIORITY cur_id,cur_namespace,cur_title
108 FROM cur,links
109 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
110 $res = wfQuery( $sql, "LinkCache::preFill" );
111 while( $s = wfFetchObject( $res ) ) {
112 $this->addGoodLink( $s->cur_id,
113 Title::makeName( $s->cur_namespace, $s->cur_title )
117 $this->suspend();
118 $id = $fromtitle->getArticleID();
119 $this->resume();
121 $sql = "SELECT HIGH_PRIORITY bl_to
122 FROM brokenlinks
123 WHERE bl_from='{$id}'";
124 $res = wfQuery( $sql, "LinkCache::preFill" );
125 while( $s = wfFetchObject( $res ) ) {
126 $this->addBadLink( $s->bl_to );
129 wfDebug("preFill dbkeyfrom=$dbkeyfrom\n");
130 $this->mOldBadLinks = $this->mBadLinks;
131 $this->mOldGoodLinks = $this->mGoodLinks;
132 $this->mPreFilled = true;
134 wfProfileOut();
137 function getGoodAdditions()
139 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
142 function getBadAdditions()
144 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
147 function getImageAdditions()
149 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
152 function getGoodDeletions()
154 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
157 function getBadDeletions()
159 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
162 function getImageDeletions()
164 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
167 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
168 # the incremental update arrays which will be filled. Returns whether or not it's
169 # worth doing the incremental version. For example, if [[List of mathematical topics]]
170 # was blanked, it would take a long, long time to do incrementally.
171 function incrementalSetup( $which, &$del, &$add )
173 if ( ! $this->mPreFilled ) {
174 return false;
177 switch ( $which ) {
178 case LINKCACHE_GOOD:
179 $old =& $this->mOldGoodLinks;
180 $cur =& $this->mGoodLinks;
181 $del = $this->getGoodDeletions();
182 $add = $this->getGoodAdditions();
183 break;
184 case LINKCACHE_BAD:
185 $old =& $this->mOldBadLinks;
186 $cur =& $this->mBadLinks;
187 $del = $this->getBadDeletions();
188 $add = $this->getBadAdditions();
189 break;
190 default: # LINKCACHE_IMAGE
191 return false;
193 wfDebug( "which = $which\n" );
194 wfDebug( '$old = ' . implode(", ", $old) . "\n" );
195 wfDebug( '$cur = ' . implode(", ", $cur) . "\n" );
196 wfDebug( '$del = ' . implode(", ", $del) . "\n" );
197 wfDebug( '$add = ' . implode(", ", $add) . "\n" );
199 # Coefficients here (1,1,3,1) could probably be put in a global object
200 $timeDumb = count( $old ) + count( $cur );
201 $timeIncr = count( $del ) * 3 + count( $new );
203 return $timeIncr < $timeDumb;
206 # Clears cache but leaves old preFill copies alone
207 function clear()
209 $this->mGoodLinks = array();
210 $this->mBadLinks = array();
211 $this->mImageLinks = array();