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