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
23 use Wikimedia\Rdbms\IDatabase
;
24 use MediaWiki\Linker\LinkTarget
;
25 use MediaWiki\MediaWikiServices
;
28 * Cache for article titles (prefixed DB keys) and ids linked from one source
33 /** @var HashBagOStuff */
35 /** @var HashBagOStuff */
37 /** @var WANObjectCache */
41 private $mForUpdate = false;
43 /** @var TitleFormatter */
44 private $titleFormatter;
47 * How many Titles to store. There are two caches, so the amount actually
48 * stored in memory can be up to twice this.
50 const MAX_SIZE
= 10000;
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;
60 * Get an instance of this class.
63 * @deprecated since 1.28, use MediaWikiServices instead
65 public static function singleton() {
66 return MediaWikiServices
::getInstance()->getLinkCache();
70 * General accessor to get/set whether the master DB should be used
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.
79 public function forUpdate( $update = null ) {
80 return wfSetVar( $this->mForUpdate
, $update );
84 * @param string $title Prefixed DB key
85 * @return int Page ID or zero
87 public function getGoodLinkID( $title ) {
88 $info = $this->mGoodLinks
->get( $title );
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
102 public function getGoodLinkFieldObj( LinkTarget
$target, $field ) {
103 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
104 $info = $this->mGoodLinks
->get( $dbkey );
108 return $info[$field];
112 * @param string $title Prefixed DB key
115 public function isBadLink( $title ) {
116 // Use get() to ensure it records as used for LRU.
117 return $this->mBadLinks
->get( $title ) !== false;
121 * Add a link for the title to the link cache
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
131 public function addGoodLinkObj( $id, LinkTarget
$target, $len = -1, $redir = null,
132 $revision = 0, $model = null, $lang = null
134 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
135 $this->mGoodLinks
->set( $dbkey, [
137 'length' => (int)$len,
138 'redirect' => (int)$redir,
139 'revision' => (int)$revision,
140 'model' => $model ?
(string)$model : null,
141 'lang' => $lang ?
(string)$lang : null,
146 * Same as above with better interface.
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
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,
165 * @param LinkTarget $target
167 public function addBadLinkObj( LinkTarget
$target ) {
168 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
169 if ( !$this->isBadLink( $dbkey ) ) {
170 $this->mBadLinks
->set( $dbkey, 1 );
175 * @param string $title Prefixed DB key
177 public function clearBadLink( $title ) {
178 $this->mBadLinks
->delete( $title );
182 * @param LinkTarget $target
184 public function clearLink( LinkTarget
$target ) {
185 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
186 $this->mBadLinks
->delete( $dbkey );
187 $this->mGoodLinks
->delete( $dbkey );
191 * Add a title to the link cache, return the page_id or zero if non-existent
193 * @deprecated since 1.27, unused
194 * @param string $title Prefixed DB key
195 * @return int Page ID or zero
197 public function addLink( $title ) {
198 $nt = Title
::newFromDBkey( $title );
202 return $this->addLinkObj( $nt );
206 * Fields that LinkCache needs to select
211 public static function getSelectFields() {
212 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
214 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
215 if ( $wgContentHandlerUseDB ) {
216 $fields[] = 'page_content_model';
218 if ( $wgPageLanguageUseDB ) {
219 $fields[] = 'page_lang';
226 * Add a title to the link cache, return the page_id or zero if non-existent
228 * @param LinkTarget $nt LinkTarget object to add
229 * @return int Page ID or zero
231 public function addLinkObj( LinkTarget
$nt ) {
232 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
233 if ( $this->isBadLink( $key ) ||
$nt->isExternal()
234 ||
$nt->inNamespace( NS_SPECIAL
)
238 $id = $this->getGoodLinkID( $key );
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() ) ),
256 function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
257 $dbr = wfGetDB( DB_REPLICA
);
258 $setOpts +
= Database
::getCacheSetOptions( $dbr );
260 $row = $this->fetchPageRow( $dbr, $nt );
261 $mtime = $row ?
wfTimestamp( TS_UNIX
, $row->page_touched
) : false;
262 $ttl = $cache->adaptiveTTL( $mtime, $ttl );
268 $row = $this->fetchPageRow( wfGetDB( DB_REPLICA
), $nt );
272 $this->addGoodLinkObjFromRow( $nt, $row );
273 $id = intval( $row->page_id
);
275 $this->addBadLinkObj( $nt );
283 * @param WANObjectCache $cache
284 * @param TitleValue $t
288 public function getMutableCacheKeys( WANObjectCache
$cache, TitleValue
$t ) {
289 if ( $this->isCacheable( $t ) ) {
290 return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
296 private function isCacheable( LinkTarget
$title ) {
297 return ( $title->inNamespace( NS_TEMPLATE
) ||
$title->inNamespace( NS_FILE
) );
300 private function fetchPageRow( IDatabase
$db, LinkTarget
$nt ) {
301 $fields = self
::getSelectFields();
302 if ( $this->isCacheable( $nt ) ) {
303 $fields[] = 'page_touched';
306 return $db->selectRow(
309 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
315 * Purge the link cache for a title
317 * @param LinkTarget $title
320 public function invalidateTitle( LinkTarget
$title ) {
321 if ( $this->isCacheable( $title ) ) {
322 $cache = ObjectCache
::getMainWANInstance();
324 $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
332 public function clear() {
333 $this->mGoodLinks
->clear();
334 $this->mBadLinks
->clear();