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 string $field ('length','redirect')
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
75 public function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
76 $dbkey = $title->getPrefixedDbKey();
77 $this->mGoodLinks
[$dbkey] = $id;
78 $this->mGoodLinkFields
[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
81 public function addBadLinkObj( $title ) {
82 $dbkey = $title->getPrefixedDbKey();
83 if ( ! $this->isBadLink( $dbkey ) ) {
84 $this->mBadLinks
[$dbkey] = 1;
88 public function clearBadLink( $title ) {
89 unset( $this->mBadLinks
[$title] );
92 /* obsolete, for old $wgLinkCacheMemcached stuff */
93 public function clearLink( $title ) {}
95 public function getGoodLinks() { return $this->mGoodLinks
; }
96 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
99 * Add a title to the link cache, return the page_id or zero if non-existent
100 * @param $title String: title to add
101 * @param $len int, page size
102 * @param $redir bool, is redirect?
105 public function addLink( $title, $len = -1, $redir = NULL ) {
106 $nt = Title
::newFromDBkey( $title );
108 return $this->addLinkObj( $nt, $len, $redir );
115 * Add a title to the link cache, return the page_id or zero if non-existent
116 * @param $nt Title to add.
117 * @param $len int, page size
118 * @param $redir bool, is redirect?
121 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
122 global $wgAntiLockFlags, $wgProfiler;
124 $title = $nt->getPrefixedDBkey();
125 if ( $this->isBadLink( $title ) ) { return 0; }
126 $id = $this->getGoodLinkID( $title );
127 if ( 0 != $id ) { return $id; }
129 $fname = 'LinkCache::addLinkObj';
130 if ( isset( $wgProfiler ) ) {
131 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
134 wfProfileIn( $fname );
136 $ns = $nt->getNamespace();
137 $t = $nt->getDBkey();
139 if ( '' == $title ) {
140 wfProfileOut( $fname );
144 # Some fields heavily used for linking...
145 if ( $this->mForUpdate
) {
146 $db = wfGetDB( DB_MASTER
);
147 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
148 $options = array( 'FOR UPDATE' );
153 $db = wfGetDB( DB_SLAVE
);
157 $s = $db->selectRow( 'page',
158 array( 'page_id', 'page_len', 'page_is_redirect' ),
159 array( 'page_namespace' => $ns, 'page_title' => $t ),
162 $id = $s ?
$s->page_id
: 0;
163 $len = $s ?
$s->page_len
: -1;
164 $redirect = $s ?
$s->page_is_redirect
: 0;
167 $this->addBadLinkObj( $nt );
169 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
171 wfProfileOut( $fname );
178 public function clear() {
179 $this->mGoodLinks
= array();
180 $this->mGoodLinkFields
= array();
181 $this->mBadLinks
= array();