3 * Page existence cache.
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.
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.
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
24 use Wikimedia\Rdbms\Database
;
25 use Wikimedia\Rdbms\IDatabase
;
26 use MediaWiki\Linker\LinkTarget
;
27 use MediaWiki\MediaWikiServices
;
30 * Cache for article titles (prefixed DB keys) and ids linked from one source
35 /** @var MapCacheLRU */
37 /** @var MapCacheLRU */
39 /** @var WANObjectCache */
43 private $mForUpdate = false;
45 /** @var TitleFormatter */
46 private $titleFormatter;
48 /** @var NamespaceInfo */
52 * How many Titles to store. There are two caches, so the amount actually
53 * stored in memory can be up to twice this.
55 const MAX_SIZE
= 10000;
57 public function __construct(
58 TitleFormatter
$titleFormatter,
59 WANObjectCache
$cache,
60 NamespaceInfo
$nsInfo = null
63 wfDeprecated( __METHOD__
. ' with no NamespaceInfo argument', '1.34' );
64 $nsInfo = MediaWikiServices
::getInstance()->getNamespaceInfo();
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;
74 * Get an instance of this class.
77 * @deprecated since 1.28, use MediaWikiServices instead
79 public static function singleton() {
80 return MediaWikiServices
::getInstance()->getLinkCache();
84 * General accessor to get/set whether the master DB should be used
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.
90 * @param bool|null $update
92 * @deprecated Since 1.34
94 public function forUpdate( $update = null ) {
95 return wfSetVar( $this->mForUpdate
, $update );
99 * @param string $title Prefixed DB key
100 * @return int Page ID or zero
102 public function getGoodLinkID( $title ) {
103 $info = $this->goodLinks
->get( $title );
111 * Get a field of a title object from cache.
112 * If this link is not a cached good title, it will return NULL.
113 * @param LinkTarget $target
114 * @param string $field ('length','redirect','revision','model')
115 * @return string|int|null
117 public function getGoodLinkFieldObj( LinkTarget
$target, $field ) {
118 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
119 $info = $this->goodLinks
->get( $dbkey );
123 return $info[$field];
127 * @param string $title Prefixed DB key
130 public function isBadLink( $title ) {
131 // Use get() to ensure it records as used for LRU.
132 return $this->badLinks
->has( $title );
136 * Add a link for the title to the link cache
138 * @param int $id Page's ID
139 * @param LinkTarget $target
140 * @param int $len Text's length
141 * @param int|null $redir Whether the page is a redirect
142 * @param int $revision Latest revision's ID
143 * @param string|null $model Latest revision's content model ID
144 * @param string|null $lang Language code of the page, if not the content language
146 public function addGoodLinkObj( $id, LinkTarget
$target, $len = -1, $redir = null,
147 $revision = 0, $model = null, $lang = null
149 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
150 $this->goodLinks
->set( $dbkey, [
152 'length' => (int)$len,
153 'redirect' => (int)$redir,
154 'revision' => (int)$revision,
155 'model' => $model ?
(string)$model : null,
156 'lang' => $lang ?
(string)$lang : null,
157 'restrictions' => null
162 * Same as above with better interface.
164 * @param LinkTarget $target
165 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
166 * page_latest and page_content_model
168 public function addGoodLinkObjFromRow( LinkTarget
$target, $row ) {
169 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
170 $this->goodLinks
->set( $dbkey, [
171 'id' => intval( $row->page_id
),
172 'length' => intval( $row->page_len
),
173 'redirect' => intval( $row->page_is_redirect
),
174 'revision' => intval( $row->page_latest
),
175 'model' => !empty( $row->page_content_model
)
176 ?
strval( $row->page_content_model
)
178 'lang' => !empty( $row->page_lang
)
179 ?
strval( $row->page_lang
)
181 'restrictions' => !empty( $row->page_restrictions
)
182 ?
strval( $row->page_restrictions
)
188 * @param LinkTarget $target
190 public function addBadLinkObj( LinkTarget
$target ) {
191 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
192 if ( !$this->isBadLink( $dbkey ) ) {
193 $this->badLinks
->set( $dbkey, 1 );
198 * @param string $title Prefixed DB key
200 public function clearBadLink( $title ) {
201 $this->badLinks
->clear( $title );
205 * @param LinkTarget $target
207 public function clearLink( LinkTarget
$target ) {
208 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
209 $this->badLinks
->clear( $dbkey );
210 $this->goodLinks
->clear( $dbkey );
214 * Fields that LinkCache needs to select
219 public static function getSelectFields() {
220 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
229 if ( $wgContentHandlerUseDB ) {
230 $fields[] = 'page_content_model';
232 if ( $wgPageLanguageUseDB ) {
233 $fields[] = 'page_lang';
240 * Add a title to the link cache, return the page_id or zero if non-existent
242 * @param LinkTarget $nt LinkTarget object to add
243 * @return int Page ID or zero
245 public function addLinkObj( LinkTarget
$nt ) {
246 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
247 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ||
$nt->getNamespace() < 0 ) {
250 $id = $this->getGoodLinkID( $key );
259 // Cache template/file pages as they are less often viewed but heavily used
260 if ( $this->mForUpdate
) {
261 $row = $this->fetchPageRow( wfGetDB( DB_MASTER
), $nt );
262 } elseif ( $this->isCacheable( $nt ) ) {
263 // These pages are often transcluded heavily, so cache them
264 $cache = $this->wanCache
;
265 $row = $cache->getWithSetCallback(
266 $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
268 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
269 $dbr = wfGetDB( DB_REPLICA
);
270 $setOpts +
= Database
::getCacheSetOptions( $dbr );
272 $row = $this->fetchPageRow( $dbr, $nt );
273 $mtime = $row ?
wfTimestamp( TS_UNIX
, $row->page_touched
) : false;
274 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
280 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA
), $nt );
284 $this->addGoodLinkObjFromRow( $nt, $row );
285 $id = intval( $row->page_id
);
287 $this->addBadLinkObj( $nt );
295 * @param WANObjectCache $cache
296 * @param LinkTarget $t
300 public function getMutableCacheKeys( WANObjectCache
$cache, LinkTarget
$t ) {
301 if ( $this->isCacheable( $t ) ) {
302 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
308 private function isCacheable( LinkTarget
$title ) {
309 $ns = $title->getNamespace();
310 if ( in_array( $ns, [ NS_TEMPLATE
, NS_FILE
, NS_CATEGORY
, NS_MEDIAWIKI
] ) ) {
313 // Focus on transcluded pages more than the main content
314 if ( $this->nsInfo
->isContent( $ns ) ) {
317 // Non-talk extension namespaces (e.g. NS_MODULE)
318 return ( $ns >= 100 && $this->nsInfo
->isSubject( $ns ) );
321 private function fetchPageRow( IDatabase
$db, LinkTarget
$nt ) {
322 $fields = self
::getSelectFields();
323 if ( $this->isCacheable( $nt ) ) {
324 $fields[] = 'page_touched';
327 return $db->selectRow(
330 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
336 * Purge the link cache for a title
338 * @param LinkTarget $title
341 public function invalidateTitle( LinkTarget
$title ) {
342 if ( $this->isCacheable( $title ) ) {
343 $cache = $this->wanCache
;
345 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
353 public function clear() {
354 $this->goodLinks
->clear();
355 $this->badLinks
->clear();