A lot more of static issues when using E_STRICT from the commandline.
[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 LinkCache() {
33 $this->mForUpdate = false;
34 $this->mPageLinks = array();
35 $this->mGoodLinks = array();
36 $this->mBadLinks = array();
37 }
38
39 /* private */ function getKey( $title ) {
40 global $wgDBname;
41 return $wgDBname.':lc:title:'.$title;
42 }
43
44 /**
45 * General accessor to get/set whether SELECT FOR UPDATE should be used
46 */
47 function forUpdate( $update = NULL ) {
48 return wfSetVar( $this->mForUpdate, $update );
49 }
50
51 function getGoodLinkID( $title ) {
52 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
53 return $this->mGoodLinks[$title];
54 } else {
55 return 0;
56 }
57 }
58
59 function isBadLink( $title ) {
60 return array_key_exists( $title, $this->mBadLinks );
61 }
62
63 function addGoodLinkObj( $id, $title ) {
64 $dbkey = $title->getPrefixedDbKey();
65 $this->mGoodLinks[$dbkey] = $id;
66 $this->mPageLinks[$dbkey] = $title;
67 }
68
69 function addBadLinkObj( $title ) {
70 $dbkey = $title->getPrefixedDbKey();
71 if ( ! $this->isBadLink( $dbkey ) ) {
72 $this->mBadLinks[$dbkey] = 1;
73 $this->mPageLinks[$dbkey] = $title;
74 }
75 }
76
77 function clearBadLink( $title ) {
78 unset( $this->mBadLinks[$title] );
79 $this->clearLink( $title );
80 }
81
82 function clearLink( $title ) {
83 global $wgMemc, $wgLinkCacheMemcached;
84 if( $wgLinkCacheMemcached )
85 $wgMemc->delete( $this->getKey( $title ) );
86 }
87
88 function getPageLinks() { return $this->mPageLinks; }
89 function getGoodLinks() { return $this->mGoodLinks; }
90 function getBadLinks() { return array_keys( $this->mBadLinks ); }
91
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
96 */
97 function addLink( $title ) {
98 $nt = Title::newFromDBkey( $title );
99 if( $nt ) {
100 return $this->addLinkObj( $nt );
101 } else {
102 return 0;
103 }
104 }
105
106 /**
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
110 */
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; }
117
118 $fname = 'LinkCache::addLinkObj';
119 global $wgProfiling, $wgProfiler;
120 if ( $wgProfiling && isset( $wgProfiler ) ) {
121 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
122 }
123
124 wfProfileIn( $fname );
125
126 $ns = $nt->getNamespace();
127 $t = $nt->getDBkey();
128
129 if ( '' == $title ) {
130 wfProfileOut( $fname );
131 return 0;
132 }
133
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();
144 }
145 } else {
146 $db =& wfGetDB( DB_SLAVE );
147 $options = array();
148 }
149
150 $id = $db->selectField( 'page', 'page_id',
151 array( 'page_namespace' => $ns, 'page_title' => $t ),
152 $fname, $options );
153 if ( !$id ) {
154 $id = 0;
155 }
156 if( $wgLinkCacheMemcached )
157 $wgMemc->add( $key, $id, 3600*24 );
158 }
159
160 if( 0 == $id ) {
161 $this->addBadLinkObj( $nt );
162 } else {
163 $this->addGoodLinkObj( $id, $nt );
164 }
165 wfProfileOut( $fname );
166 return $id;
167 }
168
169 /**
170 * Clears cache
171 */
172 function clear() {
173 $this->mPageLinks = array();
174 $this->mGoodLinks = array();
175 $this->mBadLinks = array();
176 }
177 }
178 ?>