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