Merge "Kill 'newmessageslink' and 'newmessagesdifflink' messages"
[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
55 return self::$instance;
56 }
57
58 /**
59 * Destroy the singleton instance, a new one will be created next time
60 * singleton() is called.
61 * @since 1.22
62 */
63 static function destroySingleton() {
64 self::$instance = null;
65 }
66
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
73 */
74 static function setSingleton( LinkCache $instance ) {
75 self::$instance = $instance;
76 }
77
78 /**
79 * General accessor to get/set whether SELECT FOR UPDATE should be used
80 *
81 * @return bool
82 */
83 public function forUpdate( $update = null ) {
84 return wfSetVar( $this->mForUpdate, $update );
85 }
86
87 /**
88 * @param $title
89 * @return array|int
90 */
91 public function getGoodLinkID( $title ) {
92 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
93 return $this->mGoodLinks[$title];
94 } else {
95 return 0;
96 }
97 }
98
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
105 */
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;
112 }
113 }
114
115 /**
116 * @param $title
117 * @return bool
118 */
119 public function isBadLink( $title ) {
120 return array_key_exists( $title, $this->mBadLinks );
121 }
122
123 /**
124 * Add a link for the title to the link cache
125 *
126 * @param int $id Page's ID
127 * @param Title $title
128 * @param int $len Text's length
129 * @param int $redir Whether the page is a redirect
130 * @param int $revision Latest revision's ID
131 * @param int $model Latest revision's content model ID
132 */
133 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
134 $revision = 0, $model = 0
135 ) {
136 $dbkey = $title->getPrefixedDBkey();
137 $this->mGoodLinks[$dbkey] = (int)$id;
138 $this->mGoodLinkFields[$dbkey] = array(
139 'length' => (int)$len,
140 'redirect' => (int)$redir,
141 'revision' => (int)$revision,
142 'model' => (int)$model
143 );
144 }
145
146 /**
147 * Same as above with better interface.
148 * @since 1.19
149 * @param $title Title
150 * @param $row object which has the fields page_id, page_is_redirect,
151 * page_latest and page_content_model
152 */
153 public function addGoodLinkObjFromRow( $title, $row ) {
154 $dbkey = $title->getPrefixedDBkey();
155 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
156 $this->mGoodLinkFields[$dbkey] = array(
157 'length' => intval( $row->page_len ),
158 'redirect' => intval( $row->page_is_redirect ),
159 'revision' => intval( $row->page_latest ),
160 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
161 );
162 }
163
164 /**
165 * @param $title Title
166 */
167 public function addBadLinkObj( $title ) {
168 $dbkey = $title->getPrefixedDBkey();
169 if ( !$this->isBadLink( $dbkey ) ) {
170 $this->mBadLinks[$dbkey] = 1;
171 }
172 }
173
174 public function clearBadLink( $title ) {
175 unset( $this->mBadLinks[$title] );
176 }
177
178 /**
179 * @param $title Title
180 */
181 public function clearLink( $title ) {
182 $dbkey = $title->getPrefixedDBkey();
183 unset( $this->mBadLinks[$dbkey] );
184 unset( $this->mGoodLinks[$dbkey] );
185 unset( $this->mGoodLinkFields[$dbkey] );
186 }
187
188 public function getGoodLinks() {
189 return $this->mGoodLinks;
190 }
191
192 public function getBadLinks() {
193 return array_keys( $this->mBadLinks );
194 }
195
196 /**
197 * Add a title to the link cache, return the page_id or zero if non-existent
198 *
199 * @param string $title title to add
200 * @return Integer
201 */
202 public function addLink( $title ) {
203 $nt = Title::newFromDBkey( $title );
204 if ( $nt ) {
205 return $this->addLinkObj( $nt );
206 } else {
207 return 0;
208 }
209 }
210
211 /**
212 * Add a title to the link cache, return the page_id or zero if non-existent
213 *
214 * @param $nt Title object to add
215 * @return Integer
216 */
217 public function addLinkObj( $nt ) {
218 global $wgAntiLockFlags, $wgContentHandlerUseDB;
219
220 wfProfileIn( __METHOD__ );
221
222 $key = $nt->getPrefixedDBkey();
223 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
224 wfProfileOut( __METHOD__ );
225
226 return 0;
227 }
228 $id = $this->getGoodLinkID( $key );
229 if ( $id != 0 ) {
230 wfProfileOut( __METHOD__ );
231
232 return $id;
233 }
234
235 if ( $key === '' ) {
236 wfProfileOut( __METHOD__ );
237
238 return 0;
239 }
240
241 # Some fields heavily used for linking...
242 if ( $this->mForUpdate ) {
243 $db = wfGetDB( DB_MASTER );
244 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
245 $options = array( 'FOR UPDATE' );
246 } else {
247 $options = array();
248 }
249 } else {
250 $db = wfGetDB( DB_SLAVE );
251 $options = array();
252 }
253
254 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
255 if ( $wgContentHandlerUseDB ) {
256 $f[] = 'page_content_model';
257 }
258
259 $s = $db->selectRow( 'page', $f,
260 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
261 __METHOD__, $options );
262 # Set fields...
263 if ( $s !== false ) {
264 $this->addGoodLinkObjFromRow( $nt, $s );
265 $id = intval( $s->page_id );
266 } else {
267 $this->addBadLinkObj( $nt );
268 $id = 0;
269 }
270
271 wfProfileOut( __METHOD__ );
272
273 return $id;
274 }
275
276 /**
277 * Clears cache
278 */
279 public function clear() {
280 $this->mGoodLinks = array();
281 $this->mGoodLinkFields = array();
282 $this->mBadLinks = array();
283 }
284 }