bb76f15fc692dfedfa4c6ff7fb36a06295d41443
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
25 * Cache for article titles (prefixed DB keys) and ids linked from one source
38 private $mForUpdate = false;
41 * How many Titles to store. There are two caches, so the amount actually
42 * stored in memory can be up to twice this.
44 const MAX_SIZE
= 10000;
49 protected static $instance;
51 public function __construct() {
52 $this->mGoodLinks
= new MapCacheLRU( self
::MAX_SIZE
);
53 $this->mBadLinks
= new MapCacheLRU( self
::MAX_SIZE
);
57 * Get an instance of this class.
61 public static function &singleton() {
62 if ( !self
::$instance ) {
63 self
::$instance = new LinkCache
;
66 return self
::$instance;
70 * Destroy the singleton instance
72 * A new one will be created next time singleton() is called.
76 public static function destroySingleton() {
77 self
::$instance = null;
81 * Set the singleton instance to a given object.
83 * Since we do not have an interface for LinkCache, you have to be sure the
84 * given object implements all the LinkCache public methods.
86 * @param LinkCache $instance
89 public static function setSingleton( LinkCache
$instance ) {
90 self
::$instance = $instance;
94 * General accessor to get/set whether the master DB should be used
96 * This used to also set the FOR UPDATE option (locking the rows read
97 * in order to avoid link table inconsistency), which was later removed
98 * for performance on wikis with a high edit rate.
100 * @param bool $update
103 public function forUpdate( $update = null ) {
104 return wfSetVar( $this->mForUpdate
, $update );
108 * @param string $title
109 * @return int Page ID or zero
111 public function getGoodLinkID( $title ) {
112 if ( !$this->mGoodLinks
->has( $title ) ) {
115 $info = $this->mGoodLinks
->get( $title );
120 * Get a field of a title object from cache.
121 * If this link is not a cached good title, it will return NULL.
122 * @param Title $title
123 * @param string $field ('length','redirect','revision','model')
124 * @return string|int|null
126 public function getGoodLinkFieldObj( $title, $field ) {
127 $dbkey = $title->getPrefixedDBkey();
128 if ( !$this->mGoodLinks
->has( $dbkey ) ) {
131 $info = $this->mGoodLinks
->get( $dbkey );
132 return $info[$field];
136 * @param string $title
139 public function isBadLink( $title ) {
140 // Use get() to ensure it records as used for LRU.
141 return $this->mBadLinks
->get( $title ) !== null;
145 * Add a link for the title to the link cache
147 * @param int $id Page's ID
148 * @param Title $title
149 * @param int $len Text's length
150 * @param int $redir Whether the page is a redirect
151 * @param int $revision Latest revision's ID
152 * @param string|null $model Latest revision's content model ID
154 public function addGoodLinkObj( $id, Title
$title, $len = -1, $redir = null,
155 $revision = 0, $model = null
157 $dbkey = $title->getPrefixedDBkey();
158 $this->mGoodLinks
->set( $dbkey, array(
160 'length' => (int)$len,
161 'redirect' => (int)$redir,
162 'revision' => (int)$revision,
163 'model' => $model ?
(string)$model : null,
168 * Same as above with better interface.
170 * @param Title $title
171 * @param stdClass $row Object which has the fields page_id, page_is_redirect,
172 * page_latest and page_content_model
174 public function addGoodLinkObjFromRow( Title
$title, $row ) {
175 $dbkey = $title->getPrefixedDBkey();
176 $this->mGoodLinks
->set( $dbkey, array(
177 'id' => intval( $row->page_id
),
178 'length' => intval( $row->page_len
),
179 'redirect' => intval( $row->page_is_redirect
),
180 'revision' => intval( $row->page_latest
),
181 'model' => !empty( $row->page_content_model
) ?
strval( $row->page_content_model
) : null,
186 * @param Title $title
188 public function addBadLinkObj( Title
$title ) {
189 $dbkey = $title->getPrefixedDBkey();
190 if ( !$this->isBadLink( $dbkey ) ) {
191 $this->mBadLinks
->set( $dbkey, 1 );
195 public function clearBadLink( $title ) {
196 $this->mBadLinks
->clear( array( $title ) );
200 * @param Title $title
202 public function clearLink( $title ) {
203 $dbkey = $title->getPrefixedDBkey();
204 $this->mBadLinks
->clear( array( $dbkey ) );
205 $this->mGoodLinks
->clear( array( $dbkey ) );
209 * Add a title to the link cache, return the page_id or zero if non-existent
211 * @param string $title Title to add
212 * @return int Page ID or zero
214 public function addLink( $title ) {
215 $nt = Title
::newFromDBkey( $title );
219 return $this->addLinkObj( $nt );
223 * Add a title to the link cache, return the page_id or zero if non-existent
225 * @param Title $nt Title object to add
226 * @return int Page ID or zero
228 public function addLinkObj( Title
$nt ) {
229 global $wgContentHandlerUseDB;
231 $key = $nt->getPrefixedDBkey();
232 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
235 $id = $this->getGoodLinkID( $key );
244 // Some fields heavily used for linking...
245 $db = $this->mForUpdate ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
247 $fields = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
248 if ( $wgContentHandlerUseDB ) {
249 $fields[] = 'page_content_model';
252 $row = $db->selectRow( 'page', $fields,
253 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
257 if ( $row !== false ) {
258 $this->addGoodLinkObjFromRow( $nt, $row );
259 $id = intval( $row->page_id
);
261 $this->addBadLinkObj( $nt );
271 public function clear() {
272 $this->mGoodLinks
->clear();
273 $this->mBadLinks
->clear();