Merge "Add script to find orphaned LocalRepo files"
[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 /**
31 * @var MapCacheLRU
32 */
33 private $mGoodLinks;
34 /**
35 * @var MapCacheLRU
36 */
37 private $mBadLinks;
38 private $mForUpdate = false;
39
40 /**
41 * How many Titles to store. There are two caches, so the amount actually
42 * stored in memory can be up to twice this.
43 */
44 const MAX_SIZE = 10000;
45
46 /**
47 * @var LinkCache
48 */
49 protected static $instance;
50
51 public function __construct() {
52 $this->mGoodLinks = new MapCacheLRU( self::MAX_SIZE );
53 $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
54 }
55
56 /**
57 * Get an instance of this class.
58 *
59 * @return LinkCache
60 */
61 static function &singleton() {
62 if ( self::$instance ) {
63 return self::$instance;
64 }
65 self::$instance = new LinkCache;
66
67 return self::$instance;
68 }
69
70 /**
71 * Destroy the singleton instance, a new one will be created next time
72 * singleton() is called.
73 * @since 1.22
74 */
75 static function destroySingleton() {
76 self::$instance = null;
77 }
78
79 /**
80 * Set the singleton instance to a given object.
81 * Since we do not have an interface for LinkCache, you have to be sure the
82 * given object implements all the LinkCache public methods.
83 * @param LinkCache $instance
84 * @since 1.22
85 */
86 static function setSingleton( LinkCache $instance ) {
87 self::$instance = $instance;
88 }
89
90 /**
91 * General accessor to get/set whether the master DB should be used
92 *
93 * This used to also set the FOR UPDATE option (locking the rows read
94 * in order to avoid link table inconsistency), which was later removed
95 * for performance on wikis with a high edit rate.
96 *
97 * @param bool $update
98 * @return bool
99 */
100 public function forUpdate( $update = null ) {
101 return wfSetVar( $this->mForUpdate, $update );
102 }
103
104 /**
105 * @param string $title
106 * @return int
107 */
108 public function getGoodLinkID( $title ) {
109 if ( $this->mGoodLinks->has( $title ) ) {
110 $info = $this->mGoodLinks->get( $title );
111 return $info['id'];
112 } else {
113 return 0;
114 }
115 }
116
117 /**
118 * Get a field of a title object from cache.
119 * If this link is not good, it will return NULL.
120 * @param Title $title
121 * @param string $field ('length','redirect','revision','model')
122 * @return string|null
123 */
124 public function getGoodLinkFieldObj( $title, $field ) {
125 $dbkey = $title->getPrefixedDBkey();
126 if ( $this->mGoodLinks->has( $dbkey ) ) {
127 $info = $this->mGoodLinks->get( $dbkey );
128 return $info[$field];
129 } else {
130 return null;
131 }
132 }
133
134 /**
135 * @param string $title
136 * @return bool
137 */
138 public function isBadLink( $title ) {
139 // We need to use get here since has will not call ping.
140 return $this->mBadLinks->get( $title ) !== null;
141 }
142
143 /**
144 * Add a link for the title to the link cache
145 *
146 * @param int $id Page's ID
147 * @param Title $title
148 * @param int $len Text's length
149 * @param int $redir Whether the page is a redirect
150 * @param int $revision Latest revision's ID
151 * @param string|null $model Latest revision's content model ID
152 */
153 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
154 $revision = 0, $model = null
155 ) {
156 $dbkey = $title->getPrefixedDBkey();
157 $this->mGoodLinks->set( $dbkey, array(
158 'id' => (int)$id,
159 'length' => (int)$len,
160 'redirect' => (int)$redir,
161 'revision' => (int)$revision,
162 'model' => $model ? (string)$model : null,
163 ) );
164 }
165
166 /**
167 * Same as above with better interface.
168 * @since 1.19
169 * @param Title $title
170 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
171 * page_latest and page_content_model
172 */
173 public function addGoodLinkObjFromRow( $title, $row ) {
174 $dbkey = $title->getPrefixedDBkey();
175 $this->mGoodLinks->set( $dbkey, array(
176 'id' => intval( $row->page_id ),
177 'length' => intval( $row->page_len ),
178 'redirect' => intval( $row->page_is_redirect ),
179 'revision' => intval( $row->page_latest ),
180 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
181 ) );
182 }
183
184 /**
185 * @param Title $title
186 */
187 public function addBadLinkObj( $title ) {
188 $dbkey = $title->getPrefixedDBkey();
189 if ( !$this->isBadLink( $dbkey ) ) {
190 $this->mBadLinks->set( $dbkey, 1 );
191 }
192 }
193
194 public function clearBadLink( $title ) {
195 $this->mBadLinks->clear( array( $title ) );
196 }
197
198 /**
199 * @param Title $title
200 */
201 public function clearLink( $title ) {
202 $dbkey = $title->getPrefixedDBkey();
203 $this->mBadLinks->clear( array( $dbkey ) );
204 $this->mGoodLinks->clear( array( $dbkey ) );
205 }
206
207 /**
208 * Add a title to the link cache, return the page_id or zero if non-existent
209 *
210 * @param string $title Title to add
211 * @return int
212 */
213 public function addLink( $title ) {
214 $nt = Title::newFromDBkey( $title );
215 if ( $nt ) {
216 return $this->addLinkObj( $nt );
217 } else {
218 return 0;
219 }
220 }
221
222 /**
223 * Add a title to the link cache, return the page_id or zero if non-existent
224 *
225 * @param Title $nt Title object to add
226 * @return int
227 */
228 public function addLinkObj( $nt ) {
229 global $wgContentHandlerUseDB;
230
231 $key = $nt->getPrefixedDBkey();
232 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
233 return 0;
234 }
235 $id = $this->getGoodLinkID( $key );
236 if ( $id != 0 ) {
237 return $id;
238 }
239
240 if ( $key === '' ) {
241 return 0;
242 }
243
244 # Some fields heavily used for linking...
245 if ( $this->mForUpdate ) {
246 $db = wfGetDB( DB_MASTER );
247 } else {
248 $db = wfGetDB( DB_SLAVE );
249 }
250
251 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
252 if ( $wgContentHandlerUseDB ) {
253 $f[] = 'page_content_model';
254 }
255
256 $s = $db->selectRow( 'page', $f,
257 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
258 __METHOD__ );
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;
266 }
267
268 return $id;
269 }
270
271 /**
272 * Clears cache
273 */
274 public function clear() {
275 $this->mGoodLinks->clear();
276 $this->mBadLinks->clear();
277 }
278 }