Fully deprecate $wgProxyKey. Has been marked as deprecated since 1.4, but never seems...
[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 * @ingroup 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 = 4;
11
12 /* private */ var $mGoodLinks, $mBadLinks;
13 /* private */ var $mForUpdate;
14
15 /**
16 * Get an instance of this class
17 */
18 static function &singleton() {
19 static $instance;
20 if ( !isset( $instance ) ) {
21 $instance = new LinkCache;
22 }
23 return $instance;
24 }
25
26 function __construct() {
27 $this->mForUpdate = false;
28 $this->mGoodLinks = array();
29 $this->mGoodLinkFields = array();
30 $this->mBadLinks = array();
31 }
32
33 /**
34 * General accessor to get/set whether SELECT FOR UPDATE should be used
35 */
36 public function forUpdate( $update = NULL ) {
37 return wfSetVar( $this->mForUpdate, $update );
38 }
39
40 public function getGoodLinkID( $title ) {
41 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
42 return $this->mGoodLinks[$title];
43 } else {
44 return 0;
45 }
46 }
47
48 /**
49 * Get a field of a title object from cache.
50 * If this link is not good, it will return NULL.
51 * @param Title $title
52 * @param string $field ('length','redirect')
53 * @return mixed
54 */
55 public function getGoodLinkFieldObj( $title, $field ) {
56 $dbkey = $title->getPrefixedDbKey();
57 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
58 return $this->mGoodLinkFields[$dbkey][$field];
59 } else {
60 return NULL;
61 }
62 }
63
64 public function isBadLink( $title ) {
65 return array_key_exists( $title, $this->mBadLinks );
66 }
67
68 /**
69 * Add a link for the title to the link cache
70 * @param int $id
71 * @param Title $title
72 * @param int $len
73 * @param int $redir
74 */
75 public function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
76 $dbkey = $title->getPrefixedDbKey();
77 $this->mGoodLinks[$dbkey] = $id;
78 $this->mGoodLinkFields[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
79 }
80
81 public function addBadLinkObj( $title ) {
82 $dbkey = $title->getPrefixedDbKey();
83 if ( !$this->isBadLink( $dbkey ) ) {
84 $this->mBadLinks[$dbkey] = 1;
85 }
86 }
87
88 public function clearBadLink( $title ) {
89 unset( $this->mBadLinks[$title] );
90 }
91
92 /* obsolete, for old $wgLinkCacheMemcached stuff */
93 public function clearLink( $title ) {}
94
95 public function getGoodLinks() { return $this->mGoodLinks; }
96 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
97
98 /**
99 * Add a title to the link cache, return the page_id or zero if non-existent
100 * @param $title String: title to add
101 * @param $len int, page size
102 * @param $redir bool, is redirect?
103 * @return integer
104 */
105 public function addLink( $title, $len = -1, $redir = NULL ) {
106 $nt = Title::newFromDBkey( $title );
107 if( $nt ) {
108 return $this->addLinkObj( $nt, $len, $redir );
109 } else {
110 return 0;
111 }
112 }
113
114 /**
115 * Add a title to the link cache, return the page_id or zero if non-existent
116 * @param $nt Title to add.
117 * @param $len int, page size
118 * @param $redir bool, is redirect?
119 * @return integer
120 */
121 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
122 global $wgAntiLockFlags, $wgProfiler;
123 wfProfileIn( __METHOD__ );
124
125 $key = $nt->getPrefixedDBkey();
126 if ( $this->isBadLink( $key ) ) {
127 wfProfileOut( __METHOD__ );
128 return 0;
129 }
130 $id = $this->getGoodLinkID( $key );
131 if ( $id != 0 ) {
132 wfProfileOut( __METHOD__ );
133 return $id;
134 }
135
136 if ( $key === '' ) {
137 wfProfileOut( __METHOD__ );
138 return 0;
139 }
140
141 # Some fields heavily used for linking...
142 if ( $this->mForUpdate ) {
143 $db = wfGetDB( DB_MASTER );
144 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
145 $options = array( 'FOR UPDATE' );
146 } else {
147 $options = array();
148 }
149 } else {
150 $db = wfGetDB( DB_SLAVE );
151 $options = array();
152 }
153
154 $s = $db->selectRow( 'page',
155 array( 'page_id', 'page_len', 'page_is_redirect' ),
156 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
157 __METHOD__, $options );
158 # Set fields...
159 if ( $s !== false ) {
160 $id = $s->page_id;
161 $len = $s->page_len;
162 $redirect = $s->page_is_redirect;
163 } else {
164 $len = -1;
165 $redirect = 0;
166 }
167
168 if ( $id == 0 ) {
169 $this->addBadLinkObj( $nt );
170 } else {
171 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
172 }
173 wfProfileOut( __METHOD__ );
174 return $id;
175 }
176
177 /**
178 * Clears cache
179 */
180 public function clear() {
181 $this->mGoodLinks = array();
182 $this->mGoodLinkFields = array();
183 $this->mBadLinks = array();
184 }
185 }