Merge "Tweaked WikiPage::clear() comment a bit"
[mediawiki.git] / includes / cache / LinkCache.php
blob31d79f4ee1b801399d852610daf25bf0cfea06d0
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 // Increment $mClassVer whenever old serialized versions of this class
31 // becomes incompatible with the new version.
32 private $mClassVer = 4;
34 private $mGoodLinks = array();
35 private $mGoodLinkFields = array();
36 private $mBadLinks = array();
37 private $mForUpdate = false;
39 /**
40 * @var LinkCache
42 protected static $instance;
44 /**
45 * Get an instance of this class.
47 * @return LinkCache
49 static function &singleton() {
50 if ( self::$instance ) {
51 return self::$instance;
53 self::$instance = new LinkCache;
55 return self::$instance;
58 /**
59 * Destroy the singleton instance, a new one will be created next time
60 * singleton() is called.
61 * @since 1.22
63 static function destroySingleton() {
64 self::$instance = null;
67 /**
68 * Set the singleton instance to a given object.
69 * Since we do not have an interface for LinkCache, you have to be sure the
70 * given object implements all the LinkCache public methods.
71 * @param LinkCache $instance
72 * @since 1.22
74 static function setSingleton( LinkCache $instance ) {
75 self::$instance = $instance;
78 /**
79 * General accessor to get/set whether SELECT FOR UPDATE should be used
81 * @return bool
83 public function forUpdate( $update = null ) {
84 return wfSetVar( $this->mForUpdate, $update );
87 /**
88 * @param $title
89 * @return array|int
91 public function getGoodLinkID( $title ) {
92 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
93 return $this->mGoodLinks[$title];
94 } else {
95 return 0;
99 /**
100 * Get a field of a title object from cache.
101 * If this link is not good, it will return NULL.
102 * @param $title Title
103 * @param string $field ('length','redirect','revision','model')
104 * @return mixed
106 public function getGoodLinkFieldObj( $title, $field ) {
107 $dbkey = $title->getPrefixedDBkey();
108 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
109 return $this->mGoodLinkFields[$dbkey][$field];
110 } else {
111 return null;
116 * @param $title
117 * @return bool
119 public function isBadLink( $title ) {
120 return array_key_exists( $title, $this->mBadLinks );
124 * Add a link for the title to the link cache
126 * @param $id Integer: page's ID
127 * @param $title Title object
128 * @param $len Integer: text's length
129 * @param $redir Integer: whether the page is a redirect
130 * @param $revision Integer: latest revision's ID
131 * @param $model Integer: latest revision's content model ID
133 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) {
134 $dbkey = $title->getPrefixedDBkey();
135 $this->mGoodLinks[$dbkey] = intval( $id );
136 $this->mGoodLinkFields[$dbkey] = array(
137 'length' => intval( $len ),
138 'redirect' => intval( $redir ),
139 'revision' => intval( $revision ),
140 'model' => intval( $model ) );
144 * Same as above with better interface.
145 * @since 1.19
146 * @param $title Title
147 * @param $row object which has the fields page_id, page_is_redirect,
148 * page_latest and page_content_model
150 public function addGoodLinkObjFromRow( $title, $row ) {
151 $dbkey = $title->getPrefixedDBkey();
152 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
153 $this->mGoodLinkFields[$dbkey] = array(
154 'length' => intval( $row->page_len ),
155 'redirect' => intval( $row->page_is_redirect ),
156 'revision' => intval( $row->page_latest ),
157 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
162 * @param $title Title
164 public function addBadLinkObj( $title ) {
165 $dbkey = $title->getPrefixedDBkey();
166 if ( !$this->isBadLink( $dbkey ) ) {
167 $this->mBadLinks[$dbkey] = 1;
171 public function clearBadLink( $title ) {
172 unset( $this->mBadLinks[$title] );
176 * @param $title Title
178 public function clearLink( $title ) {
179 $dbkey = $title->getPrefixedDBkey();
180 unset( $this->mBadLinks[$dbkey] );
181 unset( $this->mGoodLinks[$dbkey] );
182 unset( $this->mGoodLinkFields[$dbkey] );
185 public function getGoodLinks() {
186 return $this->mGoodLinks;
189 public function getBadLinks() {
190 return array_keys( $this->mBadLinks );
194 * Add a title to the link cache, return the page_id or zero if non-existent
196 * @param string $title title to add
197 * @return Integer
199 public function addLink( $title ) {
200 $nt = Title::newFromDBkey( $title );
201 if ( $nt ) {
202 return $this->addLinkObj( $nt );
203 } else {
204 return 0;
209 * Add a title to the link cache, return the page_id or zero if non-existent
211 * @param $nt Title object to add
212 * @return Integer
214 public function addLinkObj( $nt ) {
215 global $wgAntiLockFlags, $wgContentHandlerUseDB;
217 wfProfileIn( __METHOD__ );
219 $key = $nt->getPrefixedDBkey();
220 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
221 wfProfileOut( __METHOD__ );
223 return 0;
225 $id = $this->getGoodLinkID( $key );
226 if ( $id != 0 ) {
227 wfProfileOut( __METHOD__ );
229 return $id;
232 if ( $key === '' ) {
233 wfProfileOut( __METHOD__ );
235 return 0;
238 # Some fields heavily used for linking...
239 if ( $this->mForUpdate ) {
240 $db = wfGetDB( DB_MASTER );
241 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
242 $options = array( 'FOR UPDATE' );
243 } else {
244 $options = array();
246 } else {
247 $db = wfGetDB( DB_SLAVE );
248 $options = array();
251 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
252 if ( $wgContentHandlerUseDB ) {
253 $f[] = 'page_content_model';
256 $s = $db->selectRow( 'page', $f,
257 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
258 __METHOD__, $options );
259 # Set fields...
260 if ( $s !== false ) {
261 $this->addGoodLinkObjFromRow( $nt, $s );
262 $id = intval( $s->page_id );
263 } else {
264 $this->addBadLinkObj( $nt );
265 $id = 0;
268 wfProfileOut( __METHOD__ );
270 return $id;
274 * Clears cache
276 public function clear() {
277 $this->mGoodLinks = array();
278 $this->mGoodLinkFields = array();
279 $this->mBadLinks = array();