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