3 * Cache for article titles (prefixed DB keys) and ids linked from one source
8 // Increment $mClassVer whenever old serialized versions of this class
9 // becomes incompatible with the new version.
10 private $mClassVer = 4;
12 private $mGoodLinks = array();
13 private $mGoodLinkFields = array();
14 private $mBadLinks = array();
15 private $mForUpdate = false;
18 * Get an instance of this class
22 static function &singleton() {
24 if ( !isset( $instance ) ) {
25 $instance = new LinkCache
;
31 * General accessor to get/set whether SELECT FOR UPDATE should be used
35 public function forUpdate( $update = null ) {
36 return wfSetVar( $this->mForUpdate
, $update );
43 public function getGoodLinkID( $title ) {
44 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
45 return $this->mGoodLinks
[$title];
52 * Get a field of a title object from cache.
53 * If this link is not good, it will return NULL.
55 * @param $field String: ('length','redirect','revision')
58 public function getGoodLinkFieldObj( $title, $field ) {
59 $dbkey = $title->getPrefixedDbKey();
60 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
61 return $this->mGoodLinkFields
[$dbkey][$field];
71 public function isBadLink( $title ) {
72 return array_key_exists( $title, $this->mBadLinks
);
76 * Add a link for the title to the link cache
78 * @param $id Integer: page's ID
79 * @param $title Title object
80 * @param $len Integer: text's length
81 * @param $redir Integer: whether the page is a redirect
82 * @param $revision Integer: latest revision's ID
84 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
85 $dbkey = $title->getPrefixedDbKey();
86 $this->mGoodLinks
[$dbkey] = intval( $id );
87 $this->mGoodLinkFields
[$dbkey] = array(
88 'length' => intval( $len ),
89 'redirect' => intval( $redir ),
90 'revision' => intval( $revision ) );
94 * Same as above with better interface.
97 * @param $row object which has the fields page_id, page_is_redirect,
100 public function addGoodLinkObjFromRow( $title, $row ) {
101 $dbkey = $title->getPrefixedDbKey();
102 $this->mGoodLinks
[$dbkey] = intval( $row->page_id
);
103 $this->mGoodLinkFields
[$dbkey] = array(
104 'length' => intval( $row->page_len
),
105 'redirect' => intval( $row->page_is_redirect
),
106 'revision' => intval( $row->page_latest
),
111 * @param $title Title
113 public function addBadLinkObj( $title ) {
114 $dbkey = $title->getPrefixedDbKey();
115 if ( !$this->isBadLink( $dbkey ) ) {
116 $this->mBadLinks
[$dbkey] = 1;
120 public function clearBadLink( $title ) {
121 unset( $this->mBadLinks
[$title] );
125 * @param $title Title
127 public function clearLink( $title ) {
128 $dbkey = $title->getPrefixedDbKey();
129 unset( $this->mBadLinks
[$dbkey] );
130 unset( $this->mGoodLinks
[$dbkey] );
131 unset( $this->mGoodLinkFields
[$dbkey] );
134 public function getGoodLinks() { return $this->mGoodLinks
; }
135 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
138 * Add a title to the link cache, return the page_id or zero if non-existent
140 * @param $title String: title to add
143 public function addLink( $title ) {
144 $nt = Title
::newFromDBkey( $title );
146 return $this->addLinkObj( $nt );
153 * Add a title to the link cache, return the page_id or zero if non-existent
155 * @param $nt Title object to add
158 public function addLinkObj( $nt ) {
159 global $wgAntiLockFlags;
160 wfProfileIn( __METHOD__
);
162 $key = $nt->getPrefixedDBkey();
163 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
164 wfProfileOut( __METHOD__
);
167 $id = $this->getGoodLinkID( $key );
169 wfProfileOut( __METHOD__
);
174 wfProfileOut( __METHOD__
);
178 # Some fields heavily used for linking...
179 if ( $this->mForUpdate
) {
180 $db = wfGetDB( DB_MASTER
);
181 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
182 $options = array( 'FOR UPDATE' );
187 $db = wfGetDB( DB_SLAVE
);
191 $s = $db->selectRow( 'page',
192 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
193 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
194 __METHOD__
, $options );
196 if ( $s !== false ) {
197 $this->addGoodLinkObjFromRow( $nt, $s );
198 $id = intval( $s->page_id
);
200 $this->addBadLinkObj( $nt );
204 wfProfileOut( __METHOD__
);
211 public function clear() {
212 $this->mGoodLinks
= array();
213 $this->mGoodLinkFields
= array();
214 $this->mBadLinks
= array();