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