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 ) {
40 return wfMemcKey( 'lc', 'title', $title );
44 * General accessor to get/set whether SELECT FOR UPDATE should be used
46 function forUpdate( $update = NULL ) {
47 return wfSetVar( $this->mForUpdate
, $update );
50 function getGoodLinkID( $title ) {
51 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
52 return $this->mGoodLinks
[$title];
58 function isBadLink( $title ) {
59 return array_key_exists( $title, $this->mBadLinks
);
62 function addGoodLinkObj( $id, $title ) {
63 $dbkey = $title->getPrefixedDbKey();
64 $this->mGoodLinks
[$dbkey] = $id;
65 $this->mPageLinks
[$dbkey] = $title;
68 function addBadLinkObj( $title ) {
69 $dbkey = $title->getPrefixedDbKey();
70 if ( ! $this->isBadLink( $dbkey ) ) {
71 $this->mBadLinks
[$dbkey] = 1;
72 $this->mPageLinks
[$dbkey] = $title;
76 function clearBadLink( $title ) {
77 unset( $this->mBadLinks
[$title] );
78 $this->clearLink( $title );
81 function clearLink( $title ) {
82 global $wgMemc, $wgLinkCacheMemcached;
83 if( $wgLinkCacheMemcached )
84 $wgMemc->delete( $this->getKey( $title ) );
87 function getPageLinks() { return $this->mPageLinks
; }
88 function getGoodLinks() { return $this->mGoodLinks
; }
89 function getBadLinks() { return array_keys( $this->mBadLinks
); }
92 * Add a title to the link cache, return the page_id or zero if non-existent
93 * @param $title String: title to add
96 function addLink( $title ) {
97 $nt = Title
::newFromDBkey( $title );
99 return $this->addLinkObj( $nt );
106 * Add a title to the link cache, return the page_id or zero if non-existent
107 * @param $nt Title to add.
110 function addLinkObj( &$nt ) {
111 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
112 $title = $nt->getPrefixedDBkey();
113 if ( $this->isBadLink( $title ) ) { return 0; }
114 $id = $this->getGoodLinkID( $title );
115 if ( 0 != $id ) { return $id; }
117 $fname = 'LinkCache::addLinkObj';
118 global $wgProfiling, $wgProfiler;
119 if ( $wgProfiling && isset( $wgProfiler ) ) {
120 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
123 wfProfileIn( $fname );
125 $ns = $nt->getNamespace();
126 $t = $nt->getDBkey();
128 if ( '' == $title ) {
129 wfProfileOut( $fname );
134 if( $wgLinkCacheMemcached )
135 $id = $wgMemc->get( $key = $this->getKey( $title ) );
136 if( ! is_integer( $id ) ) {
137 if ( $this->mForUpdate
) {
138 $db =& wfGetDB( DB_MASTER
);
139 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
140 $options = array( 'FOR UPDATE' );
145 $db =& wfGetDB( DB_SLAVE
);
149 $id = $db->selectField( 'page', 'page_id',
150 array( 'page_namespace' => $ns, 'page_title' => $t ),
155 if( $wgLinkCacheMemcached )
156 $wgMemc->add( $key, $id, 3600*24 );
160 $this->addBadLinkObj( $nt );
162 $this->addGoodLinkObj( $id, $nt );
164 wfProfileOut( $fname );
172 $this->mPageLinks
= array();
173 $this->mGoodLinks
= array();
174 $this->mBadLinks
= array();