Date formatter into temp branch
[mediawiki.git] / includes / Title.php
blob7396b6b64a47e3869d25261caa19f8d5d91bfa7e
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;
8 /* private */ var $mPrefixedText;
10 /* private */ function Title()
12 $this->mInterwiki = $this->mUrlform =
13 $this->mTextform = $this->mDbkeyform = "";
14 $this->mArticleID = -1;
15 $this->mNamespace = 0;
16 $this->mRestrictionsLoaded = false;
17 $this->mRestrictions = array();
20 # Static factory methods
22 function newFromDBkey( $key )
24 $t = new Title();
25 $t->mDbkeyform = $key;
26 if( $t->secureAndSplit() )
27 return $t;
28 else
29 return NULL;
32 function newFromText( $text )
34 $fname = "Title::newFromText";
35 wfProfileIn( $fname );
37 # Note - mixing latin1 named entities and unicode numbered
38 # ones will result in a bad link.
39 $trans = get_html_translation_table( HTML_ENTITIES );
40 $trans = array_flip( $trans );
41 $text = strtr( $text, $trans );
43 $text = wfMungeToUtf8( $text );
45 $text = urldecode( $text );
47 $t = new Title();
48 $t->mDbkeyform = str_replace( " ", "_", $text );
49 wfProfileOut( $fname );
50 if( $t->secureAndSplit() ) {
51 return $t;
52 } else {
53 return NULL;
57 function newFromURL( $url )
59 global $wgLang, $wgServer;
61 $t = new Title();
62 $s = urldecode( $url ); # This is technically wrong, as anything
63 # we've gotten is already decoded by PHP.
64 # Kept for backwards compatibility with
65 # buggy URLs we had for a while...
67 # For links that came from outside, check for alternate/legacy
68 # character encoding.
69 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
70 wfDebug( "Servr: $wgServer\n" );
71 if( empty( $_SERVER["HTTP_REFERER"] ) ||
72 strncmp($wgServer, $_SERVER["HTTP_REFERER"], strlen( $wgServer ) ) )
73 $s = $wgLang->checkTitleEncoding( $s );
75 $t->mDbkeyform = str_replace( " ", "_", $s );
76 if( $t->secureAndSplit() ) {
77 return $t;
78 } else {
79 return NULL;
83 function nameOf( $id )
85 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
86 "cur_id={$id}";
87 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
88 if ( 0 == wfNumRows( $res ) ) { return NULL; }
90 $s = wfFetchObject( $res );
91 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
92 return $n;
96 function legalChars()
98 global $wgInputEncoding;
99 if( $wgInputEncoding == "utf-8" ) {
100 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
101 } else {
102 # ISO 8859-* don't allow 0x80-0x9F
103 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
104 # But that breaks interlanguage links at the moment. Temporary:
105 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
109 function getInterwikiLink( $key )
111 global $wgMemc, $wgDBname;
112 $k = "$wgDBname:interwiki:$key";
113 $s = $wgMemc->get( $k );
114 if( $s !== false ) return $s->iw_url;
116 $dkey = wfStrencode( $key );
117 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
118 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
119 if(!$res) return "";
121 $s = wfFetchObject( $res );
122 if(!$s) {
123 $s = (object)false;
124 $s->iw_url = "";
126 $wgMemc->set( $k, $s );
127 return $s->iw_url;
130 function getText() { return $this->mTextform; }
131 function getURL() { return $this->mUrlform; }
132 function getDBkey() { return $this->mDbkeyform; }
133 function getNamespace() { return $this->mNamespace; }
134 function setNamespace( $n ) { $this->mNamespace = $n; }
135 function getInterwiki() { return $this->mInterwiki; }
136 function getFragment() { return $this->mFragment; }
138 /* static */ function indexTitle( $ns, $title )
140 global $wgDBminWordLen, $wgLang;
142 $lc = SearchEngine::legalSearchChars() . "&#;";
143 $t = $wgLang->stripForSearch( $title );
144 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
145 $t = strtolower( $t );
147 # Handle 's, s'
148 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
149 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
151 $t = preg_replace( "/\\s+/", " ", $t );
153 if ( $ns == Namespace::getImage() ) {
154 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
156 return trim( $t );
159 function getIndexTitle()
161 return Title::indexTitle( $this->mNamespace, $this->mTextform );
164 /* static */ function makeName( $ns, $title )
166 global $wgLang;
168 $n = $wgLang->getNsText( $ns );
169 if ( "" == $n ) { return $title; }
170 else { return "{$n}:{$title}"; }
173 /* static */ function makeTitle( $ns, $title )
175 $t = new Title();
176 $t->mDbkeyform = Title::makeName( $ns, $title );
177 if( $t->secureAndSplit() ) {
178 return $t;
179 } else {
180 return NULL;
184 function getPrefixedDBkey()
186 $s = $this->prefix( $this->mDbkeyform );
187 $s = str_replace( " ", "_", $s );
188 return $s;
191 function getPrefixedText()
193 # TEST THIS @@@
194 if ( empty( $this->mPrefixedText ) ) {
195 $s = $this->prefix( $this->mTextform );
196 $s = str_replace( "_", " ", $s );
197 $this->mPrefixedText = $s;
199 return $this->mPrefixedText;
202 function getPrefixedURL()
204 $s = $this->prefix( $this->mDbkeyform );
205 $s = str_replace( " ", "_", $s );
207 $s = urlencode ( $s ) ;
208 # Cleaning up URL to make it look nice -- is this safe?
209 $s = preg_replace( "/%3[Aa]/", ":", $s );
210 $s = preg_replace( "/%2[Ff]/", "/", $s );
211 $s = str_replace( "%28", "(", $s );
212 $s = str_replace( "%29", ")", $s );
213 return $s;
216 function getFullURL()
218 global $wgLang, $wgArticlePath;
220 if ( "" == $this->mInterwiki ) {
221 $p = $wgArticlePath;
222 } else {
223 $p = $this->getInterwikiLink( $this->mInterwiki );
225 $n = $wgLang->getNsText( $this->mNamespace );
226 if ( "" != $n ) { $n .= ":"; }
227 $u = str_replace( "$1", $n . $this->mUrlform, $p );
228 if ( "" != $this->mFragment ) {
229 $u .= "#" . $this->mFragment;
231 return $u;
234 function getEditURL()
236 global $wgServer, $wgScript;
238 if ( "" != $this->mInterwiki ) { return ""; }
239 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
241 return $s;
244 # For the title field in <a> tags
245 function getEscapedText()
247 return wfEscapeHTML( $this->getPrefixedText() );
250 function isExternal() { return ( "" != $this->mInterwiki ); }
252 function isProtected()
254 if ( -1 == $this->mNamespace ) { return true; }
255 $a = $this->getRestrictions();
256 if ( in_array( "sysop", $a ) ) { return true; }
257 return false;
260 function isLog()
262 if ( $this->mNamespace != Namespace::getWikipedia() ) {
263 return false;
265 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
266 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
267 return true;
269 return false;
272 function userIsWatching()
274 global $wgUser;
276 if ( -1 == $this->mNamespace ) { return false; }
277 if ( 0 == $wgUser->getID() ) { return false; }
279 return $wgUser->isWatched( $this );
282 function userCanEdit()
284 global $wgUser;
286 if ( -1 == $this->mNamespace ) { return false; }
287 # if ( 0 == $this->getArticleID() ) { return false; }
288 if ( $this->mDbkeyform == "_" ) { return false; }
290 $ur = $wgUser->getRights();
291 foreach ( $this->getRestrictions() as $r ) {
292 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
293 return false;
296 return true;
299 function getRestrictions()
301 $id = $this->getArticleID();
302 if ( 0 == $id ) { return array(); }
304 if ( ! $this->mRestrictionsLoaded ) {
305 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
306 $this->mRestrictions = explode( ",", trim( $res ) );
307 $this->mRestrictionsLoaded = true;
309 return $this->mRestrictions;
312 function isDeleted() {
313 $ns = $this->getNamespace();
314 $t = wfStrencode( $this->getDBkey() );
315 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
316 if( $res = wfQuery( $sql, DB_READ ) ) {
317 $s = wfFetchObject( $res );
318 return $s->n;
320 return 0;
323 function getArticleID()
325 global $wgLinkCache;
327 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
328 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
329 return $this->mArticleID;
332 function resetArticleID( $newid )
334 global $wgLinkCache;
335 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
337 if ( 0 == $newid ) { $this->mArticleID = -1; }
338 else { $this->mArticleID = $newid; }
339 $this->mRestrictionsLoaded = false;
340 $this->mRestrictions = array();
343 function invalidateCache() {
344 $now = wfTimestampNow();
345 $ns = $this->getNamespace();
346 $ti = wfStrencode( $this->getDBkey() );
347 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
348 return wfQuery( $sql, "Title::invalidateCache" );
351 /* private */ function prefix( $name )
353 global $wgLang;
355 $p = "";
356 if ( "" != $this->mInterwiki ) {
357 $p = $this->mInterwiki . ":";
359 if ( 0 != $this->mNamespace ) {
360 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
362 return $p . $name;
365 # Assumes that mDbkeyform has been set, and is urldecoded
366 # and uses undersocres, but not otherwise munged. This function
367 # removes illegal characters, splits off the winterwiki and
368 # namespace prefixes, sets the other forms, and canonicalizes
369 # everything.
371 /* private */ function secureAndSplit()
373 global $wgLang, $wgLocalInterwiki;
374 $fname = "Title::secureAndSplit";
375 wfProfileIn( $fname );
377 static $imgpre = false;
378 static $rxTc = false;
380 # Initialisation
381 if ( $imgpre === false ) {
382 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
383 $rxTc = "/[^" . Title::legalChars() . "]/";
387 $this->mInterwiki = $this->mFragment = "";
388 $this->mNamespace = 0;
390 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
391 if ( "_" == $t{0} ) {
392 $t = substr( $t, 1 );
394 $l = strlen( $t );
395 if ( $l && ( "_" == $t{$l-1} ) ) {
396 $t = substr( $t, 0, $l-1 );
398 if ( "" == $t ) {
399 wfProfileOut( $fname );
400 return false;
403 $this->mDbkeyform = $t;
404 $done = false;
406 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
407 $t = substr( $t, 1 );
409 if ( ":" == $t{0} ) {
410 $r = substr( $t, 1 );
411 } else {
412 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
413 #$p = strtolower( $m[1] );
414 $p = $m[1];
415 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
416 $t = $m[2];
417 $this->mNamespace = $ns;
418 } elseif ( $this->getInterwikiLink( $p ) ) {
419 $t = $m[2];
420 $this->mInterwiki = $p;
422 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
423 $done = true;
424 } elseif($this->mInterwiki != $wgLocalInterwiki) {
425 $done = true;
429 $r = $t;
431 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
432 $this->mInterwiki = "";
434 # We already know that some pages won't be in the database!
436 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
437 $this->mArticleID = 0;
439 $f = strstr( $r, "#" );
440 if ( false !== $f ) {
441 $this->mFragment = substr( $f, 1 );
442 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
444 # Strip illegal characters.
446 $t = preg_replace( $rxTc, "", $r );
448 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
449 $this->mDbkeyform = $t;
450 $this->mUrlform = wfUrlencode( $t );
451 $this->mTextform = str_replace( "_", " ", $t );
453 wfProfileOut( $fname );
454 return true;
457 function getTalkPage() {
458 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
461 function getSubjectPage() {
462 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );