Compatibility with alternative memcached client. Minor performance enhancement.
[mediawiki.git] / includes / Title.php
blob5955567c2eaac85eaf162472b96716254cc29ca9
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 global $wgMemc, $wgDBname, $title_interwiki_cache;
121 $k = "$wgDBname:interwiki:$key";
123 if( array_key_exists( $k, $title_interwiki_cache ) )
124 return $title_interwiki_cache[$k]->iw_url;
126 $s = $wgMemc->get( $k );
127 if( $s ) {
128 $title_interwiki_cache[$k] = $s;
129 return $s->iw_url;
131 $dkey = wfStrencode( $key );
132 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
133 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
134 if(!$res) return "";
136 $s = wfFetchObject( $res );
137 if(!$s) {
138 $s = (object)false;
139 $s->iw_url = "";
141 $wgMemc->set( $k, $s );
142 $title_interwiki_cache[$k] = $s;
143 return $s->iw_url;
146 function getText() { return $this->mTextform; }
147 function getURL() { return $this->mUrlform; }
148 function getDBkey() { return $this->mDbkeyform; }
149 function getNamespace() { return $this->mNamespace; }
150 function setNamespace( $n ) { $this->mNamespace = $n; }
151 function getInterwiki() { return $this->mInterwiki; }
152 function getFragment() { return $this->mFragment; }
154 /* static */ function indexTitle( $ns, $title )
156 global $wgDBminWordLen, $wgLang;
158 $lc = SearchEngine::legalSearchChars() . "&#;";
159 $t = $wgLang->stripForSearch( $title );
160 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
161 $t = strtolower( $t );
163 # Handle 's, s'
164 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
165 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
167 $t = preg_replace( "/\\s+/", " ", $t );
169 if ( $ns == Namespace::getImage() ) {
170 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
172 return trim( $t );
175 function getIndexTitle()
177 return Title::indexTitle( $this->mNamespace, $this->mTextform );
180 /* static */ function makeName( $ns, $title )
182 global $wgLang;
184 $n = $wgLang->getNsText( $ns );
185 if ( "" == $n ) { return $title; }
186 else { return "{$n}:{$title}"; }
189 /* static */ function makeTitle( $ns, $title )
191 $t = new Title();
192 $t->mDbkeyform = Title::makeName( $ns, $title );
193 if( $t->secureAndSplit() ) {
194 return $t;
195 } else {
196 return NULL;
200 function getPrefixedDBkey()
202 $s = $this->prefix( $this->mDbkeyform );
203 $s = str_replace( " ", "_", $s );
204 return $s;
207 function getPrefixedText()
209 # TEST THIS @@@
210 if ( empty( $this->mPrefixedText ) ) {
211 $s = $this->prefix( $this->mTextform );
212 $s = str_replace( "_", " ", $s );
213 $this->mPrefixedText = $s;
215 return $this->mPrefixedText;
218 function getPrefixedURL()
220 $s = $this->prefix( $this->mDbkeyform );
221 $s = str_replace( " ", "_", $s );
223 $s = urlencode ( $s ) ;
224 # Cleaning up URL to make it look nice -- is this safe?
225 $s = preg_replace( "/%3[Aa]/", ":", $s );
226 $s = preg_replace( "/%2[Ff]/", "/", $s );
227 $s = str_replace( "%28", "(", $s );
228 $s = str_replace( "%29", ")", $s );
229 return $s;
232 function getFullURL()
234 global $wgLang, $wgArticlePath;
236 if ( "" == $this->mInterwiki ) {
237 $p = $wgArticlePath;
238 } else {
239 $p = $this->getInterwikiLink( $this->mInterwiki );
241 $n = $wgLang->getNsText( $this->mNamespace );
242 if ( "" != $n ) { $n .= ":"; }
243 $u = str_replace( "$1", $n . $this->mUrlform, $p );
244 if ( "" != $this->mFragment ) {
245 $u .= "#" . $this->mFragment;
247 return $u;
250 function getEditURL()
252 global $wgServer, $wgScript;
254 if ( "" != $this->mInterwiki ) { return ""; }
255 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
257 return $s;
260 # For the title field in <a> tags
261 function getEscapedText()
263 return wfEscapeHTML( $this->getPrefixedText() );
266 function isExternal() { return ( "" != $this->mInterwiki ); }
268 function isProtected()
270 if ( -1 == $this->mNamespace ) { return true; }
271 $a = $this->getRestrictions();
272 if ( in_array( "sysop", $a ) ) { return true; }
273 return false;
276 function isLog()
278 if ( $this->mNamespace != Namespace::getWikipedia() ) {
279 return false;
281 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
282 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
283 return true;
285 return false;
288 function userIsWatching()
290 global $wgUser;
292 if ( -1 == $this->mNamespace ) { return false; }
293 if ( 0 == $wgUser->getID() ) { return false; }
295 return $wgUser->isWatched( $this );
298 function userCanEdit()
300 global $wgUser;
302 if ( -1 == $this->mNamespace ) { return false; }
303 # if ( 0 == $this->getArticleID() ) { return false; }
304 if ( $this->mDbkeyform == "_" ) { return false; }
306 $ur = $wgUser->getRights();
307 foreach ( $this->getRestrictions() as $r ) {
308 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
309 return false;
312 return true;
315 function getRestrictions()
317 $id = $this->getArticleID();
318 if ( 0 == $id ) { return array(); }
320 if ( ! $this->mRestrictionsLoaded ) {
321 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
322 $this->mRestrictions = explode( ",", trim( $res ) );
323 $this->mRestrictionsLoaded = true;
325 return $this->mRestrictions;
328 function isDeleted() {
329 $ns = $this->getNamespace();
330 $t = wfStrencode( $this->getDBkey() );
331 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
332 if( $res = wfQuery( $sql, DB_READ ) ) {
333 $s = wfFetchObject( $res );
334 return $s->n;
336 return 0;
339 function getArticleID()
341 global $wgLinkCache;
343 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
344 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
345 return $this->mArticleID;
348 function resetArticleID( $newid )
350 global $wgLinkCache;
351 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
353 if ( 0 == $newid ) { $this->mArticleID = -1; }
354 else { $this->mArticleID = $newid; }
355 $this->mRestrictionsLoaded = false;
356 $this->mRestrictions = array();
359 function invalidateCache() {
360 $now = wfTimestampNow();
361 $ns = $this->getNamespace();
362 $ti = wfStrencode( $this->getDBkey() );
363 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
364 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
367 /* private */ function prefix( $name )
369 global $wgLang;
371 $p = "";
372 if ( "" != $this->mInterwiki ) {
373 $p = $this->mInterwiki . ":";
375 if ( 0 != $this->mNamespace ) {
376 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
378 return $p . $name;
381 # Assumes that mDbkeyform has been set, and is urldecoded
382 # and uses undersocres, but not otherwise munged. This function
383 # removes illegal characters, splits off the winterwiki and
384 # namespace prefixes, sets the other forms, and canonicalizes
385 # everything.
387 /* private */ function secureAndSplit()
389 global $wgLang, $wgLocalInterwiki;
390 $fname = "Title::secureAndSplit";
391 wfProfileIn( $fname );
393 static $imgpre = false;
394 static $rxTc = false;
396 # Initialisation
397 if ( $imgpre === false ) {
398 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
399 $rxTc = "/[^" . Title::legalChars() . "]/";
403 $this->mInterwiki = $this->mFragment = "";
404 $this->mNamespace = 0;
406 # Clean up whitespace
408 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
409 if ( "_" == $t{0} ) {
410 $t = substr( $t, 1 );
412 $l = strlen( $t );
413 if ( $l && ( "_" == $t{$l-1} ) ) {
414 $t = substr( $t, 0, $l-1 );
416 if ( "" == $t ) {
417 wfProfileOut( $fname );
418 return false;
421 $this->mDbkeyform = $t;
422 $done = false;
424 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
425 $t = substr( $t, 1 );
427 if ( ":" == $t{0} ) {
428 $r = substr( $t, 1 );
429 } else {
430 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
431 #$p = strtolower( $m[1] );
432 $p = $m[1];
433 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
434 $t = $m[2];
435 $this->mNamespace = $ns;
436 } elseif ( $this->getInterwikiLink( $p ) ) {
437 $t = $m[2];
438 $this->mInterwiki = $p;
440 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
441 $done = true;
442 } elseif($this->mInterwiki != $wgLocalInterwiki) {
443 $done = true;
447 $r = $t;
449 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
450 $this->mInterwiki = "";
452 # We already know that some pages won't be in the database!
454 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
455 $this->mArticleID = 0;
457 $f = strstr( $r, "#" );
458 if ( false !== $f ) {
459 $this->mFragment = substr( $f, 1 );
460 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
463 # Reject illegal characters.
465 if( preg_match( $rxTc, $r ) ) {
466 return false;
469 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
470 $this->mDbkeyform = $t;
471 $this->mUrlform = wfUrlencode( $t );
472 $this->mTextform = str_replace( "_", " ", $t );
474 wfProfileOut( $fname );
475 return true;
478 function getTalkPage() {
479 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
482 function getSubjectPage() {
483 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );