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] );
96 /* obsolete, for old $wgLinkCacheMemcached stuff */
97 public function clearLink( $title ) {}
99 public function getPageLinks() { return $this->mPageLinks
; }
100 public function getGoodLinks() { return $this->mGoodLinks
; }
101 public function getBadLinks() { return array_keys( $this->mBadLinks
); }
104 * Add a title to the link cache, return the page_id or zero if non-existent
105 * @param $title String: title to add
106 * @param $len int, page size
107 * @param $redir bool, is redirect?
110 public function addLink( $title, $len = -1, $redir = NULL ) {
111 $nt = Title
::newFromDBkey( $title );
113 return $this->addLinkObj( $nt, $len, $redir );
120 * Add a title to the link cache, return the page_id or zero if non-existent
121 * @param $nt Title to add.
122 * @param $len int, page size
123 * @param $redir bool, is redirect?
126 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
127 global $wgAntiLockFlags, $wgProfiler;
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';
135 if ( isset( $wgProfiler ) ) {
136 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
139 wfProfileIn( $fname );
141 $ns = $nt->getNamespace();
142 $t = $nt->getDBkey();
144 if ( '' == $title ) {
145 wfProfileOut( $fname );
149 # Some fields heavily used for linking...
150 if ( $this->mForUpdate
) {
151 $db = wfGetDB( DB_MASTER
);
152 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
153 $options = array( 'FOR UPDATE' );
158 $db = wfGetDB( DB_SLAVE
);
162 $s = $db->selectRow( 'page',
163 array( 'page_id', 'page_len', 'page_is_redirect' ),
164 array( 'page_namespace' => $ns, 'page_title' => $t ),
167 $id = $s ?
$s->page_id
: 0;
168 $len = $s ?
$s->page_len
: -1;
169 $redirect = $s ?
$s->page_is_redirect
: 0;
172 $this->addBadLinkObj( $nt );
174 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
176 wfProfileOut( $fname );
183 public function clear() {
184 $this->mPageLinks
= array();
185 $this->mGoodLinks
= array();
186 $this->mGoodLinkFields
= array();
187 $this->mBadLinks
= array();