0bfb8fac965f11a124bfdc8599d8bbe0a890c5c2
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 * @package MediaWiki
10 * @subpackage Cache
11 */
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;
16
17 /* private */ var $mPageLinks;
18 /* private */ var $mGoodLinks, $mBadLinks;
19 /* private */ var $mForUpdate;
20
21 /**
22 * Get an instance of this class
23 */
24 static function &singleton() {
25 static $instance;
26 if ( !isset( $instance ) ) {
27 $instance = new LinkCache;
28 }
29 return $instance;
30 }
31
32 function __construct() {
33 $this->mForUpdate = false;
34 $this->mPageLinks = array();
35 $this->mGoodLinks = array();
36 $this->mBadLinks = array();
37 }
38
39 /* private */ function getKey( $title ) {
40 return wfMemcKey( 'lc', 'title', $title );
41 }
42
43 /**
44 * General accessor to get/set whether SELECT FOR UPDATE should be used
45 */
46 function forUpdate( $update = NULL ) {
47 return wfSetVar( $this->mForUpdate, $update );
48 }
49
50 function getGoodLinkID( $title ) {
51 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
52 return $this->mGoodLinks[$title];
53 } else {
54 return 0;
55 }
56 }
57
58 function isBadLink( $title ) {
59 return array_key_exists( $title, $this->mBadLinks );
60 }
61
62 function addGoodLinkObj( $id, $title ) {
63 $dbkey = $title->getPrefixedDbKey();
64 $this->mGoodLinks[$dbkey] = $id;
65 $this->mPageLinks[$dbkey] = $title;
66 }
67
68 function addBadLinkObj( $title ) {
69 $dbkey = $title->getPrefixedDbKey();
70 if ( ! $this->isBadLink( $dbkey ) ) {
71 $this->mBadLinks[$dbkey] = 1;
72 $this->mPageLinks[$dbkey] = $title;
73 }
74 }
75
76 function clearBadLink( $title ) {
77 unset( $this->mBadLinks[$title] );
78 $this->clearLink( $title );
79 }
80
81 function clearLink( $title ) {
82 global $wgMemc, $wgLinkCacheMemcached;
83 if( $wgLinkCacheMemcached )
84 $wgMemc->delete( $this->getKey( $title ) );
85 }
86
87 function getPageLinks() { return $this->mPageLinks; }
88 function getGoodLinks() { return $this->mGoodLinks; }
89 function getBadLinks() { return array_keys( $this->mBadLinks ); }
90
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
95 */
96 function addLink( $title ) {
97 $nt = Title::newFromDBkey( $title );
98 if( $nt ) {
99 return $this->addLinkObj( $nt );
100 } else {
101 return 0;
102 }
103 }
104
105 /**
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
109 */
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; }
116
117 $fname = 'LinkCache::addLinkObj';
118 global $wgProfiling, $wgProfiler;
119 if ( $wgProfiling && isset( $wgProfiler ) ) {
120 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
121 }
122
123 wfProfileIn( $fname );
124
125 $ns = $nt->getNamespace();
126 $t = $nt->getDBkey();
127
128 if ( '' == $title ) {
129 wfProfileOut( $fname );
130 return 0;
131 }
132
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();
143 }
144 } else {
145 $db =& wfGetDB( DB_SLAVE );
146 $options = array();
147 }
148
149 $id = $db->selectField( 'page', 'page_id',
150 array( 'page_namespace' => $ns, 'page_title' => $t ),
151 $fname, $options );
152 if ( !$id ) {
153 $id = 0;
154 }
155 if( $wgLinkCacheMemcached )
156 $wgMemc->add( $key, $id, 3600*24 );
157 }
158
159 if( 0 == $id ) {
160 $this->addBadLinkObj( $nt );
161 } else {
162 $this->addGoodLinkObj( $id, $nt );
163 }
164 wfProfileOut( $fname );
165 return $id;
166 }
167
168 /**
169 * Clears cache
170 */
171 function clear() {
172 $this->mPageLinks = array();
173 $this->mGoodLinks = array();
174 $this->mBadLinks = array();
175 }
176 }
177 ?>