4 /* private static */ $title_interwiki_cache = array();
8 # * Represents a title, which may contain an interwiki designation or namespace
9 # * Can fetch various kinds of data from the database, albeit inefficiently.
12 # All member variables should be considered private
13 # Please use the accessor functions
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
27 #----------------------------------------------------------------------------
29 #----------------------------------------------------------------------------
31 /* private */ function Title()
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();
41 # From a prefixed DB key
42 /* static */ function newFromDBkey( $key )
45 $t->mDbkeyform
= $key;
46 if( $t->secureAndSplit() )
52 # From text, such as what you would find in a link
53 /* static */ function newFromText( $text )
56 $fname = "Title::newFromText";
57 wfProfileIn( $fname );
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 );
69 if( is_object( $text ) ) {
70 wfDebugDieBacktrace( "Called with object instead of string." );
72 $text = strtr( $text, $trans );
74 $text = wfMungeToUtf8( $text );
77 # What was this for? TS 2004-03-03
78 # $text = urldecode( $text );
81 $t->mDbkeyform
= str_replace( " ", "_", $text );
82 wfProfileOut( $fname );
83 if( $t->secureAndSplit() ) {
90 # From a URL-encoded title
91 /* static */ function newFromURL( $url )
93 global $wgLang, $wgServer;
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...
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 ) ) )
108 $s = $wgLang->checkTitleEncoding( $s );
111 $t->mDbkeyform
= str_replace( " ", "_", $s );
112 if( $t->secureAndSplit() ) {
120 # This is inefficiently implemented, the cur row is requested but not
121 # used for anything else
122 /* static */ function newFromID( $id )
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
);
135 # From a namespace index and a DB key
136 /* static */ function makeTitle( $ns, $title )
139 $t->mDbkeyform
= Title
::makeName( $ns, $title );
140 if( $t->secureAndSplit() ) {
147 function newMainPage()
149 return Title
::newFromText( wfMsg( "mainpage" ) );
152 #----------------------------------------------------------------------------
154 #----------------------------------------------------------------------------
156 # Get the prefixed DB key associated with an ID
157 /* static */ function nameOf( $id )
159 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
161 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
162 if ( 0 == wfNumRows( $res ) ) { return NULL; }
164 $s = wfFetchObject( $res );
165 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
169 # Get a regex character class describing the legal characters in a link
170 /* static */ function legalChars()
172 # Missing characters:
173 # * []|# Needed for link syntax
174 # * % and + are corrupted by Apache when they appear in the path
176 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
177 # this breaks interlanguage links
179 $set = " !\"$&'()*,\\-.\\/0-9:;<=>?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
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 )
187 global $wgDBminWordLen, $wgLang;
189 $lc = SearchEngine
::legalSearchChars() . "&#;";
190 $t = $wgLang->stripForSearch( $title );
191 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
192 $t = strtolower( $t );
195 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
196 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
198 $t = preg_replace( "/\\s+/", " ", $t );
200 if ( $ns == Namespace::getImage() ) {
201 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
206 # Make a prefixed DB key from a DB key and a namespace index
207 /* static */ function makeName( $ns, $title )
211 $n = $wgLang->getNsText( $ns );
212 if ( "" == $n ) { return $title; }
213 else { return "{$n}:{$title}"; }
217 # Returns the URL associated with an interwiki prefix
218 # The URL contains $1, which is replaced by the title
219 function getInterwikiLink( $key )
221 global $wgMemc, $wgDBname, $title_interwiki_cache;
222 $k = "$wgDBname:interwiki:$key";
224 if( array_key_exists( $k, $title_interwiki_cache ) )
225 return $title_interwiki_cache[$k]->iw_url
;
227 $s = $wgMemc->get( $k );
229 $title_interwiki_cache[$k] = $s;
232 $dkey = wfStrencode( $key );
233 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
234 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
237 $s = wfFetchObject( $res );
242 $wgMemc->set( $k, $s );
243 $title_interwiki_cache[$k] = $s;
247 #----------------------------------------------------------------------------
249 #----------------------------------------------------------------------------
252 # See the definitions at the top of this file
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
; }
262 # Get title for search index
263 function getIndexTitle()
265 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
268 # Get prefixed title with underscores
269 function getPrefixedDBkey()
271 $s = $this->prefix( $this->mDbkeyform
);
272 $s = str_replace( " ", "_", $s );
276 # Get prefixed title with spaces
277 # This is the form usually used for display
278 function getPrefixedText()
280 if ( empty( $this->mPrefixedText
) ) {
281 $s = $this->prefix( $this->mTextform
);
282 $s = str_replace( "_", " ", $s );
283 $this->mPrefixedText
= $s;
285 return $this->mPrefixedText
;
288 # Get a URL-encoded title (not an actual URL) including interwiki
289 function getPrefixedURL()
291 $s = $this->prefix( $this->mDbkeyform
);
292 $s = str_replace( " ", "_", $s );
294 $s = wfUrlencode ( $s ) ;
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 );
305 # Get a real URL referring to this title, with interwiki link and fragment
306 function getFullURL( $query = "" )
308 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
310 if ( "" == $this->mInterwiki
) {
312 return $wgServer . $this->getLocalUrl( $query );
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
);
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
333 die( "Call to obsolete obsolete function Title::getURL()" );
336 function getLocalURL( $query = "" )
338 global $wgLang, $wgArticlePath, $wgScript;
340 if ( $this->isExternal() ) {
341 return $this->getFullURL();
344 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
345 if ( $query == "" ) {
346 $url = str_replace( "$1", $dbkey, $wgArticlePath );
348 if ( $query == "-" ) {
351 if ( $wgScript != "" ) {
352 $url = "{$wgScript}?title={$dbkey}&{$query}";
355 $url = "/{$dbkey}?{$query}";
361 function escapeLocalURL( $query = "" ) {
362 return wfEscapeHTML( $this->getLocalURL( $query ) );
365 function escapeFullURL( $query = "" ) {
366 return wfEscapeHTML( $this->getFullURL( $query ) );
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 );
376 # Get the edit URL, or a null string if it is an interwiki link
377 function getEditURL()
379 global $wgServer, $wgScript;
381 if ( "" != $this->mInterwiki
) { return ""; }
382 $s = $this->getLocalURL( "action=edit" );
387 # Get HTML-escaped displayable text
388 # For the title field in <a> tags
389 function getEscapedText()
391 return wfEscapeHTML( $this->getPrefixedText() );
394 # Is the title interwiki?
395 function isExternal() { return ( "" != $this->mInterwiki
); }
397 # Does the title correspond to a protected article?
398 function isProtected()
400 if ( -1 == $this->mNamespace
) { return true; }
401 $a = $this->getRestrictions();
402 if ( in_array( "sysop", $a ) ) { return true; }
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.
412 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
415 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
416 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
422 # Is $wgUser is watching this page?
423 function userIsWatching()
427 if ( -1 == $this->mNamespace
) { return false; }
428 if ( 0 == $wgUser->getID() ) { return false; }
430 return $wgUser->isWatched( $this );
433 # Can $wgUser edit this page?
434 function userCanEdit()
438 if ( -1 == $this->mNamespace
) { return false; }
439 # if ( 0 == $this->getArticleID() ) { return false; }
440 if ( $this->mDbkeyform
== "_" ) { return false; }
442 $ur = $wgUser->getRights();
443 foreach ( $this->getRestrictions() as $r ) {
444 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
451 # Accessor/initialisation for mRestrictions
452 function getRestrictions()
454 $id = $this->getArticleID();
455 if ( 0 == $id ) { return array(); }
457 if ( ! $this->mRestrictionsLoaded
) {
458 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
459 $this->mRestrictions
= explode( ",", trim( $res ) );
460 $this->mRestrictionsLoaded
= true;
462 return $this->mRestrictions
;
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 );
477 # Get the article ID from the link cache
478 # Used very heavily, e.g. in Parser::replaceInternalLinks()
479 function getArticleID()
483 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
484 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
485 return $this->mArticleID
;
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 )
494 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
496 if ( 0 == $newid ) { $this->mArticleID
= -1; }
497 else { $this->mArticleID
= $newid; }
498 $this->mRestrictionsLoaded
= false;
499 $this->mRestrictions
= array();
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" );
512 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
513 /* private */ function prefix( $name )
518 if ( "" != $this->mInterwiki
) {
519 $p = $this->mInterwiki
. ":";
521 if ( 0 != $this->mNamespace
) {
522 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
527 # Secure and split - main initialisation function for this object
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
535 /* private */ function secureAndSplit()
537 global $wgLang, $wgLocalInterwiki;
538 $fname = "Title::secureAndSplit";
539 wfProfileIn( $fname );
541 static $imgpre = false;
542 static $rxTc = false;
545 if ( $imgpre === false ) {
546 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
547 $rxTc = "/[^" . Title
::legalChars() . "]/";
551 $this->mInterwiki
= $this->mFragment
= "";
552 $this->mNamespace
= 0;
554 # Clean up whitespace
556 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
557 if ( "_" == $t{0} ) {
558 $t = substr( $t, 1 );
561 if ( $l && ( "_" == $t{$l-1} ) ) {
562 $t = substr( $t, 0, $l-1 );
565 wfProfileOut( $fname );
569 $this->mDbkeyform
= $t;
573 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
574 $t = substr( $t, 1 );
577 # Redundant initial colon
578 if ( ":" == $t{0} ) {
579 $r = substr( $t, 1 );
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] );
585 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
588 $this->mNamespace
= $ns;
589 } elseif ( $this->getInterwikiLink( $p ) ) {
592 $this->mInterwiki
= $p;
594 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
596 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
604 # Redundant interwiki prefix to the local wiki
605 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
606 $this->mInterwiki
= "";
608 # We already know that some pages won't be in the database!
610 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
611 $this->mArticleID
= 0;
613 $f = strstr( $r, "#" );
614 if ( false !== $f ) {
615 $this->mFragment
= substr( $f, 1 );
616 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
619 # Reject illegal characters.
621 if( preg_match( $rxTc, $r ) ) {
625 # "." and ".." conflict with the directories of those names
626 if ( $r === "." ||
$r === ".." ) {
630 # Initial capital letter
631 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $r );
634 $this->mDbkeyform
= $t;
635 $this->mUrlform
= wfUrlencode( $t );
637 $this->mTextform
= str_replace( "_", " ", $t );
639 wfProfileOut( $fname );
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() );
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() );