Experimental: use absolute paths in require_once
[mediawiki.git] / includes / LinkCache.php
blob451b3f0c0a83b8813efe662b1bc2b7849af28deb
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 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 global $wgDBname;
41 return $wgDBname.':lc:title:'.$title;
44 /**
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];
54 } else {
55 return 0;
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 ); }
92 /**
93 * Add a title to the link cache, return the page_id or zero if non-existent
94 * @param $title String: title to add
95 * @return integer
97 function addLink( $title ) {
98 $nt = Title::newFromDBkey( $title );
99 if( $nt ) {
100 return $this->addLinkObj( $nt );
101 } else {
102 return 0;
107 * Add a title to the link cache, return the page_id or zero if non-existent
108 * @param $nt Title to add.
109 * @return integer
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 );
131 return 0;
134 $id = NULL;
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' );
142 } else {
143 $options = array();
145 } else {
146 $db =& wfGetDB( DB_SLAVE );
147 $options = array();
150 $id = $db->selectField( 'page', 'page_id',
151 array( 'page_namespace' => $ns, 'page_title' => $t ),
152 $fname, $options );
153 if ( !$id ) {
154 $id = 0;
156 if( $wgLinkCacheMemcached )
157 $wgMemc->add( $key, $id, 3600*24 );
160 if( 0 == $id ) {
161 $this->addBadLinkObj( $nt );
162 } else {
163 $this->addGoodLinkObj( $id, $nt );
165 wfProfileOut( $fname );
166 return $id;
170 * Clears cache
172 function clear() {
173 $this->mPageLinks = array();
174 $this->mGoodLinks = array();
175 $this->mBadLinks = array();