changing wfQuery to allow replication
[lhc/web/wiklou.git] / includes / Title.php
1 <?
2 # See title.doc
3
4 class Title {
5 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
6 /* private */ var $mNamespace, $mInterwiki, $mFragment;
7 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
8
9 /* private */ function Title()
10 {
11 $this->mInterwiki = $this->mUrlform =
12 $this->mTextform = $this->mDbkeyform = "";
13 $this->mArticleID = -1;
14 $this->mNamespace = 0;
15 $this->mRestrictionsLoaded = false;
16 $this->mRestrictions = array();
17 }
18
19 # Static factory methods
20 #
21 function newFromDBkey( $key )
22 {
23 $t = new Title();
24 $t->mDbkeyform = $key;
25 $t->secureAndSplit();
26 return $t;
27 }
28
29 function newFromText( $text )
30 {
31 wfProfileIn( "Title::newFromText" );
32
33 # Note - mixing latin1 named entities and unicode numbered
34 # ones will result in a bad link.
35 $trans = get_html_translation_table( HTML_ENTITIES );
36 $trans = array_flip( $trans );
37 $text = strtr( $text, $trans );
38
39 $text = wfMungeToUtf8( $text );
40
41 $text = urldecode( $text );
42
43 $t = new Title();
44 $t->mDbkeyform = str_replace( " ", "_", $text );
45 $t->secureAndSplit();
46
47 wfProfileOut();
48 return $t;
49 }
50
51 function newFromURL( $url )
52 {
53 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
54
55 $t = new Title();
56 $s = urldecode( $url ); # This is technically wrong, as anything
57 # we've gotten is already decoded by PHP.
58 # Kept for backwards compatibility with
59 # buggy URLs we had for a while...
60
61 # For links that came from outside, check for alternate/legacy
62 # character encoding.
63 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
64 $s = $wgLang->checkTitleEncoding( $s );
65
66 $t->mDbkeyform = str_replace( " ", "_", $s );
67 $t->secureAndSplit();
68 return $t;
69 }
70
71 function nameOf( $id )
72 {
73 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
74 "cur_id={$id}";
75 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
76 if ( 0 == wfNumRows( $res ) ) { return NULL; }
77
78 $s = wfFetchObject( $res );
79 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
80 return $n;
81 }
82
83
84 function legalChars()
85 {
86 global $wgInputEncoding;
87 if( $wgInputEncoding == "utf-8" ) {
88 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
89 } else {
90 # ISO 8859-* don't allow 0x80-0x9F
91 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
92 # But that breaks interlanguage links at the moment. Temporary:
93 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
94 }
95 }
96
97 function getInterwikiLink( $key )
98 {
99 global $wgMemc, $wgDBname;
100 $k = "$wgDBname:interwiki:$key";
101 $s = $wgMemc->get( $k );
102 if( $s !== false ) return $s->iw_url;
103
104 $dkey = wfStrencode( $key );
105 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
106 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
107 if(!$res) return "";
108
109 $s = wfFetchObject( $res );
110 if(!$s) {
111 $s = (object)false;
112 $s->iw_url = "";
113 }
114 $wgMemc->set( $k, $s );
115 return $s->iw_url;
116 }
117
118 function getText() { return $this->mTextform; }
119 function getURL() { return $this->mUrlform; }
120 function getDBkey() { return $this->mDbkeyform; }
121 function getNamespace() { return $this->mNamespace; }
122 function setNamespace( $n ) { $this->mNamespace = $n; }
123 function getInterwiki() { return $this->mInterwiki; }
124 function getFragment() { return $this->mFragment; }
125
126 /* static */ function indexTitle( $ns, $title )
127 {
128 global $wgDBminWordLen, $wgLang;
129
130 $lc = SearchEngine::legalSearchChars() . "&#;";
131 $t = $wgLang->stripForSearch( $title );
132 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
133 $t = strtolower( $t );
134
135 # Handle 's, s'
136 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
137 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
138
139 $t = preg_replace( "/\\s+/", " ", $t );
140
141 if ( $ns == Namespace::getImage() ) {
142 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
143 }
144 return trim( $t );
145 }
146
147 function getIndexTitle()
148 {
149 return Title::indexTitle( $this->mNamespace, $this->mTextform );
150 }
151
152 /* static */ function makeName( $ns, $title )
153 {
154 global $wgLang;
155
156 $n = $wgLang->getNsText( $ns );
157 if ( "" == $n ) { return $title; }
158 else { return "{$n}:{$title}"; }
159 }
160
161 /* static */ function makeTitle( $ns, $title )
162 {
163 $t = new Title();
164 $t->mDbkeyform = Title::makeName( $ns, $title );
165 $t->secureAndSplit();
166 return $t;
167 }
168
169 function getPrefixedDBkey()
170 {
171 $s = $this->prefix( $this->mDbkeyform );
172 $s = str_replace( " ", "_", $s );
173 return $s;
174 }
175
176 function getPrefixedText()
177 {
178 $s = $this->prefix( $this->mTextform );
179 $s = str_replace( "_", " ", $s );
180 return $s;
181 }
182
183 function getPrefixedURL()
184 {
185 $s = $this->prefix( $this->mDbkeyform );
186 $s = str_replace( " ", "_", $s );
187
188 $s = urlencode ( $s ) ;
189 # Cleaning up URL to make it look nice -- is this safe?
190 $s = preg_replace( "/%3[Aa]/", ":", $s );
191 $s = preg_replace( "/%2[Ff]/", "/", $s );
192 $s = str_replace( "%28", "(", $s );
193 $s = str_replace( "%29", ")", $s );
194 return $s;
195 }
196
197 function getFullURL()
198 {
199 global $wgLang, $wgArticlePath;
200
201 if ( "" == $this->mInterwiki ) {
202 $p = $wgArticlePath;
203 } else {
204 $p = $this->getInterwikiLink( $this->mInterwiki );
205 }
206 $n = $wgLang->getNsText( $this->mNamespace );
207 if ( "" != $n ) { $n .= ":"; }
208 $u = str_replace( "$1", $n . $this->mUrlform, $p );
209 if ( "" != $this->mFragment ) {
210 $u .= "#" . $this->mFragment;
211 }
212 return $u;
213 }
214
215 function getEditURL()
216 {
217 global $wgServer, $wgScript;
218
219 if ( "" != $this->mInterwiki ) { return ""; }
220 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
221
222 return $s;
223 }
224
225 function isExternal() { return ( "" != $this->mInterwiki ); }
226
227 function isProtected()
228 {
229 if ( -1 == $this->mNamespace ) { return true; }
230 $a = $this->getRestrictions();
231 if ( in_array( "sysop", $a ) ) { return true; }
232 return false;
233 }
234
235 function isLog()
236 {
237 if ( $this->mNamespace != Namespace::getWikipedia() ) {
238 return false;
239 }
240 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
241 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
242 return true;
243 }
244 return false;
245 }
246
247 function userIsWatching()
248 {
249 global $wgUser;
250
251 if ( -1 == $this->mNamespace ) { return false; }
252 if ( 0 == $wgUser->getID() ) { return false; }
253
254 return $wgUser->isWatched( $this );
255 }
256
257 function userCanEdit()
258 {
259 global $wgUser;
260
261 if ( -1 == $this->mNamespace ) { return false; }
262 # if ( 0 == $this->getArticleID() ) { return false; }
263 if ( $this->mDbkeyform == "_" ) { return false; }
264
265 $ur = $wgUser->getRights();
266 foreach ( $this->getRestrictions() as $r ) {
267 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
268 return false;
269 }
270 }
271 return true;
272 }
273
274 function getRestrictions()
275 {
276 $id = $this->getArticleID();
277 if ( 0 == $id ) { return array(); }
278
279 if ( ! $this->mRestrictionsLoaded ) {
280 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
281 $this->mRestrictions = explode( ",", trim( $res ) );
282 $this->mRestrictionsLoaded = true;
283 }
284 return $this->mRestrictions;
285 }
286
287 function isDeleted() {
288 $ns = $this->getNamespace();
289 $t = wfStrencode( $this->getDBkey() );
290 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
291 if( $res = wfQuery( $sql, DB_READ ) ) {
292 $s = wfFetchObject( $res );
293 return $s->n;
294 }
295 return 0;
296 }
297
298 function getArticleID()
299 {
300 global $wgLinkCache;
301
302 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
303 $this->mArticleID = $wgLinkCache->addLink(
304 $this->getPrefixedDBkey() );
305 return $this->mArticleID;
306 }
307
308 function resetArticleID( $newid )
309 {
310 global $wgLinkCache;
311 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
312
313 if ( 0 == $newid ) { $this->mArticleID = -1; }
314 else { $this->mArticleID = $newid; }
315 $this->mRestrictionsLoaded = false;
316 $this->mRestrictions = array();
317 }
318
319 /* private */ function prefix( $name )
320 {
321 global $wgLang;
322
323 $p = "";
324 if ( "" != $this->mInterwiki ) {
325 $p = $this->mInterwiki . ":";
326 }
327 if ( 0 != $this->mNamespace ) {
328 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
329 }
330 return $p . $name;
331 }
332
333 # Assumes that mDbkeyform has been set, and is urldecoded
334 # and uses undersocres, but not otherwise munged. This function
335 # removes illegal characters, splits off the winterwiki and
336 # namespace prefixes, sets the other forms, and canonicalizes
337 # everything. This one function is really at the core of
338 # Wiki--don't mess with it unless you're really sure you know
339 # what you're doing.
340 #
341 /* private */ function secureAndSplit()
342 {
343 global $wgLang, $wgLocalInterwiki;
344 wfProfileIn( "Title::secureAndSplit" );
345
346 $validNamespaces = $wgLang->getNamespaces();
347 unset( $validNamespaces[0] );
348
349 $this->mInterwiki = $this->mFragment = "";
350 $this->mNamespace = 0;
351
352 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
353 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
354 $l = strlen( $t );
355 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
356 if ( "" == $t ) { $t = "_"; }
357
358 $this->mDbkeyform = $t;
359 $done = false;
360
361 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
362 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
363 $t = substr( $t, 1 );
364 }
365 if ( ":" == $t{0} ) {
366 $r = substr( $t, 1 );
367 } else {
368 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
369 #$p = strtolower( $m[1] );
370 $p = $m[1];
371 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
372 $t = $m[2];
373 $this->mNamespace = $ns;
374 } elseif ( $this->getInterwikiLink( $p ) ) {
375 $t = $m[2];
376 $this->mInterwiki = $p;
377
378 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
379 $t, $m ) ) {
380 $p = strtolower( $m[1] );
381 } else {
382 $done = true;
383 }
384 if($this->mInterwiki != $wgLocalInterwiki)
385 $done = true;
386 }
387 }
388 $r = $t;
389 }
390 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
391 $this->mInterwiki = "";
392 }
393 # We already know that some pages won't be in the database!
394 #
395 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
396 $this->mArticleID = 0;
397 }
398 $f = strstr( $r, "#" );
399 if ( false !== $f ) {
400 $this->mFragment = substr( $f, 1 );
401 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
402 }
403 # Strip illegal characters.
404 #
405 $tc = Title::legalChars();
406 $t = preg_replace( "/[^{$tc}]/", "", $r );
407
408 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
409 $this->mDbkeyform = $t;
410 $this->mUrlform = wfUrlencode( $t );
411 $this->mTextform = str_replace( "_", " ", $t );
412
413 wfProfileOut();
414 }
415 }
416 ?>