Added per page support for inhibiting editsection links via the __NOEDITSECTION__...
[mediawiki.git] / includes / Title.php
blob7f374da276df50270fb9408a6ea1facb86ba50b6
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 if( $t->secureAndSplit() )
26 return $t;
27 else
28 return NULL;
29 return $t;
32 function newFromText( $text )
34 wfProfileIn( "Title::newFromText" );
36 # Note - mixing latin1 named entities and unicode numbered
37 # ones will result in a bad link.
38 $trans = get_html_translation_table( HTML_ENTITIES );
39 $trans = array_flip( $trans );
40 $text = strtr( $text, $trans );
42 $text = wfMungeToUtf8( $text );
44 $text = urldecode( $text );
46 $t = new Title();
47 $t->mDbkeyform = str_replace( " ", "_", $text );
48 if( $t->secureAndSplit() ) {
49 wfProfileOut();
50 return $t;
51 } else {
52 return NULL;
56 function newFromURL( $url )
58 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
60 $t = new Title();
61 $s = urldecode( $url ); # This is technically wrong, as anything
62 # we've gotten is already decoded by PHP.
63 # Kept for backwards compatibility with
64 # buggy URLs we had for a while...
66 # For links that came from outside, check for alternate/legacy
67 # character encoding.
68 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
69 $s = $wgLang->checkTitleEncoding( $s );
71 $t->mDbkeyform = str_replace( " ", "_", $s );
72 if( $t->secureAndSplit() ) {
73 return $t;
74 } else {
75 return NULL;
79 function nameOf( $id )
81 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
82 "cur_id={$id}";
83 $res = wfQuery( $sql, DB_READ, "Article::nameOf" );
84 if ( 0 == wfNumRows( $res ) ) { return NULL; }
86 $s = wfFetchObject( $res );
87 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
88 return $n;
92 function legalChars()
94 global $wgInputEncoding;
95 if( $wgInputEncoding == "utf-8" ) {
96 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
97 } else {
98 # ISO 8859-* don't allow 0x80-0x9F
99 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
100 # But that breaks interlanguage links at the moment. Temporary:
101 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
105 function getInterwikiLink( $key )
107 global $wgMemc, $wgDBname;
108 $k = "$wgDBname:interwiki:$key";
109 $s = $wgMemc->get( $k );
110 if( $s !== false ) return $s->iw_url;
112 $dkey = wfStrencode( $key );
113 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
114 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
115 if(!$res) return "";
117 $s = wfFetchObject( $res );
118 if(!$s) {
119 $s = (object)false;
120 $s->iw_url = "";
122 $wgMemc->set( $k, $s );
123 return $s->iw_url;
126 function getText() { return $this->mTextform; }
127 function getURL() { return $this->mUrlform; }
128 function getDBkey() { return $this->mDbkeyform; }
129 function getNamespace() { return $this->mNamespace; }
130 function setNamespace( $n ) { $this->mNamespace = $n; }
131 function getInterwiki() { return $this->mInterwiki; }
132 function getFragment() { return $this->mFragment; }
134 /* static */ function indexTitle( $ns, $title )
136 global $wgDBminWordLen, $wgLang;
138 $lc = SearchEngine::legalSearchChars() . "&#;";
139 $t = $wgLang->stripForSearch( $title );
140 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
141 $t = strtolower( $t );
143 # Handle 's, s'
144 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
145 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
147 $t = preg_replace( "/\\s+/", " ", $t );
149 if ( $ns == Namespace::getImage() ) {
150 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
152 return trim( $t );
155 function getIndexTitle()
157 return Title::indexTitle( $this->mNamespace, $this->mTextform );
160 /* static */ function makeName( $ns, $title )
162 global $wgLang;
164 $n = $wgLang->getNsText( $ns );
165 if ( "" == $n ) { return $title; }
166 else { return "{$n}:{$title}"; }
169 /* static */ function makeTitle( $ns, $title )
171 $t = new Title();
172 $t->mDbkeyform = Title::makeName( $ns, $title );
173 if( $t->secureAndSplit() ) {
174 return $t;
175 } else {
176 return NULL;
180 function getPrefixedDBkey()
182 $s = $this->prefix( $this->mDbkeyform );
183 $s = str_replace( " ", "_", $s );
184 return $s;
187 function getPrefixedText()
189 $s = $this->prefix( $this->mTextform );
190 $s = str_replace( "_", " ", $s );
191 return $s;
194 function getPrefixedURL()
196 $s = $this->prefix( $this->mDbkeyform );
197 $s = str_replace( " ", "_", $s );
199 $s = urlencode ( $s ) ;
200 # Cleaning up URL to make it look nice -- is this safe?
201 $s = preg_replace( "/%3[Aa]/", ":", $s );
202 $s = preg_replace( "/%2[Ff]/", "/", $s );
203 $s = str_replace( "%28", "(", $s );
204 $s = str_replace( "%29", ")", $s );
205 return $s;
208 function getFullURL()
210 global $wgLang, $wgArticlePath;
212 if ( "" == $this->mInterwiki ) {
213 $p = $wgArticlePath;
214 } else {
215 $p = $this->getInterwikiLink( $this->mInterwiki );
217 $n = $wgLang->getNsText( $this->mNamespace );
218 if ( "" != $n ) { $n .= ":"; }
219 $u = str_replace( "$1", $n . $this->mUrlform, $p );
220 if ( "" != $this->mFragment ) {
221 $u .= "#" . $this->mFragment;
223 return $u;
226 function getEditURL()
228 global $wgServer, $wgScript;
230 if ( "" != $this->mInterwiki ) { return ""; }
231 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
233 return $s;
236 function isExternal() { return ( "" != $this->mInterwiki ); }
238 function isProtected()
240 if ( -1 == $this->mNamespace ) { return true; }
241 $a = $this->getRestrictions();
242 if ( in_array( "sysop", $a ) ) { return true; }
243 return false;
246 function isLog()
248 if ( $this->mNamespace != Namespace::getWikipedia() ) {
249 return false;
251 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
252 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
253 return true;
255 return false;
258 function userIsWatching()
260 global $wgUser;
262 if ( -1 == $this->mNamespace ) { return false; }
263 if ( 0 == $wgUser->getID() ) { return false; }
265 return $wgUser->isWatched( $this );
268 function userCanEdit()
270 global $wgUser;
272 if ( -1 == $this->mNamespace ) { return false; }
273 # if ( 0 == $this->getArticleID() ) { return false; }
274 if ( $this->mDbkeyform == "_" ) { return false; }
276 $ur = $wgUser->getRights();
277 foreach ( $this->getRestrictions() as $r ) {
278 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
279 return false;
282 return true;
285 function getRestrictions()
287 $id = $this->getArticleID();
288 if ( 0 == $id ) { return array(); }
290 if ( ! $this->mRestrictionsLoaded ) {
291 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
292 $this->mRestrictions = explode( ",", trim( $res ) );
293 $this->mRestrictionsLoaded = true;
295 return $this->mRestrictions;
298 function isDeleted() {
299 $ns = $this->getNamespace();
300 $t = wfStrencode( $this->getDBkey() );
301 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
302 if( $res = wfQuery( $sql, DB_READ ) ) {
303 $s = wfFetchObject( $res );
304 return $s->n;
306 return 0;
309 function getArticleID()
311 global $wgLinkCache;
313 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
314 $this->mArticleID = $wgLinkCache->addLink(
315 $this->getPrefixedDBkey() );
316 return $this->mArticleID;
319 function resetArticleID( $newid )
321 global $wgLinkCache;
322 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
324 if ( 0 == $newid ) { $this->mArticleID = -1; }
325 else { $this->mArticleID = $newid; }
326 $this->mRestrictionsLoaded = false;
327 $this->mRestrictions = array();
330 /* private */ function prefix( $name )
332 global $wgLang;
334 $p = "";
335 if ( "" != $this->mInterwiki ) {
336 $p = $this->mInterwiki . ":";
338 if ( 0 != $this->mNamespace ) {
339 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
341 return $p . $name;
344 # Assumes that mDbkeyform has been set, and is urldecoded
345 # and uses undersocres, but not otherwise munged. This function
346 # removes illegal characters, splits off the winterwiki and
347 # namespace prefixes, sets the other forms, and canonicalizes
348 # everything. This one function is really at the core of
349 # Wiki--don't mess with it unless you're really sure you know
350 # what you're doing.
352 /* private */ function secureAndSplit()
354 global $wgLang, $wgLocalInterwiki;
355 wfProfileIn( "Title::secureAndSplit" );
357 $validNamespaces = $wgLang->getNamespaces();
358 unset( $validNamespaces[0] );
360 $this->mInterwiki = $this->mFragment = "";
361 $this->mNamespace = 0;
363 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
364 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
365 $l = strlen( $t );
366 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
367 if ( "" == $t ) {
368 wfProfileOut();
369 return false;
372 $this->mDbkeyform = $t;
373 $done = false;
375 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
376 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
377 $t = substr( $t, 1 );
379 if ( ":" == $t{0} ) {
380 $r = substr( $t, 1 );
381 } else {
382 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
383 #$p = strtolower( $m[1] );
384 $p = $m[1];
385 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
386 $t = $m[2];
387 $this->mNamespace = $ns;
388 } elseif ( $this->getInterwikiLink( $p ) ) {
389 $t = $m[2];
390 $this->mInterwiki = $p;
392 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
393 $t, $m ) ) {
394 $p = strtolower( $m[1] );
395 } else {
396 $done = true;
398 if($this->mInterwiki != $wgLocalInterwiki)
399 $done = true;
402 $r = $t;
404 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
405 $this->mInterwiki = "";
407 # We already know that some pages won't be in the database!
409 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
410 $this->mArticleID = 0;
412 $f = strstr( $r, "#" );
413 if ( false !== $f ) {
414 $this->mFragment = substr( $f, 1 );
415 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
417 # Strip illegal characters.
419 $tc = Title::legalChars();
420 $t = preg_replace( "/[^{$tc}]/", "", $r );
422 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
423 $this->mDbkeyform = $t;
424 $this->mUrlform = wfUrlencode( $t );
425 $this->mTextform = str_replace( "_", " ", $t );
427 wfProfileOut();
428 return true;