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 HashBagOStuff */
37 /** @var HashBagOStuff */
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->mGoodLinks
= new HashBagOStuff( [ 'maxKeys' => self
::MAX_SIZE
] );
56 $this->mBadLinks
= new HashBagOStuff( [ 'maxKeys' => 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.
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->mGoodLinks
->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->mGoodLinks
->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->mBadLinks
->get( $title ) !== false;
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 $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->mGoodLinks
->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,
148 * Same as above with better interface.
150 * @param LinkTarget $target
151 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
152 * page_latest and page_content_model
154 public function addGoodLinkObjFromRow( LinkTarget
$target, $row ) {
155 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
156 $this->mGoodLinks
->set( $dbkey, [
157 'id' => intval( $row->page_id
),
158 'length' => intval( $row->page_len
),
159 'redirect' => intval( $row->page_is_redirect
),
160 'revision' => intval( $row->page_latest
),
161 'model' => !empty( $row->page_content_model
) ?
strval( $row->page_content_model
) : null,
162 'lang' => !empty( $row->page_lang
) ?
strval( $row->page_lang
) : null,
167 * @param LinkTarget $target
169 public function addBadLinkObj( LinkTarget
$target ) {
170 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
171 if ( !$this->isBadLink( $dbkey ) ) {
172 $this->mBadLinks
->set( $dbkey, 1 );
177 * @param string $title Prefixed DB key
179 public function clearBadLink( $title ) {
180 $this->mBadLinks
->delete( $title );
184 * @param LinkTarget $target
186 public function clearLink( LinkTarget
$target ) {
187 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
188 $this->mBadLinks
->delete( $dbkey );
189 $this->mGoodLinks
->delete( $dbkey );
193 * Add a title to the link cache, return the page_id or zero if non-existent
195 * @deprecated since 1.27, unused
196 * @param string $title Prefixed DB key
197 * @return int Page ID or zero
199 public function addLink( $title ) {
200 $nt = Title
::newFromDBkey( $title );
204 return $this->addLinkObj( $nt );
208 * Fields that LinkCache needs to select
213 public static function getSelectFields() {
214 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
216 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
217 if ( $wgContentHandlerUseDB ) {
218 $fields[] = 'page_content_model';
220 if ( $wgPageLanguageUseDB ) {
221 $fields[] = 'page_lang';
228 * Add a title to the link cache, return the page_id or zero if non-existent
230 * @param LinkTarget $nt LinkTarget object to add
231 * @return int Page ID or zero
233 public function addLinkObj( LinkTarget
$nt ) {
234 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
235 if ( $this->isBadLink( $key ) ||
$nt->isExternal()
236 ||
$nt->inNamespace( NS_SPECIAL
)
240 $id = $this->getGoodLinkID( $key );
249 // Cache template/file pages as they are less often viewed but heavily used
250 if ( $this->mForUpdate
) {
251 $row = $this->fetchPageRow( wfGetDB( DB_MASTER
), $nt );
252 } elseif ( $this->isCacheable( $nt ) ) {
253 // These pages are often transcluded heavily, so cache them
254 $cache = $this->wanCache
;
255 $row = $cache->getWithSetCallback(
256 $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
258 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
259 $dbr = wfGetDB( DB_REPLICA
);
260 $setOpts +
= Database
::getCacheSetOptions( $dbr );
262 $row = $this->fetchPageRow( $dbr, $nt );
263 $mtime = $row ?
wfTimestamp( TS_UNIX
, $row->page_touched
) : false;
264 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
270 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA
), $nt );
274 $this->addGoodLinkObjFromRow( $nt, $row );
275 $id = intval( $row->page_id
);
277 $this->addBadLinkObj( $nt );
285 * @param WANObjectCache $cache
286 * @param TitleValue $t
290 public function getMutableCacheKeys( WANObjectCache
$cache, TitleValue
$t ) {
291 if ( $this->isCacheable( $t ) ) {
292 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
298 private function isCacheable( LinkTarget
$title ) {
299 return ( $title->inNamespace( NS_TEMPLATE
) ||
$title->inNamespace( NS_FILE
) );
302 private function fetchPageRow( IDatabase
$db, LinkTarget
$nt ) {
303 $fields = self
::getSelectFields();
304 if ( $this->isCacheable( $nt ) ) {
305 $fields[] = 'page_touched';
308 return $db->selectRow(
311 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
317 * Purge the link cache for a title
319 * @param LinkTarget $title
322 public function invalidateTitle( LinkTarget
$title ) {
323 if ( $this->isCacheable( $title ) ) {
324 $cache = ObjectCache
::getMainWANInstance();
326 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
334 public function clear() {
335 $this->mGoodLinks
->clear();
336 $this->mBadLinks
->clear();