3 * Cache for article titles (prefixed DB keys) and ids linked from one source
13 // Increment $mClassVer whenever old serialized versions of this class
14 // becomes incompatible with the new version.
15 /* private */ var $mClassVer = 3;
17 /* private */ var $mPageLinks;
18 /* private */ var $mGoodLinks, $mBadLinks;
19 /* private */ var $mForUpdate;
22 * Get an instance of this class
24 static function &singleton() {
26 if ( !isset( $instance ) ) {
27 $instance = new LinkCache
;
32 function LinkCache() {
33 $this->mForUpdate
= false;
34 $this->mPageLinks
= array();
35 $this->mGoodLinks
= array();
36 $this->mBadLinks
= array();
39 /* private */ function getKey( $title ) {
41 return $wgDBname.':lc:title:'.$title;
45 * General accessor to get/set whether SELECT FOR UPDATE should be used
47 function forUpdate( $update = NULL ) {
48 return wfSetVar( $this->mForUpdate
, $update );
51 function getGoodLinkID( $title ) {
52 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
53 return $this->mGoodLinks
[$title];
59 function isBadLink( $title ) {
60 return array_key_exists( $title, $this->mBadLinks
);
63 function addGoodLinkObj( $id, $title ) {
64 $dbkey = $title->getPrefixedDbKey();
65 $this->mGoodLinks
[$dbkey] = $id;
66 $this->mPageLinks
[$dbkey] = $title;
69 function addBadLinkObj( $title ) {
70 $dbkey = $title->getPrefixedDbKey();
71 if ( ! $this->isBadLink( $dbkey ) ) {
72 $this->mBadLinks
[$dbkey] = 1;
73 $this->mPageLinks
[$dbkey] = $title;
77 function clearBadLink( $title ) {
78 unset( $this->mBadLinks
[$title] );
79 $this->clearLink( $title );
82 function clearLink( $title ) {
83 global $wgMemc, $wgLinkCacheMemcached;
84 if( $wgLinkCacheMemcached )
85 $wgMemc->delete( $this->getKey( $title ) );
88 function getPageLinks() { return $this->mPageLinks
; }
89 function getGoodLinks() { return $this->mGoodLinks
; }
90 function getBadLinks() { return array_keys( $this->mBadLinks
); }
93 * Add a title to the link cache, return the page_id or zero if non-existent
94 * @param $title String: title to add
97 function addLink( $title ) {
98 $nt = Title
::newFromDBkey( $title );
100 return $this->addLinkObj( $nt );
107 * Add a title to the link cache, return the page_id or zero if non-existent
108 * @param $nt Title to add.
111 function addLinkObj( &$nt ) {
112 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
113 $title = $nt->getPrefixedDBkey();
114 if ( $this->isBadLink( $title ) ) { return 0; }
115 $id = $this->getGoodLinkID( $title );
116 if ( 0 != $id ) { return $id; }
118 $fname = 'LinkCache::addLinkObj';
119 global $wgProfiling, $wgProfiler;
120 if ( $wgProfiling && isset( $wgProfiler ) ) {
121 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
124 wfProfileIn( $fname );
126 $ns = $nt->getNamespace();
127 $t = $nt->getDBkey();
129 if ( '' == $title ) {
130 wfProfileOut( $fname );
135 if( $wgLinkCacheMemcached )
136 $id = $wgMemc->get( $key = $this->getKey( $title ) );
137 if( ! is_integer( $id ) ) {
138 if ( $this->mForUpdate
) {
139 $db =& wfGetDB( DB_MASTER
);
140 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
141 $options = array( 'FOR UPDATE' );
146 $db =& wfGetDB( DB_SLAVE
);
150 $id = $db->selectField( 'page', 'page_id',
151 array( 'page_namespace' => $ns, 'page_title' => $t ),
156 if( $wgLinkCacheMemcached )
157 $wgMemc->add( $key, $id, 3600*24 );
161 $this->addBadLinkObj( $nt );
163 $this->addGoodLinkObj( $id, $nt );
165 wfProfileOut( $fname );
173 $this->mPageLinks
= array();
174 $this->mGoodLinks
= array();
175 $this->mBadLinks
= array();