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 = 3;
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->mBadLinks
= array();
34 /* private */ function getKey( $title ) {
35 return wfMemcKey( 'lc', 'title', $title );
39 * General accessor to get/set whether SELECT FOR UPDATE should be used
41 function forUpdate( $update = NULL ) {
42 return wfSetVar( $this->mForUpdate
, $update );
45 function getGoodLinkID( $title ) {
46 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
47 return $this->mGoodLinks
[$title];
53 function isBadLink( $title ) {
54 return array_key_exists( $title, $this->mBadLinks
);
57 function addGoodLinkObj( $id, $title ) {
58 $dbkey = $title->getPrefixedDbKey();
59 $this->mGoodLinks
[$dbkey] = $id;
60 $this->mPageLinks
[$dbkey] = $title;
63 function addBadLinkObj( $title ) {
64 $dbkey = $title->getPrefixedDbKey();
65 if ( ! $this->isBadLink( $dbkey ) ) {
66 $this->mBadLinks
[$dbkey] = 1;
67 $this->mPageLinks
[$dbkey] = $title;
71 function clearBadLink( $title ) {
72 unset( $this->mBadLinks
[$title] );
73 $this->clearLink( $title );
76 function clearLink( $title ) {
77 global $wgMemc, $wgLinkCacheMemcached;
78 if( $wgLinkCacheMemcached )
79 $wgMemc->delete( $this->getKey( $title ) );
82 function getPageLinks() { return $this->mPageLinks
; }
83 function getGoodLinks() { return $this->mGoodLinks
; }
84 function getBadLinks() { return array_keys( $this->mBadLinks
); }
87 * Add a title to the link cache, return the page_id or zero if non-existent
88 * @param $title String: title to add
91 function addLink( $title ) {
92 $nt = Title
::newFromDBkey( $title );
94 return $this->addLinkObj( $nt );
101 * Add a title to the link cache, return the page_id or zero if non-existent
102 * @param $nt Title to add.
105 function addLinkObj( &$nt ) {
106 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
107 $title = $nt->getPrefixedDBkey();
108 if ( $this->isBadLink( $title ) ) { return 0; }
109 $id = $this->getGoodLinkID( $title );
110 if ( 0 != $id ) { return $id; }
112 $fname = 'LinkCache::addLinkObj';
113 global $wgProfiling, $wgProfiler;
114 if ( $wgProfiling && isset( $wgProfiler ) ) {
115 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
118 wfProfileIn( $fname );
120 $ns = $nt->getNamespace();
121 $t = $nt->getDBkey();
123 if ( '' == $title ) {
124 wfProfileOut( $fname );
129 if( $wgLinkCacheMemcached )
130 $id = $wgMemc->get( $key = $this->getKey( $title ) );
131 if( ! is_integer( $id ) ) {
132 if ( $this->mForUpdate
) {
133 $db = wfGetDB( DB_MASTER
);
134 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
135 $options = array( 'FOR UPDATE' );
140 $db = wfGetDB( DB_SLAVE
);
144 $id = $db->selectField( 'page', 'page_id',
145 array( 'page_namespace' => $ns, 'page_title' => $t ),
150 if( $wgLinkCacheMemcached )
151 $wgMemc->add( $key, $id, 3600*24 );
155 $this->addBadLinkObj( $nt );
157 $this->addGoodLinkObj( $id, $nt );
159 wfProfileOut( $fname );
167 $this->mPageLinks
= array();
168 $this->mGoodLinks
= array();
169 $this->mBadLinks
= array();