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 */ var $mClassVer = 4;
12 /* private */ var $mGoodLinks, $mBadLinks;
13 /* private */ var $mForUpdate;
16 * Get an instance of this class
18 static function &singleton() {
20 if ( !isset( $instance ) ) {
21 $instance = new LinkCache
;
26 function __construct() {
27 $this->mForUpdate
= false;
28 $this->mGoodLinks
= array();
29 $this->mGoodLinkFields
= array();
30 $this->mBadLinks
= array();
34 * General accessor to get/set whether SELECT FOR UPDATE should be used
36 public function forUpdate( $update = null ) {
37 return wfSetVar( $this->mForUpdate
, $update );
40 public function getGoodLinkID( $title ) {
41 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
42 return $this->mGoodLinks
[$title];
49 * Get a field of a title object from cache.
50 * If this link is not good, it will return NULL.
52 * @param $field String: ('length','redirect','revision')
55 public function getGoodLinkFieldObj( $title, $field ) {
56 $dbkey = $title->getPrefixedDbKey();
57 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
58 return $this->mGoodLinkFields
[$dbkey][$field];
64 public function isBadLink( $title ) {
65 return array_key_exists( $title, $this->mBadLinks
);
69 * Add a link for the title to the link cache
71 * @param $id Integer: page's ID
72 * @param $title Title object
73 * @param $len Integer: text's length
74 * @param $redir Integer: whether the page is a redirect
75 * @param $revision Integer: latest revision's ID
77 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
78 $dbkey = $title->getPrefixedDbKey();
79 $this->mGoodLinks
[$dbkey] = intval( $id );
80 $this->mGoodLinkFields
[$dbkey] = array(
81 'length' => intval( $len ),
82 'redirect' => intval( $redir ),
83 'revision' => intval( $revision ) );
86 public function addBadLinkObj( $title ) {
87 $dbkey = $title->getPrefixedDbKey();
88 if ( !$this->isBadLink( $dbkey ) ) {
89 $this->mBadLinks
[$dbkey] = 1;
93 public function clearBadLink( $title ) {
94 unset( $this->mBadLinks
[$title] );
97 public function clearLink( $title ) {
98 $dbkey = $title->getPrefixedDbKey();
99 if( isset($this->mBadLinks
[$dbkey]) ) {
100 unset($this->mBadLinks
[$dbkey]);
102 if( isset($this->mGoodLinks
[$dbkey]) ) {
103 unset($this->mGoodLinks
[$dbkey]);
105 if( isset($this->mGoodLinkFields
[$dbkey]) ) {
106 unset($this->mGoodLinkFields
[$dbkey]);
110 public function getGoodLinks() { return $this->mGoodLinks
; }
111 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
114 * Add a title to the link cache, return the page_id or zero if non-existent
116 * @param $title String: title to add
119 public function addLink( $title ) {
120 $nt = Title
::newFromDBkey( $title );
122 return $this->addLinkObj( $nt );
129 * Add a title to the link cache, return the page_id or zero if non-existent
131 * @param $nt Title object to add
134 public function addLinkObj( $nt ) {
135 global $wgAntiLockFlags;
136 wfProfileIn( __METHOD__
);
138 $key = $nt->getPrefixedDBkey();
139 if ( $this->isBadLink( $key ) ) {
140 wfProfileOut( __METHOD__
);
143 $id = $this->getGoodLinkID( $key );
145 wfProfileOut( __METHOD__
);
150 wfProfileOut( __METHOD__
);
154 # Some fields heavily used for linking...
155 if ( $this->mForUpdate
) {
156 $db = wfGetDB( DB_MASTER
);
157 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
158 $options = array( 'FOR UPDATE' );
163 $db = wfGetDB( DB_SLAVE
);
167 $s = $db->selectRow( 'page',
168 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
169 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
170 __METHOD__
, $options );
172 if ( $s !== false ) {
173 $id = intval( $s->page_id
);
174 $len = intval( $s->page_len
);
175 $redirect = intval( $s->page_is_redirect
);
176 $revision = intval( $s->page_latest
);
185 $this->addBadLinkObj( $nt );
187 $this->addGoodLinkObj( $id, $nt, $len, $redirect, $revision );
189 wfProfileOut( __METHOD__
);
196 public function clear() {
197 $this->mGoodLinks
= array();
198 $this->mGoodLinkFields
= array();
199 $this->mBadLinks
= array();