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 $mPageLinks;
13 /* private */ var $mGoodLinks, $mBadLinks;
14 /* private */ var $mForUpdate;
17 * Get an instance of this class
19 static function &singleton() {
21 if ( !isset( $instance ) ) {
22 $instance = new LinkCache
;
27 function __construct() {
28 $this->mForUpdate
= false;
29 $this->mPageLinks
= array();
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 string $field ('length','redirect')
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
77 public function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
78 $dbkey = $title->getPrefixedDbKey();
79 $this->mGoodLinks
[$dbkey] = $id;
80 $this->mGoodLinkFields
[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
81 $this->mPageLinks
[$dbkey] = $title;
84 public function addBadLinkObj( $title ) {
85 $dbkey = $title->getPrefixedDbKey();
86 if ( ! $this->isBadLink( $dbkey ) ) {
87 $this->mBadLinks
[$dbkey] = 1;
88 $this->mPageLinks
[$dbkey] = $title;
92 public function clearBadLink( $title ) {
93 unset( $this->mBadLinks
[$title] );
94 $this->clearLink( $title );
97 /* obsolete, for old $wgLinkCacheMemcached stuff */
98 public function clearLink( $title ) {}
100 public function getPageLinks() { return $this->mPageLinks
; }
101 public function getGoodLinks() { return $this->mGoodLinks
; }
102 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
105 * Add a title to the link cache, return the page_id or zero if non-existent
106 * @param $title String: title to add
107 * @param $len int, page size
108 * @param $redir bool, is redirect?
111 public function addLink( $title, $len = -1, $redir = NULL ) {
112 $nt = Title
::newFromDBkey( $title );
114 return $this->addLinkObj( $nt, $len, $redir );
121 * Add a title to the link cache, return the page_id or zero if non-existent
122 * @param $nt Title to add.
123 * @param $len int, page size
124 * @param $redir bool, is redirect?
127 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
128 global $wgAntiLockFlags;
129 $title = $nt->getPrefixedDBkey();
130 if ( $this->isBadLink( $title ) ) { return 0; }
131 $id = $this->getGoodLinkID( $title );
132 if ( 0 != $id ) { return $id; }
134 $fname = 'LinkCache::addLinkObj';
136 if ( isset( $wgProfiler ) ) {
137 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
140 wfProfileIn( $fname );
142 $ns = $nt->getNamespace();
143 $t = $nt->getDBkey();
145 if ( '' == $title ) {
146 wfProfileOut( $fname );
149 # Some fields heavily used for linking...
152 if( !is_integer( $id ) ) {
153 if ( $this->mForUpdate
) {
154 $db = wfGetDB( DB_MASTER
);
155 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
156 $options = array( 'FOR UPDATE' );
161 $db = wfGetDB( DB_SLAVE
);
165 $s = $db->selectRow( 'page',
166 array( 'page_id', 'page_len', 'page_is_redirect' ),
167 array( 'page_namespace' => $ns, 'page_title' => $t ),
170 $id = $s ?
$s->page_id
: 0;
171 $len = $s ?
$s->page_len
: -1;
172 $redirect = $s ?
$s->page_is_redirect
: 0;
176 $this->addBadLinkObj( $nt );
178 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
180 wfProfileOut( $fname );
187 public function clear() {
188 $this->mPageLinks
= array();
189 $this->mGoodLinks
= array();
190 $this->mGoodLinkFields
= array();
191 $this->mBadLinks
= array();