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
38 public function forUpdate( $update = null ) {
39 return wfSetVar( $this->mForUpdate
, $update );
42 public function getGoodLinkID( $title ) {
43 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
44 return $this->mGoodLinks
[$title];
51 * Get a field of a title object from cache.
52 * If this link is not good, it will return NULL.
54 * @param $field String: ('length','redirect','revision')
57 public function getGoodLinkFieldObj( $title, $field ) {
58 $dbkey = $title->getPrefixedDbKey();
59 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
60 return $this->mGoodLinkFields
[$dbkey][$field];
66 public function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks
);
71 * Add a link for the title to the link cache
73 * @param $id Integer: page's ID
74 * @param $title Title object
75 * @param $len Integer: text's length
76 * @param $redir Integer: whether the page is a redirect
77 * @param $revision Integer: latest revision's ID
79 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
80 $dbkey = $title->getPrefixedDbKey();
81 $this->mGoodLinks
[$dbkey] = intval( $id );
82 $this->mGoodLinkFields
[$dbkey] = array(
83 'length' => intval( $len ),
84 'redirect' => intval( $redir ),
85 'revision' => intval( $revision ) );
88 public function addBadLinkObj( $title ) {
89 $dbkey = $title->getPrefixedDbKey();
90 if ( !$this->isBadLink( $dbkey ) ) {
91 $this->mBadLinks
[$dbkey] = 1;
95 public function clearBadLink( $title ) {
96 unset( $this->mBadLinks
[$title] );
99 public function clearLink( $title ) {
100 $dbkey = $title->getPrefixedDbKey();
101 if( isset($this->mBadLinks
[$dbkey]) ) {
102 unset($this->mBadLinks
[$dbkey]);
104 if( isset($this->mGoodLinks
[$dbkey]) ) {
105 unset($this->mGoodLinks
[$dbkey]);
107 if( isset($this->mGoodLinkFields
[$dbkey]) ) {
108 unset($this->mGoodLinkFields
[$dbkey]);
112 public function getGoodLinks() { return $this->mGoodLinks
; }
113 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
116 * Add a title to the link cache, return the page_id or zero if non-existent
118 * @param $title String: title to add
121 public function addLink( $title ) {
122 $nt = Title
::newFromDBkey( $title );
124 return $this->addLinkObj( $nt );
131 * Add a title to the link cache, return the page_id or zero if non-existent
133 * @param $nt Title object to add
136 public function addLinkObj( $nt ) {
137 global $wgAntiLockFlags;
138 wfProfileIn( __METHOD__
);
140 $key = $nt->getPrefixedDBkey();
141 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
142 wfProfileOut( __METHOD__
);
145 $id = $this->getGoodLinkID( $key );
147 wfProfileOut( __METHOD__
);
152 wfProfileOut( __METHOD__
);
156 # Some fields heavily used for linking...
157 if ( $this->mForUpdate
) {
158 $db = wfGetDB( DB_MASTER
);
159 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
160 $options = array( 'FOR UPDATE' );
165 $db = wfGetDB( DB_SLAVE
);
169 $s = $db->selectRow( 'page',
170 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
171 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
172 __METHOD__
, $options );
174 if ( $s !== false ) {
175 $id = intval( $s->page_id
);
176 $len = intval( $s->page_len
);
177 $redirect = intval( $s->page_is_redirect
);
178 $revision = intval( $s->page_latest
);
187 $this->addBadLinkObj( $nt );
189 $this->addGoodLinkObj( $id, $nt, $len, $redirect, $revision );
191 wfProfileOut( __METHOD__
);
198 public function clear() {
199 $this->mGoodLinks
= array();
200 $this->mGoodLinkFields
= array();
201 $this->mBadLinks
= array();