Use the frame 'type' in wfFormatStackFrame, like MWExceptionHandler::prettyPrintTrace
[mediawiki.git] / includes / cache / LinkCache.php
blob3db84a6e752488fbcc2d2c53de07c366b36b3e2f
1 <?php
2 /**
3 * Page existence cache.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
27 * @ingroup Cache
29 class LinkCache {
30 /**
31 * @var MapCacheLRU
33 private $mGoodLinks;
34 /**
35 * @var MapCacheLRU
37 private $mBadLinks;
38 private $mForUpdate = false;
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.
44 const MAX_SIZE = 10000;
46 /**
47 * @var LinkCache
49 protected static $instance;
51 public function __construct() {
52 $this->mGoodLinks = new MapCacheLRU( self::MAX_SIZE );
53 $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
56 /**
57 * Get an instance of this class.
59 * @return LinkCache
61 static function &singleton() {
62 if ( self::$instance ) {
63 return self::$instance;
65 self::$instance = new LinkCache;
67 return self::$instance;
70 /**
71 * Destroy the singleton instance, a new one will be created next time
72 * singleton() is called.
73 * @since 1.22
75 static function destroySingleton() {
76 self::$instance = null;
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
86 static function setSingleton( LinkCache $instance ) {
87 self::$instance = $instance;
90 /**
91 * General accessor to get/set whether SELECT FOR UPDATE should be used
93 * @param bool $update
94 * @return bool
96 public function forUpdate( $update = null ) {
97 return wfSetVar( $this->mForUpdate, $update );
101 * @param string $title
102 * @return int
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;
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
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;
131 * @param string $title
132 * @return bool
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;
140 * Add a link for the title to the link cache
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
149 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
150 $revision = 0, $model = null
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 ) );
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
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 ) );
181 * @param Title $title
183 public function addBadLinkObj( $title ) {
184 $dbkey = $title->getPrefixedDBkey();
185 if ( !$this->isBadLink( $dbkey ) ) {
186 $this->mBadLinks->set( $dbkey, 1 );
190 public function clearBadLink( $title ) {
191 $this->mBadLinks->clear( array( $title ) );
195 * @param Title $title
197 public function clearLink( $title ) {
198 $dbkey = $title->getPrefixedDBkey();
199 $this->mBadLinks->clear( array( $dbkey ) );
200 $this->mGoodLinks->clear( array( $dbkey ) );
205 * @deprecated since 1.26
206 * @return array
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'];
216 return $links;
220 * @deprecated since 1.26
221 * @return array
223 public function getBadLinks() {
224 wfDeprecated( __METHOD__, '1.26' );
225 return $this->mBadLinks->getAllKeys();
229 * Add a title to the link cache, return the page_id or zero if non-existent
231 * @param string $title Title to add
232 * @return int
234 public function addLink( $title ) {
235 $nt = Title::newFromDBkey( $title );
236 if ( $nt ) {
237 return $this->addLinkObj( $nt );
238 } else {
239 return 0;
244 * Add a title to the link cache, return the page_id or zero if non-existent
246 * @param Title $nt Title object to add
247 * @return int
249 public function addLinkObj( $nt ) {
250 global $wgContentHandlerUseDB;
252 $key = $nt->getPrefixedDBkey();
253 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
254 return 0;
256 $id = $this->getGoodLinkID( $key );
257 if ( $id != 0 ) {
258 return $id;
261 if ( $key === '' ) {
262 return 0;
265 # Some fields heavily used for linking...
266 if ( $this->mForUpdate ) {
267 $db = wfGetDB( DB_MASTER );
268 } else {
269 $db = wfGetDB( DB_SLAVE );
272 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
273 if ( $wgContentHandlerUseDB ) {
274 $f[] = 'page_content_model';
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;
289 return $id;
293 * Clears cache
295 public function clear() {
296 $this->mGoodLinks->clear();
297 $this->mBadLinks->clear();