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;
53 public function __construct( TitleFormatter
$titleFormatter ) {
54 $this->mGoodLinks
= new HashBagOStuff( [ 'maxKeys' => self
::MAX_SIZE
] );
55 $this->mBadLinks
= new HashBagOStuff( [ 'maxKeys' => self
::MAX_SIZE
] );
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 // Some fields heavily used for linking...
248 $db = $this->mForUpdate ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
250 $row = $db->selectRow( 'page', self
::getSelectFields(),
251 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
255 if ( $row !== false ) {
256 $this->addGoodLinkObjFromRow( $nt, $row );
257 $id = intval( $row->page_id
);
259 $this->addBadLinkObj( $nt );
269 public function clear() {
270 $this->mGoodLinks
->clear();
271 $this->mBadLinks
->clear();