55aaae8805df438631837d51ab6da2c93f7afd90
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();
35 /* private */ function getKey( $title ) {
36 return wfMemcKey( 'lc', 'title', $title );
40 * General accessor to get/set whether SELECT FOR UPDATE should be used
42 function forUpdate( $update = NULL ) {
43 return wfSetVar( $this->mForUpdate
, $update );
46 function getGoodLinkID( $title ) {
47 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
48 return $this->mGoodLinks
[$title];
55 * Get a field of a title object from cache.
56 * If this link is not good, it will return NULL.
58 * @param string $field ('length','redirect')
61 function getGoodLinkFieldObj( $title, $field ) {
62 $dbkey = $title->getPrefixedDbKey();
63 if ( array_key_exists( $dbkey, $this->mGoodLinkFields
) ) {
64 return $this->mGoodLinkFields
[$dbkey][$field];
70 function isBadLink( $title ) {
71 return array_key_exists( $title, $this->mBadLinks
);
75 * Add a link for the title to the link cache
81 function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
82 $dbkey = $title->getPrefixedDbKey();
83 $this->mGoodLinks
[$dbkey] = $id;
84 $this->mGoodLinkFields
[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
85 $this->mPageLinks
[$dbkey] = $title;
88 function addBadLinkObj( $title ) {
89 $dbkey = $title->getPrefixedDbKey();
90 if ( ! $this->isBadLink( $dbkey ) ) {
91 $this->mBadLinks
[$dbkey] = 1;
92 $this->mPageLinks
[$dbkey] = $title;
96 function clearBadLink( $title ) {
97 unset( $this->mBadLinks
[$title] );
98 $this->clearLink( $title );
101 function clearLink( $title ) {
102 global $wgMemc, $wgLinkCacheMemcached;
103 if( $wgLinkCacheMemcached )
104 $wgMemc->delete( $this->getKey( $title ) );
107 function getPageLinks() { return $this->mPageLinks
; }
108 function getGoodLinks() { return $this->mGoodLinks
; }
109 function getBadLinks() { return array_keys( $this->mBadLinks
); }
112 * Add a title to the link cache, return the page_id or zero if non-existent
113 * @param $title String: title to add
116 function addLink( $title ) {
117 $nt = Title
::newFromDBkey( $title );
119 return $this->addLinkObj( $nt );
126 * Add a title to the link cache, return the page_id or zero if non-existent
127 * @param $nt Title to add.
130 function addLinkObj( &$nt ) {
131 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
132 $title = $nt->getPrefixedDBkey();
133 if ( $this->isBadLink( $title ) ) { return 0; }
134 $id = $this->getGoodLinkID( $title );
135 if ( 0 != $id ) { return $id; }
137 $fname = 'LinkCache::addLinkObj';
139 if ( isset( $wgProfiler ) ) {
140 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
143 wfProfileIn( $fname );
145 $ns = $nt->getNamespace();
146 $t = $nt->getDBkey();
148 if ( '' == $title ) {
149 wfProfileOut( $fname );
152 # Some fields heavily used for linking...
157 if( $wgLinkCacheMemcached ) {
158 $id = $wgMemc->get( $key = $this->getKey( $title ) );
160 if( !is_integer( $id ) ) {
161 if ( $this->mForUpdate
) {
162 $db = wfGetDB( DB_MASTER
);
163 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
164 $options = array( 'FOR UPDATE' );
169 $db = wfGetDB( DB_SLAVE
);
173 $s = $db->selectRow( 'page',
174 array( 'page_id', 'page_len', 'page_is_redirect' ),
175 array( 'page_namespace' => $ns, 'page_title' => $t ),
178 $id = $s ?
$s->page_id
: 0;
179 $len = $s ?
$s->page_len
: -1;
180 $redirect = $s ?
$s->page_is_redirect
: 0;
182 if( $wgLinkCacheMemcached ) {
183 $wgMemc->add( $key, $id, 3600*24 );
188 $this->addBadLinkObj( $nt );
190 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
192 wfProfileOut( $fname );
200 $this->mPageLinks
= array();
201 $this->mGoodLinks
= array();
202 $this->mGoodLinkFields
= array();
203 $this->mBadLinks
= array();