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