Added texts for categories.
[mediawiki.git] / includes / LinkCache.php
blob0955edda3a8c14cdcb8f8519579772253983c3f5
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 $this->mOldBadLinks = $this->mBadLinks;
130 $this->mOldGoodLinks = $this->mGoodLinks;
131 $this->mPreFilled = true;
133 wfProfileOut();
136 function getGoodAdditions()
138 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
141 function getBadAdditions()
143 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
146 function getImageAdditions()
148 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
151 function getGoodDeletions()
153 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
156 function getBadDeletions()
158 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
161 function getImageDeletions()
163 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
166 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
167 # the incremental update arrays which will be filled. Returns whether or not it's
168 # worth doing the incremental version. For example, if [[List of mathematical topics]]
169 # was blanked, it would take a long, long time to do incrementally.
170 function incrementalSetup( $which, &$del, &$add )
172 if ( ! $this->mPreFilled ) {
173 return false;
176 switch ( $which ) {
177 case LINKCACHE_GOOD:
178 $old =& $this->mOldGoodLinks;
179 $cur =& $this->mGoodLinks;
180 $del = $this->getGoodDeletions();
181 $add = $this->getGoodAdditions();
182 break;
183 case LINKCACHE_BAD:
184 $old =& $this->mOldBadLinks;
185 $cur =& $this->mBadLinks;
186 $del = $this->getBadDeletions();
187 $add = $this->getBadAdditions();
188 break;
189 default: # LINKCACHE_IMAGE
190 return false;
193 return true;
196 # Clears cache but leaves old preFill copies alone
197 function clear()
199 $this->mGoodLinks = array();
200 $this->mBadLinks = array();
201 $this->mImageLinks = array();