proper handling of infinite expiry
[mediawiki.git] / includes / Title.php
blobc245bf6c00253069e9269007b20dc8dd46dbbdc3
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 # Create a title from a cur id
93 # This is inefficiently implemented
94 function newFromID( $id )
96 $fname = "Title::newFromID";
97 $row = wfGetArray( "cur", array( "cur_namespace", "cur_title" ),
98 array( "cur_id" => $id ), $fname );
99 if ( $row !== false ) {
100 $title = Title::makeTitle( $row->cur_namespace, $row->cur_title );
101 } else {
102 $title = NULL;
104 return $title;
107 function nameOf( $id )
109 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
110 "cur_id={$id}";
111 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
112 if ( 0 == wfNumRows( $res ) ) { return NULL; }
114 $s = wfFetchObject( $res );
115 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
116 return $n;
120 function legalChars()
122 global $wgInputEncoding;
123 if( $wgInputEncoding == "utf-8" ) {
124 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
125 } else {
126 # ISO 8859-* don't allow 0x80-0x9F
127 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
128 # But that breaks interlanguage links at the moment. Temporary:
129 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
133 function getInterwikiLink( $key )
135 global $wgMemc, $wgDBname, $title_interwiki_cache;
136 $k = "$wgDBname:interwiki:$key";
138 if( array_key_exists( $k, $title_interwiki_cache ) )
139 return $title_interwiki_cache[$k]->iw_url;
141 $s = $wgMemc->get( $k );
142 if( $s ) {
143 $title_interwiki_cache[$k] = $s;
144 return $s->iw_url;
146 $dkey = wfStrencode( $key );
147 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
148 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
149 if(!$res) return "";
151 $s = wfFetchObject( $res );
152 if(!$s) {
153 $s = (object)false;
154 $s->iw_url = "";
156 $wgMemc->set( $k, $s );
157 $title_interwiki_cache[$k] = $s;
158 return $s->iw_url;
161 function getText() { return $this->mTextform; }
162 function getURL() { return $this->mUrlform; }
163 function getDBkey() { return $this->mDbkeyform; }
164 function getNamespace() { return $this->mNamespace; }
165 function setNamespace( $n ) { $this->mNamespace = $n; }
166 function getInterwiki() { return $this->mInterwiki; }
167 function getFragment() { return $this->mFragment; }
169 /* static */ function indexTitle( $ns, $title )
171 global $wgDBminWordLen, $wgLang;
173 $lc = SearchEngine::legalSearchChars() . "&#;";
174 $t = $wgLang->stripForSearch( $title );
175 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
176 $t = strtolower( $t );
178 # Handle 's, s'
179 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
180 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
182 $t = preg_replace( "/\\s+/", " ", $t );
184 if ( $ns == Namespace::getImage() ) {
185 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
187 return trim( $t );
190 function getIndexTitle()
192 return Title::indexTitle( $this->mNamespace, $this->mTextform );
195 /* static */ function makeName( $ns, $title )
197 global $wgLang;
199 $n = $wgLang->getNsText( $ns );
200 if ( "" == $n ) { return $title; }
201 else { return "{$n}:{$title}"; }
204 /* static */ function makeTitle( $ns, $title )
206 $t = new Title();
207 $t->mDbkeyform = Title::makeName( $ns, $title );
208 if( $t->secureAndSplit() ) {
209 return $t;
210 } else {
211 return NULL;
215 function getPrefixedDBkey()
217 $s = $this->prefix( $this->mDbkeyform );
218 $s = str_replace( " ", "_", $s );
219 return $s;
222 function getPrefixedText()
224 # TEST THIS @@@
225 if ( empty( $this->mPrefixedText ) ) {
226 $s = $this->prefix( $this->mTextform );
227 $s = str_replace( "_", " ", $s );
228 $this->mPrefixedText = $s;
230 return $this->mPrefixedText;
233 function getPrefixedURL()
235 $s = $this->prefix( $this->mDbkeyform );
236 $s = str_replace( " ", "_", $s );
238 $s = urlencode ( $s ) ;
239 # Cleaning up URL to make it look nice -- is this safe?
240 $s = preg_replace( "/%3[Aa]/", ":", $s );
241 $s = preg_replace( "/%2[Ff]/", "/", $s );
242 $s = str_replace( "%28", "(", $s );
243 $s = str_replace( "%29", ")", $s );
244 return $s;
247 function getFullURL()
249 global $wgLang, $wgArticlePath;
251 if ( "" == $this->mInterwiki ) {
252 $p = $wgArticlePath;
253 } else {
254 $p = $this->getInterwikiLink( $this->mInterwiki );
256 $n = $wgLang->getNsText( $this->mNamespace );
257 if ( "" != $n ) { $n .= ":"; }
258 $u = str_replace( "$1", $n . $this->mUrlform, $p );
259 if ( "" != $this->mFragment ) {
260 $u .= "#" . $this->mFragment;
262 return $u;
265 function getEditURL()
267 global $wgServer, $wgScript;
269 if ( "" != $this->mInterwiki ) { return ""; }
270 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
272 return $s;
275 # For the title field in <a> tags
276 function getEscapedText()
278 return wfEscapeHTML( $this->getPrefixedText() );
281 function isExternal() { return ( "" != $this->mInterwiki ); }
283 function isProtected()
285 if ( -1 == $this->mNamespace ) { return true; }
286 $a = $this->getRestrictions();
287 if ( in_array( "sysop", $a ) ) { return true; }
288 return false;
291 function isLog()
293 if ( $this->mNamespace != Namespace::getWikipedia() ) {
294 return false;
296 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
297 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
298 return true;
300 return false;
303 function userIsWatching()
305 global $wgUser;
307 if ( -1 == $this->mNamespace ) { return false; }
308 if ( 0 == $wgUser->getID() ) { return false; }
310 return $wgUser->isWatched( $this );
313 function userCanEdit()
315 global $wgUser;
317 if ( -1 == $this->mNamespace ) { return false; }
318 # if ( 0 == $this->getArticleID() ) { return false; }
319 if ( $this->mDbkeyform == "_" ) { return false; }
321 $ur = $wgUser->getRights();
322 foreach ( $this->getRestrictions() as $r ) {
323 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
324 return false;
327 return true;
330 function getRestrictions()
332 $id = $this->getArticleID();
333 if ( 0 == $id ) { return array(); }
335 if ( ! $this->mRestrictionsLoaded ) {
336 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
337 $this->mRestrictions = explode( ",", trim( $res ) );
338 $this->mRestrictionsLoaded = true;
340 return $this->mRestrictions;
343 function isDeleted() {
344 $ns = $this->getNamespace();
345 $t = wfStrencode( $this->getDBkey() );
346 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
347 if( $res = wfQuery( $sql, DB_READ ) ) {
348 $s = wfFetchObject( $res );
349 return $s->n;
351 return 0;
354 function getArticleID()
356 global $wgLinkCache;
358 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
359 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
360 return $this->mArticleID;
363 function resetArticleID( $newid )
365 global $wgLinkCache;
366 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
368 if ( 0 == $newid ) { $this->mArticleID = -1; }
369 else { $this->mArticleID = $newid; }
370 $this->mRestrictionsLoaded = false;
371 $this->mRestrictions = array();
374 function invalidateCache() {
375 $now = wfTimestampNow();
376 $ns = $this->getNamespace();
377 $ti = wfStrencode( $this->getDBkey() );
378 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
379 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
382 /* private */ function prefix( $name )
384 global $wgLang;
386 $p = "";
387 if ( "" != $this->mInterwiki ) {
388 $p = $this->mInterwiki . ":";
390 if ( 0 != $this->mNamespace ) {
391 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
393 return $p . $name;
396 # Assumes that mDbkeyform has been set, and is urldecoded
397 # and uses undersocres, but not otherwise munged. This function
398 # removes illegal characters, splits off the winterwiki and
399 # namespace prefixes, sets the other forms, and canonicalizes
400 # everything.
402 /* private */ function secureAndSplit()
404 global $wgLang, $wgLocalInterwiki;
405 $fname = "Title::secureAndSplit";
406 wfProfileIn( $fname );
408 static $imgpre = false;
409 static $rxTc = false;
411 # Initialisation
412 if ( $imgpre === false ) {
413 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
414 $rxTc = "/[^" . Title::legalChars() . "]/";
418 $this->mInterwiki = $this->mFragment = "";
419 $this->mNamespace = 0;
421 # Clean up whitespace
423 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
424 if ( "_" == $t{0} ) {
425 $t = substr( $t, 1 );
427 $l = strlen( $t );
428 if ( $l && ( "_" == $t{$l-1} ) ) {
429 $t = substr( $t, 0, $l-1 );
431 if ( "" == $t ) {
432 wfProfileOut( $fname );
433 return false;
436 $this->mDbkeyform = $t;
437 $done = false;
439 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
440 $t = substr( $t, 1 );
442 if ( ":" == $t{0} ) {
443 $r = substr( $t, 1 );
444 } else {
445 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
446 #$p = strtolower( $m[1] );
447 $p = $m[1];
448 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
449 $t = $m[2];
450 $this->mNamespace = $ns;
451 } elseif ( $this->getInterwikiLink( $p ) ) {
452 $t = $m[2];
453 $this->mInterwiki = $p;
455 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
456 $done = true;
457 } elseif($this->mInterwiki != $wgLocalInterwiki) {
458 $done = true;
462 $r = $t;
464 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
465 $this->mInterwiki = "";
467 # We already know that some pages won't be in the database!
469 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
470 $this->mArticleID = 0;
472 $f = strstr( $r, "#" );
473 if ( false !== $f ) {
474 $this->mFragment = substr( $f, 1 );
475 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
478 # Reject illegal characters.
480 if( preg_match( $rxTc, $r ) ) {
481 return false;
484 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
485 $this->mDbkeyform = $t;
486 $this->mUrlform = wfUrlencode( $t );
487 $this->mTextform = str_replace( "_", " ", $t );
489 wfProfileOut( $fname );
490 return true;
493 function getTalkPage() {
494 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
497 function getSubjectPage() {
498 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );