* Fixed documentation
[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 * @param $title Title
100 */
101 public function addBadLinkObj( $title ) {
102 $dbkey = $title->getPrefixedDbKey();
103 if ( !$this->isBadLink( $dbkey ) ) {
104 $this->mBadLinks[$dbkey] = 1;
105 }
106 }
107
108 public function clearBadLink( $title ) {
109 unset( $this->mBadLinks[$title] );
110 }
111
112 /**
113 * @param $title Title
114 */
115 public function clearLink( $title ) {
116 $dbkey = $title->getPrefixedDbKey();
117 if( isset($this->mBadLinks[$dbkey]) ) {
118 unset($this->mBadLinks[$dbkey]);
119 }
120 if( isset($this->mGoodLinks[$dbkey]) ) {
121 unset($this->mGoodLinks[$dbkey]);
122 }
123 if( isset($this->mGoodLinkFields[$dbkey]) ) {
124 unset($this->mGoodLinkFields[$dbkey]);
125 }
126 }
127
128 public function getGoodLinks() { return $this->mGoodLinks; }
129 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
130
131 /**
132 * Add a title to the link cache, return the page_id or zero if non-existent
133 *
134 * @param $title String: title to add
135 * @return Integer
136 */
137 public function addLink( $title ) {
138 $nt = Title::newFromDBkey( $title );
139 if( $nt ) {
140 return $this->addLinkObj( $nt );
141 } else {
142 return 0;
143 }
144 }
145
146 /**
147 * Add a title to the link cache, return the page_id or zero if non-existent
148 *
149 * @param $nt Title object to add
150 * @return Integer
151 */
152 public function addLinkObj( $nt ) {
153 global $wgAntiLockFlags;
154 wfProfileIn( __METHOD__ );
155
156 $key = $nt->getPrefixedDBkey();
157 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
158 wfProfileOut( __METHOD__ );
159 return 0;
160 }
161 $id = $this->getGoodLinkID( $key );
162 if ( $id != 0 ) {
163 wfProfileOut( __METHOD__ );
164 return $id;
165 }
166
167 if ( $key === '' ) {
168 wfProfileOut( __METHOD__ );
169 return 0;
170 }
171
172 # Some fields heavily used for linking...
173 if ( $this->mForUpdate ) {
174 $db = wfGetDB( DB_MASTER );
175 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
176 $options = array( 'FOR UPDATE' );
177 } else {
178 $options = array();
179 }
180 } else {
181 $db = wfGetDB( DB_SLAVE );
182 $options = array();
183 }
184
185 $s = $db->selectRow( 'page',
186 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
187 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
188 __METHOD__, $options );
189 # Set fields...
190 if ( $s !== false ) {
191 $id = intval( $s->page_id );
192 $len = intval( $s->page_len );
193 $redirect = intval( $s->page_is_redirect );
194 $revision = intval( $s->page_latest );
195 } else {
196 $id = 0;
197 $len = -1;
198 $redirect = 0;
199 $revision = 0;
200 }
201
202 if ( $id == 0 ) {
203 $this->addBadLinkObj( $nt );
204 } else {
205 $this->addGoodLinkObj( $id, $nt, $len, $redirect, $revision );
206 }
207 wfProfileOut( __METHOD__ );
208 return $id;
209 }
210
211 /**
212 * Clears cache
213 */
214 public function clear() {
215 $this->mGoodLinks = array();
216 $this->mGoodLinkFields = array();
217 $this->mBadLinks = array();
218 }
219 }