4 include_once( "$IP/Interwiki.php" );
7 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
8 /* private */ var $mNamespace, $mInterwiki, $mFragment;
9 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
11 /* private */ function Title()
13 $this->mInterwiki
= $this->mUrlform
=
14 $this->mTextform
= $this->mDbkeyform
= "";
15 $this->mArticleID
= -1;
16 $this->mNamespace
= 0;
17 $this->mRestrictionsLoaded
= false;
18 $this->mRestrictions
= array();
21 # Static factory methods
23 function newFromDBkey( $key )
26 $t->mDbkeyform
= $key;
31 function newFromText( $text )
33 # Note - mixing latin1 named entities and unicode numbered
34 # ones will result in a bad link.
35 $trans = get_html_translation_table( HTML_ENTITIES
);
36 $trans = array_flip( $trans );
37 $text = strtr( $text, $trans );
39 $text = wfMungeToUtf8( $text );
41 $text = urldecode( $text );
44 $t->mDbkeyform
= str_replace( " ", "_", $text );
49 function newFromURL( $url )
51 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
54 $s = urldecode( $url ); # This is technically wrong, as anything
55 # we've gotten is already decoded by PHP.
56 # Kept for backwards compatibility with
57 # buggy URLs we had for a while...
59 # For links that came from outside, check for alternate/legacy
61 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
62 $s = $wgLang->checkTitleEncoding( $s );
64 $t->mDbkeyform
= str_replace( " ", "_", $s );
71 global $wgInputEncoding;
72 if( $wgInputEncoding == "utf-8" ) {
73 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
75 # ISO 8859-* don't allow 0x80-0x9F
76 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
77 # But that breaks interlanguage links at the moment. Temporary:
78 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
82 function getInterwikiLink( $key )
84 global $wgValidInterwikis;
86 if ( array_key_exists( $key, $wgValidInterwikis ) ) {
87 return $wgValidInterwikis[$key];
91 function getText() { return $this->mTextform
; }
92 function getURL() { return $this->mUrlform
; }
93 function getDBkey() { return $this->mDbkeyform
; }
94 function getNamespace() { return $this->mNamespace
; }
95 function setNamespace( $n ) { $this->mNamespace
= $n; }
96 function getInterwiki() { return $this->mInterwiki
; }
97 function getFragment() { return $this->mFragment
; }
99 /* static */ function indexTitle( $ns, $title )
101 global $wgDBminWordLen, $wgLang;
103 $lc = SearchEngine
::legalSearchChars() . "&#;";
104 $t = $wgLang->stripForSearch( $title );
105 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
106 $t = strtolower( $t );
109 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
110 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
112 $t = preg_replace( "/\\s+/", " ", $t );
114 if ( $ns == Namespace::getImage() ) {
115 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
120 function getIndexTitle()
122 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
125 /* static */ function makeName( $ns, $title )
129 $n = $wgLang->getNsText( $ns );
130 if ( "" == $n ) { return $title; }
131 else { return "{$n}:{$title}"; }
134 /* static */ function makeTitle( $ns, $title )
137 $t->mDbkeyform
= Title
::makeName( $ns, $title );
138 $t->secureAndSplit();
142 function getPrefixedDBkey()
144 $s = $this->prefix( $this->mDbkeyform
);
145 $s = str_replace( " ", "_", $s );
149 function getPrefixedText()
151 $s = $this->prefix( $this->mTextform
);
152 $s = str_replace( "_", " ", $s );
156 function getPrefixedURL()
158 $s = $this->prefix( $this->mDbkeyform
);
159 $s = str_replace( " ", "_", $s );
161 $s = urlencode ( $s ) ;
162 # Cleaning up URL to make it look nice -- is this safe?
163 $s = preg_replace( "/%3[Aa]/", ":", $s );
164 $s = preg_replace( "/%2[Ff]/", "/", $s );
165 $s = str_replace( "%28", "(", $s );
166 $s = str_replace( "%29", ")", $s );
170 function getFullURL()
172 global $wgLang, $wgArticlePath, $wgValidInterwikis;
174 if ( "" == $this->mInterwiki
) {
177 $p = $wgValidInterwikis[$this->mInterwiki
];
179 $n = $wgLang->getNsText( $this->mNamespace
);
180 if ( "" != $n ) { $n .= ":"; }
181 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
182 if ( "" != $this->mFragment
) {
183 $u .= "#" . $this->mFragment
;
188 function getEditURL()
190 global $wgServer, $wgScript;
192 if ( "" != $this->mInterwiki
) { return ""; }
193 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
198 function isExternal() { return ( "" != $this->mInterwiki
); }
200 function isProtected()
202 if ( -1 == $this->mNamespace
) { return true; }
203 $a = $this->getRestrictions();
204 if ( in_array( "sysop", $a ) ) { return true; }
210 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
213 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
214 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
220 function userIsWatching()
224 if ( -1 == $this->mNamespace
) { return false; }
225 if ( 0 == $wgUser->getID() ) { return false; }
227 return $wgUser->isWatched( $this );
230 function userCanEdit()
234 if ( -1 == $this->mNamespace
) { return false; }
235 # if ( 0 == $this->getArticleID() ) { return false; }
236 if ( $this->mDbkeyform
== "_" ) { return false; }
238 $ur = $wgUser->getRights();
239 foreach ( $this->getRestrictions() as $r ) {
240 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
247 function getRestrictions()
249 $id = $this->getArticleID();
250 if ( 0 == $id ) { return array(); }
252 if ( ! $this->mRestrictionsLoaded
) {
253 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
254 $this->mRestrictions
= explode( ",", trim( $res ) );
255 $this->mRestrictionsLoaded
= true;
257 return $this->mRestrictions
;
260 function getArticleID()
264 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
265 $this->mArticleID
= $wgLinkCache->addLink(
266 $this->getPrefixedDBkey() );
267 return $this->mArticleID
;
270 function resetArticleID( $newid )
273 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
275 if ( 0 == $newid ) { $this->mArticleID
= -1; }
276 else { $this->mArticleID
= $newid; }
277 $this->mRestrictionsLoaded
= false;
278 $this->mRestrictions
= array();
281 /* private */ function prefix( $name )
286 if ( "" != $this->mInterwiki
) {
287 $p = $this->mInterwiki
. ":";
289 if ( 0 != $this->mNamespace
) {
290 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
295 # Assumes that mDbkeyform has been set, and is urldecoded
296 # and uses undersocres, but not otherwise munged. This function
297 # removes illegal characters, splits off the winterwiki and
298 # namespace prefixes, sets the other forms, and canonicalizes
299 # everything. This one function is really at the core of
300 # Wiki--don't mess with it unless you're really sure you know
303 /* private */ function secureAndSplit()
305 global $wgLang, $wgValidInterwikis, $wgLocalInterwiki;
307 $validNamespaces = $wgLang->getNamespaces();
308 unset( $validNamespaces[0] );
310 $this->mInterwiki
= $this->mFragment
= "";
311 $this->mNamespace
= 0;
313 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
314 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
316 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
317 if ( "" == $t ) { $t = "_"; }
319 $this->mDbkeyform
= $t;
322 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
323 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
324 $t = substr( $t, 1 );
326 if ( ":" == $t{0} ) {
327 $r = substr( $t, 1 );
329 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
330 #$p = strtolower( $m[1] );
332 if ( array_key_exists( $p, $wgValidInterwikis ) ) {
334 $this->mInterwiki
= $p;
336 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
338 $p = strtolower( $m[1] );
342 if($this->mInterwiki
!= $wgLocalInterwiki)
346 if ( $ns = $wgLang->getNsIndex( str_replace( " ", "_", $p ))) {
348 $this->mNamespace
= $ns;
350 # foreach ( $validNamespaces as $ns ) {
351 # if ( 0 == strcasecmp( $p, $ns ) ) {
353 # $this->mNamespace = $wgLang->getNsIndex(
354 # str_replace( " ", "_", $p ) );
362 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
363 $this->mInterwiki
= "";
365 # We already know that some pages won't be in the database!
367 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
368 $this->mArticleID
= 0;
370 $f = strstr( $r, "#" );
371 if ( false !== $f ) {
372 $this->mFragment
= substr( $f, 1 );
373 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
375 # Strip illegal characters.
377 $tc = Title
::legalChars();
378 $t = preg_replace( "/[^{$tc}]/", "", $r );
380 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
381 $this->mDbkeyform
= $t;
382 $this->mUrlform
= wfUrlencode( $t );
383 $this->mTextform
= str_replace( "_", " ", $t );