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
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;
40 * Get an instance of this class
44 static function &singleton() {
46 if ( !isset( $instance ) ) {
47 $instance = new LinkCache
;
53 * General accessor to get/set whether SELECT FOR UPDATE should be used
57 public function forUpdate( $update = null ) {
58 return wfSetVar( $this->mForUpdate
, $update );
65 public function getGoodLinkID( $title ) {
66 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
67 return $this->mGoodLinks
[$title];
74 * Get a field of a title object from cache.
75 * If this link is not good, it will return NULL.
77 * @param $field String: ('length','redirect','revision')
80 public function getGoodLinkFieldObj( $title, $field ) {
81 $dbkey = $title->getPrefixedDbKey();
82 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
83 return $this->mGoodLinkFields
[$dbkey][$field];
93 public function isBadLink( $title ) {
94 return array_key_exists( $title, $this->mBadLinks
);
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
106 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
107 $dbkey = $title->getPrefixedDbKey();
108 $this->mGoodLinks
[$dbkey] = intval( $id );
109 $this->mGoodLinkFields
[$dbkey] = array(
110 'length' => intval( $len ),
111 'redirect' => intval( $redir ),
112 'revision' => intval( $revision ) );
116 * Same as above with better interface.
118 * @param $title Title
119 * @param $row object which has the fields page_id, page_is_redirect,
122 public function addGoodLinkObjFromRow( $title, $row ) {
123 $dbkey = $title->getPrefixedDbKey();
124 $this->mGoodLinks
[$dbkey] = intval( $row->page_id
);
125 $this->mGoodLinkFields
[$dbkey] = array(
126 'length' => intval( $row->page_len
),
127 'redirect' => intval( $row->page_is_redirect
),
128 'revision' => intval( $row->page_latest
),
133 * @param $title Title
135 public function addBadLinkObj( $title ) {
136 $dbkey = $title->getPrefixedDbKey();
137 if ( !$this->isBadLink( $dbkey ) ) {
138 $this->mBadLinks
[$dbkey] = 1;
142 public function clearBadLink( $title ) {
143 unset( $this->mBadLinks
[$title] );
147 * @param $title Title
149 public function clearLink( $title ) {
150 $dbkey = $title->getPrefixedDbKey();
151 unset( $this->mBadLinks
[$dbkey] );
152 unset( $this->mGoodLinks
[$dbkey] );
153 unset( $this->mGoodLinkFields
[$dbkey] );
156 public function getGoodLinks() { return $this->mGoodLinks
; }
157 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
160 * Add a title to the link cache, return the page_id or zero if non-existent
162 * @param $title String: title to add
165 public function addLink( $title ) {
166 $nt = Title
::newFromDBkey( $title );
168 return $this->addLinkObj( $nt );
175 * Add a title to the link cache, return the page_id or zero if non-existent
177 * @param $nt Title object to add
180 public function addLinkObj( $nt ) {
181 global $wgAntiLockFlags;
182 wfProfileIn( __METHOD__
);
184 $key = $nt->getPrefixedDBkey();
185 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
186 wfProfileOut( __METHOD__
);
189 $id = $this->getGoodLinkID( $key );
191 wfProfileOut( __METHOD__
);
196 wfProfileOut( __METHOD__
);
200 # Some fields heavily used for linking...
201 if ( $this->mForUpdate
) {
202 $db = wfGetDB( DB_MASTER
);
203 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
204 $options = array( 'FOR UPDATE' );
209 $db = wfGetDB( DB_SLAVE
);
213 $s = $db->selectRow( 'page',
214 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
215 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
216 __METHOD__
, $options );
218 if ( $s !== false ) {
219 $this->addGoodLinkObjFromRow( $nt, $s );
220 $id = intval( $s->page_id
);
222 $this->addBadLinkObj( $nt );
226 wfProfileOut( __METHOD__
);
233 public function clear() {
234 $this->mGoodLinks
= array();
235 $this->mGoodLinkFields
= array();
236 $this->mBadLinks
= array();