5 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
6 /* private */ var $mNamespace, $mInterwiki, $mFragment;
7 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
9 /* private */ function Title()
11 $this->mInterwiki
= $this->mUrlform
=
12 $this->mTextform
= $this->mDbkeyform
= "";
13 $this->mArticleID
= -1;
14 $this->mNamespace
= 0;
15 $this->mRestrictionsLoaded
= false;
16 $this->mRestrictions
= array();
19 # Static factory methods
21 function newFromDBkey( $key )
24 $t->mDbkeyform
= $key;
29 function newFromText( $text )
31 wfProfileIn( "Title::newFromText" );
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 );
51 function newFromURL( $url )
53 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
56 $s = urldecode( $url ); # This is technically wrong, as anything
57 # we've gotten is already decoded by PHP.
58 # Kept for backwards compatibility with
59 # buggy URLs we had for a while...
61 # For links that came from outside, check for alternate/legacy
63 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
64 $s = $wgLang->checkTitleEncoding( $s );
66 $t->mDbkeyform
= str_replace( " ", "_", $s );
73 global $wgInputEncoding;
74 if( $wgInputEncoding == "utf-8" ) {
75 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
77 # ISO 8859-* don't allow 0x80-0x9F
78 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
79 # But that breaks interlanguage links at the moment. Temporary:
80 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
84 function getInterwikiLink( $key )
86 global $wgMemc, $wgDBname;
87 $k = "$wgDBname:interwiki:$key";
88 $s = $wgMemc->get( $k );
89 if( $s !== false ) return $s->iw_url
;
91 $dkey = wfStrencode( $key );
92 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
93 $res = wfQuery( $query, "Title::getInterwikiLink" );
96 $s = wfFetchObject( $res );
101 $wgMemc->set( $k, $s );
105 function getText() { return $this->mTextform
; }
106 function getURL() { return $this->mUrlform
; }
107 function getDBkey() { return $this->mDbkeyform
; }
108 function getNamespace() { return $this->mNamespace
; }
109 function setNamespace( $n ) { $this->mNamespace
= $n; }
110 function getInterwiki() { return $this->mInterwiki
; }
111 function getFragment() { return $this->mFragment
; }
113 /* static */ function indexTitle( $ns, $title )
115 global $wgDBminWordLen, $wgLang;
117 $lc = SearchEngine
::legalSearchChars() . "&#;";
118 $t = $wgLang->stripForSearch( $title );
119 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
120 $t = strtolower( $t );
123 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
124 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
126 $t = preg_replace( "/\\s+/", " ", $t );
128 if ( $ns == Namespace::getImage() ) {
129 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
134 function getIndexTitle()
136 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
139 /* static */ function makeName( $ns, $title )
143 $n = $wgLang->getNsText( $ns );
144 if ( "" == $n ) { return $title; }
145 else { return "{$n}:{$title}"; }
148 /* static */ function makeTitle( $ns, $title )
151 $t->mDbkeyform
= Title
::makeName( $ns, $title );
152 $t->secureAndSplit();
156 function getPrefixedDBkey()
158 $s = $this->prefix( $this->mDbkeyform
);
159 $s = str_replace( " ", "_", $s );
163 function getPrefixedText()
165 $s = $this->prefix( $this->mTextform
);
166 $s = str_replace( "_", " ", $s );
170 function getPrefixedURL()
172 $s = $this->prefix( $this->mDbkeyform
);
173 $s = str_replace( " ", "_", $s );
175 $s = urlencode ( $s ) ;
176 # Cleaning up URL to make it look nice -- is this safe?
177 $s = preg_replace( "/%3[Aa]/", ":", $s );
178 $s = preg_replace( "/%2[Ff]/", "/", $s );
179 $s = str_replace( "%28", "(", $s );
180 $s = str_replace( "%29", ")", $s );
184 function getFullURL()
186 global $wgLang, $wgArticlePath;
188 if ( "" == $this->mInterwiki
) {
191 $p = $this->getInterwikiLink( $this->mInterwiki
);
193 $n = $wgLang->getNsText( $this->mNamespace
);
194 if ( "" != $n ) { $n .= ":"; }
195 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
196 if ( "" != $this->mFragment
) {
197 $u .= "#" . $this->mFragment
;
202 function getEditURL()
204 global $wgServer, $wgScript;
206 if ( "" != $this->mInterwiki
) { return ""; }
207 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
212 function isExternal() { return ( "" != $this->mInterwiki
); }
214 function isProtected()
216 if ( -1 == $this->mNamespace
) { return true; }
217 $a = $this->getRestrictions();
218 if ( in_array( "sysop", $a ) ) { return true; }
224 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
227 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
228 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
234 function userIsWatching()
238 if ( -1 == $this->mNamespace
) { return false; }
239 if ( 0 == $wgUser->getID() ) { return false; }
241 return $wgUser->isWatched( $this );
244 function userCanEdit()
248 if ( -1 == $this->mNamespace
) { return false; }
249 # if ( 0 == $this->getArticleID() ) { return false; }
250 if ( $this->mDbkeyform
== "_" ) { return false; }
252 $ur = $wgUser->getRights();
253 foreach ( $this->getRestrictions() as $r ) {
254 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
261 function getRestrictions()
263 $id = $this->getArticleID();
264 if ( 0 == $id ) { return array(); }
266 if ( ! $this->mRestrictionsLoaded
) {
267 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
268 $this->mRestrictions
= explode( ",", trim( $res ) );
269 $this->mRestrictionsLoaded
= true;
271 return $this->mRestrictions
;
274 function isDeleted() {
275 $ns = $this->getNamespace();
276 $t = wfStrencode( $this->getDBkey() );
277 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
278 if( $res = wfQuery( $sql ) ) {
279 $s = wfFetchObject( $res );
285 function getArticleID()
289 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
290 $this->mArticleID
= $wgLinkCache->addLink(
291 $this->getPrefixedDBkey() );
292 return $this->mArticleID
;
295 function resetArticleID( $newid )
298 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
300 if ( 0 == $newid ) { $this->mArticleID
= -1; }
301 else { $this->mArticleID
= $newid; }
302 $this->mRestrictionsLoaded
= false;
303 $this->mRestrictions
= array();
306 /* private */ function prefix( $name )
311 if ( "" != $this->mInterwiki
) {
312 $p = $this->mInterwiki
. ":";
314 if ( 0 != $this->mNamespace
) {
315 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
320 # Assumes that mDbkeyform has been set, and is urldecoded
321 # and uses undersocres, but not otherwise munged. This function
322 # removes illegal characters, splits off the winterwiki and
323 # namespace prefixes, sets the other forms, and canonicalizes
324 # everything. This one function is really at the core of
325 # Wiki--don't mess with it unless you're really sure you know
328 /* private */ function secureAndSplit()
330 global $wgLang, $wgLocalInterwiki;
331 wfProfileIn( "Title::secureAndSplit" );
333 $validNamespaces = $wgLang->getNamespaces();
334 unset( $validNamespaces[0] );
336 $this->mInterwiki
= $this->mFragment
= "";
337 $this->mNamespace
= 0;
339 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
340 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
342 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
343 if ( "" == $t ) { $t = "_"; }
345 $this->mDbkeyform
= $t;
348 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
349 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
350 $t = substr( $t, 1 );
352 if ( ":" == $t{0} ) {
353 $r = substr( $t, 1 );
355 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
356 #$p = strtolower( $m[1] );
358 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
360 $this->mNamespace
= $ns;
361 } elseif ( $this->getInterwikiLink( $p ) ) {
363 $this->mInterwiki
= $p;
365 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
367 $p = strtolower( $m[1] );
371 if($this->mInterwiki
!= $wgLocalInterwiki)
377 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
378 $this->mInterwiki
= "";
380 # We already know that some pages won't be in the database!
382 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
383 $this->mArticleID
= 0;
385 $f = strstr( $r, "#" );
386 if ( false !== $f ) {
387 $this->mFragment
= substr( $f, 1 );
388 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
390 # Strip illegal characters.
392 $tc = Title
::legalChars();
393 $t = preg_replace( "/[^{$tc}]/", "", $r );
395 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
396 $this->mDbkeyform
= $t;
397 $this->mUrlform
= wfUrlencode( $t );
398 $this->mTextform
= str_replace( "_", " ", $t );