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