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