'View source' link instead of 'protected page'
[lhc/web/wiklou.git] / includes / Title.php
1 <?
2 # See title.doc
3
4 /* private static */ $title_interwiki_cache = array();
5
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;
11
12 /* private */ function Title()
13 {
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();
20 }
21
22 # Static factory methods
23 #
24 function newFromDBkey( $key )
25 {
26 $t = new Title();
27 $t->mDbkeyform = $key;
28 if( $t->secureAndSplit() )
29 return $t;
30 else
31 return NULL;
32 }
33
34 function newFromText( $text )
35 {
36 static $trans;
37 $fname = "Title::newFromText";
38 wfProfileIn( $fname );
39
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 );
47 }
48 }
49
50 $text = strtr( $text, $trans );
51
52 $text = wfMungeToUtf8( $text );
53
54 $text = urldecode( $text );
55
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;
63 }
64 }
65
66 function newFromURL( $url )
67 {
68 global $wgLang, $wgServer;
69
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...
75
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 );
83
84 $t->mDbkeyform = str_replace( " ", "_", $s );
85 if( $t->secureAndSplit() ) {
86 return $t;
87 } else {
88 return NULL;
89 }
90 }
91
92 function nameOf( $id )
93 {
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; }
98
99 $s = wfFetchObject( $res );
100 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
101 return $n;
102 }
103
104
105 function legalChars()
106 {
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";
115 }
116 }
117
118 function getInterwikiLink( $key )
119 {
120 # Performance note: It would probably be a good idea to
121 # get/set/fetch the entire $title_interwiki_cache with memcache
122 # here, reducing some overhead for the repeated memcache accesses.
123 # Popular pages often has many interwiki links anyways.
124
125 global $wgMemc, $wgDBname, $title_interwiki_cache;
126 $k = "$wgDBname:interwiki:$key";
127
128 if( array_key_exists( $k, $title_interwiki_cache ) )
129 return $title_interwiki_cache[$k]->iw_url;
130
131 $s = $wgMemc->get( $k );
132 if( $s !== false ) return $s->iw_url;
133
134 $dkey = wfStrencode( $key );
135 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
136 $res = wfQuery( $query, DB_READ, "Title::getInterwikiLink" );
137 if(!$res) return "";
138
139 $s = wfFetchObject( $res );
140 if(!$s) {
141 $s = (object)false;
142 $s->iw_url = "";
143 }
144 $wgMemc->set( $k, $s );
145 $title_interwiki_cache[$k] = $s;
146 return $s->iw_url;
147 }
148
149 function getText() { return $this->mTextform; }
150 function getURL() { return $this->mUrlform; }
151 function getDBkey() { return $this->mDbkeyform; }
152 function getNamespace() { return $this->mNamespace; }
153 function setNamespace( $n ) { $this->mNamespace = $n; }
154 function getInterwiki() { return $this->mInterwiki; }
155 function getFragment() { return $this->mFragment; }
156
157 /* static */ function indexTitle( $ns, $title )
158 {
159 global $wgDBminWordLen, $wgLang;
160
161 $lc = SearchEngine::legalSearchChars() . "&#;";
162 $t = $wgLang->stripForSearch( $title );
163 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
164 $t = strtolower( $t );
165
166 # Handle 's, s'
167 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
168 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
169
170 $t = preg_replace( "/\\s+/", " ", $t );
171
172 if ( $ns == Namespace::getImage() ) {
173 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
174 }
175 return trim( $t );
176 }
177
178 function getIndexTitle()
179 {
180 return Title::indexTitle( $this->mNamespace, $this->mTextform );
181 }
182
183 /* static */ function makeName( $ns, $title )
184 {
185 global $wgLang;
186
187 $n = $wgLang->getNsText( $ns );
188 if ( "" == $n ) { return $title; }
189 else { return "{$n}:{$title}"; }
190 }
191
192 /* static */ function makeTitle( $ns, $title )
193 {
194 $t = new Title();
195 $t->mDbkeyform = Title::makeName( $ns, $title );
196 if( $t->secureAndSplit() ) {
197 return $t;
198 } else {
199 return NULL;
200 }
201 }
202
203 function getPrefixedDBkey()
204 {
205 $s = $this->prefix( $this->mDbkeyform );
206 $s = str_replace( " ", "_", $s );
207 return $s;
208 }
209
210 function getPrefixedText()
211 {
212 # TEST THIS @@@
213 if ( empty( $this->mPrefixedText ) ) {
214 $s = $this->prefix( $this->mTextform );
215 $s = str_replace( "_", " ", $s );
216 $this->mPrefixedText = $s;
217 }
218 return $this->mPrefixedText;
219 }
220
221 function getPrefixedURL()
222 {
223 $s = $this->prefix( $this->mDbkeyform );
224 $s = str_replace( " ", "_", $s );
225
226 $s = urlencode ( $s ) ;
227 # Cleaning up URL to make it look nice -- is this safe?
228 $s = preg_replace( "/%3[Aa]/", ":", $s );
229 $s = preg_replace( "/%2[Ff]/", "/", $s );
230 $s = str_replace( "%28", "(", $s );
231 $s = str_replace( "%29", ")", $s );
232 return $s;
233 }
234
235 function getFullURL()
236 {
237 global $wgLang, $wgArticlePath;
238
239 if ( "" == $this->mInterwiki ) {
240 $p = $wgArticlePath;
241 } else {
242 $p = $this->getInterwikiLink( $this->mInterwiki );
243 }
244 $n = $wgLang->getNsText( $this->mNamespace );
245 if ( "" != $n ) { $n .= ":"; }
246 $u = str_replace( "$1", $n . $this->mUrlform, $p );
247 if ( "" != $this->mFragment ) {
248 $u .= "#" . $this->mFragment;
249 }
250 return $u;
251 }
252
253 function getEditURL()
254 {
255 global $wgServer, $wgScript;
256
257 if ( "" != $this->mInterwiki ) { return ""; }
258 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
259
260 return $s;
261 }
262
263 # For the title field in <a> tags
264 function getEscapedText()
265 {
266 return wfEscapeHTML( $this->getPrefixedText() );
267 }
268
269 function isExternal() { return ( "" != $this->mInterwiki ); }
270
271 function isProtected()
272 {
273 if ( -1 == $this->mNamespace ) { return true; }
274 $a = $this->getRestrictions();
275 if ( in_array( "sysop", $a ) ) { return true; }
276 return false;
277 }
278
279 function isLog()
280 {
281 if ( $this->mNamespace != Namespace::getWikipedia() ) {
282 return false;
283 }
284 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
285 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
286 return true;
287 }
288 return false;
289 }
290
291 function userIsWatching()
292 {
293 global $wgUser;
294
295 if ( -1 == $this->mNamespace ) { return false; }
296 if ( 0 == $wgUser->getID() ) { return false; }
297
298 return $wgUser->isWatched( $this );
299 }
300
301 function userCanEdit()
302 {
303 global $wgUser;
304
305 if ( -1 == $this->mNamespace ) { return false; }
306 # if ( 0 == $this->getArticleID() ) { return false; }
307 if ( $this->mDbkeyform == "_" ) { return false; }
308
309 $ur = $wgUser->getRights();
310 foreach ( $this->getRestrictions() as $r ) {
311 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
312 return false;
313 }
314 }
315 return true;
316 }
317
318 function getRestrictions()
319 {
320 $id = $this->getArticleID();
321 if ( 0 == $id ) { return array(); }
322
323 if ( ! $this->mRestrictionsLoaded ) {
324 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
325 $this->mRestrictions = explode( ",", trim( $res ) );
326 $this->mRestrictionsLoaded = true;
327 }
328 return $this->mRestrictions;
329 }
330
331 function isDeleted() {
332 $ns = $this->getNamespace();
333 $t = wfStrencode( $this->getDBkey() );
334 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
335 if( $res = wfQuery( $sql, DB_READ ) ) {
336 $s = wfFetchObject( $res );
337 return $s->n;
338 }
339 return 0;
340 }
341
342 function getArticleID()
343 {
344 global $wgLinkCache;
345
346 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
347 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
348 return $this->mArticleID;
349 }
350
351 function resetArticleID( $newid )
352 {
353 global $wgLinkCache;
354 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
355
356 if ( 0 == $newid ) { $this->mArticleID = -1; }
357 else { $this->mArticleID = $newid; }
358 $this->mRestrictionsLoaded = false;
359 $this->mRestrictions = array();
360 }
361
362 function invalidateCache() {
363 $now = wfTimestampNow();
364 $ns = $this->getNamespace();
365 $ti = wfStrencode( $this->getDBkey() );
366 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
367 return wfQuery( $sql, DB_WRITE, "Title::invalidateCache" );
368 }
369
370 /* private */ function prefix( $name )
371 {
372 global $wgLang;
373
374 $p = "";
375 if ( "" != $this->mInterwiki ) {
376 $p = $this->mInterwiki . ":";
377 }
378 if ( 0 != $this->mNamespace ) {
379 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
380 }
381 return $p . $name;
382 }
383
384 # Assumes that mDbkeyform has been set, and is urldecoded
385 # and uses undersocres, but not otherwise munged. This function
386 # removes illegal characters, splits off the winterwiki and
387 # namespace prefixes, sets the other forms, and canonicalizes
388 # everything.
389 #
390 /* private */ function secureAndSplit()
391 {
392 global $wgLang, $wgLocalInterwiki;
393 $fname = "Title::secureAndSplit";
394 wfProfileIn( $fname );
395
396 static $imgpre = false;
397 static $rxTc = false;
398
399 # Initialisation
400 if ( $imgpre === false ) {
401 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
402 $rxTc = "/[^" . Title::legalChars() . "]/";
403 }
404
405
406 $this->mInterwiki = $this->mFragment = "";
407 $this->mNamespace = 0;
408
409 # Clean up whitespace
410 #
411 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
412 if ( "_" == $t{0} ) {
413 $t = substr( $t, 1 );
414 }
415 $l = strlen( $t );
416 if ( $l && ( "_" == $t{$l-1} ) ) {
417 $t = substr( $t, 0, $l-1 );
418 }
419 if ( "" == $t ) {
420 wfProfileOut( $fname );
421 return false;
422 }
423
424 $this->mDbkeyform = $t;
425 $done = false;
426
427 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
428 $t = substr( $t, 1 );
429 }
430 if ( ":" == $t{0} ) {
431 $r = substr( $t, 1 );
432 } else {
433 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
434 #$p = strtolower( $m[1] );
435 $p = $m[1];
436 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
437 $t = $m[2];
438 $this->mNamespace = $ns;
439 } elseif ( $this->getInterwikiLink( $p ) ) {
440 $t = $m[2];
441 $this->mInterwiki = $p;
442
443 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
444 $done = true;
445 } elseif($this->mInterwiki != $wgLocalInterwiki) {
446 $done = true;
447 }
448 }
449 }
450 $r = $t;
451 }
452 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
453 $this->mInterwiki = "";
454 }
455 # We already know that some pages won't be in the database!
456 #
457 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
458 $this->mArticleID = 0;
459 }
460 $f = strstr( $r, "#" );
461 if ( false !== $f ) {
462 $this->mFragment = substr( $f, 1 );
463 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
464 }
465
466 # Reject illegal characters.
467 #
468 if( preg_match( $rxTc, $r ) ) {
469 return false;
470 }
471
472 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $r );
473 $this->mDbkeyform = $t;
474 $this->mUrlform = wfUrlencode( $t );
475 $this->mTextform = str_replace( "_", " ", $t );
476
477 wfProfileOut( $fname );
478 return true;
479 }
480
481 function getTalkPage() {
482 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
483 }
484
485 function getSubjectPage() {
486 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
487 }
488 }
489 ?>