3 * Cache for article titles (prefixed DB keys) and ids linked from one source
11 // Increment $mClassVer whenever old serialized versions of this class
12 // becomes incompatible with the new version.
13 /* private */ var $mClassVer = 3;
15 /* private */ var $mPageLinks;
16 /* private */ var $mGoodLinks, $mBadLinks;
17 /* private */ var $mForUpdate;
20 * Get an instance of this class
22 static function &singleton() {
24 if ( !isset( $instance ) ) {
25 $instance = new LinkCache
;
30 function __construct() {
31 $this->mForUpdate
= false;
32 $this->mPageLinks
= array();
33 $this->mGoodLinks
= array();
34 $this->mBadLinks
= array();
37 /* private */ function getKey( $title ) {
38 return wfMemcKey( 'lc', 'title', $title );
42 * General accessor to get/set whether SELECT FOR UPDATE should be used
44 function forUpdate( $update = NULL ) {
45 return wfSetVar( $this->mForUpdate
, $update );
48 function getGoodLinkID( $title ) {
49 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
50 return $this->mGoodLinks
[$title];
56 function isBadLink( $title ) {
57 return array_key_exists( $title, $this->mBadLinks
);
60 function addGoodLinkObj( $id, $title ) {
61 $dbkey = $title->getPrefixedDbKey();
62 $this->mGoodLinks
[$dbkey] = $id;
63 $this->mPageLinks
[$dbkey] = $title;
66 function addBadLinkObj( $title ) {
67 $dbkey = $title->getPrefixedDbKey();
68 if ( ! $this->isBadLink( $dbkey ) ) {
69 $this->mBadLinks
[$dbkey] = 1;
70 $this->mPageLinks
[$dbkey] = $title;
74 function clearBadLink( $title ) {
75 unset( $this->mBadLinks
[$title] );
76 $this->clearLink( $title );
79 function clearLink( $title ) {
80 global $wgMemc, $wgLinkCacheMemcached;
81 if( $wgLinkCacheMemcached )
82 $wgMemc->delete( $this->getKey( $title ) );
85 function getPageLinks() { return $this->mPageLinks
; }
86 function getGoodLinks() { return $this->mGoodLinks
; }
87 function getBadLinks() { return array_keys( $this->mBadLinks
); }
90 * Add a title to the link cache, return the page_id or zero if non-existent
91 * @param $title String: title to add
94 function addLink( $title ) {
95 $nt = Title
::newFromDBkey( $title );
97 return $this->addLinkObj( $nt );
104 * Add a title to the link cache, return the page_id or zero if non-existent
105 * @param $nt Title to add.
108 function addLinkObj( &$nt ) {
109 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
110 $title = $nt->getPrefixedDBkey();
111 if ( $this->isBadLink( $title ) ) { return 0; }
112 $id = $this->getGoodLinkID( $title );
113 if ( 0 != $id ) { return $id; }
115 $fname = 'LinkCache::addLinkObj';
116 global $wgProfiling, $wgProfiler;
117 if ( $wgProfiling && isset( $wgProfiler ) ) {
118 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
121 wfProfileIn( $fname );
123 $ns = $nt->getNamespace();
124 $t = $nt->getDBkey();
126 if ( '' == $title ) {
127 wfProfileOut( $fname );
132 if( $wgLinkCacheMemcached )
133 $id = $wgMemc->get( $key = $this->getKey( $title ) );
134 if( ! is_integer( $id ) ) {
135 if ( $this->mForUpdate
) {
136 $db =& wfGetDB( DB_MASTER
);
137 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
138 $options = array( 'FOR UPDATE' );
143 $db =& wfGetDB( DB_SLAVE
);
147 $id = $db->selectField( 'page', 'page_id',
148 array( 'page_namespace' => $ns, 'page_title' => $t ),
153 if( $wgLinkCacheMemcached )
154 $wgMemc->add( $key, $id, 3600*24 );
158 $this->addBadLinkObj( $nt );
160 $this->addGoodLinkObj( $id, $nt );
162 wfProfileOut( $fname );
170 $this->mPageLinks
= array();
171 $this->mGoodLinks
= array();
172 $this->mBadLinks
= array();