* Fixed magic quotes in $_REQUEST, in Setup.php
[lhc/web/wiklou.git] / includes / Title.php
1 <?php
2 # See title.doc
3
4 /* private static */ $title_interwiki_cache = array();
5
6 # Title class
7 #
8 # * Represents a title, which may contain an interwiki designation or namespace
9 # * Can fetch various kinds of data from the database, albeit inefficiently.
10 #
11 class Title {
12 # All member variables should be considered private
13 # Please use the accessor functions
14
15 var $mTextform; # Text form (spaces not underscores) of the main part
16 var $mUrlform; # URL-encoded form of the main part
17 var $mDbkeyform; # Main part with underscores
18 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
19 var $mInterwiki; # Interwiki prefix (or null string)
20 var $mFragment; # Title fragment (i.e. the bit after the #)
21 var $mArticleID; # Article ID, fetched from the link cache on demand
22 var $mRestrictions; # Array of groups allowed to edit this article
23 # Only null or "sysop" are supported
24 var $mRestrictionsLoaded; # Boolean for initialisation on demand
25 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
26
27 #----------------------------------------------------------------------------
28 # Construction
29 #----------------------------------------------------------------------------
30
31 /* private */ function Title()
32 {
33 $this->mInterwiki = $this->mUrlform =
34 $this->mTextform = $this->mDbkeyform = "";
35 $this->mArticleID = -1;
36 $this->mNamespace = 0;
37 $this->mRestrictionsLoaded = false;
38 $this->mRestrictions = array();
39 }
40
41 # From a prefixed DB key
42 /* static */ function newFromDBkey( $key )
43 {
44 $t = new Title();
45 $t->mDbkeyform = $key;
46 if( $t->secureAndSplit() )
47 return $t;
48 else
49 return NULL;
50 }
51
52 # From text, such as what you would find in a link
53 /* static */ function newFromText( $text )
54 {
55 static $trans;
56 $fname = "Title::newFromText";
57 wfProfileIn( $fname );
58
59 # Note - mixing latin1 named entities and unicode numbered
60 # ones will result in a bad link.
61 if( !isset( $trans ) ) {
62 global $wgInputEncoding;
63 $trans = array_flip( get_html_translation_table( HTML_ENTITIES ) );
64 if( strcasecmp( "utf-8", $wgInputEncoding ) == 0 ) {
65 $trans = array_map( "utf8_encode", $trans );
66 }
67 }
68
69 $text = strtr( $text, $trans );
70
71 $text = wfMungeToUtf8( $text );
72
73
74 # What was this for? TS 2004-03-03
75 # $text = urldecode( $text );
76
77 $t = new Title();
78 $t->mDbkeyform = str_replace( " ", "_", $text );
79 wfProfileOut( $fname );
80 if( $t->secureAndSplit() ) {
81 return $t;
82 } else {
83 return NULL;
84 }
85 }
86
87 # From a URL-encoded title
88 /* static */ function newFromURL( $url )
89 {
90 global $wgLang, $wgServer;
91 $t = new Title();
92 $s = urldecode( $url ); # This is technically wrong, as anything
93 # we've gotten is already decoded by PHP.
94 # Kept for backwards compatibility with
95 # buggy URLs we had for a while...
96 $s = $url;
97
98 # For links that came from outside, check for alternate/legacy
99 # character encoding.
100 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
101 wfDebug( "Servr: $wgServer\n" );
102 if( empty( $_SERVER["HTTP_REFERER"] ) ||
103 strncmp($wgServer, $_SERVER["HTTP_REFERER"], strlen( $wgServer ) ) )
104 {
105 $s = $wgLang->checkTitleEncoding( $s );
106 }
107
108 $t->mDbkeyform = str_replace( " ", "_", $s );
109 if( $t->secureAndSplit() ) {
110 return $t;
111 } else {
112 return NULL;
113 }
114 }
115
116 # From a cur_id
117 # This is inefficiently implemented, the cur row is requested but not
118 # used for anything else
119 /* static */ function newFromID( $id )
120 {
121 $fname = "Title::newFromID";
122 $row = wfGetArray( "cur", array( "cur_namespace", "cur_title" ),
123 array( "cur_id" => $id ), $fname );
124 if ( $row !== false ) {
125 $title = Title::makeTitle( $row->cur_namespace, $row->cur_title );
126 } else {
127 $title = NULL;
128 }
129 return $title;
130 }
131
132 # From a namespace index and a DB key
133 /* static */ function makeTitle( $ns, $title )
134 {
135 $t = new Title();
136 $t->mDbkeyform = Title::makeName( $ns, $title );
137 if( $t->secureAndSplit() ) {
138 return $t;
139 } else {
140 return NULL;
141 }
142 }
143
144 function newMainPage()
145 {
146 return Title::newFromText( wfMsg( "mainpage" ) );
147 }
148
149 #----------------------------------------------------------------------------
150 # Static functions
151 #----------------------------------------------------------------------------
152
153 # Get the prefixed DB key associated with an ID
154 /* static */ function nameOf( $id )
155 {
156 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
157 "cur_id={$id}";
158 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
159 if ( 0 == wfNumRows( $res ) ) { return NULL; }
160
161 $s = wfFetchObject( $res );
162 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
163 return $n;
164 }
165
166 # Get a regex character class describing the legal characters in a link
167 /* static */ function legalChars()
168 {
169 # Missing characters:
170 # * []|# Needed for link syntax
171 # * % and + are corrupted by Apache when they appear in the path
172 #
173 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
174 # this breaks interlanguage links
175
176 $set = " !\"$&'()*,\\-.\\/0-9:;<=>?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
177 return $set;
178 }
179
180 # Returns a stripped-down a title string ready for the search index
181 # Takes a namespace index and a text-form main part
182 /* static */ function indexTitle( $ns, $title )
183 {
184 global $wgDBminWordLen, $wgLang;
185
186 $lc = SearchEngine::legalSearchChars() . "&#;";
187 $t = $wgLang->stripForSearch( $title );
188 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
189 $t = strtolower( $t );
190
191 # Handle 's, s'
192 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
193 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
194
195 $t = preg_replace( "/\\s+/", " ", $t );
196
197 if ( $ns == Namespace::getImage() ) {
198 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
199 }
200 return trim( $t );
201 }
202
203 # Make a prefixed DB key from a DB key and a namespace index
204 /* static */ function makeName( $ns, $title )
205 {
206 global $wgLang;
207
208 $n = $wgLang->getNsText( $ns );
209 if ( "" == $n ) { return $title; }
210 else { return "{$n}:{$title}"; }
211 }
212
213 # Arguably static
214 # Returns the URL associated with an interwiki prefix
215 # The URL contains $1, which is replaced by the title
216 function getInterwikiLink( $key )
217 {
218 global $wgMemc, $wgDBname, $title_interwiki_cache;
219 $k = "$wgDBname:interwiki:$key";
220
221 if( array_key_exists( $k, $title_interwiki_cache ) )
222 return $title_interwiki_cache[$k]->iw_url;
223
224 $s = $wgMemc->get( $k );
225 if( $s ) {
226 $title_interwiki_cache[$k] = $s;
227 return $s->iw_url;
228 }
229 $dkey = wfStrencode( $key );
230 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
231 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
232 if(!$res) return "";
233
234 $s = wfFetchObject( $res );
235 if(!$s) {
236 $s = (object)false;
237 $s->iw_url = "";
238 }
239 $wgMemc->set( $k, $s );
240 $title_interwiki_cache[$k] = $s;
241 return $s->iw_url;
242 }
243
244 #----------------------------------------------------------------------------
245 # Other stuff
246 #----------------------------------------------------------------------------
247
248 # Simple accessors
249 # See the definitions at the top of this file
250
251 function getText() { return $this->mTextform; }
252 function getPartialURL() { return $this->mUrlform; }
253 function getDBkey() { return $this->mDbkeyform; }
254 function getNamespace() { return $this->mNamespace; }
255 function setNamespace( $n ) { $this->mNamespace = $n; }
256 function getInterwiki() { return $this->mInterwiki; }
257 function getFragment() { return $this->mFragment; }
258
259 # Get title for search index
260 function getIndexTitle()
261 {
262 return Title::indexTitle( $this->mNamespace, $this->mTextform );
263 }
264
265 # Get prefixed title with underscores
266 function getPrefixedDBkey()
267 {
268 $s = $this->prefix( $this->mDbkeyform );
269 $s = str_replace( " ", "_", $s );
270 return $s;
271 }
272
273 # Get prefixed title with spaces
274 # This is the form usually used for display
275 function getPrefixedText()
276 {
277 if ( empty( $this->mPrefixedText ) ) {
278 $s = $this->prefix( $this->mTextform );
279 $s = str_replace( "_", " ", $s );
280 $this->mPrefixedText = $s;
281 }
282 return $this->mPrefixedText;
283 }
284
285 # Get a URL-encoded title (not an actual URL) including interwiki
286 function getPrefixedURL()
287 {
288 $s = $this->prefix( $this->mDbkeyform );
289 $s = str_replace( " ", "_", $s );
290
291 $s = wfUrlencode ( $s ) ;
292
293 # Cleaning up URL to make it look nice -- is this safe?
294 $s = preg_replace( "/%3[Aa]/", ":", $s );
295 $s = preg_replace( "/%2[Ff]/", "/", $s );
296 $s = str_replace( "%28", "(", $s );
297 $s = str_replace( "%29", ")", $s );
298
299 return $s;
300 }
301
302 # Get a real URL referring to this title, with interwiki link and fragment
303 function getFullURL()
304 {
305 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
306
307 if ( "" == $this->mInterwiki ) {
308 $p = $wgArticlePath;
309 } else {
310 $p = $this->getInterwikiLink( $this->mInterwiki );
311 }
312 $n = $wgLang->getNsText( $this->mNamespace );
313 if ( "" != $n ) { $n .= ":"; }
314 $u = str_replace( "$1", $n . $this->mUrlform, $p );
315 if ( "" != $this->mFragment ) {
316 $u .= "#" . $this->mFragment;
317 }
318 return $u;
319 }
320
321 # Get a URL with an optional query string, no fragment
322 # * If $query=="", it will use $wgArticlePath
323 # * Returns a full for an interwiki link, loses any query string
324 # * Optionally adds the server and escapes for HTML
325 # * Setting $query to "-" makes an old-style URL with nothing in the
326 # query except a title
327 function getURL( $query = "", $escape = false, $full = false )
328 {
329 global $wgLang, $wgArticlePath, $wgScript, $wgServer;
330
331 if ( $this->isExternal() ) {
332 return $this->getFullURL();
333 }
334
335 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
336 if ( $query == "" ) {
337 $url = str_replace( "$1", $dbkey, $wgArticlePath );
338 } else {
339 if ( $query == "-" ) {
340 $query = "";
341 }
342 if ( $wgScript != "" ) {
343 $url = "{$wgScript}?title={$dbkey}&{$query}";
344 } else {
345 # Top level wiki
346 $url = "/{$dbkey}&{$query}";
347 }
348 }
349
350 if ( $full ) {
351 $url = $wgServer . $url;
352 }
353
354 if ( $escape ) {
355 $url = wfEscapeHTML( $url );
356 }
357 return $url;
358 }
359
360 # Get the edit URL, or a null string if it is an interwiki link
361 function getEditURL()
362 {
363 global $wgServer, $wgScript;
364
365 if ( "" != $this->mInterwiki ) { return ""; }
366 $s = $this->getURL( "action=edit" );
367
368 return $s;
369 }
370
371 # Get HTML-escaped displayable text
372 # For the title field in <a> tags
373 function getEscapedText()
374 {
375 return wfEscapeHTML( $this->getPrefixedText() );
376 }
377
378 # Is the title interwiki?
379 function isExternal() { return ( "" != $this->mInterwiki ); }
380
381 # Does the title correspond to a protected article?
382 function isProtected()
383 {
384 if ( -1 == $this->mNamespace ) { return true; }
385 $a = $this->getRestrictions();
386 if ( in_array( "sysop", $a ) ) { return true; }
387 return false;
388 }
389
390 # Is the page a log page, i.e. one where the history is messed up by
391 # LogPage.php? This used to be used for suppressing diff links in recent
392 # changes, but now that's done by setting a flag in the recentchanges
393 # table. Hence, this probably is no longer used.
394 function isLog()
395 {
396 if ( $this->mNamespace != Namespace::getWikipedia() ) {
397 return false;
398 }
399 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
400 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
401 return true;
402 }
403 return false;
404 }
405
406 # Is $wgUser is watching this page?
407 function userIsWatching()
408 {
409 global $wgUser;
410
411 if ( -1 == $this->mNamespace ) { return false; }
412 if ( 0 == $wgUser->getID() ) { return false; }
413
414 return $wgUser->isWatched( $this );
415 }
416
417 # Can $wgUser edit this page?
418 function userCanEdit()
419 {
420 global $wgUser;
421
422 if ( -1 == $this->mNamespace ) { return false; }
423 # if ( 0 == $this->getArticleID() ) { return false; }
424 if ( $this->mDbkeyform == "_" ) { return false; }
425
426 $ur = $wgUser->getRights();
427 foreach ( $this->getRestrictions() as $r ) {
428 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
429 return false;
430 }
431 }
432 return true;
433 }
434
435 # Accessor/initialisation for mRestrictions
436 function getRestrictions()
437 {
438 $id = $this->getArticleID();
439 if ( 0 == $id ) { return array(); }
440
441 if ( ! $this->mRestrictionsLoaded ) {
442 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
443 $this->mRestrictions = explode( ",", trim( $res ) );
444 $this->mRestrictionsLoaded = true;
445 }
446 return $this->mRestrictions;
447 }
448
449 # Is there a version of this page in the deletion archive?
450 function isDeleted() {
451 $ns = $this->getNamespace();
452 $t = wfStrencode( $this->getDBkey() );
453 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
454 if( $res = wfQuery( $sql, DB_READ ) ) {
455 $s = wfFetchObject( $res );
456 return $s->n;
457 }
458 return 0;
459 }
460
461 # Get the article ID from the link cache
462 # Used very heavily, e.g. in Parser::replaceInternalLinks()
463 function getArticleID()
464 {
465 global $wgLinkCache;
466
467 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
468 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
469 return $this->mArticleID;
470 }
471
472 # This clears some fields in this object, and clears any associated keys in the
473 # "bad links" section of $wgLinkCache. This is called from Article::insertNewArticle()
474 # to allow loading of the new cur_id. It's also called from Article::doDeleteArticle()
475 function resetArticleID( $newid )
476 {
477 global $wgLinkCache;
478 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
479
480 if ( 0 == $newid ) { $this->mArticleID = -1; }
481 else { $this->mArticleID = $newid; }
482 $this->mRestrictionsLoaded = false;
483 $this->mRestrictions = array();
484 }
485
486 # Updates cur_touched
487 # Called from LinksUpdate.php
488 function invalidateCache() {
489 $now = wfTimestampNow();
490 $ns = $this->getNamespace();
491 $ti = wfStrencode( $this->getDBkey() );
492 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
493 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
494 }
495
496 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
497 /* private */ function prefix( $name )
498 {
499 global $wgLang;
500
501 $p = "";
502 if ( "" != $this->mInterwiki ) {
503 $p = $this->mInterwiki . ":";
504 }
505 if ( 0 != $this->mNamespace ) {
506 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
507 }
508 return $p . $name;
509 }
510
511 # Secure and split - main initialisation function for this object
512 #
513 # Assumes that mDbkeyform has been set, and is urldecoded
514 # and uses undersocres, but not otherwise munged. This function
515 # removes illegal characters, splits off the winterwiki and
516 # namespace prefixes, sets the other forms, and canonicalizes
517 # everything.
518 #
519 /* private */ function secureAndSplit()
520 {
521 global $wgLang, $wgLocalInterwiki;
522 $fname = "Title::secureAndSplit";
523 wfProfileIn( $fname );
524
525 static $imgpre = false;
526 static $rxTc = false;
527
528 # Initialisation
529 if ( $imgpre === false ) {
530 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
531 $rxTc = "/[^" . Title::legalChars() . "]/";
532 }
533
534
535 $this->mInterwiki = $this->mFragment = "";
536 $this->mNamespace = 0;
537
538 # Clean up whitespace
539 #
540 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
541 if ( "_" == $t{0} ) {
542 $t = substr( $t, 1 );
543 }
544 $l = strlen( $t );
545 if ( $l && ( "_" == $t{$l-1} ) ) {
546 $t = substr( $t, 0, $l-1 );
547 }
548 if ( "" == $t ) {
549 wfProfileOut( $fname );
550 return false;
551 }
552
553 $this->mDbkeyform = $t;
554 $done = false;
555
556 # :Image: namespace
557 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
558 $t = substr( $t, 1 );
559 }
560
561 # Redundant initial colon
562 if ( ":" == $t{0} ) {
563 $r = substr( $t, 1 );
564 } else {
565 # Namespace or interwiki prefix
566 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
567 #$p = strtolower( $m[1] );
568 $p = $m[1];
569 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
570 # Ordinary namespace
571 $t = $m[2];
572 $this->mNamespace = $ns;
573 } elseif ( $this->getInterwikiLink( $p ) ) {
574 # Interwiki link
575 $t = $m[2];
576 $this->mInterwiki = $p;
577
578 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
579 $done = true;
580 } elseif($this->mInterwiki != $wgLocalInterwiki) {
581 $done = true;
582 }
583 }
584 }
585 $r = $t;
586 }
587
588 # Redundant interwiki prefix to the local wiki
589 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
590 $this->mInterwiki = "";
591 }
592 # We already know that some pages won't be in the database!
593 #
594 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
595 $this->mArticleID = 0;
596 }
597 $f = strstr( $r, "#" );
598 if ( false !== $f ) {
599 $this->mFragment = substr( $f, 1 );
600 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
601 }
602
603 # Reject illegal characters.
604 #
605 if( preg_match( $rxTc, $r ) ) {
606 return false;
607 }
608
609 # "." and ".." conflict with the directories of those names
610 if ( $r === "." || $r === ".." ) {
611 return false;
612 }
613
614 # Initial capital letter
615 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
616
617 # Fill fields
618 $this->mDbkeyform = $t;
619 $this->mUrlform = wfUrlencode( $t );
620
621 $this->mTextform = str_replace( "_", " ", $t );
622
623 wfProfileOut( $fname );
624 return true;
625 }
626
627 # Get a title object associated with the talk page of this article
628 function getTalkPage() {
629 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
630 }
631
632 # Get a title object associated with the subject page of this talk page
633 function getSubjectPage() {
634 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
635 }
636 }
637 ?>