Removed warning for invalid links since it scares users needlessly.
[mediawiki.git] / includes / Title.php
blob2b3da1fae3528baeec6c5d1b7a740c9c2edb737c
1 <?
2 # See title.doc
4 /* private static */ $title_interwiki_cache = array();
6 class Title {
7 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
8 /* private */ var $mNamespace, $mInterwiki, $mFragment;
9 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
10 /* private */ var $mPrefixedText;
12 /* private */ function Title()
14 $this->mInterwiki = $this->mUrlform =
15 $this->mTextform = $this->mDbkeyform = "";
16 $this->mArticleID = -1;
17 $this->mNamespace = 0;
18 $this->mRestrictionsLoaded = false;
19 $this->mRestrictions = array();
22 # Static factory methods
24 function newFromDBkey( $key )
26 $t = new Title();
27 $t->mDbkeyform = $key;
28 if( $t->secureAndSplit() )
29 return $t;
30 else
31 return NULL;
34 function newFromText( $text )
36 static $trans;
37 $fname = "Title::newFromText";
38 wfProfileIn( $fname );
40 # Note - mixing latin1 named entities and unicode numbered
41 # ones will result in a bad link.
42 if( !isset( $trans ) ) {
43 global $wgInputEncoding;
44 $trans = array_flip( get_html_translation_table( HTML_ENTITIES ) );
45 if( strcasecmp( "utf-8", $wgInputEncoding ) == 0 ) {
46 $trans = array_map( "utf8_encode", $trans );
50 $text = strtr( $text, $trans );
52 $text = wfMungeToUtf8( $text );
54 $text = urldecode( $text );
56 $t = new Title();
57 $t->mDbkeyform = str_replace( " ", "_", $text );
58 wfProfileOut( $fname );
59 if( $t->secureAndSplit() ) {
60 return $t;
61 } else {
62 return NULL;
66 function newFromURL( $url )
68 global $wgLang, $wgServer;
70 $t = new Title();
71 $s = urldecode( $url ); # This is technically wrong, as anything
72 # we've gotten is already decoded by PHP.
73 # Kept for backwards compatibility with
74 # buggy URLs we had for a while...
76 # For links that came from outside, check for alternate/legacy
77 # character encoding.
78 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
79 wfDebug( "Servr: $wgServer\n" );
80 if( empty( $_SERVER["HTTP_REFERER"] ) ||
81 strncmp($wgServer, $_SERVER["HTTP_REFERER"], strlen( $wgServer ) ) )
82 $s = $wgLang->checkTitleEncoding( $s );
84 $t->mDbkeyform = str_replace( " ", "_", $s );
85 if( $t->secureAndSplit() ) {
86 return $t;
87 } else {
88 return NULL;
92 function nameOf( $id )
94 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
95 "cur_id={$id}";
96 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
97 if ( 0 == wfNumRows( $res ) ) { return NULL; }
99 $s = wfFetchObject( $res );
100 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
101 return $n;
105 function legalChars()
107 global $wgInputEncoding;
108 if( $wgInputEncoding == "utf-8" ) {
109 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
110 } else {
111 # ISO 8859-* don't allow 0x80-0x9F
112 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
113 # But that breaks interlanguage links at the moment. Temporary:
114 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
118 function getInterwikiLink( $key )
120 # Performance note: It would probably be a good idea to
121 # get/set/fetch the entire $title_interwiki_cache with memcache
122 # here, reducing some overhead for the repeated memcache accesses.
123 # Popular pages often has many interwiki links anyways.
125 global $wgMemc, $wgDBname, $title_interwiki_cache;
126 $k = "$wgDBname:interwiki:$key";
128 if( array_key_exists( $k, $title_interwiki_cache ) )
129 return $title_interwiki_cache[$k]->iw_url;
131 $s = $wgMemc->get( $k );
132 if( $s !== false ) return $s->iw_url;
134 $dkey = wfStrencode( $key );
135 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
136 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
137 if(!$res) return "";
139 $s = wfFetchObject( $res );
140 if(!$s) {
141 $s = (object)false;
142 $s->iw_url = "";
144 $wgMemc->set( $k, $s );
145 $title_interwiki_cache[$k] = $s;
146 return $s->iw_url;
149 function getText() { return $this->mTextform; }
150 function getURL() { return $this->mUrlform; }
151 function getDBkey() { return $this->mDbkeyform; }
152 function getNamespace() { return $this->mNamespace; }
153 function setNamespace( $n ) { $this->mNamespace = $n; }
154 function getInterwiki() { return $this->mInterwiki; }
155 function getFragment() { return $this->mFragment; }
157 /* static */ function indexTitle( $ns, $title )
159 global $wgDBminWordLen, $wgLang;
161 $lc = SearchEngine::legalSearchChars() . "&#;";
162 $t = $wgLang->stripForSearch( $title );
163 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
164 $t = strtolower( $t );
166 # Handle 's, s'
167 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
168 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
170 $t = preg_replace( "/\\s+/", " ", $t );
172 if ( $ns == Namespace::getImage() ) {
173 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
175 return trim( $t );
178 function getIndexTitle()
180 return Title::indexTitle( $this->mNamespace, $this->mTextform );
183 /* static */ function makeName( $ns, $title )
185 global $wgLang;
187 $n = $wgLang->getNsText( $ns );
188 if ( "" == $n ) { return $title; }
189 else { return "{$n}:{$title}"; }
192 /* static */ function makeTitle( $ns, $title )
194 $t = new Title();
195 $t->mDbkeyform = Title::makeName( $ns, $title );
196 if( $t->secureAndSplit() ) {
197 return $t;
198 } else {
199 return NULL;
203 function getPrefixedDBkey()
205 $s = $this->prefix( $this->mDbkeyform );
206 $s = str_replace( " ", "_", $s );
207 return $s;
210 function getPrefixedText()
212 # TEST THIS @@@
213 if ( empty( $this->mPrefixedText ) ) {
214 $s = $this->prefix( $this->mTextform );
215 $s = str_replace( "_", " ", $s );
216 $this->mPrefixedText = $s;
218 return $this->mPrefixedText;
221 function getPrefixedURL()
223 $s = $this->prefix( $this->mDbkeyform );
224 $s = str_replace( " ", "_", $s );
226 $s = urlencode ( $s ) ;
227 # Cleaning up URL to make it look nice -- is this safe?
228 $s = preg_replace( "/%3[Aa]/", ":", $s );
229 $s = preg_replace( "/%2[Ff]/", "/", $s );
230 $s = str_replace( "%28", "(", $s );
231 $s = str_replace( "%29", ")", $s );
232 return $s;
235 function getFullURL()
237 global $wgLang, $wgArticlePath;
239 if ( "" == $this->mInterwiki ) {
240 $p = $wgArticlePath;
241 } else {
242 $p = $this->getInterwikiLink( $this->mInterwiki );
244 $n = $wgLang->getNsText( $this->mNamespace );
245 if ( "" != $n ) { $n .= ":"; }
246 $u = str_replace( "$1", $n . $this->mUrlform, $p );
247 if ( "" != $this->mFragment ) {
248 $u .= "#" . $this->mFragment;
250 return $u;
253 function getEditURL()
255 global $wgServer, $wgScript;
257 if ( "" != $this->mInterwiki ) { return ""; }
258 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
260 return $s;
263 # For the title field in <a> tags
264 function getEscapedText()
266 return wfEscapeHTML( $this->getPrefixedText() );
269 function isExternal() { return ( "" != $this->mInterwiki ); }
271 function isProtected()
273 if ( -1 == $this->mNamespace ) { return true; }
274 $a = $this->getRestrictions();
275 if ( in_array( "sysop", $a ) ) { return true; }
276 return false;
279 function isLog()
281 if ( $this->mNamespace != Namespace::getWikipedia() ) {
282 return false;
284 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
285 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
286 return true;
288 return false;
291 function userIsWatching()
293 global $wgUser;
295 if ( -1 == $this->mNamespace ) { return false; }
296 if ( 0 == $wgUser->getID() ) { return false; }
298 return $wgUser->isWatched( $this );
301 function userCanEdit()
303 global $wgUser;
305 if ( -1 == $this->mNamespace ) { return false; }
306 # if ( 0 == $this->getArticleID() ) { return false; }
307 if ( $this->mDbkeyform == "_" ) { return false; }
309 $ur = $wgUser->getRights();
310 foreach ( $this->getRestrictions() as $r ) {
311 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
312 return false;
315 return true;
318 function getRestrictions()
320 $id = $this->getArticleID();
321 if ( 0 == $id ) { return array(); }
323 if ( ! $this->mRestrictionsLoaded ) {
324 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
325 $this->mRestrictions = explode( ",", trim( $res ) );
326 $this->mRestrictionsLoaded = true;
328 return $this->mRestrictions;
331 function isDeleted() {
332 $ns = $this->getNamespace();
333 $t = wfStrencode( $this->getDBkey() );
334 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
335 if( $res = wfQuery( $sql, DB_READ ) ) {
336 $s = wfFetchObject( $res );
337 return $s->n;
339 return 0;
342 function getArticleID()
344 global $wgLinkCache;
346 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
347 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
348 return $this->mArticleID;
351 function resetArticleID( $newid )
353 global $wgLinkCache;
354 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
356 if ( 0 == $newid ) { $this->mArticleID = -1; }
357 else { $this->mArticleID = $newid; }
358 $this->mRestrictionsLoaded = false;
359 $this->mRestrictions = array();
362 function invalidateCache() {
363 $now = wfTimestampNow();
364 $ns = $this->getNamespace();
365 $ti = wfStrencode( $this->getDBkey() );
366 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
367 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
370 /* private */ function prefix( $name )
372 global $wgLang;
374 $p = "";
375 if ( "" != $this->mInterwiki ) {
376 $p = $this->mInterwiki . ":";
378 if ( 0 != $this->mNamespace ) {
379 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
381 return $p . $name;
384 # Assumes that mDbkeyform has been set, and is urldecoded
385 # and uses undersocres, but not otherwise munged. This function
386 # removes illegal characters, splits off the winterwiki and
387 # namespace prefixes, sets the other forms, and canonicalizes
388 # everything.
390 /* private */ function secureAndSplit()
392 global $wgLang, $wgLocalInterwiki;
393 $fname = "Title::secureAndSplit";
394 wfProfileIn( $fname );
396 static $imgpre = false;
397 static $rxTc = false;
399 # Initialisation
400 if ( $imgpre === false ) {
401 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
402 $rxTc = "/[^" . Title::legalChars() . "]/";
406 $this->mInterwiki = $this->mFragment = "";
407 $this->mNamespace = 0;
409 # Clean up whitespace
411 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
412 if ( "_" == $t{0} ) {
413 $t = substr( $t, 1 );
415 $l = strlen( $t );
416 if ( $l && ( "_" == $t{$l-1} ) ) {
417 $t = substr( $t, 0, $l-1 );
419 if ( "" == $t ) {
420 wfProfileOut( $fname );
421 return false;
424 $this->mDbkeyform = $t;
425 $done = false;
427 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
428 $t = substr( $t, 1 );
430 if ( ":" == $t{0} ) {
431 $r = substr( $t, 1 );
432 } else {
433 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
434 #$p = strtolower( $m[1] );
435 $p = $m[1];
436 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
437 $t = $m[2];
438 $this->mNamespace = $ns;
439 } elseif ( $this->getInterwikiLink( $p ) ) {
440 $t = $m[2];
441 $this->mInterwiki = $p;
443 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
444 $done = true;
445 } elseif($this->mInterwiki != $wgLocalInterwiki) {
446 $done = true;
450 $r = $t;
452 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
453 $this->mInterwiki = "";
455 # We already know that some pages won't be in the database!
457 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
458 $this->mArticleID = 0;
460 $f = strstr( $r, "#" );
461 if ( false !== $f ) {
462 $this->mFragment = substr( $f, 1 );
463 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
466 # Reject illegal characters.
468 if( preg_match( $rxTc, $r ) ) {
469 return false;
472 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
473 $this->mDbkeyform = $t;
474 $this->mUrlform = wfUrlencode( $t );
475 $this->mTextform = str_replace( "_", " ", $t );
477 wfProfileOut( $fname );
478 return true;
481 function getTalkPage() {
482 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
485 function getSubjectPage() {
486 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );