Move interwiki management from big ugly array into the database, with
[mediawiki.git] / includes / Title.php
bloba342a31a4fa7b5eb0eb971b2dcb692f04d65dc1e
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;
9 /* private */ function Title()
11 $this->mInterwiki = $this->mUrlform =
12 $this->mTextform = $this->mDbkeyform = "";
13 $this->mArticleID = -1;
14 $this->mNamespace = 0;
15 $this->mRestrictionsLoaded = false;
16 $this->mRestrictions = array();
19 # Static factory methods
21 function newFromDBkey( $key )
23 $t = new Title();
24 $t->mDbkeyform = $key;
25 $t->secureAndSplit();
26 return $t;
29 function newFromText( $text )
31 # Note - mixing latin1 named entities and unicode numbered
32 # ones will result in a bad link.
33 $trans = get_html_translation_table( HTML_ENTITIES );
34 $trans = array_flip( $trans );
35 $text = strtr( $text, $trans );
37 $text = wfMungeToUtf8( $text );
39 $text = urldecode( $text );
41 $t = new Title();
42 $t->mDbkeyform = str_replace( " ", "_", $text );
43 $t->secureAndSplit();
44 return $t;
47 function newFromURL( $url )
49 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
51 $t = new Title();
52 $s = urldecode( $url ); # This is technically wrong, as anything
53 # we've gotten is already decoded by PHP.
54 # Kept for backwards compatibility with
55 # buggy URLs we had for a while...
57 # For links that came from outside, check for alternate/legacy
58 # character encoding.
59 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
60 $s = $wgLang->checkTitleEncoding( $s );
62 $t->mDbkeyform = str_replace( " ", "_", $s );
63 $t->secureAndSplit();
64 return $t;
67 function legalChars()
69 global $wgInputEncoding;
70 if( $wgInputEncoding == "utf-8" ) {
71 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
72 } else {
73 # ISO 8859-* don't allow 0x80-0x9F
74 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
75 # But that breaks interlanguage links at the moment. Temporary:
76 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
80 function getInterwikiLink( $key )
82 global $wgMemc, $wgDBname;
83 $k = "$wgDBname:interwiki:$key";
84 $s = $wgMemc->get( $k );
85 if( $s !== false ) return $s->iw_url;
87 $dkey = wfStrencode( $key );
88 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
89 $res = wfQuery( $query, "Title::getInterwikiLink" );
90 if(!$res) return "";
92 $s = wfFetchObject( $res );
93 if(!$s) {
94 $s = (object)false;
95 $s->iw_url = "";
97 $wgMemc->set( $k, $s );
98 return $s->iw_url;
101 function getText() { return $this->mTextform; }
102 function getURL() { return $this->mUrlform; }
103 function getDBkey() { return $this->mDbkeyform; }
104 function getNamespace() { return $this->mNamespace; }
105 function setNamespace( $n ) { $this->mNamespace = $n; }
106 function getInterwiki() { return $this->mInterwiki; }
107 function getFragment() { return $this->mFragment; }
109 /* static */ function indexTitle( $ns, $title )
111 global $wgDBminWordLen, $wgLang;
113 $lc = SearchEngine::legalSearchChars() . "&#;";
114 $t = $wgLang->stripForSearch( $title );
115 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
116 $t = strtolower( $t );
118 # Handle 's, s'
119 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
120 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
122 $t = preg_replace( "/\\s+/", " ", $t );
124 if ( $ns == Namespace::getImage() ) {
125 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
127 return trim( $t );
130 function getIndexTitle()
132 return Title::indexTitle( $this->mNamespace, $this->mTextform );
135 /* static */ function makeName( $ns, $title )
137 global $wgLang;
139 $n = $wgLang->getNsText( $ns );
140 if ( "" == $n ) { return $title; }
141 else { return "{$n}:{$title}"; }
144 /* static */ function makeTitle( $ns, $title )
146 $t = new Title();
147 $t->mDbkeyform = Title::makeName( $ns, $title );
148 $t->secureAndSplit();
149 return $t;
152 function getPrefixedDBkey()
154 $s = $this->prefix( $this->mDbkeyform );
155 $s = str_replace( " ", "_", $s );
156 return $s;
159 function getPrefixedText()
161 $s = $this->prefix( $this->mTextform );
162 $s = str_replace( "_", " ", $s );
163 return $s;
166 function getPrefixedURL()
168 $s = $this->prefix( $this->mDbkeyform );
169 $s = str_replace( " ", "_", $s );
171 $s = urlencode ( $s ) ;
172 # Cleaning up URL to make it look nice -- is this safe?
173 $s = preg_replace( "/%3[Aa]/", ":", $s );
174 $s = preg_replace( "/%2[Ff]/", "/", $s );
175 $s = str_replace( "%28", "(", $s );
176 $s = str_replace( "%29", ")", $s );
177 return $s;
180 function getFullURL()
182 global $wgLang, $wgArticlePath;
184 if ( "" == $this->mInterwiki ) {
185 $p = $wgArticlePath;
186 } else {
187 $p = $this->getInterwikiLink( $this->mInterwiki );
189 $n = $wgLang->getNsText( $this->mNamespace );
190 if ( "" != $n ) { $n .= ":"; }
191 $u = str_replace( "$1", $n . $this->mUrlform, $p );
192 if ( "" != $this->mFragment ) {
193 $u .= "#" . $this->mFragment;
195 return $u;
198 function getEditURL()
200 global $wgServer, $wgScript;
202 if ( "" != $this->mInterwiki ) { return ""; }
203 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
205 return $s;
208 function isExternal() { return ( "" != $this->mInterwiki ); }
210 function isProtected()
212 if ( -1 == $this->mNamespace ) { return true; }
213 $a = $this->getRestrictions();
214 if ( in_array( "sysop", $a ) ) { return true; }
215 return false;
218 function isLog()
220 if ( $this->mNamespace != Namespace::getWikipedia() ) {
221 return false;
223 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
224 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
225 return true;
227 return false;
230 function userIsWatching()
232 global $wgUser;
234 if ( -1 == $this->mNamespace ) { return false; }
235 if ( 0 == $wgUser->getID() ) { return false; }
237 return $wgUser->isWatched( $this );
240 function userCanEdit()
242 global $wgUser;
244 if ( -1 == $this->mNamespace ) { return false; }
245 # if ( 0 == $this->getArticleID() ) { return false; }
246 if ( $this->mDbkeyform == "_" ) { return false; }
248 $ur = $wgUser->getRights();
249 foreach ( $this->getRestrictions() as $r ) {
250 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
251 return false;
254 return true;
257 function getRestrictions()
259 $id = $this->getArticleID();
260 if ( 0 == $id ) { return array(); }
262 if ( ! $this->mRestrictionsLoaded ) {
263 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
264 $this->mRestrictions = explode( ",", trim( $res ) );
265 $this->mRestrictionsLoaded = true;
267 return $this->mRestrictions;
270 function getArticleID()
272 global $wgLinkCache;
274 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
275 $this->mArticleID = $wgLinkCache->addLink(
276 $this->getPrefixedDBkey() );
277 return $this->mArticleID;
280 function resetArticleID( $newid )
282 global $wgLinkCache;
283 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
285 if ( 0 == $newid ) { $this->mArticleID = -1; }
286 else { $this->mArticleID = $newid; }
287 $this->mRestrictionsLoaded = false;
288 $this->mRestrictions = array();
291 /* private */ function prefix( $name )
293 global $wgLang;
295 $p = "";
296 if ( "" != $this->mInterwiki ) {
297 $p = $this->mInterwiki . ":";
299 if ( 0 != $this->mNamespace ) {
300 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
302 return $p . $name;
305 # Assumes that mDbkeyform has been set, and is urldecoded
306 # and uses undersocres, but not otherwise munged. This function
307 # removes illegal characters, splits off the winterwiki and
308 # namespace prefixes, sets the other forms, and canonicalizes
309 # everything. This one function is really at the core of
310 # Wiki--don't mess with it unless you're really sure you know
311 # what you're doing.
313 /* private */ function secureAndSplit()
315 global $wgLang, $wgLocalInterwiki;
317 $validNamespaces = $wgLang->getNamespaces();
318 unset( $validNamespaces[0] );
320 $this->mInterwiki = $this->mFragment = "";
321 $this->mNamespace = 0;
323 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
324 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
325 $l = strlen( $t );
326 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
327 if ( "" == $t ) { $t = "_"; }
329 $this->mDbkeyform = $t;
330 $done = false;
332 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
333 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
334 $t = substr( $t, 1 );
336 if ( ":" == $t{0} ) {
337 $r = substr( $t, 1 );
338 } else {
339 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
340 #$p = strtolower( $m[1] );
341 $p = $m[1];
342 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
343 $t = $m[2];
344 $this->mNamespace = $ns;
345 } elseif ( $this->getInterwikiLink( $p ) ) {
346 $t = $m[2];
347 $this->mInterwiki = $p;
349 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
350 $t, $m ) ) {
351 $p = strtolower( $m[1] );
352 } else {
353 $done = true;
355 if($this->mInterwiki != $wgLocalInterwiki)
356 $done = true;
359 $r = $t;
361 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
362 $this->mInterwiki = "";
364 # We already know that some pages won't be in the database!
366 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
367 $this->mArticleID = 0;
369 $f = strstr( $r, "#" );
370 if ( false !== $f ) {
371 $this->mFragment = substr( $f, 1 );
372 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
374 # Strip illegal characters.
376 $tc = Title::legalChars();
377 $t = preg_replace( "/[^{$tc}]/", "", $r );
379 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
380 $this->mDbkeyform = $t;
381 $this->mUrlform = wfUrlencode( $t );
382 $this->mTextform = str_replace( "_", " ", $t );