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