Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / cache / LinkCache.php
blob31982e0b11a281371f60b4afc88030310b474f52
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 * Get an instance of this class
42 * @return LinkCache
44 static function &singleton() {
45 static $instance;
46 if ( !isset( $instance ) ) {
47 $instance = new LinkCache;
49 return $instance;
52 /**
53 * General accessor to get/set whether SELECT FOR UPDATE should be used
55 * @return bool
57 public function forUpdate( $update = null ) {
58 return wfSetVar( $this->mForUpdate, $update );
61 /**
62 * @param $title
63 * @return array|int
65 public function getGoodLinkID( $title ) {
66 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
67 return $this->mGoodLinks[$title];
68 } else {
69 return 0;
73 /**
74 * Get a field of a title object from cache.
75 * If this link is not good, it will return NULL.
76 * @param $title Title
77 * @param string $field ('length','redirect','revision','model')
78 * @return mixed
80 public function getGoodLinkFieldObj( $title, $field ) {
81 $dbkey = $title->getPrefixedDBkey();
82 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
83 return $this->mGoodLinkFields[$dbkey][$field];
84 } else {
85 return null;
89 /**
90 * @param $title
91 * @return bool
93 public function isBadLink( $title ) {
94 return array_key_exists( $title, $this->mBadLinks );
97 /**
98 * Add a link for the title to the link cache
100 * @param $id Integer: page's ID
101 * @param $title Title object
102 * @param $len Integer: text's length
103 * @param $redir Integer: whether the page is a redirect
104 * @param $revision Integer: latest revision's ID
105 * @param $model Integer: latest revision's content model ID
107 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) {
108 $dbkey = $title->getPrefixedDBkey();
109 $this->mGoodLinks[$dbkey] = intval( $id );
110 $this->mGoodLinkFields[$dbkey] = array(
111 'length' => intval( $len ),
112 'redirect' => intval( $redir ),
113 'revision' => intval( $revision ),
114 'model' => intval( $model ) );
118 * Same as above with better interface.
119 * @since 1.19
120 * @param $title Title
121 * @param $row object which has the fields page_id, page_is_redirect,
122 * page_latest and page_content_model
124 public function addGoodLinkObjFromRow( $title, $row ) {
125 $dbkey = $title->getPrefixedDBkey();
126 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
127 $this->mGoodLinkFields[$dbkey] = array(
128 'length' => intval( $row->page_len ),
129 'redirect' => intval( $row->page_is_redirect ),
130 'revision' => intval( $row->page_latest ),
131 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
136 * @param $title Title
138 public function addBadLinkObj( $title ) {
139 $dbkey = $title->getPrefixedDBkey();
140 if ( !$this->isBadLink( $dbkey ) ) {
141 $this->mBadLinks[$dbkey] = 1;
145 public function clearBadLink( $title ) {
146 unset( $this->mBadLinks[$title] );
150 * @param $title Title
152 public function clearLink( $title ) {
153 $dbkey = $title->getPrefixedDBkey();
154 unset( $this->mBadLinks[$dbkey] );
155 unset( $this->mGoodLinks[$dbkey] );
156 unset( $this->mGoodLinkFields[$dbkey] );
159 public function getGoodLinks() {
160 return $this->mGoodLinks;
163 public function getBadLinks() {
164 return array_keys( $this->mBadLinks );
168 * Add a title to the link cache, return the page_id or zero if non-existent
170 * @param string $title title to add
171 * @return Integer
173 public function addLink( $title ) {
174 $nt = Title::newFromDBkey( $title );
175 if ( $nt ) {
176 return $this->addLinkObj( $nt );
177 } else {
178 return 0;
183 * Add a title to the link cache, return the page_id or zero if non-existent
185 * @param $nt Title object to add
186 * @return Integer
188 public function addLinkObj( $nt ) {
189 global $wgAntiLockFlags, $wgContentHandlerUseDB;
191 wfProfileIn( __METHOD__ );
193 $key = $nt->getPrefixedDBkey();
194 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
195 wfProfileOut( __METHOD__ );
196 return 0;
198 $id = $this->getGoodLinkID( $key );
199 if ( $id != 0 ) {
200 wfProfileOut( __METHOD__ );
201 return $id;
204 if ( $key === '' ) {
205 wfProfileOut( __METHOD__ );
206 return 0;
209 # Some fields heavily used for linking...
210 if ( $this->mForUpdate ) {
211 $db = wfGetDB( DB_MASTER );
212 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
213 $options = array( 'FOR UPDATE' );
214 } else {
215 $options = array();
217 } else {
218 $db = wfGetDB( DB_SLAVE );
219 $options = array();
222 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
223 if ( $wgContentHandlerUseDB ) {
224 $f[] = 'page_content_model';
227 $s = $db->selectRow( 'page', $f,
228 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
229 __METHOD__, $options );
230 # Set fields...
231 if ( $s !== false ) {
232 $this->addGoodLinkObjFromRow( $nt, $s );
233 $id = intval( $s->page_id );
234 } else {
235 $this->addBadLinkObj( $nt );
236 $id = 0;
239 wfProfileOut( __METHOD__ );
240 return $id;
244 * Clears cache
246 public function clear() {
247 $this->mGoodLinks = array();
248 $this->mGoodLinkFields = array();
249 $this->mBadLinks = array();