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 MediaWiki\Linker\LinkTarget
;
24 use MediaWiki\MediaWikiServices
;
27 * Cache for article titles (prefixed DB keys) and ids linked from one source
40 private $mForUpdate = false;
45 private $titleFormatter;
48 * How many Titles to store. There are two caches, so the amount actually
49 * stored in memory can be up to twice this.
51 const MAX_SIZE
= 10000;
56 protected static $instance;
58 public function __construct( TitleFormatter
$titleFormatter ) {
59 $this->mGoodLinks
= new HashBagOStuff( [ 'maxKeys' => self
::MAX_SIZE
] );
60 $this->mBadLinks
= new HashBagOStuff( [ 'maxKeys' => self
::MAX_SIZE
] );
61 $this->titleFormatter
= $titleFormatter;
65 * Get an instance of this class.
69 public static function singleton() {
70 if ( !self
::$instance ) {
71 self
::$instance = new LinkCache(
72 MediaWikiServices
::getInstance()->getTitleFormatter()
76 return self
::$instance;
80 * Destroy the singleton instance
82 * A new one will be created next time singleton() is called.
86 public static function destroySingleton() {
87 self
::$instance = null;
91 * Set the singleton instance to a given object.
93 * Since we do not have an interface for LinkCache, you have to be sure the
94 * given object implements all the LinkCache public methods.
96 * @param LinkCache $instance
99 public static function setSingleton( LinkCache
$instance ) {
100 self
::$instance = $instance;
104 * General accessor to get/set whether the master DB should be used
106 * This used to also set the FOR UPDATE option (locking the rows read
107 * in order to avoid link table inconsistency), which was later removed
108 * for performance on wikis with a high edit rate.
110 * @param bool $update
113 public function forUpdate( $update = null ) {
114 return wfSetVar( $this->mForUpdate
, $update );
118 * @param string $title Prefixed DB key
119 * @return int Page ID or zero
121 public function getGoodLinkID( $title ) {
122 $info = $this->mGoodLinks
->get( $title );
130 * Get a field of a title object from cache.
131 * If this link is not a cached good title, it will return NULL.
132 * @param LinkTarget $target
133 * @param string $field ('length','redirect','revision','model')
134 * @return string|int|null
136 public function getGoodLinkFieldObj( LinkTarget
$target, $field ) {
137 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
138 $info = $this->mGoodLinks
->get( $dbkey );
142 return $info[$field];
146 * @param string $title Prefixed DB key
149 public function isBadLink( $title ) {
150 // Use get() to ensure it records as used for LRU.
151 return $this->mBadLinks
->get( $title ) !== false;
155 * Add a link for the title to the link cache
157 * @param int $id Page's ID
158 * @param LinkTarget $target
159 * @param int $len Text's length
160 * @param int $redir Whether the page is a redirect
161 * @param int $revision Latest revision's ID
162 * @param string|null $model Latest revision's content model ID
163 * @param string|null $lang Language code of the page, if not the content language
165 public function addGoodLinkObj( $id, LinkTarget
$target, $len = -1, $redir = null,
166 $revision = 0, $model = null, $lang = null
168 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
169 $this->mGoodLinks
->set( $dbkey, [
171 'length' => (int)$len,
172 'redirect' => (int)$redir,
173 'revision' => (int)$revision,
174 'model' => $model ?
(string)$model : null,
175 'lang' => $lang ?
(string)$lang : null,
180 * Same as above with better interface.
182 * @param LinkTarget $target
183 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
184 * page_latest and page_content_model
186 public function addGoodLinkObjFromRow( LinkTarget
$target, $row ) {
187 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
188 $this->mGoodLinks
->set( $dbkey, [
189 'id' => intval( $row->page_id
),
190 'length' => intval( $row->page_len
),
191 'redirect' => intval( $row->page_is_redirect
),
192 'revision' => intval( $row->page_latest
),
193 'model' => !empty( $row->page_content_model
) ?
strval( $row->page_content_model
) : null,
194 'lang' => !empty( $row->page_lang
) ?
strval( $row->page_lang
) : null,
199 * @param LinkTarget $target
201 public function addBadLinkObj( LinkTarget
$target ) {
202 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
203 if ( !$this->isBadLink( $dbkey ) ) {
204 $this->mBadLinks
->set( $dbkey, 1 );
209 * @param string $title Prefixed DB key
211 public function clearBadLink( $title ) {
212 $this->mBadLinks
->delete( $title );
216 * @param LinkTarget $target
218 public function clearLink( LinkTarget
$target ) {
219 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
220 $this->mBadLinks
->delete( $dbkey );
221 $this->mGoodLinks
->delete( $dbkey );
225 * Add a title to the link cache, return the page_id or zero if non-existent
227 * @deprecated since 1.27, unused
228 * @param string $title Prefixed DB key
229 * @return int Page ID or zero
231 public function addLink( $title ) {
232 $nt = Title
::newFromDBkey( $title );
236 return $this->addLinkObj( $nt );
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 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
248 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
249 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
252 $id = $this->getGoodLinkID( $key );
261 // Some fields heavily used for linking...
262 $db = $this->mForUpdate ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
264 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
265 if ( $wgContentHandlerUseDB ) {
266 $fields[] = 'page_content_model';
268 if ( $wgPageLanguageUseDB ) {
269 $fields[] = 'page_lang';
272 $row = $db->selectRow( 'page', $fields,
273 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
277 if ( $row !== false ) {
278 $this->addGoodLinkObjFromRow( $nt, $row );
279 $id = intval( $row->page_id
);
281 $this->addBadLinkObj( $nt );
291 public function clear() {
292 $this->mGoodLinks
->clear();
293 $this->mBadLinks
->clear();