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