Add LinkCache to MediaWikiServices
[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 * @deprecated since 1.28, use MediaWikiServices instead
69 */
70 public static function singleton() {
71 return MediaWikiServices::getInstance()->getLinkCache();
72 }
73
74 /**
75 * General accessor to get/set whether the master DB should be used
76 *
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.
80 *
81 * @param bool $update
82 * @return bool
83 */
84 public function forUpdate( $update = null ) {
85 return wfSetVar( $this->mForUpdate, $update );
86 }
87
88 /**
89 * @param string $title Prefixed DB key
90 * @return int Page ID or zero
91 */
92 public function getGoodLinkID( $title ) {
93 $info = $this->mGoodLinks->get( $title );
94 if ( !$info ) {
95 return 0;
96 }
97 return $info['id'];
98 }
99
100 /**
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
106 */
107 public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
108 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
109 $info = $this->mGoodLinks->get( $dbkey );
110 if ( !$info ) {
111 return null;
112 }
113 return $info[$field];
114 }
115
116 /**
117 * @param string $title Prefixed DB key
118 * @return bool
119 */
120 public function isBadLink( $title ) {
121 // Use get() to ensure it records as used for LRU.
122 return $this->mBadLinks->get( $title ) !== false;
123 }
124
125 /**
126 * Add a link for the title to the link cache
127 *
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
135 */
136 public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
137 $revision = 0, $model = null, $lang = null
138 ) {
139 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
140 $this->mGoodLinks->set( $dbkey, [
141 'id' => (int)$id,
142 'length' => (int)$len,
143 'redirect' => (int)$redir,
144 'revision' => (int)$revision,
145 'model' => $model ? (string)$model : null,
146 'lang' => $lang ? (string)$lang : null,
147 ] );
148 }
149
150 /**
151 * Same as above with better interface.
152 * @since 1.19
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
156 */
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,
166 ] );
167 }
168
169 /**
170 * @param LinkTarget $target
171 */
172 public function addBadLinkObj( LinkTarget $target ) {
173 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
174 if ( !$this->isBadLink( $dbkey ) ) {
175 $this->mBadLinks->set( $dbkey, 1 );
176 }
177 }
178
179 /**
180 * @param string $title Prefixed DB key
181 */
182 public function clearBadLink( $title ) {
183 $this->mBadLinks->delete( $title );
184 }
185
186 /**
187 * @param LinkTarget $target
188 */
189 public function clearLink( LinkTarget $target ) {
190 $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
191 $this->mBadLinks->delete( $dbkey );
192 $this->mGoodLinks->delete( $dbkey );
193 }
194
195 /**
196 * Add a title to the link cache, return the page_id or zero if non-existent
197 *
198 * @deprecated since 1.27, unused
199 * @param string $title Prefixed DB key
200 * @return int Page ID or zero
201 */
202 public function addLink( $title ) {
203 $nt = Title::newFromDBkey( $title );
204 if ( !$nt ) {
205 return 0;
206 }
207 return $this->addLinkObj( $nt );
208 }
209
210 /**
211 * Add a title to the link cache, return the page_id or zero if non-existent
212 *
213 * @param LinkTarget $nt LinkTarget object to add
214 * @return int Page ID or zero
215 */
216 public function addLinkObj( LinkTarget $nt ) {
217 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
218
219 $key = $this->titleFormatter->getPrefixedDBkey( $nt );
220 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
221 return 0;
222 }
223 $id = $this->getGoodLinkID( $key );
224 if ( $id != 0 ) {
225 return $id;
226 }
227
228 if ( $key === '' ) {
229 return 0;
230 }
231
232 // Some fields heavily used for linking...
233 $db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
234
235 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
236 if ( $wgContentHandlerUseDB ) {
237 $fields[] = 'page_content_model';
238 }
239 if ( $wgPageLanguageUseDB ) {
240 $fields[] = 'page_lang';
241 }
242
243 $row = $db->selectRow( 'page', $fields,
244 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
245 __METHOD__
246 );
247
248 if ( $row !== false ) {
249 $this->addGoodLinkObjFromRow( $nt, $row );
250 $id = intval( $row->page_id );
251 } else {
252 $this->addBadLinkObj( $nt );
253 $id = 0;
254 }
255
256 return $id;
257 }
258
259 /**
260 * Clears cache
261 */
262 public function clear() {
263 $this->mGoodLinks->clear();
264 $this->mBadLinks->clear();
265 }
266 }