Update LinkCache to use NamespaceInfo
[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 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\IDatabase;
26 use MediaWiki\Linker\LinkTarget;
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Cache for article titles (prefixed DB keys) and ids linked from one source
31 *
32 * @ingroup Cache
33 */
34 class LinkCache {
35 /** @var MapCacheLRU */
36 private $goodLinks;
37 /** @var MapCacheLRU */
38 private $badLinks;
39 /** @var WANObjectCache */
40 private $wanCache;
41
42 /** @var bool */
43 private $mForUpdate = false;
44
45 /** @var TitleFormatter */
46 private $titleFormatter;
47
48 /** @var NamespaceInfo */
49 private $nsInfo;
50
51 /**
52 * How many Titles to store. There are two caches, so the amount actually
53 * stored in memory can be up to twice this.
54 */
55 const MAX_SIZE = 10000;
56
57 public function __construct(
58 TitleFormatter $titleFormatter,
59 WANObjectCache $cache,
60 NamespaceInfo $nsInfo = null
61 ) {
62 if ( !$nsInfo ) {
63 wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
64 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
65 }
66 $this->goodLinks = new MapCacheLRU( self::MAX_SIZE );
67 $this->badLinks = new MapCacheLRU( self::MAX_SIZE );
68 $this->wanCache = $cache;
69 $this->titleFormatter = $titleFormatter;
70 $this->nsInfo = $nsInfo;
71 }
72
73 /**
74 * Get an instance of this class.
75 *
76 * @return LinkCache
77 * @deprecated since 1.28, use MediaWikiServices instead
78 */
79 public static function singleton() {
80 return MediaWikiServices::getInstance()->getLinkCache();
81 }
82
83 /**
84 * General accessor to get/set whether the master DB should be used
85 *
86 * This used to also set the FOR UPDATE option (locking the rows read
87 * in order to avoid link table inconsistency), which was later removed
88 * for performance on wikis with a high edit rate.
89 *
90 * @param bool|null $update
91 * @return bool
92 */
93 public function forUpdate( $update = null ) {
94 return wfSetVar( $this->mForUpdate, $update );
95 }
96
97 /**
98 * @param string $title Prefixed DB key
99 * @return int Page ID or zero
100 */
101 public function getGoodLinkID( $title ) {
102 $info = $this->goodLinks->get( $title );
103 if ( !$info ) {
104 return 0;
105 }
106 return $info['id'];
107 }
108
109 /**
110 * Get a field of a title object from cache.
111 * If this link is not a cached good title, it will return NULL.
112 * @param LinkTarget $target
113 * @param string $field ('length','redirect','revision','model')
114 * @return string|int|null
115 */
116 public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
117 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
118 $info = $this->goodLinks->get( $dbkey );
119 if ( !$info ) {
120 return null;
121 }
122 return $info[$field];
123 }
124
125 /**
126 * @param string $title Prefixed DB key
127 * @return bool
128 */
129 public function isBadLink( $title ) {
130 // Use get() to ensure it records as used for LRU.
131 return $this->badLinks->has( $title );
132 }
133
134 /**
135 * Add a link for the title to the link cache
136 *
137 * @param int $id Page's ID
138 * @param LinkTarget $target
139 * @param int $len Text's length
140 * @param int|null $redir Whether the page is a redirect
141 * @param int $revision Latest revision's ID
142 * @param string|null $model Latest revision's content model ID
143 * @param string|null $lang Language code of the page, if not the content language
144 */
145 public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
146 $revision = 0, $model = null, $lang = null
147 ) {
148 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
149 $this->goodLinks->set( $dbkey, [
150 'id' => (int)$id,
151 'length' => (int)$len,
152 'redirect' => (int)$redir,
153 'revision' => (int)$revision,
154 'model' => $model ? (string)$model : null,
155 'lang' => $lang ? (string)$lang : null,
156 'restrictions' => null
157 ] );
158 }
159
160 /**
161 * Same as above with better interface.
162 * @since 1.19
163 * @param LinkTarget $target
164 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
165 * page_latest and page_content_model
166 */
167 public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
168 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
169 $this->goodLinks->set( $dbkey, [
170 'id' => intval( $row->page_id ),
171 'length' => intval( $row->page_len ),
172 'redirect' => intval( $row->page_is_redirect ),
173 'revision' => intval( $row->page_latest ),
174 'model' => !empty( $row->page_content_model )
175 ? strval( $row->page_content_model )
176 : null,
177 'lang' => !empty( $row->page_lang )
178 ? strval( $row->page_lang )
179 : null,
180 'restrictions' => !empty( $row->page_restrictions )
181 ? strval( $row->page_restrictions )
182 : null
183 ] );
184 }
185
186 /**
187 * @param LinkTarget $target
188 */
189 public function addBadLinkObj( LinkTarget $target ) {
190 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
191 if ( !$this->isBadLink( $dbkey ) ) {
192 $this->badLinks->set( $dbkey, 1 );
193 }
194 }
195
196 /**
197 * @param string $title Prefixed DB key
198 */
199 public function clearBadLink( $title ) {
200 $this->badLinks->clear( $title );
201 }
202
203 /**
204 * @param LinkTarget $target
205 */
206 public function clearLink( LinkTarget $target ) {
207 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
208 $this->badLinks->clear( $dbkey );
209 $this->goodLinks->clear( $dbkey );
210 }
211
212 /**
213 * Fields that LinkCache needs to select
214 *
215 * @since 1.28
216 * @return array
217 */
218 public static function getSelectFields() {
219 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
220
221 $fields = [
222 'page_id',
223 'page_len',
224 'page_is_redirect',
225 'page_latest',
226 'page_restrictions'
227 ];
228 if ( $wgContentHandlerUseDB ) {
229 $fields[] = 'page_content_model';
230 }
231 if ( $wgPageLanguageUseDB ) {
232 $fields[] = 'page_lang';
233 }
234
235 return $fields;
236 }
237
238 /**
239 * Add a title to the link cache, return the page_id or zero if non-existent
240 *
241 * @param LinkTarget $nt LinkTarget object to add
242 * @return int Page ID or zero
243 */
244 public function addLinkObj( LinkTarget $nt ) {
245 $key = $this->titleFormatter->getPrefixedDBkey( $nt );
246 if ( $this->isBadLink( $key ) || $nt->isExternal()
247 || $nt->inNamespace( NS_SPECIAL )
248 ) {
249 return 0;
250 }
251 $id = $this->getGoodLinkID( $key );
252 if ( $id != 0 ) {
253 return $id;
254 }
255
256 if ( $key === '' ) {
257 return 0;
258 }
259
260 // Cache template/file pages as they are less often viewed but heavily used
261 if ( $this->mForUpdate ) {
262 $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
263 } elseif ( $this->isCacheable( $nt ) ) {
264 // These pages are often transcluded heavily, so cache them
265 $cache = $this->wanCache;
266 $row = $cache->getWithSetCallback(
267 $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
268 $cache::TTL_DAY,
269 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
270 $dbr = wfGetDB( DB_REPLICA );
271 $setOpts += Database::getCacheSetOptions( $dbr );
272
273 $row = $this->fetchPageRow( $dbr, $nt );
274 $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
275 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
276
277 return $row;
278 }
279 );
280 } else {
281 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
282 }
283
284 if ( $row ) {
285 $this->addGoodLinkObjFromRow( $nt, $row );
286 $id = intval( $row->page_id );
287 } else {
288 $this->addBadLinkObj( $nt );
289 $id = 0;
290 }
291
292 return $id;
293 }
294
295 /**
296 * @param WANObjectCache $cache
297 * @param LinkTarget $t
298 * @return string[]
299 * @since 1.28
300 */
301 public function getMutableCacheKeys( WANObjectCache $cache, LinkTarget $t ) {
302 if ( $this->isCacheable( $t ) ) {
303 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
304 }
305
306 return [];
307 }
308
309 private function isCacheable( LinkTarget $title ) {
310 $ns = $title->getNamespace();
311 if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY ] ) ) {
312 return true;
313 }
314 // Focus on transcluded pages more than the main content
315 if ( $this->nsInfo->isContent( $ns ) ) {
316 return false;
317 }
318 // Non-talk extension namespaces (e.g. NS_MODULE)
319 return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
320 }
321
322 private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
323 $fields = self::getSelectFields();
324 if ( $this->isCacheable( $nt ) ) {
325 $fields[] = 'page_touched';
326 }
327
328 return $db->selectRow(
329 'page',
330 $fields,
331 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
332 __METHOD__
333 );
334 }
335
336 /**
337 * Purge the link cache for a title
338 *
339 * @param LinkTarget $title
340 * @since 1.28
341 */
342 public function invalidateTitle( LinkTarget $title ) {
343 if ( $this->isCacheable( $title ) ) {
344 $cache = $this->wanCache;
345 $cache->delete(
346 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
347 );
348 }
349 }
350
351 /**
352 * Clears cache
353 */
354 public function clear() {
355 $this->goodLinks->clear();
356 $this->badLinks->clear();
357 }
358 }