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
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
30 // Increment $mClassVer whenever old serialized versions of this class
31 // becomes incompatible with the new version.
32 private $mClassVer = 5;
42 private $mForUpdate = false;
45 * How many Titles to store. There are two caches, so the amount actually
46 * stored in memory can be up to twice this.
48 const MAX_SIZE
= 10000;
53 protected static $instance;
55 public function __construct() {
56 $this->mGoodLinks
= new MapCacheLRU( self
::MAX_SIZE
);
57 $this->mBadLinks
= new MapCacheLRU( self
::MAX_SIZE
);
61 * Get an instance of this class.
65 static function &singleton() {
66 if ( self
::$instance ) {
67 return self
::$instance;
69 self
::$instance = new LinkCache
;
71 return self
::$instance;
75 * Destroy the singleton instance, a new one will be created next time
76 * singleton() is called.
79 static function destroySingleton() {
80 self
::$instance = null;
84 * Set the singleton instance to a given object.
85 * Since we do not have an interface for LinkCache, you have to be sure the
86 * given object implements all the LinkCache public methods.
87 * @param LinkCache $instance
90 static function setSingleton( LinkCache
$instance ) {
91 self
::$instance = $instance;
95 * General accessor to get/set whether SELECT FOR UPDATE should be used
100 public function forUpdate( $update = null ) {
101 return wfSetVar( $this->mForUpdate
, $update );
105 * @param string $title
108 public function getGoodLinkID( $title ) {
109 if ( $this->mGoodLinks
->has( $title ) ) {
110 $info = $this->mGoodLinks
->get( $title );
118 * Get a field of a title object from cache.
119 * If this link is not good, it will return NULL.
120 * @param Title $title
121 * @param string $field ('length','redirect','revision','model')
122 * @return string|null
124 public function getGoodLinkFieldObj( $title, $field ) {
125 $dbkey = $title->getPrefixedDBkey();
126 if ( $this->mGoodLinks
->has( $dbkey ) ) {
127 $info = $this->mGoodLinks
->get( $dbkey );
128 return $info[$field];
135 * @param string $title
138 public function isBadLink( $title ) {
139 // We need to use get here since has will not call ping.
140 return $this->mBadLinks
->get( $title ) !== null;
144 * Add a link for the title to the link cache
146 * @param int $id Page's ID
147 * @param Title $title
148 * @param int $len Text's length
149 * @param int $redir Whether the page is a redirect
150 * @param int $revision Latest revision's ID
151 * @param string|null $model Latest revision's content model ID
153 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
154 $revision = 0, $model = null
156 $dbkey = $title->getPrefixedDBkey();
157 $this->mGoodLinks
->set( $dbkey, array(
159 'length' => (int)$len,
160 'redirect' => (int)$redir,
161 'revision' => (int)$revision,
162 'model' => $model ?
(string)$model : null,
167 * Same as above with better interface.
169 * @param Title $title
170 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
171 * page_latest and page_content_model
173 public function addGoodLinkObjFromRow( $title, $row ) {
174 $dbkey = $title->getPrefixedDBkey();
175 $this->mGoodLinks
->set( $dbkey, array(
176 'id' => intval( $row->page_id
),
177 'length' => intval( $row->page_len
),
178 'redirect' => intval( $row->page_is_redirect
),
179 'revision' => intval( $row->page_latest
),
180 'model' => !empty( $row->page_content_model
) ?
strval( $row->page_content_model
) : null,
185 * @param Title $title
187 public function addBadLinkObj( $title ) {
188 $dbkey = $title->getPrefixedDBkey();
189 if ( !$this->isBadLink( $dbkey ) ) {
190 $this->mBadLinks
->set( $dbkey, 1 );
194 public function clearBadLink( $title ) {
195 $this->mBadLinks
->clear( array( $title ) );
199 * @param Title $title
201 public function clearLink( $title ) {
202 $dbkey = $title->getPrefixedDBkey();
203 $this->mBadLinks
->clear( array( $dbkey ) );
204 $this->mGoodLinks
->clear( array( $dbkey ) );
209 * @deprecated since 1.26
212 public function getGoodLinks() {
213 wfDeprecated( __METHOD__
, '1.26' );
215 foreach ( $this->mGoodLinks
->getAllKeys() as $key ) {
216 $info = $this->mGoodLinks
->get( $key );
217 $links[$key] = $info['id'];
224 * @deprecated since 1.26
227 public function getBadLinks() {
228 wfDeprecated( __METHOD__
, '1.26' );
229 return $this->mBadLinks
->getAllKeys();
233 * Add a title to the link cache, return the page_id or zero if non-existent
235 * @param string $title Title to add
238 public function addLink( $title ) {
239 $nt = Title
::newFromDBkey( $title );
241 return $this->addLinkObj( $nt );
248 * Add a title to the link cache, return the page_id or zero if non-existent
250 * @param Title $nt Title object to add
253 public function addLinkObj( $nt ) {
254 global $wgContentHandlerUseDB;
256 $key = $nt->getPrefixedDBkey();
257 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
260 $id = $this->getGoodLinkID( $key );
269 # Some fields heavily used for linking...
270 if ( $this->mForUpdate
) {
271 $db = wfGetDB( DB_MASTER
);
273 $db = wfGetDB( DB_SLAVE
);
276 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
277 if ( $wgContentHandlerUseDB ) {
278 $f[] = 'page_content_model';
281 $s = $db->selectRow( 'page', $f,
282 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
285 if ( $s !== false ) {
286 $this->addGoodLinkObjFromRow( $nt, $s );
287 $id = intval( $s->page_id
);
289 $this->addBadLinkObj( $nt );
299 public function clear() {
300 $this->mGoodLinks
->clear();
301 $this->mBadLinks
->clear();