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