Added per page support for inhibiting editsection links via the __NOEDITSECTION__...
[mediawiki.git] / includes / LinkCache.php
blob379da89f4a6afdec845169eacc159f27d99f6e7c
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] );
65 global $wgMemc, $wgDBname;
66 $wgMemc->delete( "$wgDBname:lc:title:$title" );
69 function suspend() { $this->mActive = false; }
70 function resume() { $this->mActive = true; }
71 function getGoodLinks() { return $this->mGoodLinks; }
72 function getBadLinks() { return $this->mBadLinks; }
73 function getImageLinks() { return $this->mImageLinks; }
75 function addLink( $title )
77 if ( $this->isBadLink( $title ) ) { return 0; }
78 $id = $this->getGoodLinkID( $title );
79 if ( 0 != $id ) { return $id; }
81 global $wgMemc, $wgDBname;
82 wfProfileIn( "LinkCache::addLink-checkdatabase" );
84 $nt = Title::newFromDBkey( $title );
85 $ns = $nt->getNamespace();
86 $t = $nt->getDBkey();
88 if ( "" == $t ) { return 0; }
90 $id = $wgMemc->get( $key = "$wgDBname:lc:title:$title" );
91 if( $id === FALSE ) {
92 $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" .
93 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
94 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
96 if ( 0 == wfNumRows( $res ) ) {
97 $id = 0;
98 } else {
99 $s = wfFetchObject( $res );
100 $id = $s->cur_id;
102 $wgMemc->add( $key, $id, time()+3600 );
104 if ( 0 == $id ) { $this->addBadLink( $title ); }
105 else { $this->addGoodLink( $id, $title ); }
106 wfProfileOut();
107 return $id;
110 function preFill( $fromtitle )
112 wfProfileIn( "LinkCache::preFill" );
113 # Note -- $fromtitle is a Title *object*
114 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
115 $sql = "SELECT HIGH_PRIORITY cur_id,cur_namespace,cur_title
116 FROM cur,links
117 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
118 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
119 while( $s = wfFetchObject( $res ) ) {
120 $this->addGoodLink( $s->cur_id,
121 Title::makeName( $s->cur_namespace, $s->cur_title )
125 $this->suspend();
126 $id = $fromtitle->getArticleID();
127 $this->resume();
129 $sql = "SELECT HIGH_PRIORITY bl_to
130 FROM brokenlinks
131 WHERE bl_from='{$id}'";
132 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
133 while( $s = wfFetchObject( $res ) ) {
134 $this->addBadLink( $s->bl_to );
137 $this->mOldBadLinks = $this->mBadLinks;
138 $this->mOldGoodLinks = $this->mGoodLinks;
139 $this->mPreFilled = true;
141 wfProfileOut();
144 function getGoodAdditions()
146 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
149 function getBadAdditions()
151 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
154 function getImageAdditions()
156 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
159 function getGoodDeletions()
161 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
164 function getBadDeletions()
166 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
169 function getImageDeletions()
171 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
174 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
175 # the incremental update arrays which will be filled. Returns whether or not it's
176 # worth doing the incremental version. For example, if [[List of mathematical topics]]
177 # was blanked, it would take a long, long time to do incrementally.
178 function incrementalSetup( $which, &$del, &$add )
180 if ( ! $this->mPreFilled ) {
181 return false;
184 switch ( $which ) {
185 case LINKCACHE_GOOD:
186 $old =& $this->mOldGoodLinks;
187 $cur =& $this->mGoodLinks;
188 $del = $this->getGoodDeletions();
189 $add = $this->getGoodAdditions();
190 break;
191 case LINKCACHE_BAD:
192 $old =& $this->mOldBadLinks;
193 $cur =& $this->mBadLinks;
194 $del = $this->getBadDeletions();
195 $add = $this->getBadAdditions();
196 break;
197 default: # LINKCACHE_IMAGE
198 return false;
201 return true;
204 # Clears cache but leaves old preFill copies alone
205 function clear()
207 $this->mGoodLinks = array();
208 $this->mBadLinks = array();
209 $this->mImageLinks = array();