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;
49 * How many Titles to store. There are two caches, so the amount actually
50 * stored in memory can be up to twice this.
52 const MAX_SIZE
= 10000;
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;
62 * Get an instance of this class.
65 * @deprecated since 1.28, use MediaWikiServices instead
67 public static function singleton() {
68 return MediaWikiServices
::getInstance()->getLinkCache();
72 * General accessor to get/set whether the master DB should be used
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.
78 * @param bool|null $update
81 public function forUpdate( $update = null ) {
82 return wfSetVar( $this->mForUpdate
, $update );
86 * @param string $title Prefixed DB key
87 * @return int Page ID or zero
89 public function getGoodLinkID( $title ) {
90 $info = $this->goodLinks
->get( $title );
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
104 public function getGoodLinkFieldObj( LinkTarget
$target, $field ) {
105 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
106 $info = $this->goodLinks
->get( $dbkey );
110 return $info[$field];
114 * @param string $title Prefixed DB key
117 public function isBadLink( $title ) {
118 // Use get() to ensure it records as used for LRU.
119 return $this->badLinks
->has( $title );
123 * Add a link for the title to the link cache
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
133 public function addGoodLinkObj( $id, LinkTarget
$target, $len = -1, $redir = null,
134 $revision = 0, $model = null, $lang = null
136 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
137 $this->goodLinks
->set( $dbkey, [
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
149 * Same as above with better interface.
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
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
)
165 'lang' => !empty( $row->page_lang
)
166 ?
strval( $row->page_lang
)
168 'restrictions' => !empty( $row->page_restrictions
)
169 ?
strval( $row->page_restrictions
)
175 * @param LinkTarget $target
177 public function addBadLinkObj( LinkTarget
$target ) {
178 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
179 if ( !$this->isBadLink( $dbkey ) ) {
180 $this->badLinks
->set( $dbkey, 1 );
185 * @param string $title Prefixed DB key
187 public function clearBadLink( $title ) {
188 $this->badLinks
->clear( $title );
192 * @param LinkTarget $target
194 public function clearLink( LinkTarget
$target ) {
195 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
196 $this->badLinks
->clear( $dbkey );
197 $this->goodLinks
->clear( $dbkey );
201 * Fields that LinkCache needs to select
206 public static function getSelectFields() {
207 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
216 if ( $wgContentHandlerUseDB ) {
217 $fields[] = 'page_content_model';
219 if ( $wgPageLanguageUseDB ) {
220 $fields[] = 'page_lang';
227 * Add a title to the link cache, return the page_id or zero if non-existent
229 * @param LinkTarget $nt LinkTarget object to add
230 * @return int Page ID or zero
232 public function addLinkObj( LinkTarget
$nt ) {
233 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
234 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ||
$nt->getNamespace() < 0 ) {
237 $id = $this->getGoodLinkID( $key );
246 // Cache template/file pages as they are less often viewed but heavily used
247 if ( $this->mForUpdate
) {
248 $row = $this->fetchPageRow( wfGetDB( DB_MASTER
), $nt );
249 } elseif ( $this->isCacheable( $nt ) ) {
250 // These pages are often transcluded heavily, so cache them
251 $cache = $this->wanCache
;
252 $row = $cache->getWithSetCallback(
253 $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
255 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
256 $dbr = wfGetDB( DB_REPLICA
);
257 $setOpts +
= Database
::getCacheSetOptions( $dbr );
259 $row = $this->fetchPageRow( $dbr, $nt );
260 $mtime = $row ?
wfTimestamp( TS_UNIX
, $row->page_touched
) : false;
261 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
267 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA
), $nt );
271 $this->addGoodLinkObjFromRow( $nt, $row );
272 $id = intval( $row->page_id
);
274 $this->addBadLinkObj( $nt );
282 * @param WANObjectCache $cache
283 * @param LinkTarget $t
287 public function getMutableCacheKeys( WANObjectCache
$cache, LinkTarget
$t ) {
288 if ( $this->isCacheable( $t ) ) {
289 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
295 private function isCacheable( LinkTarget
$title ) {
296 $ns = $title->getNamespace();
297 if ( in_array( $ns, [ NS_TEMPLATE
, NS_FILE
, NS_CATEGORY
] ) ) {
300 // Focus on transcluded pages more than the main content
301 if ( MWNamespace
::isContent( $ns ) ) {
304 // Non-talk extension namespaces (e.g. NS_MODULE)
305 return ( $ns >= 100 && MWNamespace
::isSubject( $ns ) );
308 private function fetchPageRow( IDatabase
$db, LinkTarget
$nt ) {
309 $fields = self
::getSelectFields();
310 if ( $this->isCacheable( $nt ) ) {
311 $fields[] = 'page_touched';
314 return $db->selectRow(
317 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
323 * Purge the link cache for a title
325 * @param LinkTarget $title
328 public function invalidateTitle( LinkTarget
$title ) {
329 if ( $this->isCacheable( $title ) ) {
330 $cache = $this->wanCache
;
332 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
340 public function clear() {
341 $this->goodLinks
->clear();
342 $this->badLinks
->clear();