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 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
74 function getInterwikiLink( $key )
76 global $wgValidInterwikis;
78 if ( array_key_exists( $key, $wgValidInterwikis ) ) {
79 return $wgValidInterwikis[$key];
83 function getText() { return $this->mTextform
; }
84 function getURL() { return $this->mUrlform
; }
85 function getDBkey() { return $this->mDbkeyform
; }
86 function getNamespace() { return $this->mNamespace
; }
87 function setNamespace( $n ) { $this->mNamespace
= $n; }
88 function getInterwiki() { return $this->mInterwiki
; }
89 function getFragment() { return $this->mFragment
; }
91 /* static */ function indexTitle( $ns, $title )
93 global $wgDBminWordLen, $wgLang;
95 $lc = SearchEngine
::legalSearchChars() . "&#;";
96 $t = $wgLang->stripForSearch( $title );
97 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
98 $t = strtolower( $t );
101 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
102 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
104 $t = preg_replace( "/\\s+/", " ", $t );
106 if ( $ns == Namespace::getImage() ) {
107 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
112 function getIndexTitle()
114 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
117 /* static */ function makeName( $ns, $title )
121 $n = $wgLang->getNsText( $ns );
122 if ( "" == $n ) { return $title; }
123 else { return "{$n}:{$title}"; }
126 function getPrefixedDBkey()
128 $s = $this->prefix( $this->mDbkeyform
);
129 $s = str_replace( " ", "_", $s );
133 function getPrefixedText()
135 $s = $this->prefix( $this->mTextform
);
136 $s = str_replace( "_", " ", $s );
140 function getPrefixedURL()
142 $s = $this->prefix( $this->mDbkeyform
);
143 $s = str_replace( " ", "_", $s );
145 $s = urlencode ( $s ) ;
146 # Cleaning up URL to make it look nice -- is this safe?
147 $s = preg_replace( "/%3[Aa]/", ":", $s );
148 $s = preg_replace( "/%2[Ff]/", "/", $s );
149 $s = str_replace( "%28", "(", $s );
150 $s = str_replace( "%29", ")", $s );
154 function getFullURL()
156 global $wgLang, $wgArticlePath, $wgValidInterwikis;
158 if ( "" == $this->mInterwiki
) {
161 $p = $wgValidInterwikis[$this->mInterwiki
];
163 $n = $wgLang->getNsText( $this->mNamespace
);
164 if ( "" != $n ) { $n .= ":"; }
165 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
166 if ( "" != $this->mFragment
) {
167 $u .= "#" . $this->mFragment
;
172 function getEditURL()
174 global $wgServer, $wgScript;
176 if ( "" != $this->mInterwiki
) { return ""; }
177 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
182 function isExternal() { return ( "" != $this->mInterwiki
); }
184 function isProtected()
186 if ( -1 == $this->mNamespace
) { return true; }
187 $a = $this->getRestrictions();
188 if ( in_array( "sysop", $a ) ) { return true; }
194 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
197 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
198 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
204 function userIsWatching()
208 if ( -1 == $this->mNamespace
) { return false; }
209 if ( 0 == $wgUser->getID() ) { return false; }
211 return $wgUser->isWatched( $this );
214 function userCanEdit()
218 if ( -1 == $this->mNamespace
) { return false; }
219 # if ( 0 == $this->getArticleID() ) { return false; }
220 if ( $this->mDbkeyform
== "_" ) { return false; }
222 $ur = $wgUser->getRights();
223 foreach ( $this->getRestrictions() as $r ) {
224 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
231 function getRestrictions()
233 $id = $this->getArticleID();
234 if ( 0 == $id ) { return array(); }
236 if ( ! $this->mRestrictionsLoaded
) {
237 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
238 $this->mRestrictions
= explode( ",", trim( $res ) );
239 $this->mRestrictionsLoaded
= true;
241 return $this->mRestrictions
;
244 function getArticleID()
248 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
249 $this->mArticleID
= $wgLinkCache->addLink(
250 $this->getPrefixedDBkey() );
251 return $this->mArticleID
;
254 function resetArticleID( $newid )
257 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
259 if ( 0 == $newid ) { $this->mArticleID
= -1; }
260 else { $this->mArticleID
= $newid; }
261 $this->mRestrictionsLoaded
= false;
262 $this->mRestrictions
= array();
265 /* private */ function prefix( $name )
270 if ( "" != $this->mInterwiki
) {
271 $p = $this->mInterwiki
. ":";
273 if ( 0 != $this->mNamespace
) {
274 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
279 # Assumes that mDbkeyform has been set, and is urldecoded
280 # and uses undersocres, but not otherwise munged. This function
281 # removes illegal characters, splits off the winterwiki and
282 # namespace prefixes, sets the other forms, and canonicalizes
283 # everything. This one function is really at the core of
284 # Wiki--don't mess with it unless you're really sure you know
287 /* private */ function secureAndSplit()
289 global $wgLang, $wgValidInterwikis, $wgLocalInterwiki;
291 $validNamespaces = $wgLang->getNamespaces();
292 unset( $validNamespaces[0] );
294 $this->mInterwiki
= $this->mFragment
= "";
295 $this->mNamespace
= 0;
297 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
298 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
300 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
301 if ( "" == $t ) { $t = "_"; }
303 $this->mDbkeyform
= $t;
306 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
307 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
308 $t = substr( $t, 1 );
310 if ( ":" == $t{0} ) {
311 $r = substr( $t, 1 );
313 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
314 #$p = strtolower( $m[1] );
316 if ( array_key_exists( $p, $wgValidInterwikis ) ) {
318 $this->mInterwiki
= $p;
320 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
322 $p = strtolower( $m[1] );
326 if($this->mInterwiki
!= $wgLocalInterwiki)
330 if ( $ns = $wgLang->getNsIndex( str_replace( " ", "_", $p ))) {
332 $this->mNamespace
= $ns;
334 # foreach ( $validNamespaces as $ns ) {
335 # if ( 0 == strcasecmp( $p, $ns ) ) {
337 # $this->mNamespace = $wgLang->getNsIndex(
338 # str_replace( " ", "_", $p ) );
346 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
347 $this->mInterwiki
= "";
349 # We already know that some pages won't be in the database!
351 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
352 $this->mArticleID
= 0;
354 $f = strstr( $r, "#" );
355 if ( false !== $f ) {
356 $this->mFragment
= substr( $f, 1 );
357 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
359 # Strip illegal characters.
361 $tc = Title
::legalChars();
362 $t = preg_replace( "/[^{$tc}]/", "", $r );
364 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
365 $this->mDbkeyform
= $t;
366 $this->mUrlform
= wfUrlencode( $t );
367 $this->mTextform
= str_replace( "_", " ", $t );