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