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