276e8188249129698910a8ccaf2f36806f48b9f0
[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
24 /**
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
26 *
27 * @ingroup Cache
28 */
29 class LinkCache {
30 /**
31 * @var HashBagOStuff
32 */
33 private $mGoodLinks;
34 /**
35 * @var HashBagOStuff
36 */
37 private $mBadLinks;
38 private $mForUpdate = false;
39
40 /**
41 * How many Titles to store. There are two caches, so the amount actually
42 * stored in memory can be up to twice this.
43 */
44 const MAX_SIZE = 10000;
45
46 /**
47 * @var LinkCache
48 */
49 protected static $instance;
50
51 public function __construct() {
52 $this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
53 $this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
54 }
55
56 /**
57 * Get an instance of this class.
58 *
59 * @return LinkCache
60 */
61 public static function &singleton() {
62 if ( !self::$instance ) {
63 self::$instance = new LinkCache;
64 }
65
66 return self::$instance;
67 }
68
69 /**
70 * Destroy the singleton instance
71 *
72 * A new one will be created next time singleton() is called.
73 *
74 * @since 1.22
75 */
76 public static function destroySingleton() {
77 self::$instance = null;
78 }
79
80 /**
81 * Set the singleton instance to a given object.
82 *
83 * Since we do not have an interface for LinkCache, you have to be sure the
84 * given object implements all the LinkCache public methods.
85 *
86 * @param LinkCache $instance
87 * @since 1.22
88 */
89 public static function setSingleton( LinkCache $instance ) {
90 self::$instance = $instance;
91 }
92
93 /**
94 * General accessor to get/set whether the master DB should be used
95 *
96 * This used to also set the FOR UPDATE option (locking the rows read
97 * in order to avoid link table inconsistency), which was later removed
98 * for performance on wikis with a high edit rate.
99 *
100 * @param bool $update
101 * @return bool
102 */
103 public function forUpdate( $update = null ) {
104 return wfSetVar( $this->mForUpdate, $update );
105 }
106
107 /**
108 * @param string $title
109 * @return int Page ID or zero
110 */
111 public function getGoodLinkID( $title ) {
112 $info = $this->mGoodLinks->get( $title );
113 if ( !$info ) {
114 return 0;
115 }
116 return $info['id'];
117 }
118
119 /**
120 * Get a field of a title object from cache.
121 * If this link is not a cached good title, it will return NULL.
122 * @param Title $title
123 * @param string $field ('length','redirect','revision','model')
124 * @return string|int|null
125 */
126 public function getGoodLinkFieldObj( $title, $field ) {
127 $dbkey = $title->getPrefixedDBkey();
128 $info = $this->mGoodLinks->get( $dbkey );
129 if ( !$info ) {
130 return null;
131 }
132 return $info[$field];
133 }
134
135 /**
136 * @param string $title
137 * @return bool
138 */
139 public function isBadLink( $title ) {
140 // Use get() to ensure it records as used for LRU.
141 return $this->mBadLinks->get( $title ) !== false;
142 }
143
144 /**
145 * Add a link for the title to the link cache
146 *
147 * @param int $id Page's ID
148 * @param Title $title
149 * @param int $len Text's length
150 * @param int $redir Whether the page is a redirect
151 * @param int $revision Latest revision's ID
152 * @param string|null $model Latest revision's content model ID
153 * @param string|null $lang Language code of the page, if not the content language
154 */
155 public function addGoodLinkObj( $id, Title $title, $len = -1, $redir = null,
156 $revision = 0, $model = null, $lang = null
157 ) {
158 $dbkey = $title->getPrefixedDBkey();
159 $this->mGoodLinks->set( $dbkey, [
160 'id' => (int)$id,
161 'length' => (int)$len,
162 'redirect' => (int)$redir,
163 'revision' => (int)$revision,
164 'model' => $model ? (string)$model : null,
165 'lang' => $lang ? (string)$lang : null,
166 ] );
167 }
168
169 /**
170 * Same as above with better interface.
171 * @since 1.19
172 * @param Title $title
173 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
174 * page_latest and page_content_model
175 */
176 public function addGoodLinkObjFromRow( Title $title, $row ) {
177 $dbkey = $title->getPrefixedDBkey();
178 $this->mGoodLinks->set( $dbkey, [
179 'id' => intval( $row->page_id ),
180 'length' => intval( $row->page_len ),
181 'redirect' => intval( $row->page_is_redirect ),
182 'revision' => intval( $row->page_latest ),
183 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
184 'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
185 ] );
186 }
187
188 /**
189 * @param Title $title
190 */
191 public function addBadLinkObj( Title $title ) {
192 $dbkey = $title->getPrefixedDBkey();
193 if ( !$this->isBadLink( $dbkey ) ) {
194 $this->mBadLinks->set( $dbkey, 1 );
195 }
196 }
197
198 /**
199 * @param string $title prefixed dbkey
200 */
201 public function clearBadLink( $title ) {
202 $this->mBadLinks->delete( $title );
203 }
204
205 /**
206 * @param Title $title
207 */
208 public function clearLink( $title ) {
209 $dbkey = $title->getPrefixedDBkey();
210 $this->mBadLinks->delete( $dbkey );
211 $this->mGoodLinks->delete( $dbkey );
212 }
213
214 /**
215 * Add a title to the link cache, return the page_id or zero if non-existent
216 *
217 * @param string $title Title to add
218 * @return int Page ID or zero
219 */
220 public function addLink( $title ) {
221 $nt = Title::newFromDBkey( $title );
222 if ( !$nt ) {
223 return 0;
224 }
225 return $this->addLinkObj( $nt );
226 }
227
228 /**
229 * Add a title to the link cache, return the page_id or zero if non-existent
230 *
231 * @param Title $nt Title object to add
232 * @return int Page ID or zero
233 */
234 public function addLinkObj( Title $nt ) {
235 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
236
237 $key = $nt->getPrefixedDBkey();
238 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
239 return 0;
240 }
241 $id = $this->getGoodLinkID( $key );
242 if ( $id != 0 ) {
243 return $id;
244 }
245
246 if ( $key === '' ) {
247 return 0;
248 }
249
250 // Some fields heavily used for linking...
251 $db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
252
253 $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
254 if ( $wgContentHandlerUseDB ) {
255 $fields[] = 'page_content_model';
256 }
257 if ( $wgPageLanguageUseDB ) {
258 $fields[] = 'page_lang';
259 }
260
261 $row = $db->selectRow( 'page', $fields,
262 [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
263 __METHOD__
264 );
265
266 if ( $row !== false ) {
267 $this->addGoodLinkObjFromRow( $nt, $row );
268 $id = intval( $row->page_id );
269 } else {
270 $this->addBadLinkObj( $nt );
271 $id = 0;
272 }
273
274 return $id;
275 }
276
277 /**
278 * Clears cache
279 */
280 public function clear() {
281 $this->mGoodLinks->clear();
282 $this->mBadLinks->clear();
283 }
284 }