(bug 32412) TOC links on [[Special:EditWatchlist]] now points to the fieldset
[lhc/web/wiklou.git] / includes / cache / 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 $mClassVer = 4;
11
12 private $mGoodLinks, $mBadLinks;
13 private $mForUpdate;
14
15 /**
16 * Get an instance of this class
17 *
18 * @return LinkCache
19 */
20 static function &singleton() {
21 static $instance;
22 if ( !isset( $instance ) ) {
23 $instance = new LinkCache;
24 }
25 return $instance;
26 }
27
28 function __construct() {
29 $this->mForUpdate = false;
30 $this->mGoodLinks = array();
31 $this->mGoodLinkFields = array();
32 $this->mBadLinks = array();
33 }
34
35 /**
36 * General accessor to get/set whether SELECT FOR UPDATE should be used
37 *
38 * @return bool
39 */
40 public function forUpdate( $update = null ) {
41 return wfSetVar( $this->mForUpdate, $update );
42 }
43
44 /**
45 * @param $title
46 * @return array|int
47 */
48 public function getGoodLinkID( $title ) {
49 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
50 return $this->mGoodLinks[$title];
51 } else {
52 return 0;
53 }
54 }
55
56 /**
57 * Get a field of a title object from cache.
58 * If this link is not good, it will return NULL.
59 * @param $title Title
60 * @param $field String: ('length','redirect','revision')
61 * @return mixed
62 */
63 public function getGoodLinkFieldObj( $title, $field ) {
64 $dbkey = $title->getPrefixedDbKey();
65 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
66 return $this->mGoodLinkFields[$dbkey][$field];
67 } else {
68 return null;
69 }
70 }
71
72 /**
73 * @param $title
74 * @return bool
75 */
76 public function isBadLink( $title ) {
77 return array_key_exists( $title, $this->mBadLinks );
78 }
79
80 /**
81 * Add a link for the title to the link cache
82 *
83 * @param $id Integer: page's ID
84 * @param $title Title object
85 * @param $len Integer: text's length
86 * @param $redir Integer: whether the page is a redirect
87 * @param $revision Integer: latest revision's ID
88 */
89 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
90 $dbkey = $title->getPrefixedDbKey();
91 $this->mGoodLinks[$dbkey] = intval( $id );
92 $this->mGoodLinkFields[$dbkey] = array(
93 'length' => intval( $len ),
94 'redirect' => intval( $redir ),
95 'revision' => intval( $revision ) );
96 }
97
98 /**
99 * Same as above with better interface.
100 * @since 1.19
101 * @param $title Title
102 * @param $row object which has the fields page_id, page_is_redirect,
103 * page_latest
104 */
105 public function addGoodLinkObjFromRow( $title, $row ) {
106 $dbkey = $title->getPrefixedDbKey();
107 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
108 $this->mGoodLinkFields[$dbkey] = array(
109 'length' => intval( $row->page_len ),
110 'redirect' => intval( $row->page_is_redirect ),
111 'revision' => intval( $row->page_latest ),
112 );
113 }
114
115 /**
116 * @param $title Title
117 */
118 public function addBadLinkObj( $title ) {
119 $dbkey = $title->getPrefixedDbKey();
120 if ( !$this->isBadLink( $dbkey ) ) {
121 $this->mBadLinks[$dbkey] = 1;
122 }
123 }
124
125 public function clearBadLink( $title ) {
126 unset( $this->mBadLinks[$title] );
127 }
128
129 /**
130 * @param $title Title
131 */
132 public function clearLink( $title ) {
133 $dbkey = $title->getPrefixedDbKey();
134 unset( $this->mBadLinks[$dbkey] );
135 unset( $this->mGoodLinks[$dbkey] );
136 unset( $this->mGoodLinkFields[$dbkey] );
137 }
138
139 public function getGoodLinks() { return $this->mGoodLinks; }
140 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
141
142 /**
143 * Add a title to the link cache, return the page_id or zero if non-existent
144 *
145 * @param $title String: title to add
146 * @return Integer
147 */
148 public function addLink( $title ) {
149 $nt = Title::newFromDBkey( $title );
150 if( $nt ) {
151 return $this->addLinkObj( $nt );
152 } else {
153 return 0;
154 }
155 }
156
157 /**
158 * Add a title to the link cache, return the page_id or zero if non-existent
159 *
160 * @param $nt Title object to add
161 * @return Integer
162 */
163 public function addLinkObj( $nt ) {
164 global $wgAntiLockFlags;
165 wfProfileIn( __METHOD__ );
166
167 $key = $nt->getPrefixedDBkey();
168 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
169 wfProfileOut( __METHOD__ );
170 return 0;
171 }
172 $id = $this->getGoodLinkID( $key );
173 if ( $id != 0 ) {
174 wfProfileOut( __METHOD__ );
175 return $id;
176 }
177
178 if ( $key === '' ) {
179 wfProfileOut( __METHOD__ );
180 return 0;
181 }
182
183 # Some fields heavily used for linking...
184 if ( $this->mForUpdate ) {
185 $db = wfGetDB( DB_MASTER );
186 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
187 $options = array( 'FOR UPDATE' );
188 } else {
189 $options = array();
190 }
191 } else {
192 $db = wfGetDB( DB_SLAVE );
193 $options = array();
194 }
195
196 $s = $db->selectRow( 'page',
197 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
198 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
199 __METHOD__, $options );
200 # Set fields...
201 if ( $s !== false ) {
202 $this->addGoodLinkObjFromRow( $nt, $s );
203 $id = intval( $s->page_id );
204 } else {
205 $this->addBadLinkObj( $nt );
206 $id = 0;
207 }
208
209 wfProfileOut( __METHOD__ );
210 return $id;
211 }
212
213 /**
214 * Clears cache
215 */
216 public function clear() {
217 $this->mGoodLinks = array();
218 $this->mGoodLinkFields = array();
219 $this->mBadLinks = array();
220 }
221 }