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";
80 function getInterwikiLink( $key )
82 global $wgValidInterwikis;
84 if ( array_key_exists( $key, $wgValidInterwikis ) ) {
85 return $wgValidInterwikis[$key];
89 function getText() { return $this->mTextform
; }
90 function getURL() { return $this->mUrlform
; }
91 function getDBkey() { return $this->mDbkeyform
; }
92 function getNamespace() { return $this->mNamespace
; }
93 function setNamespace( $n ) { $this->mNamespace
= $n; }
94 function getInterwiki() { return $this->mInterwiki
; }
95 function getFragment() { return $this->mFragment
; }
97 /* static */ function indexTitle( $ns, $title )
99 global $wgDBminWordLen, $wgLang;
101 $lc = SearchEngine
::legalSearchChars() . "&#;";
102 $t = $wgLang->stripForSearch( $title );
103 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
104 $t = strtolower( $t );
107 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
108 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
110 $t = preg_replace( "/\\s+/", " ", $t );
112 if ( $ns == Namespace::getImage() ) {
113 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
118 function getIndexTitle()
120 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
123 /* static */ function makeName( $ns, $title )
127 $n = $wgLang->getNsText( $ns );
128 if ( "" == $n ) { return $title; }
129 else { return "{$n}:{$title}"; }
132 /* static */ function makeTitle( $ns, $title )
135 $t->mDbkeyform
= Title
::makeName( $ns, $title );
136 $t->secureAndSplit();
140 function getPrefixedDBkey()
142 $s = $this->prefix( $this->mDbkeyform
);
143 $s = str_replace( " ", "_", $s );
147 function getPrefixedText()
149 $s = $this->prefix( $this->mTextform
);
150 $s = str_replace( "_", " ", $s );
154 function getPrefixedURL()
156 $s = $this->prefix( $this->mDbkeyform
);
157 $s = str_replace( " ", "_", $s );
159 $s = urlencode ( $s ) ;
160 # Cleaning up URL to make it look nice -- is this safe?
161 $s = preg_replace( "/%3[Aa]/", ":", $s );
162 $s = preg_replace( "/%2[Ff]/", "/", $s );
163 $s = str_replace( "%28", "(", $s );
164 $s = str_replace( "%29", ")", $s );
168 function getFullURL()
170 global $wgLang, $wgArticlePath, $wgValidInterwikis;
172 if ( "" == $this->mInterwiki
) {
175 $p = $wgValidInterwikis[$this->mInterwiki
];
177 $n = $wgLang->getNsText( $this->mNamespace
);
178 if ( "" != $n ) { $n .= ":"; }
179 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
180 if ( "" != $this->mFragment
) {
181 $u .= "#" . $this->mFragment
;
186 function getEditURL()
188 global $wgServer, $wgScript;
190 if ( "" != $this->mInterwiki
) { return ""; }
191 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
196 function isExternal() { return ( "" != $this->mInterwiki
); }
198 function isProtected()
200 if ( -1 == $this->mNamespace
) { return true; }
201 $a = $this->getRestrictions();
202 if ( in_array( "sysop", $a ) ) { return true; }
208 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
211 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
212 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
218 function userIsWatching()
222 if ( -1 == $this->mNamespace
) { return false; }
223 if ( 0 == $wgUser->getID() ) { return false; }
225 return $wgUser->isWatched( $this );
228 function userCanEdit()
232 if ( -1 == $this->mNamespace
) { return false; }
233 # if ( 0 == $this->getArticleID() ) { return false; }
234 if ( $this->mDbkeyform
== "_" ) { return false; }
236 $ur = $wgUser->getRights();
237 foreach ( $this->getRestrictions() as $r ) {
238 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
245 function getRestrictions()
247 $id = $this->getArticleID();
248 if ( 0 == $id ) { return array(); }
250 if ( ! $this->mRestrictionsLoaded
) {
251 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
252 $this->mRestrictions
= explode( ",", trim( $res ) );
253 $this->mRestrictionsLoaded
= true;
255 return $this->mRestrictions
;
258 function getArticleID()
262 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
263 $this->mArticleID
= $wgLinkCache->addLink(
264 $this->getPrefixedDBkey() );
265 return $this->mArticleID
;
268 function resetArticleID( $newid )
271 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
273 if ( 0 == $newid ) { $this->mArticleID
= -1; }
274 else { $this->mArticleID
= $newid; }
275 $this->mRestrictionsLoaded
= false;
276 $this->mRestrictions
= array();
279 /* private */ function prefix( $name )
284 if ( "" != $this->mInterwiki
) {
285 $p = $this->mInterwiki
. ":";
287 if ( 0 != $this->mNamespace
) {
288 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
293 # Assumes that mDbkeyform has been set, and is urldecoded
294 # and uses undersocres, but not otherwise munged. This function
295 # removes illegal characters, splits off the winterwiki and
296 # namespace prefixes, sets the other forms, and canonicalizes
297 # everything. This one function is really at the core of
298 # Wiki--don't mess with it unless you're really sure you know
301 /* private */ function secureAndSplit()
303 global $wgLang, $wgValidInterwikis, $wgLocalInterwiki;
305 $validNamespaces = $wgLang->getNamespaces();
306 unset( $validNamespaces[0] );
308 $this->mInterwiki
= $this->mFragment
= "";
309 $this->mNamespace
= 0;
311 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
312 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
314 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
315 if ( "" == $t ) { $t = "_"; }
317 $this->mDbkeyform
= $t;
320 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
321 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
322 $t = substr( $t, 1 );
324 if ( ":" == $t{0} ) {
325 $r = substr( $t, 1 );
327 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
328 #$p = strtolower( $m[1] );
330 if ( array_key_exists( $p, $wgValidInterwikis ) ) {
332 $this->mInterwiki
= $p;
334 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
336 $p = strtolower( $m[1] );
340 if($this->mInterwiki
!= $wgLocalInterwiki)
344 if ( $ns = $wgLang->getNsIndex( str_replace( " ", "_", $p ))) {
346 $this->mNamespace
= $ns;
348 # foreach ( $validNamespaces as $ns ) {
349 # if ( 0 == strcasecmp( $p, $ns ) ) {
351 # $this->mNamespace = $wgLang->getNsIndex(
352 # str_replace( " ", "_", $p ) );
360 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
361 $this->mInterwiki
= "";
363 # We already know that some pages won't be in the database!
365 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
366 $this->mArticleID
= 0;
368 $f = strstr( $r, "#" );
369 if ( false !== $f ) {
370 $this->mFragment
= substr( $f, 1 );
371 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
373 # Strip illegal characters.
375 $tc = Title
::legalChars();
376 $t = preg_replace( "/[^{$tc}]/", "", $r );
378 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
379 $this->mDbkeyform
= $t;
380 $this->mUrlform
= wfUrlencode( $t );
381 $this->mTextform
= str_replace( "_", " ", $t );