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