Merge "API: Fixes for AuthManager"
[lhc/web/wiklou.git] / includes / cache / LinkCache.php
1 <?php
2 /**
3 * Page existence cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23 use MediaWiki\Linker\LinkTarget;
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Cache for article titles (prefixed DB keys) and ids linked from one source
28 *
29 * @ingroup Cache
30 */
31 class LinkCache {
32 /**
33 * @var HashBagOStuff
34 */
35 private $mGoodLinks;
36 /**
37 * @var HashBagOStuff
38 */
39 private $mBadLinks;
40 private $mForUpdate = false;
41
42 /**
43 * @var TitleFormatter
44 */
45 private $titleFormatter;
46
47 /**
48 * How many Titles to store. There are two caches, so the amount actually
49 * stored in memory can be up to twice this.
50 */
51 const MAX_SIZE = 10000;
52
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;
57 }
58
59 /**
60 * Get an instance of this class.
61 *
62 * @return LinkCache
63 * @deprecated since 1.28, use MediaWikiServices instead
64 */
65 public static function singleton() {
66 return MediaWikiServices::getInstance()->getLinkCache();
67 }
68
69 /**
70 * General accessor to get/set whether the master DB should be used
71 *
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.
75 *
76 * @param bool $update
77 * @return bool
78 */
79 public function forUpdate( $update = null ) {
80 return wfSetVar( $this->mForUpdate, $update );
81 }
82
83 /**
84 * @param string $title Prefixed DB key
85 * @return int Page ID or zero
86 */
87 public function getGoodLinkID( $title ) {
88 $info = $this->mGoodLinks->get( $title );
89 if ( !$info ) {
90 return 0;
91 }
92 return $info['id'];
93 }
94
95 /**
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
101 */
102 public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
103 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
104 $info = $this->mGoodLinks->get( $dbkey );
105 if ( !$info ) {
106 return null;
107 }
108 return $info[$field];
109 }
110
111 /**
112 * @param string $title Prefixed DB key
113 * @return bool
114 */
115 public function isBadLink( $title ) {
116 // Use get() to ensure it records as used for LRU.
117 return $this->mBadLinks->get( $title ) !== false;
118 }
119
120 /**
121 * Add a link for the title to the link cache
122 *
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
130 */
131 public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
132 $revision = 0, $model = null, $lang = null
133 ) {
134 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
135 $this->mGoodLinks->set( $dbkey, [
136 'id' => (int)$id,
137 'length' => (int)$len,
138 'redirect' => (int)$redir,
139 'revision' => (int)$revision,
140 'model' => $model ? (string)$model : null,
141 'lang' => $lang ? (string)$lang : null,
142 ] );
143 }
144
145 /**
146 * Same as above with better interface.
147 * @since 1.19
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
151 */
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,
161 ] );
162 }
163
164 /**
165 * @param LinkTarget $target
166 */
167 public function addBadLinkObj( LinkTarget $target ) {
168 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
169 if ( !$this->isBadLink( $dbkey ) ) {
170 $this->mBadLinks->set( $dbkey, 1 );
171 }
172 }
173
174 /**
175 * @param string $title Prefixed DB key
176 */
177 public function clearBadLink( $title ) {
178 $this->mBadLinks->delete( $title );
179 }
180
181 /**
182 * @param LinkTarget $target
183 */
184 public function clearLink( LinkTarget $target ) {
185 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
186 $this->mBadLinks->delete( $dbkey );
187 $this->mGoodLinks->delete( $dbkey );
188 }
189
190 /**
191 * Add a title to the link cache, return the page_id or zero if non-existent
192 *
193 * @deprecated since 1.27, unused
194 * @param string $title Prefixed DB key
195 * @return int Page ID or zero
196 */
197 public function addLink( $title ) {
198 $nt = Title::newFromDBkey( $title );
199 if ( !$nt ) {
200 return 0;
201 }
202 return $this->addLinkObj( $nt );
203 }
204
205 /**
206 * Fields that LinkCache needs to select
207 *
208 * @since 1.28
209 * @return array
210 */
211 public static function getSelectFields() {
212 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
213
214 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
215 if ( $wgContentHandlerUseDB ) {
216 $fields[] = 'page_content_model';
217 }
218 if ( $wgPageLanguageUseDB ) {
219 $fields[] = 'page_lang';
220 }
221
222 return $fields;
223 }
224
225 /**
226 * Add a title to the link cache, return the page_id or zero if non-existent
227 *
228 * @param LinkTarget $nt LinkTarget object to add
229 * @return int Page ID or zero
230 */
231 public function addLinkObj( LinkTarget $nt ) {
232 $key = $this->titleFormatter->getPrefixedDBkey( $nt );
233 if ( $this->isBadLink( $key ) || $nt->isExternal()
234 || $nt->inNamespace( NS_SPECIAL )
235 ) {
236 return 0;
237 }
238 $id = $this->getGoodLinkID( $key );
239 if ( $id != 0 ) {
240 return $id;
241 }
242
243 if ( $key === '' ) {
244 return 0;
245 }
246
247 // Some fields heavily used for linking...
248 $db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
249
250 $row = $db->selectRow( 'page', self::getSelectFields(),
251 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
252 __METHOD__
253 );
254
255 if ( $row !== false ) {
256 $this->addGoodLinkObjFromRow( $nt, $row );
257 $id = intval( $row->page_id );
258 } else {
259 $this->addBadLinkObj( $nt );
260 $id = 0;
261 }
262
263 return $id;
264 }
265
266 /**
267 * Clears cache
268 */
269 public function clear() {
270 $this->mGoodLinks->clear();
271 $this->mBadLinks->clear();
272 }
273 }