a60007bea4fefb276d6b8f36087a440c26c83110
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
1 <?
2 # Global functions used everywhere
3
4 $wgNumberOfArticles = -1; # Unset
5 $wgTotalViews = -1;
6 $wgTotalEdits = -1;
7
8 global $IP;
9 include_once( "$IP/DatabaseFunctions.php" );
10 include_once( "$IP/UpdateClasses.php" );
11 include_once( "$IP/LogPage.php" );
12
13 # PHP 4.1+ has array_key_exists, PHP 4.0.6 has key_exists instead, and earlier
14 # versions of PHP have neither. So we roll our own. Note that this
15 # function will return false even for keys that exist but whose associated
16 # value is NULL.
17 #
18 if ( phpversion() == "4.0.6" ) {
19 function array_key_exists( $k, $a ) {
20 return key_exists( $k, $a );
21 }
22 } else if (phpversion() < "4.1") {
23 function array_key_exists( $k, $a ) {
24 return isset($a[$k]);
25 }
26 }
27
28 $wgRandomSeeded = false;
29
30 function wfSeedRandom()
31 {
32 global $wgRandomSeeded;
33
34 if ( ! $wgRandomSeeded ) {
35 mt_srand( (double)microtime() * 1000000 );
36 $wgRandomSeeded = true;
37 }
38 }
39
40 function wfLocalUrl( $a, $q = "" )
41 {
42 global $wgServer, $wgScript, $wgArticlePath;
43
44 $a = str_replace( " ", "_", $a );
45 #$a = wfUrlencode( $a ); # This stuff is _already_ URL-encoded.
46
47 if ( "" == $a ) {
48 if( "" == $q ) {
49 $a = $wgServer . $wgScript;
50 } else {
51 $a = "{$wgServer}{$wgScript}?{$q}";
52 }
53 } else if ( "" == $q ) {
54 $a = str_replace( "$1", $a, $wgArticlePath );
55 } else {
56 $a = "{$wgServer}{$wgScript}?title={$a}&{$q}";
57 }
58 return $a;
59 }
60
61 function wfLocalUrlE( $a, $q = "" )
62 {
63 return wfEscapeHTML( wfLocalUrl( $a, $q ) );
64 }
65
66 function wfImageUrl( $img )
67 {
68 global $wgUploadPath;
69
70 $nt = Title::newFromText( $img );
71 $name = $nt->getDBkey();
72 $hash = md5( $name );
73
74 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
75 substr( $hash, 0, 2 ) . "/{$name}";
76 return wfUrlencode( $url );
77 }
78
79 function wfImageArchiveUrl( $name )
80 {
81 global $wgUploadPath;
82
83 $hash = md5( substr( $name, 15) );
84 $url = "{$wgUploadPath}/archive/" . $hash{0} . "/" .
85 substr( $hash, 0, 2 ) . "/{$name}";
86 return $url;
87 }
88
89 function wfUrlencode ( $s )
90 {
91 $ulink = urlencode( $s );
92 $ulink = preg_replace( "/%3[Aa]/", ":", $ulink );
93 $ulink = preg_replace( "/%2[Ff]/", "/", $ulink );
94 return $ulink;
95 }
96
97 function wfUtf8Sequence($codepoint) {
98 if($codepoint < 0x80) return chr($codepoint);
99 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
100 chr($codepoint & 0x3f | 0x80);
101 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
102 chr($codepoint >> 6 & 0x3f | 0x80) .
103 chr($codepoint & 0x3f | 0x80);
104 if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this
105 chr($codepoint >> 12 & 0x3f | 0x80) .
106 chr($codepoint >> 6 & 0x3f | 0x80) .
107 chr($codepoint & 0x3f | 0x80);
108 # Doesn't yet handle outside the BMP
109 return "&#$codepoint;";
110 }
111
112 function wfMungeToUtf8($string) {
113 global $wgInputEncoding; # This is debatable
114 #$string = iconv($wgInputEncoding, "UTF-8", $string);
115 $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
116 $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
117 # Should also do named entities here
118 return $string;
119 }
120
121 function wfDebug( $text, $logonly = false )
122 {
123 global $wgOut, $wgDebugLogFile;
124
125 if ( ! $logonly ) {
126 $wgOut->debug( $text );
127 }
128 if ( "" != $wgDebugLogFile ) {
129 error_log( $text, 3, $wgDebugLogFile );
130 }
131 }
132
133 if( !isset( $wgProfiling ) )
134 $wgProfiling = false;
135 $wgProfileStack = array();
136 $wgProfileWorkStack = array();
137
138 if( $wgProfiling ) {
139 function wfProfileIn( $functionname )
140 {
141 global $wgProfileStack, $wgProfileWorkStack;
142 array_push( $wgProfileWorkStack, "$functionname " .
143 count( $wgProfileWorkStack ) . " " . microtime() );
144 }
145
146 function wfProfileOut() {
147 global $wgProfileStack, $wgProfileWorkStack;
148 $bit = array_pop( $wgProfileWorkStack );
149 $bit .= " " . microtime();
150 array_push( $wgProfileStack, $bit );
151 }
152 } else {
153 function wfProfileIn( $functionname ) { }
154 function wfProfileOut( ) { }
155 }
156
157 function wfReadOnly()
158 {
159 global $wgReadOnlyFile;
160
161 if ( "" == $wgReadOnlyFile ) { return false; }
162 return is_file( $wgReadOnlyFile );
163 }
164
165 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
166 function wfMsg( $key )
167 {
168 global $wgLang, $wgReplacementKeys;
169 $ret = $wgLang->getMessage( $key );
170
171 if( func_num_args() > 1 ) {
172 $reps = func_get_args();
173 array_shift( $reps );
174 $ret = str_replace( $wgReplacementKeys, $reps, $ret );
175 }
176
177 if ( "" == $ret ) {
178 user_error( "Couldn't find text for message \"{$key}\"." );
179 }
180 return $ret;
181 }
182
183 function wfCleanFormFields( $fields )
184 {
185 global $HTTP_POST_VARS;
186 global $wgInputEncoding, $wgOutputEncoding, $wgEditEncoding, $wgLang;
187
188 if ( get_magic_quotes_gpc() ) {
189 foreach ( $fields as $fname ) {
190 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
191 $HTTP_POST_VARS[$fname] = stripslashes(
192 $HTTP_POST_VARS[$fname] );
193 }
194 global ${$fname};
195 if ( isset( ${$fname} ) ) {
196 ${$fname} = stripslashes( ${$fname} );
197 }
198 }
199 }
200 $enc = $wgOutputEncoding;
201 if( $wgEditEncoding != "") $enc = $wgEditEncoding;
202 if ( $enc != $wgInputEncoding ) {
203 foreach ( $fields as $fname ) {
204 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
205 $HTTP_POST_VARS[$fname] = $wgLang->iconv(
206 $wgOutputEncoding, $wgInputEncoding,
207 $HTTP_POST_VARS[$fname] );
208 }
209 global ${$fname};
210 if ( isset( ${$fname} ) ) {
211 ${$fname} = $wgLang->iconv(
212 $enc, $wgInputEncoding, ${$fname} );
213 }
214 }
215 }
216 }
217
218 function wfMungeQuotes( $in )
219 {
220 $out = str_replace( "%", "%25", $in );
221 $out = str_replace( "'", "%27", $out );
222 $out = str_replace( "\"", "%22", $out );
223 return $out;
224 }
225
226 function wfDemungeQuotes( $in )
227 {
228 $out = str_replace( "%22", "\"", $in );
229 $out = str_replace( "%27", "'", $out );
230 $out = str_replace( "%25", "%", $out );
231 return $out;
232 }
233
234 function wfCleanQueryVar( $var )
235 {
236 global $wgLang;
237 if ( get_magic_quotes_gpc() ) {
238 $var = stripslashes( $var );
239 }
240 return $wgLang->recodeInput( $var );
241 }
242
243 function wfSpecialPage()
244 {
245 global $wgUser, $wgOut, $wgTitle, $wgLang;
246
247 $validSP = $wgLang->getValidSpecialPages();
248 $sysopSP = $wgLang->getSysopSpecialPages();
249 $devSP = $wgLang->getDeveloperSpecialPages();
250
251 $wgOut->setArticleFlag( false );
252 $wgOut->setRobotpolicy( "noindex,follow" );
253
254 $t = $wgTitle->getDBkey();
255 if ( array_key_exists( $t, $validSP ) ||
256 ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) ||
257 ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) {
258 $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) );
259
260 $inc = "Special" . $t . ".php";
261 include_once( $inc );
262 $call = "wfSpecial" . $t;
263 $call();
264 } else if ( array_key_exists( $t, $sysopSP ) ) {
265 $wgOut->sysopRequired();
266 } else if ( array_key_exists( $t, $devSP ) ) {
267 $wgOut->developerRequired();
268 } else {
269 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
270 }
271 }
272
273 function wfSearch( $s )
274 {
275 $se = new SearchEngine( wfCleanQueryVar( $s ) );
276 $se->showResults();
277 }
278
279 function wfGo( $s )
280 { # pick the nearest match
281 $se = new SearchEngine( wfCleanQueryVar( $s ) );
282 $se->goResult();
283 }
284
285 function wfNumberOfArticles()
286 {
287 global $wgNumberOfArticles;
288
289 wfLoadSiteStats();
290 return $wgNumberOfArticles;
291 }
292
293 /* private */ function wfLoadSiteStats()
294 {
295 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
296 if ( -1 != $wgNumberOfArticles ) return;
297
298 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
299 "FROM site_stats WHERE ss_row_id=1";
300 $res = wfQuery( $sql, "wfLoadSiteStats" );
301
302 if ( 0 == wfNumRows( $res ) ) { return; }
303 else {
304 $s = wfFetchObject( $res );
305 $wgTotalViews = $s->ss_total_views;
306 $wgTotalEdits = $s->ss_total_edits;
307 $wgNumberOfArticles = $s->ss_good_articles;
308 }
309 }
310
311 function wfEscapeHTML( $in )
312 {
313 return str_replace(
314 array( "&", "\"", ">", "<" ),
315 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
316 $in );
317 }
318
319 function wfEscapeHTMLTagsOnly( $in ) {
320 return str_replace(
321 array( "\"", ">", "<" ),
322 array( "&quot;", "&gt;", "&lt;" ),
323 $in );
324 }
325
326 function wfUnescapeHTML( $in )
327 {
328 $in = str_replace( "&lt;", "<", $in );
329 $in = str_replace( "&gt;", ">", $in );
330 $in = str_replace( "&quot;", "\"", $in );
331 $in = str_replace( "&amp;", "&", $in );
332 return $in;
333 }
334
335 function wfImageDir( $fname )
336 {
337 global $wgUploadDirectory;
338
339 $hash = md5( $fname );
340 $oldumask = umask(0);
341 $dest = $wgUploadDirectory . "/" . $hash{0};
342 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
343 $dest .= "/" . substr( $hash, 0, 2 );
344 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
345
346 umask( $oldumask );
347 return $dest;
348 }
349
350 function wfImageArchiveDir( $fname )
351 {
352 global $wgUploadDirectory;
353
354 $hash = md5( $fname );
355 $oldumask = umask(0);
356 $archive = "{$wgUploadDirectory}/archive";
357 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
358 $archive .= "/" . $hash{0};
359 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
360 $archive .= "/" . substr( $hash, 0, 2 );
361 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
362
363 umask( $oldumask );
364 return $archive;
365 }
366
367 function wfRecordUpload( $name, $oldver, $size, $desc )
368 {
369 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
370 $fname = "wfRecordUpload";
371
372 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
373 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
374 $res = wfQuery( $sql, $fname );
375
376 if ( 0 == wfNumRows( $res ) ) {
377 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
378 "img_description,img_user,img_user_text) VALUES ('" .
379 wfStrencode( $name ) . "',{$size},'" . date( "YmdHis" ) . "','" .
380 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
381 "', '" . wfStrencode( $wgUser->getName() ) . "')";
382 wfQuery( $sql, $fname );
383
384 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
385 Namespace::getImage() . " AND cur_title='" .
386 wfStrencode( $name ) . "'";
387 $res = wfQuery( $sql, $fname );
388 if ( 0 == wfNumRows( $res ) ) {
389 $now = wfTimestampNow();
390 $won = wfInvertTimestamp( $now );
391 $common =
392 Namespace::getImage() . ",'" .
393 wfStrencode( $name ) . "','" .
394 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
395 wfStrencode( $wgUser->getName() ) . "','" . $now .
396 "',1";
397 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
398 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
399 "cur_text,inverse_timestamp) VALUES (" .
400 $common .
401 ",'" . wfStrencode( $desc ) . "','{$won}')";
402 wfQuery( $sql, $fname );
403 $id = wfInsertId() or 0; # We should throw an error instead
404 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
405 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
406 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
407 wfQuery( $sql, $fname );
408 $u = new SearchUpdate( $id, $name, $desc );
409 $u->doUpdate();
410 }
411 } else {
412 $s = wfFetchObject( $res );
413
414 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
415 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
416 wfStrencode( $s->img_name ) . "','" .
417 wfStrencode( $oldver ) .
418 "',{$s->img_size},'{$s->img_timestamp}','" .
419 wfStrencode( $s->img_description ) . "','" .
420 wfStrencode( $s->img_user ) . "','" .
421 wfStrencode( $s->img_user_text) . "')";
422 wfQuery( $sql, $fname );
423
424 $sql = "UPDATE image SET img_size={$size}," .
425 "img_timestamp='" . date( "YmdHis" ) . "',img_user='" .
426 $wgUser->getID() . "',img_user_text='" .
427 wfStrencode( $wgUser->getName() ) . "', img_description='" .
428 wfStrencode( $desc ) . "' WHERE img_name='" .
429 wfStrencode( $name ) . "'";
430 wfQuery( $sql, $fname );
431 }
432
433 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
434 $da = str_replace( "$1", "[[:" . $wgLang->getNsText(
435 Namespace::getImage() ) . ":{$name}|{$name}]]",
436 wfMsg( "uploadedimage" ) );
437 $ta = str_replace( "$1", $name, wfMsg( "uploadedimage" ) );
438 $log->addEntry( $da, $desc, $ta );
439 }
440
441
442 /* Some generic result counters, pulled out of SearchEngine */
443
444 function wfShowingResults( $offset, $limit )
445 {
446 $top = str_replace( "$1", $limit, wfMsg( "showingresults" ) );
447 $top = str_replace( "$2", $offset+1, $top );
448 return $top;
449 }
450
451 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
452 {
453 global $wgUser;
454 $prev = str_replace( "$1", $limit, wfMsg( "prevn" ) );
455 $next = str_replace( "$1", $limit, wfMsg( "nextn" ) );
456
457 $sk = $wgUser->getSkin();
458 if ( 0 != $offset ) {
459 $po = $offset - $limit;
460 if ( $po < 0 ) { $po = 0; }
461 $q = "limit={$limit}&offset={$po}";
462 if ( "" != $query ) { $q .= "&{$query}"; }
463 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
464 } else { $plink = $prev; }
465
466 $no = $offset + $limit;
467 $q = "limit={$limit}&offset={$no}";
468 if ( "" != $query ) { $q .= "&{$query}"; }
469
470 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
471 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
472 wfNumLink( $offset, 50, $link, $query ) . " | " .
473 wfNumLink( $offset, 100, $link, $query ) . " | " .
474 wfNumLink( $offset, 250, $link, $query ) . " | " .
475 wfNumLink( $offset, 500, $link, $query );
476
477 $sl = str_replace( "$1", $plink, wfMsg( "viewprevnext" ) );
478 $sl = str_replace( "$2", $nlink, $sl );
479 $sl = str_replace( "$3", $nums, $sl );
480 return $sl;
481 }
482
483 function wfNumLink( $offset, $limit, $link, $query = "" )
484 {
485 global $wgUser;
486 if ( "" == $query ) { $q = ""; }
487 else { $q = "{$query}&"; }
488 $q .= "limit={$limit}&offset={$offset}";
489
490 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
491 return $s;
492 }
493
494 ?>