Nov. branch merge. Various features backported from stable, various bug fixes.
[mediawiki.git] / includes / Title.php
blob91596054a6a605eec2dd91d38dcb2f68fa18b4bb
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, $HTTP_SERVER_VARS;
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 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
70 $s = $wgLang->checkTitleEncoding( $s );
72 $t->mDbkeyform = str_replace( " ", "_", $s );
73 if( $t->secureAndSplit() ) {
74 return $t;
75 } else {
76 return NULL;
80 function nameOf( $id )
82 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
83 "cur_id={$id}";
84 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
85 if ( 0 == wfNumRows( $res ) ) { return NULL; }
87 $s = wfFetchObject( $res );
88 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
89 return $n;
93 function legalChars()
95 global $wgInputEncoding;
96 if( $wgInputEncoding == "utf-8" ) {
97 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
98 } else {
99 # ISO 8859-* don't allow 0x80-0x9F
100 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
101 # But that breaks interlanguage links at the moment. Temporary:
102 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
106 function getInterwikiLink( $key )
108 global $wgMemc, $wgDBname;
109 $k = "$wgDBname:interwiki:$key";
110 $s = $wgMemc->get( $k );
111 if( $s !== false ) return $s->iw_url;
113 $dkey = wfStrencode( $key );
114 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
115 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
116 if(!$res) return "";
118 $s = wfFetchObject( $res );
119 if(!$s) {
120 $s = (object)false;
121 $s->iw_url = "";
123 $wgMemc->set( $k, $s );
124 return $s->iw_url;
127 function getText() { return $this->mTextform; }
128 function getURL() { return $this->mUrlform; }
129 function getDBkey() { return $this->mDbkeyform; }
130 function getNamespace() { return $this->mNamespace; }
131 function setNamespace( $n ) { $this->mNamespace = $n; }
132 function getInterwiki() { return $this->mInterwiki; }
133 function getFragment() { return $this->mFragment; }
135 /* static */ function indexTitle( $ns, $title )
137 global $wgDBminWordLen, $wgLang;
139 $lc = SearchEngine::legalSearchChars() . "&#;";
140 $t = $wgLang->stripForSearch( $title );
141 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
142 $t = strtolower( $t );
144 # Handle 's, s'
145 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
146 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
148 $t = preg_replace( "/\\s+/", " ", $t );
150 if ( $ns == Namespace::getImage() ) {
151 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
153 return trim( $t );
156 function getIndexTitle()
158 return Title::indexTitle( $this->mNamespace, $this->mTextform );
161 /* static */ function makeName( $ns, $title )
163 global $wgLang;
165 $n = $wgLang->getNsText( $ns );
166 if ( "" == $n ) { return $title; }
167 else { return "{$n}:{$title}"; }
170 /* static */ function makeTitle( $ns, $title )
172 $t = new Title();
173 $t->mDbkeyform = Title::makeName( $ns, $title );
174 if( $t->secureAndSplit() ) {
175 return $t;
176 } else {
177 return NULL;
181 function getPrefixedDBkey()
183 $s = $this->prefix( $this->mDbkeyform );
184 $s = str_replace( " ", "_", $s );
185 return $s;
188 function getPrefixedText()
190 # TEST THIS @@@
191 if ( empty( $this->mPrefixedText ) ) {
192 $s = $this->prefix( $this->mTextform );
193 $s = str_replace( "_", " ", $s );
194 $this->mPrefixedText = $s;
196 return $this->mPrefixedText;
199 function getPrefixedURL()
201 $s = $this->prefix( $this->mDbkeyform );
202 $s = str_replace( " ", "_", $s );
204 $s = urlencode ( $s ) ;
205 # Cleaning up URL to make it look nice -- is this safe?
206 $s = preg_replace( "/%3[Aa]/", ":", $s );
207 $s = preg_replace( "/%2[Ff]/", "/", $s );
208 $s = str_replace( "%28", "(", $s );
209 $s = str_replace( "%29", ")", $s );
210 return $s;
213 function getFullURL()
215 global $wgLang, $wgArticlePath;
217 if ( "" == $this->mInterwiki ) {
218 $p = $wgArticlePath;
219 } else {
220 $p = $this->getInterwikiLink( $this->mInterwiki );
222 $n = $wgLang->getNsText( $this->mNamespace );
223 if ( "" != $n ) { $n .= ":"; }
224 $u = str_replace( "$1", $n . $this->mUrlform, $p );
225 if ( "" != $this->mFragment ) {
226 $u .= "#" . $this->mFragment;
228 return $u;
231 function getEditURL()
233 global $wgServer, $wgScript;
235 if ( "" != $this->mInterwiki ) { return ""; }
236 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
238 return $s;
241 # For the title field in <a> tags
242 function getEscapedText()
244 return wfEscapeHTML( $this->getPrefixedText() );
247 function isExternal() { return ( "" != $this->mInterwiki ); }
249 function isProtected()
251 if ( -1 == $this->mNamespace ) { return true; }
252 $a = $this->getRestrictions();
253 if ( in_array( "sysop", $a ) ) { return true; }
254 return false;
257 function isLog()
259 if ( $this->mNamespace != Namespace::getWikipedia() ) {
260 return false;
262 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
263 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
264 return true;
266 return false;
269 function userIsWatching()
271 global $wgUser;
273 if ( -1 == $this->mNamespace ) { return false; }
274 if ( 0 == $wgUser->getID() ) { return false; }
276 return $wgUser->isWatched( $this );
279 function userCanEdit()
281 global $wgUser;
283 if ( -1 == $this->mNamespace ) { return false; }
284 # if ( 0 == $this->getArticleID() ) { return false; }
285 if ( $this->mDbkeyform == "_" ) { return false; }
287 $ur = $wgUser->getRights();
288 foreach ( $this->getRestrictions() as $r ) {
289 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
290 return false;
293 return true;
296 function getRestrictions()
298 $id = $this->getArticleID();
299 if ( 0 == $id ) { return array(); }
301 if ( ! $this->mRestrictionsLoaded ) {
302 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
303 $this->mRestrictions = explode( ",", trim( $res ) );
304 $this->mRestrictionsLoaded = true;
306 return $this->mRestrictions;
309 function isDeleted() {
310 $ns = $this->getNamespace();
311 $t = wfStrencode( $this->getDBkey() );
312 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
313 if( $res = wfQuery( $sql, DB_READ ) ) {
314 $s = wfFetchObject( $res );
315 return $s->n;
317 return 0;
320 function getArticleID()
322 global $wgLinkCache;
324 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
325 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
326 return $this->mArticleID;
329 function resetArticleID( $newid )
331 global $wgLinkCache;
332 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
334 if ( 0 == $newid ) { $this->mArticleID = -1; }
335 else { $this->mArticleID = $newid; }
336 $this->mRestrictionsLoaded = false;
337 $this->mRestrictions = array();
340 function invalidateCache() {
341 $now = wfTimestampNow();
342 $ns = $this->getNamespace();
343 $ti = wfStrencode( $this->getDBkey() );
344 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
345 return wfQuery( $sql, "Title::invalidateCache" );
348 /* private */ function prefix( $name )
350 global $wgLang;
352 $p = "";
353 if ( "" != $this->mInterwiki ) {
354 $p = $this->mInterwiki . ":";
356 if ( 0 != $this->mNamespace ) {
357 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
359 return $p . $name;
362 # Assumes that mDbkeyform has been set, and is urldecoded
363 # and uses undersocres, but not otherwise munged. This function
364 # removes illegal characters, splits off the winterwiki and
365 # namespace prefixes, sets the other forms, and canonicalizes
366 # everything.
368 /* private */ function secureAndSplit()
370 global $wgLang, $wgLocalInterwiki;
371 $fname = "Title::secureAndSplit";
372 wfProfileIn( $fname );
374 static $imgpre = false;
375 static $rxTc = false;
377 # Initialisation
378 if ( $imgpre === false ) {
379 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
380 $rxTc = "/[^" . Title::legalChars() . "]/";
384 $this->mInterwiki = $this->mFragment = "";
385 $this->mNamespace = 0;
387 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
388 if ( "_" == $t{0} ) {
389 $t = substr( $t, 1 );
391 $l = strlen( $t );
392 if ( $l && ( "_" == $t{$l-1} ) ) {
393 $t = substr( $t, 0, $l-1 );
395 if ( "" == $t ) {
396 wfProfileOut( $fname );
397 return false;
400 $this->mDbkeyform = $t;
401 $done = false;
403 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
404 $t = substr( $t, 1 );
406 if ( ":" == $t{0} ) {
407 $r = substr( $t, 1 );
408 } else {
409 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
410 #$p = strtolower( $m[1] );
411 $p = $m[1];
412 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
413 $t = $m[2];
414 $this->mNamespace = $ns;
415 } elseif ( $this->getInterwikiLink( $p ) ) {
416 $t = $m[2];
417 $this->mInterwiki = $p;
419 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
420 $done = true;
421 } elseif($this->mInterwiki != $wgLocalInterwiki) {
422 $done = true;
426 $r = $t;
428 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
429 $this->mInterwiki = "";
431 # We already know that some pages won't be in the database!
433 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
434 $this->mArticleID = 0;
436 $f = strstr( $r, "#" );
437 if ( false !== $f ) {
438 $this->mFragment = substr( $f, 1 );
439 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
441 # Strip illegal characters.
443 $t = preg_replace( $rxTc, "", $r );
445 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
446 $this->mDbkeyform = $t;
447 $this->mUrlform = wfUrlencode( $t );
448 $this->mTextform = str_replace( "_", " ", $t );
450 wfProfileOut( $fname );
451 return true;