LinkCache: Use LinkTarget instead of Title
[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 /**
54 * @var LinkCache
55 */
56 protected static $instance;
57
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;
62 }
63
64 /**
65 * Get an instance of this class.
66 *
67 * @return LinkCache
68 */
69 public static function singleton() {
70 if ( !self::$instance ) {
71 self::$instance = new LinkCache(
72 MediaWikiServices::getInstance()->getTitleFormatter()
73 );
74 }
75
76 return self::$instance;
77 }
78
79 /**
80 * Destroy the singleton instance
81 *
82 * A new one will be created next time singleton() is called.
83 *
84 * @since 1.22
85 */
86 public static function destroySingleton() {
87 self::$instance = null;
88 }
89
90 /**
91 * Set the singleton instance to a given object.
92 *
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.
95 *
96 * @param LinkCache $instance
97 * @since 1.22
98 */
99 public static function setSingleton( LinkCache $instance ) {
100 self::$instance = $instance;
101 }
102
103 /**
104 * General accessor to get/set whether the master DB should be used
105 *
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.
109 *
110 * @param bool $update
111 * @return bool
112 */
113 public function forUpdate( $update = null ) {
114 return wfSetVar( $this->mForUpdate, $update );
115 }
116
117 /**
118 * @param string $title Prefixed DB key
119 * @return int Page ID or zero
120 */
121 public function getGoodLinkID( $title ) {
122 $info = $this->mGoodLinks->get( $title );
123 if ( !$info ) {
124 return 0;
125 }
126 return $info['id'];
127 }
128
129 /**
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
135 */
136 public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
137 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
138 $info = $this->mGoodLinks->get( $dbkey );
139 if ( !$info ) {
140 return null;
141 }
142 return $info[$field];
143 }
144
145 /**
146 * @param string $title Prefixed DB key
147 * @return bool
148 */
149 public function isBadLink( $title ) {
150 // Use get() to ensure it records as used for LRU.
151 return $this->mBadLinks->get( $title ) !== false;
152 }
153
154 /**
155 * Add a link for the title to the link cache
156 *
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
164 */
165 public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
166 $revision = 0, $model = null, $lang = null
167 ) {
168 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
169 $this->mGoodLinks->set( $dbkey, [
170 'id' => (int)$id,
171 'length' => (int)$len,
172 'redirect' => (int)$redir,
173 'revision' => (int)$revision,
174 'model' => $model ? (string)$model : null,
175 'lang' => $lang ? (string)$lang : null,
176 ] );
177 }
178
179 /**
180 * Same as above with better interface.
181 * @since 1.19
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
185 */
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,
195 ] );
196 }
197
198 /**
199 * @param LinkTarget $target
200 */
201 public function addBadLinkObj( LinkTarget $target ) {
202 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
203 if ( !$this->isBadLink( $dbkey ) ) {
204 $this->mBadLinks->set( $dbkey, 1 );
205 }
206 }
207
208 /**
209 * @param string $title Prefixed DB key
210 */
211 public function clearBadLink( $title ) {
212 $this->mBadLinks->delete( $title );
213 }
214
215 /**
216 * @param LinkTarget $target
217 */
218 public function clearLink( LinkTarget $target ) {
219 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
220 $this->mBadLinks->delete( $dbkey );
221 $this->mGoodLinks->delete( $dbkey );
222 }
223
224 /**
225 * Add a title to the link cache, return the page_id or zero if non-existent
226 *
227 * @deprecated since 1.27, unused
228 * @param string $title Prefixed DB key
229 * @return int Page ID or zero
230 */
231 public function addLink( $title ) {
232 $nt = Title::newFromDBkey( $title );
233 if ( !$nt ) {
234 return 0;
235 }
236 return $this->addLinkObj( $nt );
237 }
238
239 /**
240 * Add a title to the link cache, return the page_id or zero if non-existent
241 *
242 * @param LinkTarget $nt LinkTarget object to add
243 * @return int Page ID or zero
244 */
245 public function addLinkObj( LinkTarget $nt ) {
246 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
247
248 $key = $this->titleFormatter->getPrefixedDBkey( $nt );
249 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
250 return 0;
251 }
252 $id = $this->getGoodLinkID( $key );
253 if ( $id != 0 ) {
254 return $id;
255 }
256
257 if ( $key === '' ) {
258 return 0;
259 }
260
261 // Some fields heavily used for linking...
262 $db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
263
264 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
265 if ( $wgContentHandlerUseDB ) {
266 $fields[] = 'page_content_model';
267 }
268 if ( $wgPageLanguageUseDB ) {
269 $fields[] = 'page_lang';
270 }
271
272 $row = $db->selectRow( 'page', $fields,
273 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
274 __METHOD__
275 );
276
277 if ( $row !== false ) {
278 $this->addGoodLinkObjFromRow( $nt, $row );
279 $id = intval( $row->page_id );
280 } else {
281 $this->addBadLinkObj( $nt );
282 $id = 0;
283 }
284
285 return $id;
286 }
287
288 /**
289 * Clears cache
290 */
291 public function clear() {
292 $this->mGoodLinks->clear();
293 $this->mBadLinks->clear();
294 }
295 }