Enhances special page links; allow some parameters to be passed via wikilinks to...
[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 }
78 }
79
80 function getInterwikiLink( $key )
81 {
82 global $wgValidInterwikis;
83
84 if ( array_key_exists( $key, $wgValidInterwikis ) ) {
85 return $wgValidInterwikis[$key];
86 } else return "";
87 }
88
89 function getText() { return $this->mTextform; }
90 function getURL() { return $this->mUrlform; }
91 function getDBkey() { return $this->mDbkeyform; }
92 function getNamespace() { return $this->mNamespace; }
93 function setNamespace( $n ) { $this->mNamespace = $n; }
94 function getInterwiki() { return $this->mInterwiki; }
95 function getFragment() { return $this->mFragment; }
96
97 /* static */ function indexTitle( $ns, $title )
98 {
99 global $wgDBminWordLen, $wgLang;
100
101 $lc = SearchEngine::legalSearchChars() . "&#;";
102 $t = $wgLang->stripForSearch( $title );
103 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
104 $t = strtolower( $t );
105
106 # Handle 's, s'
107 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
108 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
109
110 $t = preg_replace( "/\\s+/", " ", $t );
111
112 if ( $ns == Namespace::getImage() ) {
113 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
114 }
115 return trim( $t );
116 }
117
118 function getIndexTitle()
119 {
120 return Title::indexTitle( $this->mNamespace, $this->mTextform );
121 }
122
123 /* static */ function makeName( $ns, $title )
124 {
125 global $wgLang;
126
127 $n = $wgLang->getNsText( $ns );
128 if ( "" == $n ) { return $title; }
129 else { return "{$n}:{$title}"; }
130 }
131
132 /* static */ function makeTitle( $ns, $title )
133 {
134 $t = new Title();
135 $t->mDbkeyform = Title::makeName( $ns, $title );
136 $t->secureAndSplit();
137 return $t;
138 }
139
140 function getPrefixedDBkey()
141 {
142 $s = $this->prefix( $this->mDbkeyform );
143 $s = str_replace( " ", "_", $s );
144 return $s;
145 }
146
147 function getPrefixedText()
148 {
149 $s = $this->prefix( $this->mTextform );
150 $s = str_replace( "_", " ", $s );
151 return $s;
152 }
153
154 function getPrefixedURL()
155 {
156 $s = $this->prefix( $this->mDbkeyform );
157 $s = str_replace( " ", "_", $s );
158
159 $s = urlencode ( $s ) ;
160 # Cleaning up URL to make it look nice -- is this safe?
161 $s = preg_replace( "/%3[Aa]/", ":", $s );
162 $s = preg_replace( "/%2[Ff]/", "/", $s );
163 $s = str_replace( "%28", "(", $s );
164 $s = str_replace( "%29", ")", $s );
165 return $s;
166 }
167
168 function getFullURL()
169 {
170 global $wgLang, $wgArticlePath, $wgValidInterwikis;
171
172 if ( "" == $this->mInterwiki ) {
173 $p = $wgArticlePath;
174 } else {
175 $p = $wgValidInterwikis[$this->mInterwiki];
176 }
177 $n = $wgLang->getNsText( $this->mNamespace );
178 if ( "" != $n ) { $n .= ":"; }
179 $u = str_replace( "$1", $n . $this->mUrlform, $p );
180 if ( "" != $this->mFragment ) {
181 $u .= "#" . $this->mFragment;
182 }
183 return $u;
184 }
185
186 function getEditURL()
187 {
188 global $wgServer, $wgScript;
189
190 if ( "" != $this->mInterwiki ) { return ""; }
191 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
192
193 return $s;
194 }
195
196 function isExternal() { return ( "" != $this->mInterwiki ); }
197
198 function isProtected()
199 {
200 if ( -1 == $this->mNamespace ) { return true; }
201 $a = $this->getRestrictions();
202 if ( in_array( "sysop", $a ) ) { return true; }
203 return false;
204 }
205
206 function isLog()
207 {
208 if ( $this->mNamespace != Namespace::getWikipedia() ) {
209 return false;
210 }
211 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform ) ) ||
212 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform ) ) ) {
213 return true;
214 }
215 return false;
216 }
217
218 function userIsWatching()
219 {
220 global $wgUser;
221
222 if ( -1 == $this->mNamespace ) { return false; }
223 if ( 0 == $wgUser->getID() ) { return false; }
224
225 return $wgUser->isWatched( $this );
226 }
227
228 function userCanEdit()
229 {
230 global $wgUser;
231
232 if ( -1 == $this->mNamespace ) { return false; }
233 # if ( 0 == $this->getArticleID() ) { return false; }
234 if ( $this->mDbkeyform == "_" ) { return false; }
235
236 $ur = $wgUser->getRights();
237 foreach ( $this->getRestrictions() as $r ) {
238 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
239 return false;
240 }
241 }
242 return true;
243 }
244
245 function getRestrictions()
246 {
247 $id = $this->getArticleID();
248 if ( 0 == $id ) { return array(); }
249
250 if ( ! $this->mRestrictionsLoaded ) {
251 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
252 $this->mRestrictions = explode( ",", trim( $res ) );
253 $this->mRestrictionsLoaded = true;
254 }
255 return $this->mRestrictions;
256 }
257
258 function getArticleID()
259 {
260 global $wgLinkCache;
261
262 if ( -1 != $this->mArticleID ) { return $this->mArticleID; }
263 $this->mArticleID = $wgLinkCache->addLink(
264 $this->getPrefixedDBkey() );
265 return $this->mArticleID;
266 }
267
268 function resetArticleID( $newid )
269 {
270 global $wgLinkCache;
271 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
272
273 if ( 0 == $newid ) { $this->mArticleID = -1; }
274 else { $this->mArticleID = $newid; }
275 $this->mRestrictionsLoaded = false;
276 $this->mRestrictions = array();
277 }
278
279 /* private */ function prefix( $name )
280 {
281 global $wgLang;
282
283 $p = "";
284 if ( "" != $this->mInterwiki ) {
285 $p = $this->mInterwiki . ":";
286 }
287 if ( 0 != $this->mNamespace ) {
288 $p .= $wgLang->getNsText( $this->mNamespace ) . ":";
289 }
290 return $p . $name;
291 }
292
293 # Assumes that mDbkeyform has been set, and is urldecoded
294 # and uses undersocres, but not otherwise munged. This function
295 # removes illegal characters, splits off the winterwiki and
296 # namespace prefixes, sets the other forms, and canonicalizes
297 # everything. This one function is really at the core of
298 # Wiki--don't mess with it unless you're really sure you know
299 # what you're doing.
300 #
301 /* private */ function secureAndSplit()
302 {
303 global $wgLang, $wgValidInterwikis, $wgLocalInterwiki;
304
305 $validNamespaces = $wgLang->getNamespaces();
306 unset( $validNamespaces[0] );
307
308 $this->mInterwiki = $this->mFragment = "";
309 $this->mNamespace = 0;
310
311 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform );
312 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
313 $l = strlen( $t );
314 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
315 if ( "" == $t ) { $t = "_"; }
316
317 $this->mDbkeyform = $t;
318 $done = false;
319
320 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
321 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
322 $t = substr( $t, 1 );
323 }
324 if ( ":" == $t{0} ) {
325 $r = substr( $t, 1 );
326 } else {
327 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
328 #$p = strtolower( $m[1] );
329 $p = $m[1];
330 if ( array_key_exists( $p, $wgValidInterwikis ) ) {
331 $t = $m[2];
332 $this->mInterwiki = $p;
333
334 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
335 $t, $m ) ) {
336 $p = strtolower( $m[1] );
337 } else {
338 $done = true;
339 }
340 if($this->mInterwiki != $wgLocalInterwiki)
341 $done = true;
342 }
343 if ( ! $done ) {
344 if ( $ns = $wgLang->getNsIndex( str_replace( " ", "_", $p ))) {
345 $t = $m[2];
346 $this->mNamespace = $ns;
347 }
348 # foreach ( $validNamespaces as $ns ) {
349 # if ( 0 == strcasecmp( $p, $ns ) ) {
350 # $t = $m[2];
351 # $this->mNamespace = $wgLang->getNsIndex(
352 # str_replace( " ", "_", $p ) );
353 # break;
354 # }
355 # }
356 }
357 }
358 $r = $t;
359 }
360 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
361 $this->mInterwiki = "";
362 }
363 # We already know that some pages won't be in the database!
364 #
365 if ( "" != $this->mInterwiki || -1 == $this->mNamespace ) {
366 $this->mArticleID = 0;
367 }
368 $f = strstr( $r, "#" );
369 if ( false !== $f ) {
370 $this->mFragment = substr( $f, 1 );
371 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
372 }
373 # Strip illegal characters.
374 #
375 $tc = Title::legalChars();
376 $t = preg_replace( "/[^{$tc}]/", "", $r );
377
378 if( $this->mInterwiki == "") $t = $wgLang->ucfirst( $t );
379 $this->mDbkeyform = $t;
380 $this->mUrlform = wfUrlencode( $t );
381 $this->mTextform = str_replace( "_", " ", $t );
382 }
383 }
384 ?>