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