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 ) );
99 * Same as above with better interface.
101 * @param $title Title
102 * @param $row object which has the fields page_id, page_is_redirect,
105 public function addGoodLinkObjFromRow( $title, $row ) {
106 $dbkey = $title->getPrefixedDbKey();
107 $this->mGoodLinks
[$dbkey] = intval( $row->page_id
);
108 $this->mGoodLinkFields
[$dbkey] = array(
109 'length' => intval( $row->page_len
),
110 'redirect' => intval( $row->page_is_redirect
),
111 'revision' => intval( $row->page_latest
),
116 * @param $title Title
118 public function addBadLinkObj( $title ) {
119 $dbkey = $title->getPrefixedDbKey();
120 if ( !$this->isBadLink( $dbkey ) ) {
121 $this->mBadLinks
[$dbkey] = 1;
125 public function clearBadLink( $title ) {
126 unset( $this->mBadLinks
[$title] );
130 * @param $title Title
132 public function clearLink( $title ) {
133 $dbkey = $title->getPrefixedDbKey();
134 unset( $this->mBadLinks
[$dbkey] );
135 unset( $this->mGoodLinks
[$dbkey] );
136 unset( $this->mGoodLinkFields
[$dbkey] );
139 public function getGoodLinks() { return $this->mGoodLinks
; }
140 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
143 * Add a title to the link cache, return the page_id or zero if non-existent
145 * @param $title String: title to add
148 public function addLink( $title ) {
149 $nt = Title
::newFromDBkey( $title );
151 return $this->addLinkObj( $nt );
158 * Add a title to the link cache, return the page_id or zero if non-existent
160 * @param $nt Title object to add
163 public function addLinkObj( $nt ) {
164 global $wgAntiLockFlags;
165 wfProfileIn( __METHOD__
);
167 $key = $nt->getPrefixedDBkey();
168 if ( $this->isBadLink( $key ) ||
$nt->isExternal() ) {
169 wfProfileOut( __METHOD__
);
172 $id = $this->getGoodLinkID( $key );
174 wfProfileOut( __METHOD__
);
179 wfProfileOut( __METHOD__
);
183 # Some fields heavily used for linking...
184 if ( $this->mForUpdate
) {
185 $db = wfGetDB( DB_MASTER
);
186 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
187 $options = array( 'FOR UPDATE' );
192 $db = wfGetDB( DB_SLAVE
);
196 $s = $db->selectRow( 'page',
197 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
198 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
199 __METHOD__
, $options );
201 if ( $s !== false ) {
202 $this->addGoodLinkObjFromRow( $nt, $s );
203 $id = intval( $s->page_id
);
205 $this->addBadLinkObj( $nt );
209 wfProfileOut( __METHOD__
);
216 public function clear() {
217 $this->mGoodLinks
= array();
218 $this->mGoodLinkFields
= array();
219 $this->mBadLinks
= array();