Bug 7687: Fix movetalk box checks itself when confirming a delete and move; Patch...
[mediawiki.git] / includes / LinkCache.php
blob8e56225ba0f09c9829eb2a412566928b1ad4f858
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
8 /**
9 * @package MediaWiki
10 * @subpackage Cache
12 class LinkCache {
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;
21 /**
22 * Get an instance of this class
24 static function &singleton() {
25 static $instance;
26 if ( !isset( $instance ) ) {
27 $instance = new LinkCache;
29 return $instance;
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 );
43 /**
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];
53 } else {
54 return 0;
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 ); }
91 /**
92 * Add a title to the link cache, return the page_id or zero if non-existent
93 * @param $title String: title to add
94 * @return integer
96 function addLink( $title ) {
97 $nt = Title::newFromDBkey( $title );
98 if( $nt ) {
99 return $this->addLinkObj( $nt );
100 } else {
101 return 0;
106 * Add a title to the link cache, return the page_id or zero if non-existent
107 * @param $nt Title to add.
108 * @return integer
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 );
130 return 0;
133 $id = NULL;
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' );
141 } else {
142 $options = array();
144 } else {
145 $db =& wfGetDB( DB_SLAVE );
146 $options = array();
149 $id = $db->selectField( 'page', 'page_id',
150 array( 'page_namespace' => $ns, 'page_title' => $t ),
151 $fname, $options );
152 if ( !$id ) {
153 $id = 0;
155 if( $wgLinkCacheMemcached )
156 $wgMemc->add( $key, $id, 3600*24 );
159 if( 0 == $id ) {
160 $this->addBadLinkObj( $nt );
161 } else {
162 $this->addGoodLinkObj( $id, $nt );
164 wfProfileOut( $fname );
165 return $id;
169 * Clears cache
171 function clear() {
172 $this->mPageLinks = array();
173 $this->mGoodLinks = array();
174 $this->mBadLinks = array();