Fix hooks documentation for ArticleProtect/ArticleProtectComplete
[lhc/web/wiklou.git] / includes / cache / LinkCache.php
1 <?php
2 /**
3 * Page existence cache.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
26 *
27 * @ingroup Cache
28 */
29 class LinkCache {
30 // Increment $mClassVer whenever old serialized versions of this class
31 // becomes incompatible with the new version.
32 private $mClassVer = 5;
33
34 /**
35 * @var MapCacheLRU
36 */
37 private $mGoodLinks;
38 /**
39 * @var MapCacheLRU
40 */
41 private $mBadLinks;
42 private $mForUpdate = false;
43
44 /**
45 * How many Titles to store. There are two caches, so the amount actually
46 * stored in memory can be up to twice this.
47 */
48 const MAX_SIZE = 10000;
49
50 /**
51 * @var LinkCache
52 */
53 protected static $instance;
54
55 public function __construct() {
56 $this->mGoodLinks = new MapCacheLRU( self::MAX_SIZE );
57 $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
58 }
59
60 /**
61 * Get an instance of this class.
62 *
63 * @return LinkCache
64 */
65 static function &singleton() {
66 if ( self::$instance ) {
67 return self::$instance;
68 }
69 self::$instance = new LinkCache;
70
71 return self::$instance;
72 }
73
74 /**
75 * Destroy the singleton instance, a new one will be created next time
76 * singleton() is called.
77 * @since 1.22
78 */
79 static function destroySingleton() {
80 self::$instance = null;
81 }
82
83 /**
84 * Set the singleton instance to a given object.
85 * Since we do not have an interface for LinkCache, you have to be sure the
86 * given object implements all the LinkCache public methods.
87 * @param LinkCache $instance
88 * @since 1.22
89 */
90 static function setSingleton( LinkCache $instance ) {
91 self::$instance = $instance;
92 }
93
94 /**
95 * General accessor to get/set whether SELECT FOR UPDATE should be used
96 *
97 * @param bool $update
98 * @return bool
99 */
100 public function forUpdate( $update = null ) {
101 return wfSetVar( $this->mForUpdate, $update );
102 }
103
104 /**
105 * @param string $title
106 * @return int
107 */
108 public function getGoodLinkID( $title ) {
109 if ( $this->mGoodLinks->has( $title ) ) {
110 $info = $this->mGoodLinks->get( $title );
111 return $info['id'];
112 } else {
113 return 0;
114 }
115 }
116
117 /**
118 * Get a field of a title object from cache.
119 * If this link is not good, it will return NULL.
120 * @param Title $title
121 * @param string $field ('length','redirect','revision','model')
122 * @return string|null
123 */
124 public function getGoodLinkFieldObj( $title, $field ) {
125 $dbkey = $title->getPrefixedDBkey();
126 if ( $this->mGoodLinks->has( $dbkey ) ) {
127 $info = $this->mGoodLinks->get( $dbkey );
128 return $info[$field];
129 } else {
130 return null;
131 }
132 }
133
134 /**
135 * @param string $title
136 * @return bool
137 */
138 public function isBadLink( $title ) {
139 // We need to use get here since has will not call ping.
140 return $this->mBadLinks->get( $title ) !== null;
141 }
142
143 /**
144 * Add a link for the title to the link cache
145 *
146 * @param int $id Page's ID
147 * @param Title $title
148 * @param int $len Text's length
149 * @param int $redir Whether the page is a redirect
150 * @param int $revision Latest revision's ID
151 * @param string|null $model Latest revision's content model ID
152 */
153 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
154 $revision = 0, $model = null
155 ) {
156 $dbkey = $title->getPrefixedDBkey();
157 $this->mGoodLinks->set( $dbkey, array(
158 'id' => (int)$id,
159 'length' => (int)$len,
160 'redirect' => (int)$redir,
161 'revision' => (int)$revision,
162 'model' => $model ? (string)$model : null,
163 ) );
164 }
165
166 /**
167 * Same as above with better interface.
168 * @since 1.19
169 * @param Title $title
170 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
171 * page_latest and page_content_model
172 */
173 public function addGoodLinkObjFromRow( $title, $row ) {
174 $dbkey = $title->getPrefixedDBkey();
175 $this->mGoodLinks->set( $dbkey, array(
176 'id' => intval( $row->page_id ),
177 'length' => intval( $row->page_len ),
178 'redirect' => intval( $row->page_is_redirect ),
179 'revision' => intval( $row->page_latest ),
180 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
181 ) );
182 }
183
184 /**
185 * @param Title $title
186 */
187 public function addBadLinkObj( $title ) {
188 $dbkey = $title->getPrefixedDBkey();
189 if ( !$this->isBadLink( $dbkey ) ) {
190 $this->mBadLinks->set( $dbkey, 1 );
191 }
192 }
193
194 public function clearBadLink( $title ) {
195 $this->mBadLinks->clear( array( $title ) );
196 }
197
198 /**
199 * @param Title $title
200 */
201 public function clearLink( $title ) {
202 $dbkey = $title->getPrefixedDBkey();
203 $this->mBadLinks->clear( array( $dbkey ) );
204 $this->mGoodLinks->clear( array( $dbkey ) );
205 }
206
207
208 /**
209 * @deprecated since 1.26
210 * @return array
211 */
212 public function getGoodLinks() {
213 wfDeprecated( __METHOD__, '1.26' );
214 $links = array();
215 foreach ( $this->mGoodLinks->getAllKeys() as $key ) {
216 $info = $this->mGoodLinks->get( $key );
217 $links[$key] = $info['id'];
218 }
219
220 return $links;
221 }
222
223 /**
224 * @deprecated since 1.26
225 * @return array
226 */
227 public function getBadLinks() {
228 wfDeprecated( __METHOD__, '1.26' );
229 return $this->mBadLinks->getAllKeys();
230 }
231
232 /**
233 * Add a title to the link cache, return the page_id or zero if non-existent
234 *
235 * @param string $title Title to add
236 * @return int
237 */
238 public function addLink( $title ) {
239 $nt = Title::newFromDBkey( $title );
240 if ( $nt ) {
241 return $this->addLinkObj( $nt );
242 } else {
243 return 0;
244 }
245 }
246
247 /**
248 * Add a title to the link cache, return the page_id or zero if non-existent
249 *
250 * @param Title $nt Title object to add
251 * @return int
252 */
253 public function addLinkObj( $nt ) {
254 global $wgContentHandlerUseDB;
255
256 $key = $nt->getPrefixedDBkey();
257 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
258 return 0;
259 }
260 $id = $this->getGoodLinkID( $key );
261 if ( $id != 0 ) {
262 return $id;
263 }
264
265 if ( $key === '' ) {
266 return 0;
267 }
268
269 # Some fields heavily used for linking...
270 if ( $this->mForUpdate ) {
271 $db = wfGetDB( DB_MASTER );
272 } else {
273 $db = wfGetDB( DB_SLAVE );
274 }
275
276 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
277 if ( $wgContentHandlerUseDB ) {
278 $f[] = 'page_content_model';
279 }
280
281 $s = $db->selectRow( 'page', $f,
282 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
283 __METHOD__ );
284 # Set fields...
285 if ( $s !== false ) {
286 $this->addGoodLinkObjFromRow( $nt, $s );
287 $id = intval( $s->page_id );
288 } else {
289 $this->addBadLinkObj( $nt );
290 $id = 0;
291 }
292
293 return $id;
294 }
295
296 /**
297 * Clears cache
298 */
299 public function clear() {
300 $this->mGoodLinks->clear();
301 $this->mBadLinks->clear();
302 }
303 }