db4e1122c0e3ad6e67137c4ad3564a2693e36ab0
5 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
6 /* private */ var $mNamespace, $mInterwiki, $mFragment;
7 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
8 /* private */ var $mPrefixedText;
10 /* private */ function Title()
12 $this->mInterwiki
= $this->mUrlform
=
13 $this->mTextform
= $this->mDbkeyform
= "";
14 $this->mArticleID
= -1;
15 $this->mNamespace
= 0;
16 $this->mRestrictionsLoaded
= false;
17 $this->mRestrictions
= array();
18 $this->mPrefixedText
= false;
21 # Static factory methods
23 function newFromDBkey( $key )
26 $t->mDbkeyform
= $key;
27 if( $t->secureAndSplit() )
34 function newFromText( $text )
36 $fname = "Title::newFromText";
37 wfProfileIn( $fname );
39 # Note - mixing latin1 named entities and unicode numbered
40 # ones will result in a bad link.
41 $trans = get_html_translation_table( HTML_ENTITIES
);
42 $trans = array_flip( $trans );
43 $text = strtr( $text, $trans );
45 $text = wfMungeToUtf8( $text );
47 $text = urldecode( $text );
50 $t->mDbkeyform
= str_replace( " ", "_", $text );
51 wfProfileOut( $fname );
52 if( $t->secureAndSplit() ) {
59 function newFromURL( $url )
61 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
64 $s = urldecode( $url ); # This is technically wrong, as anything
65 # we've gotten is already decoded by PHP.
66 # Kept for backwards compatibility with
67 # buggy URLs we had for a while...
69 # For links that came from outside, check for alternate/legacy
71 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
72 $s = $wgLang->checkTitleEncoding( $s );
74 $t->mDbkeyform
= str_replace( " ", "_", $s );
75 if( $t->secureAndSplit() ) {
82 function nameOf( $id )
84 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
86 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
87 if ( 0 == wfNumRows( $res ) ) { return NULL; }
89 $s = wfFetchObject( $res );
90 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
97 global $wgInputEncoding;
98 if( $wgInputEncoding == "utf-8" ) {
99 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
101 # ISO 8859-* don't allow 0x80-0x9F
102 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
103 # But that breaks interlanguage links at the moment. Temporary:
104 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
108 function getInterwikiLink( $key )
110 global $wgMemc, $wgDBname;
111 $k = "$wgDBname:interwiki:$key";
112 $s = $wgMemc->get( $k );
113 if( $s !== false ) return $s->iw_url
;
115 $dkey = wfStrencode( $key );
116 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
117 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
120 $s = wfFetchObject( $res );
125 $wgMemc->set( $k, $s );
129 function getText() { return $this->mTextform
; }
130 function getURL() { return $this->mUrlform
; }
131 function getDBkey() { return $this->mDbkeyform
; }
132 function getNamespace() { return $this->mNamespace
; }
133 function setNamespace( $n ) { $this->mNamespace
= $n; }
134 function getInterwiki() { return $this->mInterwiki
; }
135 function getFragment() { return $this->mFragment
; }
137 /* static */ function indexTitle( $ns, $title )
139 global $wgDBminWordLen, $wgLang;
141 $lc = SearchEngine
::legalSearchChars() . "&#;";
142 $t = $wgLang->stripForSearch( $title );
143 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
144 $t = strtolower( $t );
147 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
148 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
150 $t = preg_replace( "/\\s+/", " ", $t );
152 if ( $ns == Namespace::getImage() ) {
153 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
158 function getIndexTitle()
160 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
163 /* static */ function makeName( $ns, $title )
167 $n = $wgLang->getNsText( $ns );
168 if ( "" == $n ) { return $title; }
169 else { return "{$n}:{$title}"; }
172 /* static */ function makeTitle( $ns, $title )
175 $t->mDbkeyform
= Title
::makeName( $ns, $title );
176 if( $t->secureAndSplit() ) {
183 function getPrefixedDBkey()
185 $s = $this->prefix( $this->mDbkeyform
);
186 $s = str_replace( " ", "_", $s );
190 function getPrefixedText()
192 if ( $this->mPrefixedText
=== false ) {
193 $s = $this->prefix( $this->mTextform
);
194 $s = str_replace( "_", " ", $s );
195 $this->mPrefixedText
= $s;
197 return $this->mPrefixedText
;
200 function getPrefixedURL()
202 $s = $this->prefix( $this->mDbkeyform
);
203 $s = str_replace( " ", "_", $s );
205 $s = urlencode ( $s ) ;
206 # Cleaning up URL to make it look nice -- is this safe?
207 $s = preg_replace( "/%3[Aa]/", ":", $s );
208 $s = preg_replace( "/%2[Ff]/", "/", $s );
209 $s = str_replace( "%28", "(", $s );
210 $s = str_replace( "%29", ")", $s );
214 function getFullURL()
216 global $wgLang, $wgArticlePath;
218 if ( "" == $this->mInterwiki
) {
221 $p = $this->getInterwikiLink( $this->mInterwiki
);
223 $n = $wgLang->getNsText( $this->mNamespace
);
224 if ( "" != $n ) { $n .= ":"; }
225 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
226 if ( "" != $this->mFragment
) {
227 $u .= "#" . $this->mFragment
;
232 function getEditURL()
234 global $wgServer, $wgScript;
236 if ( "" != $this->mInterwiki
) { return ""; }
237 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
242 # For the title field in <a> tags
243 function getEscapedText()
245 return wfEscapeHTML( $this->getPrefixedText() );
248 function isExternal() { return ( "" != $this->mInterwiki
); }
250 function isProtected()
252 if ( -1 == $this->mNamespace
) { return true; }
253 $a = $this->getRestrictions();
254 if ( in_array( "sysop", $a ) ) { return true; }
260 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
263 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
264 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
270 function userIsWatching()
274 if ( -1 == $this->mNamespace
) { return false; }
275 if ( 0 == $wgUser->getID() ) { return false; }
277 return $wgUser->isWatched( $this );
280 function userCanEdit()
284 if ( -1 == $this->mNamespace
) { return false; }
285 # if ( 0 == $this->getArticleID() ) { return false; }
286 if ( $this->mDbkeyform
== "_" ) { return false; }
288 $ur = $wgUser->getRights();
289 foreach ( $this->getRestrictions() as $r ) {
290 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
297 function getRestrictions()
299 $id = $this->getArticleID();
300 if ( 0 == $id ) { return array(); }
302 if ( ! $this->mRestrictionsLoaded
) {
303 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
304 $this->mRestrictions
= explode( ",", trim( $res ) );
305 $this->mRestrictionsLoaded
= true;
307 return $this->mRestrictions
;
310 function isDeleted() {
311 $ns = $this->getNamespace();
312 $t = wfStrencode( $this->getDBkey() );
313 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
314 if( $res = wfQuery( $sql, DB_READ
) ) {
315 $s = wfFetchObject( $res );
321 function getArticleID()
325 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
326 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
327 return $this->mArticleID
;
330 function resetArticleID( $newid )
333 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
335 if ( 0 == $newid ) { $this->mArticleID
= -1; }
336 else { $this->mArticleID
= $newid; }
337 $this->mRestrictionsLoaded
= false;
338 $this->mRestrictions
= array();
341 /* private */ function prefix( $name )
346 if ( "" != $this->mInterwiki
) {
347 $p = $this->mInterwiki
. ":";
349 if ( 0 != $this->mNamespace
) {
350 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
355 # Assumes that mDbkeyform has been set, and is urldecoded
356 # and uses undersocres, but not otherwise munged. This function
357 # removes illegal characters, splits off the winterwiki and
358 # namespace prefixes, sets the other forms, and canonicalizes
361 /* private */ function secureAndSplit()
363 global $wgLang, $wgLocalInterwiki;
364 $fname = "Title::secureAndSplit";
365 wfProfileIn( $fname );
367 static $imgpre = false;
368 static $rxTc = false;
371 if ( $imgpre === false ) {
372 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
373 $rxTc = "/[^" . Title
::legalChars() . "]/";
377 $this->mInterwiki
= $this->mFragment
= "";
378 $this->mNamespace
= 0;
380 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
381 if ( "_" == $t{0} ) {
382 $t = substr( $t, 1 );
385 if ( $l && ( "_" == $t{$l-1} ) ) {
386 $t = substr( $t, 0, $l-1 );
389 wfProfileOut( $fname );
393 $this->mDbkeyform
= $t;
396 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
397 $t = substr( $t, 1 );
399 if ( ":" == $t{0} ) {
400 $r = substr( $t, 1 );
402 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
403 #$p = strtolower( $m[1] );
405 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
407 $this->mNamespace
= $ns;
408 } elseif ( $this->getInterwikiLink( $p ) ) {
410 $this->mInterwiki
= $p;
412 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
414 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
421 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
422 $this->mInterwiki
= "";
424 # We already know that some pages won't be in the database!
426 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
427 $this->mArticleID
= 0;
429 $f = strstr( $r, "#" );
430 if ( false !== $f ) {
431 $this->mFragment
= substr( $f, 1 );
432 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
434 # Strip illegal characters.
436 $t = preg_replace( $rxTc, "", $r );
438 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
439 $this->mDbkeyform
= $t;
440 $this->mUrlform
= wfUrlencode( $t );
441 $this->mTextform
= str_replace( "_", " ", $t );
443 wfProfileOut( $fname );