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, $mBadLinks;
16 * Get an instance of this class
20 static function &singleton() {
22 if ( !isset( $instance ) ) {
23 $instance = new LinkCache
;
28 function __construct() {
29 $this->mForUpdate
= false;
30 $this->mGoodLinks
= array();
31 $this->mGoodLinkFields
= array();
32 $this->mBadLinks
= array();
36 * General accessor to get/set whether SELECT FOR UPDATE should be used
40 public function forUpdate( $update = null ) {
41 return wfSetVar( $this->mForUpdate
, $update );
48 public function getGoodLinkID( $title ) {
49 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
50 return $this->mGoodLinks
[$title];
57 * Get a field of a title object from cache.
58 * If this link is not good, it will return NULL.
60 * @param $field String: ('length','redirect','revision')
63 public function getGoodLinkFieldObj( $title, $field ) {
64 $dbkey = $title->getPrefixedDbKey();
65 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
66 return $this->mGoodLinkFields
[$dbkey][$field];
76 public function isBadLink( $title ) {
77 return array_key_exists( $title, $this->mBadLinks
);
81 * Add a link for the title to the link cache
83 * @param $id Integer: page's ID
84 * @param $title Title object
85 * @param $len Integer: text's length
86 * @param $redir Integer: whether the page is a redirect
87 * @param $revision Integer: latest revision's ID
89 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
90 $dbkey = $title->getPrefixedDbKey();
91 $this->mGoodLinks
[$dbkey] = intval( $id );
92 $this->mGoodLinkFields
[$dbkey] = array(
93 'length' => intval( $len ),
94 'redirect' => intval( $redir ),
95 'revision' => intval( $revision ) );
101 public function addBadLinkObj( $title ) {
102 $dbkey = $title->getPrefixedDbKey();
103 if ( !$this->isBadLink( $dbkey ) ) {
104 $this->mBadLinks
[$dbkey] = 1;
108 public function clearBadLink( $title ) {
109 unset( $this->mBadLinks
[$title] );
113 * @param $title Title
115 public function clearLink( $title ) {
116 $dbkey = $title->getPrefixedDbKey();
117 if( isset($this->mBadLinks
[$dbkey]) ) {
118 unset($this->mBadLinks
[$dbkey]);
120 if( isset($this->mGoodLinks
[$dbkey]) ) {
121 unset($this->mGoodLinks
[$dbkey]);
123 if( isset($this->mGoodLinkFields
[$dbkey]) ) {
124 unset($this->mGoodLinkFields
[$dbkey]);
128 public function getGoodLinks() { return $this->mGoodLinks
; }
129 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
132 * Add a title to the link cache, return the page_id or zero if non-existent
134 * @param $title String: title to add
137 public function addLink( $title ) {
138 $nt = Title
::newFromDBkey( $title );
140 return $this->addLinkObj( $nt );
147 * Add a title to the link cache, return the page_id or zero if non-existent
149 * @param $nt Title object to add
152 public function addLinkObj( $nt ) {
153 global $wgAntiLockFlags;
154 wfProfileIn( __METHOD__
);
156 $key = $nt->getPrefixedDBkey();
157 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
158 wfProfileOut( __METHOD__
);
161 $id = $this->getGoodLinkID( $key );
163 wfProfileOut( __METHOD__
);
168 wfProfileOut( __METHOD__
);
172 # Some fields heavily used for linking...
173 if ( $this->mForUpdate
) {
174 $db = wfGetDB( DB_MASTER
);
175 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
176 $options = array( 'FOR UPDATE' );
181 $db = wfGetDB( DB_SLAVE
);
185 $s = $db->selectRow( 'page',
186 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
187 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
188 __METHOD__
, $options );
190 if ( $s !== false ) {
191 $id = intval( $s->page_id
);
192 $len = intval( $s->page_len
);
193 $redirect = intval( $s->page_is_redirect
);
194 $revision = intval( $s->page_latest
);
203 $this->addBadLinkObj( $nt );
205 $this->addGoodLinkObj( $id, $nt, $len, $redirect, $revision );
207 wfProfileOut( __METHOD__
);
214 public function clear() {
215 $this->mGoodLinks
= array();
216 $this->mGoodLinkFields
= array();
217 $this->mBadLinks
= array();