Merge "Typo fix"
[mediawiki.git] / includes / cache / LinkCache.php
blob6ac7e7abe460712dc734f463dc2e5057ad06d7c9
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;
54 return self::$instance;
57 /**
58 * Destroy the singleton instance, a new one will be created next time
59 * singleton() is called.
60 * @since 1.22
62 static function destroySingleton() {
63 self::$instance = null;
66 /**
67 * Set the singleton instance to a given object.
68 * Since we do not have an interface for LinkCache, you have to be sure the
69 * given object implements all the LinkCache public methods.
70 * @param LinkCache $instance
71 * @since 1.22
73 static function setSingleton( LinkCache $instance ) {
74 self::$instance = $instance;
77 /**
78 * General accessor to get/set whether SELECT FOR UPDATE should be used
80 * @return bool
82 public function forUpdate( $update = null ) {
83 return wfSetVar( $this->mForUpdate, $update );
86 /**
87 * @param $title
88 * @return array|int
90 public function getGoodLinkID( $title ) {
91 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
92 return $this->mGoodLinks[$title];
93 } else {
94 return 0;
98 /**
99 * Get a field of a title object from cache.
100 * If this link is not good, it will return NULL.
101 * @param $title Title
102 * @param string $field ('length','redirect','revision','model')
103 * @return mixed
105 public function getGoodLinkFieldObj( $title, $field ) {
106 $dbkey = $title->getPrefixedDBkey();
107 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
108 return $this->mGoodLinkFields[$dbkey][$field];
109 } else {
110 return null;
115 * @param $title
116 * @return bool
118 public function isBadLink( $title ) {
119 return array_key_exists( $title, $this->mBadLinks );
123 * Add a link for the title to the link cache
125 * @param $id Integer: page's ID
126 * @param $title Title object
127 * @param $len Integer: text's length
128 * @param $redir Integer: whether the page is a redirect
129 * @param $revision Integer: latest revision's ID
130 * @param $model Integer: latest revision's content model ID
132 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) {
133 $dbkey = $title->getPrefixedDBkey();
134 $this->mGoodLinks[$dbkey] = intval( $id );
135 $this->mGoodLinkFields[$dbkey] = array(
136 'length' => intval( $len ),
137 'redirect' => intval( $redir ),
138 'revision' => intval( $revision ),
139 'model' => intval( $model ) );
143 * Same as above with better interface.
144 * @since 1.19
145 * @param $title Title
146 * @param $row object which has the fields page_id, page_is_redirect,
147 * page_latest and page_content_model
149 public function addGoodLinkObjFromRow( $title, $row ) {
150 $dbkey = $title->getPrefixedDBkey();
151 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
152 $this->mGoodLinkFields[$dbkey] = array(
153 'length' => intval( $row->page_len ),
154 'redirect' => intval( $row->page_is_redirect ),
155 'revision' => intval( $row->page_latest ),
156 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
161 * @param $title Title
163 public function addBadLinkObj( $title ) {
164 $dbkey = $title->getPrefixedDBkey();
165 if ( !$this->isBadLink( $dbkey ) ) {
166 $this->mBadLinks[$dbkey] = 1;
170 public function clearBadLink( $title ) {
171 unset( $this->mBadLinks[$title] );
175 * @param $title Title
177 public function clearLink( $title ) {
178 $dbkey = $title->getPrefixedDBkey();
179 unset( $this->mBadLinks[$dbkey] );
180 unset( $this->mGoodLinks[$dbkey] );
181 unset( $this->mGoodLinkFields[$dbkey] );
184 public function getGoodLinks() {
185 return $this->mGoodLinks;
188 public function getBadLinks() {
189 return array_keys( $this->mBadLinks );
193 * Add a title to the link cache, return the page_id or zero if non-existent
195 * @param string $title title to add
196 * @return Integer
198 public function addLink( $title ) {
199 $nt = Title::newFromDBkey( $title );
200 if ( $nt ) {
201 return $this->addLinkObj( $nt );
202 } else {
203 return 0;
208 * Add a title to the link cache, return the page_id or zero if non-existent
210 * @param $nt Title object to add
211 * @return Integer
213 public function addLinkObj( $nt ) {
214 global $wgAntiLockFlags, $wgContentHandlerUseDB;
216 wfProfileIn( __METHOD__ );
218 $key = $nt->getPrefixedDBkey();
219 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
220 wfProfileOut( __METHOD__ );
221 return 0;
223 $id = $this->getGoodLinkID( $key );
224 if ( $id != 0 ) {
225 wfProfileOut( __METHOD__ );
226 return $id;
229 if ( $key === '' ) {
230 wfProfileOut( __METHOD__ );
231 return 0;
234 # Some fields heavily used for linking...
235 if ( $this->mForUpdate ) {
236 $db = wfGetDB( DB_MASTER );
237 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
238 $options = array( 'FOR UPDATE' );
239 } else {
240 $options = array();
242 } else {
243 $db = wfGetDB( DB_SLAVE );
244 $options = array();
247 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
248 if ( $wgContentHandlerUseDB ) {
249 $f[] = 'page_content_model';
252 $s = $db->selectRow( 'page', $f,
253 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
254 __METHOD__, $options );
255 # Set fields...
256 if ( $s !== false ) {
257 $this->addGoodLinkObjFromRow( $nt, $s );
258 $id = intval( $s->page_id );
259 } else {
260 $this->addBadLinkObj( $nt );
261 $id = 0;
264 wfProfileOut( __METHOD__ );
265 return $id;
269 * Clears cache
271 public function clear() {
272 $this->mGoodLinks = array();
273 $this->mGoodLinkFields = array();
274 $this->mBadLinks = array();