Replace the random boolean parameters on Title::getURL() with a set of
[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( $query = "" )
304 {
305 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
306
307 if ( "" == $this->mInterwiki ) {
308 $p = $wgArticlePath;
309 return $wgServer . $this->getLocalUrl( $query );
310 }
311
312 $p = $this->getInterwikiLink( $this->mInterwiki );
313 $n = $wgLang->getNsText( $this->mNamespace );
314 if ( "" != $n ) { $n .= ":"; }
315 $u = str_replace( "$1", $n . $this->mUrlform, $p );
316 if ( "" != $this->mFragment ) {
317 $u .= "#" . wfUrlencode( $this->mFragment );
318 }
319 return $u;
320 }
321
322 # Get a URL with an optional query string, no fragment
323 # * If $query=="", it will use $wgArticlePath
324 # * Returns a full for an interwiki link, loses any query string
325 # * Optionally adds the server and escapes for HTML
326 # * Setting $query to "-" makes an old-style URL with nothing in the
327 # query except a title
328
329 function getURL() {
330 die( "Call to obsolete obsolete function Title::getURL()" );
331 }
332
333 function getLocalURL( $query = "" )
334 {
335 global $wgLang, $wgArticlePath, $wgScript;
336
337 if ( $this->isExternal() ) {
338 return $this->getFullURL();
339 }
340
341 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
342 if ( $query == "" ) {
343 $url = str_replace( "$1", $dbkey, $wgArticlePath );
344 } else {
345 if ( $query == "-" ) {
346 $query = "";
347 }
348 if ( $wgScript != "" ) {
349 $url = "{$wgScript}?title={$dbkey}&{$query}";
350 } else {
351 # Top level wiki
352 $url = "/{$dbkey}?{$query}";
353 }
354 }
355 return $url;
356 }
357
358 function escapeLocalURL( $query = "" ) {
359 return wfEscapeHTML( $this->getLocalURL( $query ) );
360 }
361
362 function escapeFullURL( $query = "" ) {
363 return wfEscapeHTML( $this->getFullURL( $query ) );
364 }
365
366 function getInternalURL( $query = "" ) {
367 # Used in various Squid-related code, in case we have a different
368 # internal hostname for the server than the exposed one.
369 global $wgInternalServer;
370 return $wgInternalServer . $this->getLocalURL( $query );
371 }
372
373 # Get the edit URL, or a null string if it is an interwiki link
374 function getEditURL()
375 {
376 global $wgServer, $wgScript;
377
378 if ( "" != $this->mInterwiki ) { return ""; }
379 $s = $this->getLocalURL( "action=edit" );
380
381 return $s;
382 }
383
384 # Get HTML-escaped displayable text
385 # For the title field in <a> tags
386 function getEscapedText()
387 {
388 return wfEscapeHTML( $this->getPrefixedText() );
389 }
390
391 # Is the title interwiki?
392 function isExternal() { return ( "" != $this->mInterwiki ); }
393
394 # Does the title correspond to a protected article?
395 function isProtected()
396 {
397 if ( -1 == $this->mNamespace ) { return true; }
398 $a = $this->getRestrictions();
399 if ( in_array( "sysop", $a ) ) { return true; }
400 return false;
401 }
402
403 # Is the page a log page, i.e. one where the history is messed up by
404 # LogPage.php? This used to be used for suppressing diff links in recent
405 # changes, but now that's done by setting a flag in the recentchanges
406 # table. Hence, this probably is no longer used.
407 function isLog()
408 {
409 if ( $this->mNamespace != Namespace::getWikipedia() ) {
410 return false;
411 }
412 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
413 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
414 return true;
415 }
416 return false;
417 }
418
419 # Is $wgUser is watching this page?
420 function userIsWatching()
421 {
422 global $wgUser;
423
424 if ( -1 == $this->mNamespace ) { return false; }
425 if ( 0 == $wgUser->getID() ) { return false; }
426
427 return $wgUser->isWatched( $this );
428 }
429
430 # Can $wgUser edit this page?
431 function userCanEdit()
432 {
433 global $wgUser;
434
435 if ( -1 == $this->mNamespace ) { return false; }
436 # if ( 0 == $this->getArticleID() ) { return false; }
437 if ( $this->mDbkeyform == "_" ) { return false; }
438
439 $ur = $wgUser->getRights();
440 foreach ( $this->getRestrictions() as $r ) {
441 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
442 return false;
443 }
444 }
445 return true;
446 }
447
448 # Accessor/initialisation for mRestrictions
449 function getRestrictions()
450 {
451 $id = $this->getArticleID();
452 if ( 0 == $id ) { return array(); }
453
454 if ( ! $this->mRestrictionsLoaded ) {
455 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
456 $this->mRestrictions = explode( ",", trim( $res ) );
457 $this->mRestrictionsLoaded = true;
458 }
459 return $this->mRestrictions;
460 }
461
462 # Is there a version of this page in the deletion archive?
463 function isDeleted() {
464 $ns = $this->getNamespace();
465 $t = wfStrencode( $this->getDBkey() );
466 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
467 if( $res = wfQuery( $sql, DB_READ ) ) {
468 $s = wfFetchObject( $res );
469 return $s->n;
470 }
471 return 0;
472 }
473
474 # Get the article ID from the link cache
475 # Used very heavily, e.g. in Parser::replaceInternalLinks()
476 function getArticleID()
477 {
478 global $wgLinkCache;
479
480 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
481 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
482 return $this->mArticleID;
483 }
484
485 # This clears some fields in this object, and clears any associated keys in the
486 # "bad links" section of $wgLinkCache. This is called from Article::insertNewArticle()
487 # to allow loading of the new cur_id. It's also called from Article::doDeleteArticle()
488 function resetArticleID( $newid )
489 {
490 global $wgLinkCache;
491 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
492
493 if ( 0 == $newid ) { $this->mArticleID = -1; }
494 else { $this->mArticleID = $newid; }
495 $this->mRestrictionsLoaded = false;
496 $this->mRestrictions = array();
497 }
498
499 # Updates cur_touched
500 # Called from LinksUpdate.php
501 function invalidateCache() {
502 $now = wfTimestampNow();
503 $ns = $this->getNamespace();
504 $ti = wfStrencode( $this->getDBkey() );
505 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
506 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
507 }
508
509 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
510 /* private */ function prefix( $name )
511 {
512 global $wgLang;
513
514 $p = "";
515 if ( "" != $this->mInterwiki ) {
516 $p = $this->mInterwiki . ":";
517 }
518 if ( 0 != $this->mNamespace ) {
519 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
520 }
521 return $p . $name;
522 }
523
524 # Secure and split - main initialisation function for this object
525 #
526 # Assumes that mDbkeyform has been set, and is urldecoded
527 # and uses undersocres, but not otherwise munged. This function
528 # removes illegal characters, splits off the winterwiki and
529 # namespace prefixes, sets the other forms, and canonicalizes
530 # everything.
531 #
532 /* private */ function secureAndSplit()
533 {
534 global $wgLang, $wgLocalInterwiki;
535 $fname = "Title::secureAndSplit";
536 wfProfileIn( $fname );
537
538 static $imgpre = false;
539 static $rxTc = false;
540
541 # Initialisation
542 if ( $imgpre === false ) {
543 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
544 $rxTc = "/[^" . Title::legalChars() . "]/";
545 }
546
547
548 $this->mInterwiki = $this->mFragment = "";
549 $this->mNamespace = 0;
550
551 # Clean up whitespace
552 #
553 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
554 if ( "_" == $t{0} ) {
555 $t = substr( $t, 1 );
556 }
557 $l = strlen( $t );
558 if ( $l && ( "_" == $t{$l-1} ) ) {
559 $t = substr( $t, 0, $l-1 );
560 }
561 if ( "" == $t ) {
562 wfProfileOut( $fname );
563 return false;
564 }
565
566 $this->mDbkeyform = $t;
567 $done = false;
568
569 # :Image: namespace
570 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
571 $t = substr( $t, 1 );
572 }
573
574 # Redundant initial colon
575 if ( ":" == $t{0} ) {
576 $r = substr( $t, 1 );
577 } else {
578 # Namespace or interwiki prefix
579 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
580 #$p = strtolower( $m[1] );
581 $p = $m[1];
582 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
583 # Ordinary namespace
584 $t = $m[2];
585 $this->mNamespace = $ns;
586 } elseif ( $this->getInterwikiLink( $p ) ) {
587 # Interwiki link
588 $t = $m[2];
589 $this->mInterwiki = $p;
590
591 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
592 $done = true;
593 } elseif($this->mInterwiki != $wgLocalInterwiki) {
594 $done = true;
595 }
596 }
597 }
598 $r = $t;
599 }
600
601 # Redundant interwiki prefix to the local wiki
602 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
603 $this->mInterwiki = "";
604 }
605 # We already know that some pages won't be in the database!
606 #
607 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
608 $this->mArticleID = 0;
609 }
610 $f = strstr( $r, "#" );
611 if ( false !== $f ) {
612 $this->mFragment = substr( $f, 1 );
613 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
614 }
615
616 # Reject illegal characters.
617 #
618 if( preg_match( $rxTc, $r ) ) {
619 return false;
620 }
621
622 # "." and ".." conflict with the directories of those names
623 if ( $r === "." || $r === ".." ) {
624 return false;
625 }
626
627 # Initial capital letter
628 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
629
630 # Fill fields
631 $this->mDbkeyform = $t;
632 $this->mUrlform = wfUrlencode( $t );
633
634 $this->mTextform = str_replace( "_", " ", $t );
635
636 wfProfileOut( $fname );
637 return true;
638 }
639
640 # Get a title object associated with the talk page of this article
641 function getTalkPage() {
642 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
643 }
644
645 # Get a title object associated with the subject page of this talk page
646 function getSubjectPage() {
647 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
648 }
649 }
650 ?>