Update to r32375 / bug 11874 -- !important may have whitespace between ! and important
[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 *
5 * @addtogroup Cache
6 */
7 class LinkCache {
8 // Increment $mClassVer whenever old serialized versions of this class
9 // becomes incompatible with the new version.
10 /* private */ var $mClassVer = 3;
11
12 /* private */ var $mPageLinks;
13 /* private */ var $mGoodLinks, $mBadLinks;
14 /* private */ var $mForUpdate;
15
16 /**
17 * Get an instance of this class
18 */
19 static function &singleton() {
20 static $instance;
21 if ( !isset( $instance ) ) {
22 $instance = new LinkCache;
23 }
24 return $instance;
25 }
26
27 function __construct() {
28 $this->mForUpdate = false;
29 $this->mPageLinks = array();
30 $this->mGoodLinks = array();
31 $this->mBadLinks = array();
32 }
33
34 /* private */ function getKey( $title ) {
35 return wfMemcKey( 'lc', 'title', $title );
36 }
37
38 /**
39 * General accessor to get/set whether SELECT FOR UPDATE should be used
40 */
41 function forUpdate( $update = NULL ) {
42 return wfSetVar( $this->mForUpdate, $update );
43 }
44
45 function getGoodLinkID( $title ) {
46 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
47 return $this->mGoodLinks[$title];
48 } else {
49 return 0;
50 }
51 }
52
53 function isBadLink( $title ) {
54 return array_key_exists( $title, $this->mBadLinks );
55 }
56
57 function addGoodLinkObj( $id, $title ) {
58 $dbkey = $title->getPrefixedDbKey();
59 $this->mGoodLinks[$dbkey] = $id;
60 $this->mPageLinks[$dbkey] = $title;
61 }
62
63 function addBadLinkObj( $title ) {
64 $dbkey = $title->getPrefixedDbKey();
65 if ( ! $this->isBadLink( $dbkey ) ) {
66 $this->mBadLinks[$dbkey] = 1;
67 $this->mPageLinks[$dbkey] = $title;
68 }
69 }
70
71 function clearBadLink( $title ) {
72 unset( $this->mBadLinks[$title] );
73 $this->clearLink( $title );
74 }
75
76 function clearLink( $title ) {
77 global $wgMemc, $wgLinkCacheMemcached;
78 if( $wgLinkCacheMemcached )
79 $wgMemc->delete( $this->getKey( $title ) );
80 }
81
82 function getPageLinks() { return $this->mPageLinks; }
83 function getGoodLinks() { return $this->mGoodLinks; }
84 function getBadLinks() { return array_keys( $this->mBadLinks ); }
85
86 /**
87 * Add a title to the link cache, return the page_id or zero if non-existent
88 * @param $title String: title to add
89 * @return integer
90 */
91 function addLink( $title ) {
92 $nt = Title::newFromDBkey( $title );
93 if( $nt ) {
94 return $this->addLinkObj( $nt );
95 } else {
96 return 0;
97 }
98 }
99
100 /**
101 * Add a title to the link cache, return the page_id or zero if non-existent
102 * @param $nt Title to add.
103 * @return integer
104 */
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; }
111
112 $fname = 'LinkCache::addLinkObj';
113 global $wgProfiling, $wgProfiler;
114 if ( $wgProfiling && isset( $wgProfiler ) ) {
115 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
116 }
117
118 wfProfileIn( $fname );
119
120 $ns = $nt->getNamespace();
121 $t = $nt->getDBkey();
122
123 if ( '' == $title ) {
124 wfProfileOut( $fname );
125 return 0;
126 }
127
128 $id = NULL;
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' );
136 } else {
137 $options = array();
138 }
139 } else {
140 $db = wfGetDB( DB_SLAVE );
141 $options = array();
142 }
143
144 $id = $db->selectField( 'page', 'page_id',
145 array( 'page_namespace' => $ns, 'page_title' => $t ),
146 $fname, $options );
147 if ( !$id ) {
148 $id = 0;
149 }
150 if( $wgLinkCacheMemcached )
151 $wgMemc->add( $key, $id, 3600*24 );
152 }
153
154 if( 0 == $id ) {
155 $this->addBadLinkObj( $nt );
156 } else {
157 $this->addGoodLinkObj( $id, $nt );
158 }
159 wfProfileOut( $fname );
160 return $id;
161 }
162
163 /**
164 * Clears cache
165 */
166 function clear() {
167 $this->mPageLinks = array();
168 $this->mGoodLinks = array();
169 $this->mBadLinks = array();
170 }
171 }
172