4 /* private static */ $title_html_translation_table = array_flip( get_html_translation_table( HTML_ENTITIES
) );
5 /* private static */ $title_interwiki_cache = array();
8 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
9 /* private */ var $mNamespace, $mInterwiki, $mFragment;
10 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
11 /* private */ var $mPrefixedText;
13 /* private */ function Title()
15 $this->mInterwiki
= $this->mUrlform
=
16 $this->mTextform
= $this->mDbkeyform
= "";
17 $this->mArticleID
= -1;
18 $this->mNamespace
= 0;
19 $this->mRestrictionsLoaded
= false;
20 $this->mRestrictions
= array();
23 # Static factory methods
25 function newFromDBkey( $key )
28 $t->mDbkeyform
= $key;
29 if( $t->secureAndSplit() )
35 function newFromText( $text )
37 global $title_html_translation_table;
38 $fname = "Title::newFromText";
39 wfProfileIn( $fname );
41 # Note - mixing latin1 named entities and unicode numbered
42 # ones will result in a bad link.
43 $trans =& $title_html_translation_table;
45 $text = strtr( $text, $trans );
47 $text = wfMungeToUtf8( $text );
49 $text = urldecode( $text );
52 $t->mDbkeyform
= str_replace( " ", "_", $text );
53 wfProfileOut( $fname );
54 if( $t->secureAndSplit() ) {
61 function newFromURL( $url )
63 global $wgLang, $wgServer;
66 $s = urldecode( $url ); # This is technically wrong, as anything
67 # we've gotten is already decoded by PHP.
68 # Kept for backwards compatibility with
69 # buggy URLs we had for a while...
71 # For links that came from outside, check for alternate/legacy
73 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
74 wfDebug( "Servr: $wgServer\n" );
75 if( empty( $_SERVER["HTTP_REFERER"] ) ||
76 strncmp($wgServer, $_SERVER["HTTP_REFERER"], strlen( $wgServer ) ) )
77 $s = $wgLang->checkTitleEncoding( $s );
79 $t->mDbkeyform
= str_replace( " ", "_", $s );
80 if( $t->secureAndSplit() ) {
87 function nameOf( $id )
89 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
91 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
92 if ( 0 == wfNumRows( $res ) ) { return NULL; }
94 $s = wfFetchObject( $res );
95 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
100 function legalChars()
102 global $wgInputEncoding;
103 if( $wgInputEncoding == "utf-8" ) {
104 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
106 # ISO 8859-* don't allow 0x80-0x9F
107 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
108 # But that breaks interlanguage links at the moment. Temporary:
109 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
113 function getInterwikiLink( $key )
115 # Performance note: It would probably be a good idea to
116 # get/set/fetch the entire $title_interwiki_cache with memcache
117 # here, reducing some overhead for the repeated memcache accesses.
118 # Popular pages often has many interwiki links anyways.
120 global $wgMemc, $wgDBname, $title_interwiki_cache;
121 $k = "$wgDBname:interwiki:$key";
123 if( array_key_exists( $k, $title_interwiki_cache ) )
124 return $title_interwiki_cache[$k]->iw_url
;
126 $s = $wgMemc->get( $k );
127 if( $s !== false ) return $s->iw_url
;
129 $dkey = wfStrencode( $key );
130 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
131 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
134 $s = wfFetchObject( $res );
139 $wgMemc->set( $k, $s );
140 $title_interwiki_cache[$k] = $s;
144 function getText() { return $this->mTextform
; }
145 function getURL() { return $this->mUrlform
; }
146 function getDBkey() { return $this->mDbkeyform
; }
147 function getNamespace() { return $this->mNamespace
; }
148 function setNamespace( $n ) { $this->mNamespace
= $n; }
149 function getInterwiki() { return $this->mInterwiki
; }
150 function getFragment() { return $this->mFragment
; }
152 /* static */ function indexTitle( $ns, $title )
154 global $wgDBminWordLen, $wgLang;
156 $lc = SearchEngine
::legalSearchChars() . "&#;";
157 $t = $wgLang->stripForSearch( $title );
158 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
159 $t = strtolower( $t );
162 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
163 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
165 $t = preg_replace( "/\\s+/", " ", $t );
167 if ( $ns == Namespace::getImage() ) {
168 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
173 function getIndexTitle()
175 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
178 /* static */ function makeName( $ns, $title )
182 $n = $wgLang->getNsText( $ns );
183 if ( "" == $n ) { return $title; }
184 else { return "{$n}:{$title}"; }
187 /* static */ function makeTitle( $ns, $title )
190 $t->mDbkeyform
= Title
::makeName( $ns, $title );
191 if( $t->secureAndSplit() ) {
198 function getPrefixedDBkey()
200 $s = $this->prefix( $this->mDbkeyform
);
201 $s = str_replace( " ", "_", $s );
205 function getPrefixedText()
208 if ( empty( $this->mPrefixedText
) ) {
209 $s = $this->prefix( $this->mTextform
);
210 $s = str_replace( "_", " ", $s );
211 $this->mPrefixedText
= $s;
213 return $this->mPrefixedText
;
216 function getPrefixedURL()
218 $s = $this->prefix( $this->mDbkeyform
);
219 $s = str_replace( " ", "_", $s );
221 $s = urlencode ( $s ) ;
222 # Cleaning up URL to make it look nice -- is this safe?
223 $s = preg_replace( "/%3[Aa]/", ":", $s );
224 $s = preg_replace( "/%2[Ff]/", "/", $s );
225 $s = str_replace( "%28", "(", $s );
226 $s = str_replace( "%29", ")", $s );
230 function getFullURL()
232 global $wgLang, $wgArticlePath;
234 if ( "" == $this->mInterwiki
) {
237 $p = $this->getInterwikiLink( $this->mInterwiki
);
239 $n = $wgLang->getNsText( $this->mNamespace
);
240 if ( "" != $n ) { $n .= ":"; }
241 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
242 if ( "" != $this->mFragment
) {
243 $u .= "#" . $this->mFragment
;
248 function getEditURL()
250 global $wgServer, $wgScript;
252 if ( "" != $this->mInterwiki
) { return ""; }
253 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
258 # For the title field in <a> tags
259 function getEscapedText()
261 return wfEscapeHTML( $this->getPrefixedText() );
264 function isExternal() { return ( "" != $this->mInterwiki
); }
266 function isProtected()
268 if ( -1 == $this->mNamespace
) { return true; }
269 $a = $this->getRestrictions();
270 if ( in_array( "sysop", $a ) ) { return true; }
276 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
279 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
280 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
286 function userIsWatching()
290 if ( -1 == $this->mNamespace
) { return false; }
291 if ( 0 == $wgUser->getID() ) { return false; }
293 return $wgUser->isWatched( $this );
296 function userCanEdit()
300 if ( -1 == $this->mNamespace
) { return false; }
301 # if ( 0 == $this->getArticleID() ) { return false; }
302 if ( $this->mDbkeyform
== "_" ) { return false; }
304 $ur = $wgUser->getRights();
305 foreach ( $this->getRestrictions() as $r ) {
306 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
313 function getRestrictions()
315 $id = $this->getArticleID();
316 if ( 0 == $id ) { return array(); }
318 if ( ! $this->mRestrictionsLoaded
) {
319 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
320 $this->mRestrictions
= explode( ",", trim( $res ) );
321 $this->mRestrictionsLoaded
= true;
323 return $this->mRestrictions
;
326 function isDeleted() {
327 $ns = $this->getNamespace();
328 $t = wfStrencode( $this->getDBkey() );
329 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
330 if( $res = wfQuery( $sql, DB_READ
) ) {
331 $s = wfFetchObject( $res );
337 function getArticleID()
341 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
342 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
343 return $this->mArticleID
;
346 function resetArticleID( $newid )
349 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
351 if ( 0 == $newid ) { $this->mArticleID
= -1; }
352 else { $this->mArticleID
= $newid; }
353 $this->mRestrictionsLoaded
= false;
354 $this->mRestrictions
= array();
357 function invalidateCache() {
358 $now = wfTimestampNow();
359 $ns = $this->getNamespace();
360 $ti = wfStrencode( $this->getDBkey() );
361 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
362 return wfQuery( $sql, DB_WRITE
, "Title::invalidateCache" );
365 /* private */ function prefix( $name )
370 if ( "" != $this->mInterwiki
) {
371 $p = $this->mInterwiki
. ":";
373 if ( 0 != $this->mNamespace
) {
374 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
379 # Assumes that mDbkeyform has been set, and is urldecoded
380 # and uses undersocres, but not otherwise munged. This function
381 # removes illegal characters, splits off the winterwiki and
382 # namespace prefixes, sets the other forms, and canonicalizes
385 /* private */ function secureAndSplit()
387 global $wgLang, $wgLocalInterwiki;
388 $fname = "Title::secureAndSplit";
389 wfProfileIn( $fname );
391 static $imgpre = false;
392 static $rxTc = false;
395 if ( $imgpre === false ) {
396 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
397 $rxTc = "/[^" . Title
::legalChars() . "]/";
401 $this->mInterwiki
= $this->mFragment
= "";
402 $this->mNamespace
= 0;
404 # Clean up whitespace
406 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
407 if ( "_" == $t{0} ) {
408 $t = substr( $t, 1 );
411 if ( $l && ( "_" == $t{$l-1} ) ) {
412 $t = substr( $t, 0, $l-1 );
415 wfProfileOut( $fname );
419 $this->mDbkeyform
= $t;
422 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
423 $t = substr( $t, 1 );
425 if ( ":" == $t{0} ) {
426 $r = substr( $t, 1 );
428 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
429 #$p = strtolower( $m[1] );
431 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
433 $this->mNamespace
= $ns;
434 } elseif ( $this->getInterwikiLink( $p ) ) {
436 $this->mInterwiki
= $p;
438 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
440 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
447 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
448 $this->mInterwiki
= "";
450 # We already know that some pages won't be in the database!
452 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
453 $this->mArticleID
= 0;
455 $f = strstr( $r, "#" );
456 if ( false !== $f ) {
457 $this->mFragment
= substr( $f, 1 );
458 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
461 # Reject illegal characters.
463 if( preg_match( $rxTc, $r ) ) {
467 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $r );
468 $this->mDbkeyform
= $t;
469 $this->mUrlform
= wfUrlencode( $t );
470 $this->mTextform
= str_replace( "_", " ", $t );
472 wfProfileOut( $fname );
476 function getTalkPage() {
477 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
480 function getSubjectPage() {
481 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );