URL-escape the main page link on the logo
[mediawiki.git] / includes / Title.php
blobcbc870db7bd27a161a6b744b8e720adc3cf8d772
1 <?
2 # See title.doc
4 class Title {
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 )
23 $t = new Title();
24 $t->mDbkeyform = $key;
25 $t->secureAndSplit();
26 return $t;
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 );
43 $t = new Title();
44 $t->mDbkeyform = str_replace( " ", "_", $text );
45 $t->secureAndSplit();
47 wfProfileOut();
48 return $t;
51 function newFromURL( $url )
53 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
55 $t = new Title();
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
62 # character encoding.
63 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
64 $s = $wgLang->checkTitleEncoding( $s );
66 $t->mDbkeyform = str_replace( " ", "_", $s );
67 $t->secureAndSplit();
68 return $t;
71 function nameOf( $id )
73 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
74 "cur_id={$id}";
75 $res = wfQuery( $sql, "Article::nameOf" );
76 if ( 0 == wfNumRows( $res ) ) { return NULL; }
78 $s = wfFetchObject( $res );
79 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
80 return $n;
84 function legalChars()
86 global $wgInputEncoding;
87 if( $wgInputEncoding == "utf-8" ) {
88 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
89 } else {
90 # ISO 8859-* don't allow 0x80-0x9F
91 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
92 # But that breaks interlanguage links at the moment. Temporary:
93 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
97 function getInterwikiLink( $key )
99 global $wgMemc, $wgDBname;
100 $k = "$wgDBname:interwiki:$key";
101 $s = $wgMemc->get( $k );
102 if( $s !== false ) return $s->iw_url;
104 $dkey = wfStrencode( $key );
105 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
106 $res = wfQuery( $query, "Title::getInterwikiLink" );
107 if(!$res) return "";
109 $s = wfFetchObject( $res );
110 if(!$s) {
111 $s = (object)false;
112 $s->iw_url = "";
114 $wgMemc->set( $k, $s );
115 return $s->iw_url;
118 function getText() { return $this->mTextform; }
119 function getURL() { return $this->mUrlform; }
120 function getDBkey() { return $this->mDbkeyform; }
121 function getNamespace() { return $this->mNamespace; }
122 function setNamespace( $n ) { $this->mNamespace = $n; }
123 function getInterwiki() { return $this->mInterwiki; }
124 function getFragment() { return $this->mFragment; }
126 /* static */ function indexTitle( $ns, $title )
128 global $wgDBminWordLen, $wgLang;
130 $lc = SearchEngine::legalSearchChars() . "&#;";
131 $t = $wgLang->stripForSearch( $title );
132 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
133 $t = strtolower( $t );
135 # Handle 's, s'
136 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
137 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
139 $t = preg_replace( "/\\s+/", " ", $t );
141 if ( $ns == Namespace::getImage() ) {
142 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
144 return trim( $t );
147 function getIndexTitle()
149 return Title::indexTitle( $this->mNamespace, $this->mTextform );
152 /* static */ function makeName( $ns, $title )
154 global $wgLang;
156 $n = $wgLang->getNsText( $ns );
157 if ( "" == $n ) { return $title; }
158 else { return "{$n}:{$title}"; }
161 /* static */ function makeTitle( $ns, $title )
163 $t = new Title();
164 $t->mDbkeyform = Title::makeName( $ns, $title );
165 $t->secureAndSplit();
166 return $t;
169 function getPrefixedDBkey()
171 $s = $this->prefix( $this->mDbkeyform );
172 $s = str_replace( " ", "_", $s );
173 return $s;
176 function getPrefixedText()
178 $s = $this->prefix( $this->mTextform );
179 $s = str_replace( "_", " ", $s );
180 return $s;
183 function getPrefixedURL()
185 $s = $this->prefix( $this->mDbkeyform );
186 $s = str_replace( " ", "_", $s );
188 $s = urlencode ( $s ) ;
189 # Cleaning up URL to make it look nice -- is this safe?
190 $s = preg_replace( "/%3[Aa]/", ":", $s );
191 $s = preg_replace( "/%2[Ff]/", "/", $s );
192 $s = str_replace( "%28", "(", $s );
193 $s = str_replace( "%29", ")", $s );
194 return $s;
197 function getFullURL()
199 global $wgLang, $wgArticlePath;
201 if ( "" == $this->mInterwiki ) {
202 $p = $wgArticlePath;
203 } else {
204 $p = $this->getInterwikiLink( $this->mInterwiki );
206 $n = $wgLang->getNsText( $this->mNamespace );
207 if ( "" != $n ) { $n .= ":"; }
208 $u = str_replace( "$1", $n . $this->mUrlform, $p );
209 if ( "" != $this->mFragment ) {
210 $u .= "#" . $this->mFragment;
212 return $u;
215 function getEditURL()
217 global $wgServer, $wgScript;
219 if ( "" != $this->mInterwiki ) { return ""; }
220 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
222 return $s;
225 function isExternal() { return ( "" != $this->mInterwiki ); }
227 function isProtected()
229 if ( -1 == $this->mNamespace ) { return true; }
230 $a = $this->getRestrictions();
231 if ( in_array( "sysop", $a ) ) { return true; }
232 return false;
235 function isLog()
237 if ( $this->mNamespace != Namespace::getWikipedia() ) {
238 return false;
240 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
241 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
242 return true;
244 return false;
247 function userIsWatching()
249 global $wgUser;
251 if ( -1 == $this->mNamespace ) { return false; }
252 if ( 0 == $wgUser->getID() ) { return false; }
254 return $wgUser->isWatched( $this );
257 function userCanEdit()
259 global $wgUser;
261 if ( -1 == $this->mNamespace ) { return false; }
262 # if ( 0 == $this->getArticleID() ) { return false; }
263 if ( $this->mDbkeyform == "_" ) { return false; }
265 $ur = $wgUser->getRights();
266 foreach ( $this->getRestrictions() as $r ) {
267 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
268 return false;
271 return true;
274 function getRestrictions()
276 $id = $this->getArticleID();
277 if ( 0 == $id ) { return array(); }
279 if ( ! $this->mRestrictionsLoaded ) {
280 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
281 $this->mRestrictions = explode( ",", trim( $res ) );
282 $this->mRestrictionsLoaded = true;
284 return $this->mRestrictions;
287 function isDeleted() {
288 $ns = $this->getNamespace();
289 $t = wfStrencode( $this->getDBkey() );
290 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
291 if( $res = wfQuery( $sql ) ) {
292 $s = wfFetchObject( $res );
293 return $s->n;
295 return 0;
298 function getArticleID()
300 global $wgLinkCache;
302 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
303 $this->mArticleID = $wgLinkCache->addLink(
304 $this->getPrefixedDBkey() );
305 return $this->mArticleID;
308 function resetArticleID( $newid )
310 global $wgLinkCache;
311 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
313 if ( 0 == $newid ) { $this->mArticleID = -1; }
314 else { $this->mArticleID = $newid; }
315 $this->mRestrictionsLoaded = false;
316 $this->mRestrictions = array();
319 /* private */ function prefix( $name )
321 global $wgLang;
323 $p = "";
324 if ( "" != $this->mInterwiki ) {
325 $p = $this->mInterwiki . ":";
327 if ( 0 != $this->mNamespace ) {
328 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
330 return $p . $name;
333 # Assumes that mDbkeyform has been set, and is urldecoded
334 # and uses undersocres, but not otherwise munged. This function
335 # removes illegal characters, splits off the winterwiki and
336 # namespace prefixes, sets the other forms, and canonicalizes
337 # everything. This one function is really at the core of
338 # Wiki--don't mess with it unless you're really sure you know
339 # what you're doing.
341 /* private */ function secureAndSplit()
343 global $wgLang, $wgLocalInterwiki;
344 wfProfileIn( "Title::secureAndSplit" );
346 $validNamespaces = $wgLang->getNamespaces();
347 unset( $validNamespaces[0] );
349 $this->mInterwiki = $this->mFragment = "";
350 $this->mNamespace = 0;
352 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
353 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
354 $l = strlen( $t );
355 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
356 if ( "" == $t ) { $t = "_"; }
358 $this->mDbkeyform = $t;
359 $done = false;
361 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
362 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
363 $t = substr( $t, 1 );
365 if ( ":" == $t{0} ) {
366 $r = substr( $t, 1 );
367 } else {
368 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
369 #$p = strtolower( $m[1] );
370 $p = $m[1];
371 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
372 $t = $m[2];
373 $this->mNamespace = $ns;
374 } elseif ( $this->getInterwikiLink( $p ) ) {
375 $t = $m[2];
376 $this->mInterwiki = $p;
378 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
379 $t, $m ) ) {
380 $p = strtolower( $m[1] );
381 } else {
382 $done = true;
384 if($this->mInterwiki != $wgLocalInterwiki)
385 $done = true;
388 $r = $t;
390 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
391 $this->mInterwiki = "";
393 # We already know that some pages won't be in the database!
395 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
396 $this->mArticleID = 0;
398 $f = strstr( $r, "#" );
399 if ( false !== $f ) {
400 $this->mFragment = substr( $f, 1 );
401 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
403 # Strip illegal characters.
405 $tc = Title::legalChars();
406 $t = preg_replace( "/[^{$tc}]/", "", $r );
408 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
409 $this->mDbkeyform = $t;
410 $this->mUrlform = wfUrlencode( $t );
411 $this->mTextform = str_replace( "_", " ", $t );
413 wfProfileOut();