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