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