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.
68 * @deprecated since 1.28, use MediaWikiServices instead
70 public static function singleton() {
71 return MediaWikiServices
::getInstance()->getLinkCache();
75 * General accessor to get/set whether the master DB should be used
77 * This used to also set the FOR UPDATE option (locking the rows read
78 * in order to avoid link table inconsistency), which was later removed
79 * for performance on wikis with a high edit rate.
84 public function forUpdate( $update = null ) {
85 return wfSetVar( $this->mForUpdate
, $update );
89 * @param string $title Prefixed DB key
90 * @return int Page ID or zero
92 public function getGoodLinkID( $title ) {
93 $info = $this->mGoodLinks
->get( $title );
101 * Get a field of a title object from cache.
102 * If this link is not a cached good title, it will return NULL.
103 * @param LinkTarget $target
104 * @param string $field ('length','redirect','revision','model')
105 * @return string|int|null
107 public function getGoodLinkFieldObj( LinkTarget
$target, $field ) {
108 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
109 $info = $this->mGoodLinks
->get( $dbkey );
113 return $info[$field];
117 * @param string $title Prefixed DB key
120 public function isBadLink( $title ) {
121 // Use get() to ensure it records as used for LRU.
122 return $this->mBadLinks
->get( $title ) !== false;
126 * Add a link for the title to the link cache
128 * @param int $id Page's ID
129 * @param LinkTarget $target
130 * @param int $len Text's length
131 * @param int $redir Whether the page is a redirect
132 * @param int $revision Latest revision's ID
133 * @param string|null $model Latest revision's content model ID
134 * @param string|null $lang Language code of the page, if not the content language
136 public function addGoodLinkObj( $id, LinkTarget
$target, $len = -1, $redir = null,
137 $revision = 0, $model = null, $lang = null
139 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
140 $this->mGoodLinks
->set( $dbkey, [
142 'length' => (int)$len,
143 'redirect' => (int)$redir,
144 'revision' => (int)$revision,
145 'model' => $model ?
(string)$model : null,
146 'lang' => $lang ?
(string)$lang : null,
151 * Same as above with better interface.
153 * @param LinkTarget $target
154 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
155 * page_latest and page_content_model
157 public function addGoodLinkObjFromRow( LinkTarget
$target, $row ) {
158 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
159 $this->mGoodLinks
->set( $dbkey, [
160 'id' => intval( $row->page_id
),
161 'length' => intval( $row->page_len
),
162 'redirect' => intval( $row->page_is_redirect
),
163 'revision' => intval( $row->page_latest
),
164 'model' => !empty( $row->page_content_model
) ?
strval( $row->page_content_model
) : null,
165 'lang' => !empty( $row->page_lang
) ?
strval( $row->page_lang
) : null,
170 * @param LinkTarget $target
172 public function addBadLinkObj( LinkTarget
$target ) {
173 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
174 if ( !$this->isBadLink( $dbkey ) ) {
175 $this->mBadLinks
->set( $dbkey, 1 );
180 * @param string $title Prefixed DB key
182 public function clearBadLink( $title ) {
183 $this->mBadLinks
->delete( $title );
187 * @param LinkTarget $target
189 public function clearLink( LinkTarget
$target ) {
190 $dbkey = $this->titleFormatter
->getPrefixedDBkey( $target );
191 $this->mBadLinks
->delete( $dbkey );
192 $this->mGoodLinks
->delete( $dbkey );
196 * Add a title to the link cache, return the page_id or zero if non-existent
198 * @deprecated since 1.27, unused
199 * @param string $title Prefixed DB key
200 * @return int Page ID or zero
202 public function addLink( $title ) {
203 $nt = Title
::newFromDBkey( $title );
207 return $this->addLinkObj( $nt );
211 * Add a title to the link cache, return the page_id or zero if non-existent
213 * @param LinkTarget $nt LinkTarget object to add
214 * @return int Page ID or zero
216 public function addLinkObj( LinkTarget
$nt ) {
217 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
219 $key = $this->titleFormatter
->getPrefixedDBkey( $nt );
220 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
223 $id = $this->getGoodLinkID( $key );
232 // Some fields heavily used for linking...
233 $db = $this->mForUpdate ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
235 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
236 if ( $wgContentHandlerUseDB ) {
237 $fields[] = 'page_content_model';
239 if ( $wgPageLanguageUseDB ) {
240 $fields[] = 'page_lang';
243 $row = $db->selectRow( 'page', $fields,
244 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
248 if ( $row !== false ) {
249 $this->addGoodLinkObjFromRow( $nt, $row );
250 $id = intval( $row->page_id
);
252 $this->addBadLinkObj( $nt );
262 public function clear() {
263 $this->mGoodLinks
->clear();
264 $this->mBadLinks
->clear();