Merge "RecentChange: deprecate newFromCurRow and loadFromCurRow"
[lhc/web/wiklou.git] / includes / cache / LinkCache.php
1 <?php
2 /**
3 * Page existence cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
26 *
27 * @ingroup Cache
28 */
29 class LinkCache {
30 // Increment $mClassVer whenever old serialized versions of this class
31 // becomes incompatible with the new version.
32 private $mClassVer = 4;
33
34 private $mGoodLinks = array();
35 private $mGoodLinkFields = array();
36 private $mBadLinks = array();
37 private $mForUpdate = false;
38
39 /**
40 * @var LinkCache
41 */
42 protected static $instance;
43
44 /**
45 * Get an instance of this class.
46 *
47 * @return LinkCache
48 */
49 static function &singleton() {
50 if ( self::$instance ) {
51 return self::$instance;
52 }
53 self::$instance = new LinkCache;
54 return self::$instance;
55 }
56
57 /**
58 * Destroy the singleton instance, a new one will be created next time
59 * singleton() is called.
60 * @since 1.22
61 */
62 static function destroySingleton() {
63 self::$instance = null;
64 }
65
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
72 */
73 static function setSingleton( LinkCache $instance ) {
74 self::$instance = $instance;
75 }
76
77 /**
78 * General accessor to get/set whether SELECT FOR UPDATE should be used
79 *
80 * @return bool
81 */
82 public function forUpdate( $update = null ) {
83 return wfSetVar( $this->mForUpdate, $update );
84 }
85
86 /**
87 * @param $title
88 * @return array|int
89 */
90 public function getGoodLinkID( $title ) {
91 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
92 return $this->mGoodLinks[$title];
93 } else {
94 return 0;
95 }
96 }
97
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
104 */
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;
111 }
112 }
113
114 /**
115 * @param $title
116 * @return bool
117 */
118 public function isBadLink( $title ) {
119 return array_key_exists( $title, $this->mBadLinks );
120 }
121
122 /**
123 * Add a link for the title to the link cache
124 *
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
131 */
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 ) );
140 }
141
142 /**
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
148 */
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,
157 );
158 }
159
160 /**
161 * @param $title Title
162 */
163 public function addBadLinkObj( $title ) {
164 $dbkey = $title->getPrefixedDBkey();
165 if ( !$this->isBadLink( $dbkey ) ) {
166 $this->mBadLinks[$dbkey] = 1;
167 }
168 }
169
170 public function clearBadLink( $title ) {
171 unset( $this->mBadLinks[$title] );
172 }
173
174 /**
175 * @param $title Title
176 */
177 public function clearLink( $title ) {
178 $dbkey = $title->getPrefixedDBkey();
179 unset( $this->mBadLinks[$dbkey] );
180 unset( $this->mGoodLinks[$dbkey] );
181 unset( $this->mGoodLinkFields[$dbkey] );
182 }
183
184 public function getGoodLinks() {
185 return $this->mGoodLinks;
186 }
187
188 public function getBadLinks() {
189 return array_keys( $this->mBadLinks );
190 }
191
192 /**
193 * Add a title to the link cache, return the page_id or zero if non-existent
194 *
195 * @param string $title title to add
196 * @return Integer
197 */
198 public function addLink( $title ) {
199 $nt = Title::newFromDBkey( $title );
200 if ( $nt ) {
201 return $this->addLinkObj( $nt );
202 } else {
203 return 0;
204 }
205 }
206
207 /**
208 * Add a title to the link cache, return the page_id or zero if non-existent
209 *
210 * @param $nt Title object to add
211 * @return Integer
212 */
213 public function addLinkObj( $nt ) {
214 global $wgAntiLockFlags, $wgContentHandlerUseDB;
215
216 wfProfileIn( __METHOD__ );
217
218 $key = $nt->getPrefixedDBkey();
219 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
220 wfProfileOut( __METHOD__ );
221 return 0;
222 }
223 $id = $this->getGoodLinkID( $key );
224 if ( $id != 0 ) {
225 wfProfileOut( __METHOD__ );
226 return $id;
227 }
228
229 if ( $key === '' ) {
230 wfProfileOut( __METHOD__ );
231 return 0;
232 }
233
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();
241 }
242 } else {
243 $db = wfGetDB( DB_SLAVE );
244 $options = array();
245 }
246
247 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
248 if ( $wgContentHandlerUseDB ) {
249 $f[] = 'page_content_model';
250 }
251
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;
262 }
263
264 wfProfileOut( __METHOD__ );
265 return $id;
266 }
267
268 /**
269 * Clears cache
270 */
271 public function clear() {
272 $this->mGoodLinks = array();
273 $this->mGoodLinkFields = array();
274 $this->mBadLinks = array();
275 }
276 }