4 /* private static */ $title_interwiki_cache = array();
7 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
8 /* private */ var $mNamespace, $mInterwiki, $mFragment;
9 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
10 /* private */ var $mPrefixedText;
12 /* private */ function Title()
14 $this->mInterwiki
= $this->mUrlform
=
15 $this->mTextform
= $this->mDbkeyform
= "";
16 $this->mArticleID
= -1;
17 $this->mNamespace
= 0;
18 $this->mRestrictionsLoaded
= false;
19 $this->mRestrictions
= array();
22 # Static factory methods
24 function newFromDBkey( $key )
27 $t->mDbkeyform
= $key;
28 if( $t->secureAndSplit() )
34 function newFromText( $text )
37 $fname = "Title::newFromText";
38 wfProfileIn( $fname );
40 # Note - mixing latin1 named entities and unicode numbered
41 # ones will result in a bad link.
42 if( !isset( $trans ) ) {
43 global $wgInputEncoding;
44 $trans = array_flip( get_html_translation_table( HTML_ENTITIES
) );
45 if( strcasecmp( "utf-8", $wgInputEncoding ) == 0 ) {
46 $trans = array_map( "utf8_encode", $trans );
50 $text = strtr( $text, $trans );
52 $text = wfMungeToUtf8( $text );
54 $text = urldecode( $text );
57 $t->mDbkeyform
= str_replace( " ", "_", $text );
58 wfProfileOut( $fname );
59 if( $t->secureAndSplit() ) {
66 function newFromURL( $url )
68 global $wgLang, $wgServer;
71 $s = urldecode( $url ); # This is technically wrong, as anything
72 # we've gotten is already decoded by PHP.
73 # Kept for backwards compatibility with
74 # buggy URLs we had for a while...
76 # For links that came from outside, check for alternate/legacy
78 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
79 wfDebug( "Servr: $wgServer\n" );
80 if( empty( $_SERVER["HTTP_REFERER"] ) ||
81 strncmp($wgServer, $_SERVER["HTTP_REFERER"], strlen( $wgServer ) ) )
82 $s = $wgLang->checkTitleEncoding( $s );
84 $t->mDbkeyform
= str_replace( " ", "_", $s );
85 if( $t->secureAndSplit() ) {
92 # Create a title from a cur id
93 # This is inefficiently implemented
94 function newFromID( $id )
96 $fname = "Title::newFromID";
97 $row = wfGetArray( "cur", array( "cur_namespace", "cur_title" ),
98 array( "cur_id" => $id ), $fname );
99 if ( $row !== false ) {
100 $title = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
);
107 function nameOf( $id )
109 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
111 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
112 if ( 0 == wfNumRows( $res ) ) { return NULL; }
114 $s = wfFetchObject( $res );
115 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
120 function legalChars()
122 global $wgInputEncoding;
123 if( $wgInputEncoding == "utf-8" ) {
124 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
126 # ISO 8859-* don't allow 0x80-0x9F
127 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
128 # But that breaks interlanguage links at the moment. Temporary:
129 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
133 function getInterwikiLink( $key )
135 global $wgMemc, $wgDBname, $title_interwiki_cache;
136 $k = "$wgDBname:interwiki:$key";
138 if( array_key_exists( $k, $title_interwiki_cache ) )
139 return $title_interwiki_cache[$k]->iw_url
;
141 $s = $wgMemc->get( $k );
143 $title_interwiki_cache[$k] = $s;
146 $dkey = wfStrencode( $key );
147 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
148 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
151 $s = wfFetchObject( $res );
156 $wgMemc->set( $k, $s );
157 $title_interwiki_cache[$k] = $s;
161 function getText() { return $this->mTextform
; }
162 function getURL() { return $this->mUrlform
; }
163 function getDBkey() { return $this->mDbkeyform
; }
164 function getNamespace() { return $this->mNamespace
; }
165 function setNamespace( $n ) { $this->mNamespace
= $n; }
166 function getInterwiki() { return $this->mInterwiki
; }
167 function getFragment() { return $this->mFragment
; }
169 /* static */ function indexTitle( $ns, $title )
171 global $wgDBminWordLen, $wgLang;
173 $lc = SearchEngine
::legalSearchChars() . "&#;";
174 $t = $wgLang->stripForSearch( $title );
175 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
176 $t = strtolower( $t );
179 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
180 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
182 $t = preg_replace( "/\\s+/", " ", $t );
184 if ( $ns == Namespace::getImage() ) {
185 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
190 function getIndexTitle()
192 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
195 /* static */ function makeName( $ns, $title )
199 $n = $wgLang->getNsText( $ns );
200 if ( "" == $n ) { return $title; }
201 else { return "{$n}:{$title}"; }
204 /* static */ function makeTitle( $ns, $title )
207 $t->mDbkeyform
= Title
::makeName( $ns, $title );
208 if( $t->secureAndSplit() ) {
215 function getPrefixedDBkey()
217 $s = $this->prefix( $this->mDbkeyform
);
218 $s = str_replace( " ", "_", $s );
222 function getPrefixedText()
225 if ( empty( $this->mPrefixedText
) ) {
226 $s = $this->prefix( $this->mTextform
);
227 $s = str_replace( "_", " ", $s );
228 $this->mPrefixedText
= $s;
230 return $this->mPrefixedText
;
233 function getPrefixedURL()
235 $s = $this->prefix( $this->mDbkeyform
);
236 $s = str_replace( " ", "_", $s );
238 $s = urlencode ( $s ) ;
239 # Cleaning up URL to make it look nice -- is this safe?
240 $s = preg_replace( "/%3[Aa]/", ":", $s );
241 $s = preg_replace( "/%2[Ff]/", "/", $s );
242 $s = str_replace( "%28", "(", $s );
243 $s = str_replace( "%29", ")", $s );
247 function getFullURL()
249 global $wgLang, $wgArticlePath;
251 if ( "" == $this->mInterwiki
) {
254 $p = $this->getInterwikiLink( $this->mInterwiki
);
256 $n = $wgLang->getNsText( $this->mNamespace
);
257 if ( "" != $n ) { $n .= ":"; }
258 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
259 if ( "" != $this->mFragment
) {
260 $u .= "#" . $this->mFragment
;
265 function getEditURL()
267 global $wgServer, $wgScript;
269 if ( "" != $this->mInterwiki
) { return ""; }
270 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
275 # For the title field in <a> tags
276 function getEscapedText()
278 return wfEscapeHTML( $this->getPrefixedText() );
281 function isExternal() { return ( "" != $this->mInterwiki
); }
283 function isProtected()
285 if ( -1 == $this->mNamespace
) { return true; }
286 $a = $this->getRestrictions();
287 if ( in_array( "sysop", $a ) ) { return true; }
293 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
296 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
297 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
303 function userIsWatching()
307 if ( -1 == $this->mNamespace
) { return false; }
308 if ( 0 == $wgUser->getID() ) { return false; }
310 return $wgUser->isWatched( $this );
313 function userCanEdit()
317 if ( -1 == $this->mNamespace
) { return false; }
318 # if ( 0 == $this->getArticleID() ) { return false; }
319 if ( $this->mDbkeyform
== "_" ) { return false; }
321 $ur = $wgUser->getRights();
322 foreach ( $this->getRestrictions() as $r ) {
323 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
330 function getRestrictions()
332 $id = $this->getArticleID();
333 if ( 0 == $id ) { return array(); }
335 if ( ! $this->mRestrictionsLoaded
) {
336 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
337 $this->mRestrictions
= explode( ",", trim( $res ) );
338 $this->mRestrictionsLoaded
= true;
340 return $this->mRestrictions
;
343 function isDeleted() {
344 $ns = $this->getNamespace();
345 $t = wfStrencode( $this->getDBkey() );
346 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
347 if( $res = wfQuery( $sql, DB_READ
) ) {
348 $s = wfFetchObject( $res );
354 function getArticleID()
358 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
359 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
360 return $this->mArticleID
;
363 function resetArticleID( $newid )
366 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
368 if ( 0 == $newid ) { $this->mArticleID
= -1; }
369 else { $this->mArticleID
= $newid; }
370 $this->mRestrictionsLoaded
= false;
371 $this->mRestrictions
= array();
374 function invalidateCache() {
375 $now = wfTimestampNow();
376 $ns = $this->getNamespace();
377 $ti = wfStrencode( $this->getDBkey() );
378 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
379 return wfQuery( $sql, DB_WRITE
, "Title::invalidateCache" );
382 /* private */ function prefix( $name )
387 if ( "" != $this->mInterwiki
) {
388 $p = $this->mInterwiki
. ":";
390 if ( 0 != $this->mNamespace
) {
391 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
396 # Assumes that mDbkeyform has been set, and is urldecoded
397 # and uses undersocres, but not otherwise munged. This function
398 # removes illegal characters, splits off the winterwiki and
399 # namespace prefixes, sets the other forms, and canonicalizes
402 /* private */ function secureAndSplit()
404 global $wgLang, $wgLocalInterwiki;
405 $fname = "Title::secureAndSplit";
406 wfProfileIn( $fname );
408 static $imgpre = false;
409 static $rxTc = false;
412 if ( $imgpre === false ) {
413 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
414 $rxTc = "/[^" . Title
::legalChars() . "]/";
418 $this->mInterwiki
= $this->mFragment
= "";
419 $this->mNamespace
= 0;
421 # Clean up whitespace
423 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
424 if ( "_" == $t{0} ) {
425 $t = substr( $t, 1 );
428 if ( $l && ( "_" == $t{$l-1} ) ) {
429 $t = substr( $t, 0, $l-1 );
432 wfProfileOut( $fname );
436 $this->mDbkeyform
= $t;
439 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
440 $t = substr( $t, 1 );
442 if ( ":" == $t{0} ) {
443 $r = substr( $t, 1 );
445 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
446 #$p = strtolower( $m[1] );
448 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
450 $this->mNamespace
= $ns;
451 } elseif ( $this->getInterwikiLink( $p ) ) {
453 $this->mInterwiki
= $p;
455 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
457 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
464 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
465 $this->mInterwiki
= "";
467 # We already know that some pages won't be in the database!
469 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
470 $this->mArticleID
= 0;
472 $f = strstr( $r, "#" );
473 if ( false !== $f ) {
474 $this->mFragment
= substr( $f, 1 );
475 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
478 # Reject illegal characters.
480 if( preg_match( $rxTc, $r ) ) {
484 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $r );
485 $this->mDbkeyform
= $t;
486 $this->mUrlform
= wfUrlencode( $t );
487 $this->mTextform
= str_replace( "_", " ", $t );
489 wfProfileOut( $fname );
493 function getTalkPage() {
494 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
497 function getSubjectPage() {
498 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );