62bc12d746aa9eaef8719eb415d179f168e913e3
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 $text = strtr( $text, $trans );
71 $text = wfMungeToUtf8( $text );
74 # What was this for? TS 2004-03-03
75 # $text = urldecode( $text );
78 $t->mDbkeyform
= str_replace( " ", "_", $text );
79 wfProfileOut( $fname );
80 if( $t->secureAndSplit() ) {
87 # From a URL-encoded title
88 /* static */ function newFromURL( $url )
90 global $wgLang, $wgServer;
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...
98 # For links that came from outside, check for alternate/legacy
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 ) ) )
105 $s = $wgLang->checkTitleEncoding( $s );
108 $t->mDbkeyform
= str_replace( " ", "_", $s );
109 if( $t->secureAndSplit() ) {
117 # This is inefficiently implemented, the cur row is requested but not
118 # used for anything else
119 /* static */ function newFromID( $id )
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
);
132 # From a namespace index and a DB key
133 /* static */ function makeTitle( $ns, $title )
136 $t->mDbkeyform
= Title
::makeName( $ns, $title );
137 if( $t->secureAndSplit() ) {
144 function newMainPage()
146 return Title
::newFromText( wfMsg( "mainpage" ) );
149 #----------------------------------------------------------------------------
151 #----------------------------------------------------------------------------
153 # Get the prefixed DB key associated with an ID
154 /* static */ function nameOf( $id )
156 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
158 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
159 if ( 0 == wfNumRows( $res ) ) { return NULL; }
161 $s = wfFetchObject( $res );
162 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
166 # Get a regex character class describing the legal characters in a link
167 /* static */ function legalChars()
169 # Missing characters:
170 # * []|# Needed for link syntax
171 # * % and + are corrupted by Apache when they appear in the path
173 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
174 # this breaks interlanguage links
176 $set = " !\"$&'()*,\\-.\\/0-9:;<=>?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
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 )
184 global $wgDBminWordLen, $wgLang;
186 $lc = SearchEngine
::legalSearchChars() . "&#;";
187 $t = $wgLang->stripForSearch( $title );
188 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
189 $t = strtolower( $t );
192 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
193 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
195 $t = preg_replace( "/\\s+/", " ", $t );
197 if ( $ns == Namespace::getImage() ) {
198 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
203 # Make a prefixed DB key from a DB key and a namespace index
204 /* static */ function makeName( $ns, $title )
208 $n = $wgLang->getNsText( $ns );
209 if ( "" == $n ) { return $title; }
210 else { return "{$n}:{$title}"; }
214 # Returns the URL associated with an interwiki prefix
215 # The URL contains $1, which is replaced by the title
216 function getInterwikiLink( $key )
218 global $wgMemc, $wgDBname, $title_interwiki_cache;
219 $k = "$wgDBname:interwiki:$key";
221 if( array_key_exists( $k, $title_interwiki_cache ) )
222 return $title_interwiki_cache[$k]->iw_url
;
224 $s = $wgMemc->get( $k );
226 $title_interwiki_cache[$k] = $s;
229 $dkey = wfStrencode( $key );
230 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
231 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
234 $s = wfFetchObject( $res );
239 $wgMemc->set( $k, $s );
240 $title_interwiki_cache[$k] = $s;
244 #----------------------------------------------------------------------------
246 #----------------------------------------------------------------------------
249 # See the definitions at the top of this file
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
; }
259 # Get title for search index
260 function getIndexTitle()
262 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
265 # Get prefixed title with underscores
266 function getPrefixedDBkey()
268 $s = $this->prefix( $this->mDbkeyform
);
269 $s = str_replace( " ", "_", $s );
273 # Get prefixed title with spaces
274 # This is the form usually used for display
275 function getPrefixedText()
277 if ( empty( $this->mPrefixedText
) ) {
278 $s = $this->prefix( $this->mTextform
);
279 $s = str_replace( "_", " ", $s );
280 $this->mPrefixedText
= $s;
282 return $this->mPrefixedText
;
285 # Get a URL-encoded title (not an actual URL) including interwiki
286 function getPrefixedURL()
288 $s = $this->prefix( $this->mDbkeyform
);
289 $s = str_replace( " ", "_", $s );
291 $s = wfUrlencode ( $s ) ;
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 );
302 # Get a real URL referring to this title, with interwiki link and fragment
303 function getFullURL( $query = "" )
305 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
307 if ( "" == $this->mInterwiki
) {
309 return $wgServer . $this->getLocalUrl( $query );
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
);
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
330 die( "Call to obsolete obsolete function Title::getURL()" );
333 function getLocalURL( $query = "" )
335 global $wgLang, $wgArticlePath, $wgScript;
337 if ( $this->isExternal() ) {
338 return $this->getFullURL();
341 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
342 if ( $query == "" ) {
343 $url = str_replace( "$1", $dbkey, $wgArticlePath );
345 if ( $query == "-" ) {
348 if ( $wgScript != "" ) {
349 $url = "{$wgScript}?title={$dbkey}&{$query}";
352 $url = "/{$dbkey}?{$query}";
358 function escapeLocalURL( $query = "" ) {
359 return wfEscapeHTML( $this->getLocalURL( $query ) );
362 function escapeFullURL( $query = "" ) {
363 return wfEscapeHTML( $this->getFullURL( $query ) );
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 );
373 # Get the edit URL, or a null string if it is an interwiki link
374 function getEditURL()
376 global $wgServer, $wgScript;
378 if ( "" != $this->mInterwiki
) { return ""; }
379 $s = $this->getLocalURL( "action=edit" );
384 # Get HTML-escaped displayable text
385 # For the title field in <a> tags
386 function getEscapedText()
388 return wfEscapeHTML( $this->getPrefixedText() );
391 # Is the title interwiki?
392 function isExternal() { return ( "" != $this->mInterwiki
); }
394 # Does the title correspond to a protected article?
395 function isProtected()
397 if ( -1 == $this->mNamespace
) { return true; }
398 $a = $this->getRestrictions();
399 if ( in_array( "sysop", $a ) ) { return true; }
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.
409 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
412 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
413 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
419 # Is $wgUser is watching this page?
420 function userIsWatching()
424 if ( -1 == $this->mNamespace
) { return false; }
425 if ( 0 == $wgUser->getID() ) { return false; }
427 return $wgUser->isWatched( $this );
430 # Can $wgUser edit this page?
431 function userCanEdit()
435 if ( -1 == $this->mNamespace
) { return false; }
436 # if ( 0 == $this->getArticleID() ) { return false; }
437 if ( $this->mDbkeyform
== "_" ) { return false; }
439 $ur = $wgUser->getRights();
440 foreach ( $this->getRestrictions() as $r ) {
441 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
448 # Accessor/initialisation for mRestrictions
449 function getRestrictions()
451 $id = $this->getArticleID();
452 if ( 0 == $id ) { return array(); }
454 if ( ! $this->mRestrictionsLoaded
) {
455 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
456 $this->mRestrictions
= explode( ",", trim( $res ) );
457 $this->mRestrictionsLoaded
= true;
459 return $this->mRestrictions
;
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 );
474 # Get the article ID from the link cache
475 # Used very heavily, e.g. in Parser::replaceInternalLinks()
476 function getArticleID()
480 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
481 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
482 return $this->mArticleID
;
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 )
491 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
493 if ( 0 == $newid ) { $this->mArticleID
= -1; }
494 else { $this->mArticleID
= $newid; }
495 $this->mRestrictionsLoaded
= false;
496 $this->mRestrictions
= array();
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" );
509 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
510 /* private */ function prefix( $name )
515 if ( "" != $this->mInterwiki
) {
516 $p = $this->mInterwiki
. ":";
518 if ( 0 != $this->mNamespace
) {
519 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
524 # Secure and split - main initialisation function for this object
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
532 /* private */ function secureAndSplit()
534 global $wgLang, $wgLocalInterwiki;
535 $fname = "Title::secureAndSplit";
536 wfProfileIn( $fname );
538 static $imgpre = false;
539 static $rxTc = false;
542 if ( $imgpre === false ) {
543 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
544 $rxTc = "/[^" . Title
::legalChars() . "]/";
548 $this->mInterwiki
= $this->mFragment
= "";
549 $this->mNamespace
= 0;
551 # Clean up whitespace
553 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
554 if ( "_" == $t{0} ) {
555 $t = substr( $t, 1 );
558 if ( $l && ( "_" == $t{$l-1} ) ) {
559 $t = substr( $t, 0, $l-1 );
562 wfProfileOut( $fname );
566 $this->mDbkeyform
= $t;
570 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
571 $t = substr( $t, 1 );
574 # Redundant initial colon
575 if ( ":" == $t{0} ) {
576 $r = substr( $t, 1 );
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] );
582 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
585 $this->mNamespace
= $ns;
586 } elseif ( $this->getInterwikiLink( $p ) ) {
589 $this->mInterwiki
= $p;
591 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
593 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
601 # Redundant interwiki prefix to the local wiki
602 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
603 $this->mInterwiki
= "";
605 # We already know that some pages won't be in the database!
607 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
608 $this->mArticleID
= 0;
610 $f = strstr( $r, "#" );
611 if ( false !== $f ) {
612 $this->mFragment
= substr( $f, 1 );
613 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
616 # Reject illegal characters.
618 if( preg_match( $rxTc, $r ) ) {
622 # "." and ".." conflict with the directories of those names
623 if ( $r === "." ||
$r === ".." ) {
627 # Initial capital letter
628 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $r );
631 $this->mDbkeyform
= $t;
632 $this->mUrlform
= wfUrlencode( $t );
634 $this->mTextform
= str_replace( "_", " ", $t );
636 wfProfileOut( $fname );
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() );
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() );