Localisation updates for core and extension messages from translatewiki.net (2010...
[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 $field String: ('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 $id Integer
71 * @param $title Title
72 * @param $len Integer
73 * @param $redir Integer
74 */
75 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
76 $dbkey = $title->getPrefixedDbKey();
77 $this->mGoodLinks[$dbkey] = intval( $id );
78 $this->mGoodLinkFields[$dbkey] = array(
79 'length' => intval( $len ),
80 'redirect' => intval( $redir ),
81 'revision' => intval( $revision ) );
82 }
83
84 public function addBadLinkObj( $title ) {
85 $dbkey = $title->getPrefixedDbKey();
86 if ( !$this->isBadLink( $dbkey ) ) {
87 $this->mBadLinks[$dbkey] = 1;
88 }
89 }
90
91 public function clearBadLink( $title ) {
92 unset( $this->mBadLinks[$title] );
93 }
94
95 public function clearLink( $title ) {
96 $dbkey = $title->getPrefixedDbKey();
97 if( isset($this->mBadLinks[$dbkey]) ) {
98 unset($this->mBadLinks[$dbkey]);
99 }
100 if( isset($this->mGoodLinks[$dbkey]) ) {
101 unset($this->mGoodLinks[$dbkey]);
102 }
103 if( isset($this->mGoodLinkFields[$dbkey]) ) {
104 unset($this->mGoodLinkFields[$dbkey]);
105 }
106 }
107
108 public function getGoodLinks() { return $this->mGoodLinks; }
109 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
110
111 /**
112 * Add a title to the link cache, return the page_id or zero if non-existent
113 * @param $title String: title to add
114 * @param $len int, page size
115 * @param $redir bool, is redirect?
116 * @return integer
117 */
118 public function addLink( $title, $len = -1, $redir = null ) {
119 $nt = Title::newFromDBkey( $title );
120 if( $nt ) {
121 return $this->addLinkObj( $nt, $len, $redir );
122 } else {
123 return 0;
124 }
125 }
126
127 /**
128 * Add a title to the link cache, return the page_id or zero if non-existent
129 * @param $nt Title to add.
130 * @param $len int, page size
131 * @param $redirect bool, is redirect?
132 * @return integer
133 */
134 public function addLinkObj( &$nt, $len = -1, $redirect = null ) {
135 global $wgAntiLockFlags, $wgProfiler;
136 wfProfileIn( __METHOD__ );
137
138 $key = $nt->getPrefixedDBkey();
139 if ( $this->isBadLink( $key ) ) {
140 wfProfileOut( __METHOD__ );
141 return 0;
142 }
143 $id = $this->getGoodLinkID( $key );
144 if ( $id != 0 ) {
145 wfProfileOut( __METHOD__ );
146 return $id;
147 }
148
149 if ( $key === '' ) {
150 wfProfileOut( __METHOD__ );
151 return 0;
152 }
153
154 # Some fields heavily used for linking...
155 if ( $this->mForUpdate ) {
156 $db = wfGetDB( DB_MASTER );
157 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
158 $options = array( 'FOR UPDATE' );
159 } else {
160 $options = array();
161 }
162 } else {
163 $db = wfGetDB( DB_SLAVE );
164 $options = array();
165 }
166
167 $s = $db->selectRow( 'page',
168 array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
169 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
170 __METHOD__, $options );
171 # Set fields...
172 if ( $s !== false ) {
173 $id = intval( $s->page_id );
174 $len = intval( $s->page_len );
175 $redirect = intval( $s->page_is_redirect );
176 $revision = intval( $s->page_latest );
177 } else {
178 $len = -1;
179 $redirect = 0;
180 $revision = 0;
181 }
182
183 if ( $id == 0 ) {
184 $this->addBadLinkObj( $nt );
185 } else {
186 $this->addGoodLinkObj( $id, $nt, $len, $redirect, $revision );
187 }
188 wfProfileOut( __METHOD__ );
189 return $id;
190 }
191
192 /**
193 * Clears cache
194 */
195 public function clear() {
196 $this->mGoodLinks = array();
197 $this->mGoodLinkFields = array();
198 $this->mBadLinks = array();
199 }
200 }