Merge "Add option to expose original sha1 in thumb url"
[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 MapCacheLRU
32 */
33 private $mGoodLinks;
34 /**
35 * @var MapCacheLRU
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 MapCacheLRU( self::MAX_SIZE );
53 $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
54 }
55
56 /**
57 * Get an instance of this class.
58 *
59 * @return LinkCache
60 */
61 static function &singleton() {
62 if ( self::$instance ) {
63 return self::$instance;
64 }
65 self::$instance = new LinkCache;
66
67 return self::$instance;
68 }
69
70 /**
71 * Destroy the singleton instance, a new one will be created next time
72 * singleton() is called.
73 * @since 1.22
74 */
75 static function destroySingleton() {
76 self::$instance = null;
77 }
78
79 /**
80 * Set the singleton instance to a given object.
81 * Since we do not have an interface for LinkCache, you have to be sure the
82 * given object implements all the LinkCache public methods.
83 * @param LinkCache $instance
84 * @since 1.22
85 */
86 static function setSingleton( LinkCache $instance ) {
87 self::$instance = $instance;
88 }
89
90 /**
91 * General accessor to get/set whether SELECT FOR UPDATE should be used
92 *
93 * @param bool $update
94 * @return bool
95 */
96 public function forUpdate( $update = null ) {
97 return wfSetVar( $this->mForUpdate, $update );
98 }
99
100 /**
101 * @param string $title
102 * @return int
103 */
104 public function getGoodLinkID( $title ) {
105 if ( $this->mGoodLinks->has( $title ) ) {
106 $info = $this->mGoodLinks->get( $title );
107 return $info['id'];
108 } else {
109 return 0;
110 }
111 }
112
113 /**
114 * Get a field of a title object from cache.
115 * If this link is not good, it will return NULL.
116 * @param Title $title
117 * @param string $field ('length','redirect','revision','model')
118 * @return string|null
119 */
120 public function getGoodLinkFieldObj( $title, $field ) {
121 $dbkey = $title->getPrefixedDBkey();
122 if ( $this->mGoodLinks->has( $dbkey ) ) {
123 $info = $this->mGoodLinks->get( $dbkey );
124 return $info[$field];
125 } else {
126 return null;
127 }
128 }
129
130 /**
131 * @param string $title
132 * @return bool
133 */
134 public function isBadLink( $title ) {
135 // We need to use get here since has will not call ping.
136 return $this->mBadLinks->get( $title ) !== null;
137 }
138
139 /**
140 * Add a link for the title to the link cache
141 *
142 * @param int $id Page's ID
143 * @param Title $title
144 * @param int $len Text's length
145 * @param int $redir Whether the page is a redirect
146 * @param int $revision Latest revision's ID
147 * @param string|null $model Latest revision's content model ID
148 */
149 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
150 $revision = 0, $model = null
151 ) {
152 $dbkey = $title->getPrefixedDBkey();
153 $this->mGoodLinks->set( $dbkey, array(
154 'id' => (int)$id,
155 'length' => (int)$len,
156 'redirect' => (int)$redir,
157 'revision' => (int)$revision,
158 'model' => $model ? (string)$model : null,
159 ) );
160 }
161
162 /**
163 * Same as above with better interface.
164 * @since 1.19
165 * @param Title $title
166 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
167 * page_latest and page_content_model
168 */
169 public function addGoodLinkObjFromRow( $title, $row ) {
170 $dbkey = $title->getPrefixedDBkey();
171 $this->mGoodLinks->set( $dbkey, array(
172 'id' => intval( $row->page_id ),
173 'length' => intval( $row->page_len ),
174 'redirect' => intval( $row->page_is_redirect ),
175 'revision' => intval( $row->page_latest ),
176 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
177 ) );
178 }
179
180 /**
181 * @param Title $title
182 */
183 public function addBadLinkObj( $title ) {
184 $dbkey = $title->getPrefixedDBkey();
185 if ( !$this->isBadLink( $dbkey ) ) {
186 $this->mBadLinks->set( $dbkey, 1 );
187 }
188 }
189
190 public function clearBadLink( $title ) {
191 $this->mBadLinks->clear( array( $title ) );
192 }
193
194 /**
195 * @param Title $title
196 */
197 public function clearLink( $title ) {
198 $dbkey = $title->getPrefixedDBkey();
199 $this->mBadLinks->clear( array( $dbkey ) );
200 $this->mGoodLinks->clear( array( $dbkey ) );
201 }
202
203
204 /**
205 * @deprecated since 1.26
206 * @return array
207 */
208 public function getGoodLinks() {
209 wfDeprecated( __METHOD__, '1.26' );
210 $links = array();
211 foreach ( $this->mGoodLinks->getAllKeys() as $key ) {
212 $info = $this->mGoodLinks->get( $key );
213 $links[$key] = $info['id'];
214 }
215
216 return $links;
217 }
218
219 /**
220 * @deprecated since 1.26
221 * @return array
222 */
223 public function getBadLinks() {
224 wfDeprecated( __METHOD__, '1.26' );
225 return $this->mBadLinks->getAllKeys();
226 }
227
228 /**
229 * Add a title to the link cache, return the page_id or zero if non-existent
230 *
231 * @param string $title Title to add
232 * @return int
233 */
234 public function addLink( $title ) {
235 $nt = Title::newFromDBkey( $title );
236 if ( $nt ) {
237 return $this->addLinkObj( $nt );
238 } else {
239 return 0;
240 }
241 }
242
243 /**
244 * Add a title to the link cache, return the page_id or zero if non-existent
245 *
246 * @param Title $nt Title object to add
247 * @return int
248 */
249 public function addLinkObj( $nt ) {
250 global $wgContentHandlerUseDB;
251
252 $key = $nt->getPrefixedDBkey();
253 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
254 return 0;
255 }
256 $id = $this->getGoodLinkID( $key );
257 if ( $id != 0 ) {
258 return $id;
259 }
260
261 if ( $key === '' ) {
262 return 0;
263 }
264
265 # Some fields heavily used for linking...
266 if ( $this->mForUpdate ) {
267 $db = wfGetDB( DB_MASTER );
268 } else {
269 $db = wfGetDB( DB_SLAVE );
270 }
271
272 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
273 if ( $wgContentHandlerUseDB ) {
274 $f[] = 'page_content_model';
275 }
276
277 $s = $db->selectRow( 'page', $f,
278 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
279 __METHOD__ );
280 # Set fields...
281 if ( $s !== false ) {
282 $this->addGoodLinkObjFromRow( $nt, $s );
283 $id = intval( $s->page_id );
284 } else {
285 $this->addBadLinkObj( $nt );
286 $id = 0;
287 }
288
289 return $id;
290 }
291
292 /**
293 * Clears cache
294 */
295 public function clear() {
296 $this->mGoodLinks->clear();
297 $this->mBadLinks->clear();
298 }
299 }