63ad667fd74c96bfb1f1035ffc06455cc8c2749c
[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 include_once( "DatabaseFunctions.php" );
9 include_once( "UpdateClasses.php" );
10 include_once( "LogPage.php" );
11
12 /*
13 * Compatibility functions
14 */
15
16 # PHP <4.3.x is not actively supported; 4.1.x and 4.2.x might or might not work.
17 # <4.1.x will not work, as we use a number of features introduced in 4.1.0
18 # such as the new autoglobals.
19
20 if( !function_exists('iconv') ) {
21 # iconv support is not in the default configuration and so may not be present.
22 # Assume will only ever use utf-8 and iso-8859-1.
23 # This will *not* work in all circumstances.
24 function iconv( $from, $to, $string ) {
25 if(strcasecmp( $from, $to ) == 0) return $string;
26 if(strcasecmp( $from, "utf-8" ) == 0) return utf8_decode( $string );
27 if(strcasecmp( $to, "utf-8" ) == 0) return utf8_encode( $string );
28 return $string;
29 }
30 }
31
32 if( !function_exists('file_get_contents') ) {
33 # Exists in PHP 4.3.0+
34 function file_get_contents( $filename ) {
35 return implode( "", file( $filename ) );
36 }
37 }
38
39 $wgRandomSeeded = false;
40
41 function wfSeedRandom()
42 {
43 global $wgRandomSeeded;
44
45 if ( ! $wgRandomSeeded ) {
46 $seed = hexdec(substr(md5(microtime()),-8)) & 0x7fffffff;
47 mt_srand( $seed );
48 $wgRandomSeeded = true;
49 }
50 }
51
52 function wfLocalUrl( $a, $q = "" )
53 {
54 global $wgServer, $wgScript, $wgArticlePath;
55
56 $a = str_replace( " ", "_", $a );
57 #$a = wfUrlencode( $a ); # This stuff is _already_ URL-encoded.
58
59 if ( "" == $a ) {
60 if( "" == $q ) {
61 $a = $wgScript;
62 } else {
63 $a = "{$wgScript}?{$q}";
64 }
65 } else if ( "" == $q ) {
66 $a = str_replace( "$1", $a, $wgArticlePath );
67 } else {
68 $a = "{$wgScript}?title={$a}&{$q}";
69 }
70 return $a;
71 }
72
73 function wfLocalUrlE( $a, $q = "" )
74 {
75 return wfEscapeHTML( wfLocalUrl( $a, $q ) );
76 }
77
78 function wfFullUrl( $a, $q = "" ) {
79 global $wgServer;
80 return $wgServer . wfLocalUrl( $a, $q );
81 }
82
83 function wfFullUrlE( $a, $q = "" ) {
84 return wfEscapeHTML( wfFullUrl( $a, $q ) );
85 }
86
87 function wfImageUrl( $img )
88 {
89 global $wgUploadPath;
90
91 $nt = Title::newFromText( $img );
92 if( !$nt ) return "";
93
94 $name = $nt->getDBkey();
95 $hash = md5( $name );
96
97 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
98 substr( $hash, 0, 2 ) . "/{$name}";
99 return wfUrlencode( $url );
100 }
101
102 function wfImageArchiveUrl( $name )
103 {
104 global $wgUploadPath;
105
106 $hash = md5( substr( $name, 15) );
107 $url = "{$wgUploadPath}/archive/" . $hash{0} . "/" .
108 substr( $hash, 0, 2 ) . "/{$name}";
109 return $url;
110 }
111
112 function wfUrlencode ( $s )
113 {
114 $ulink = urlencode( $s );
115 $ulink = preg_replace( "/%3[Aa]/", ":", $ulink );
116 $ulink = preg_replace( "/%2[Ff]/", "/", $ulink );
117 return $ulink;
118 }
119
120 function wfUtf8Sequence($codepoint) {
121 if($codepoint < 0x80) return chr($codepoint);
122 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
123 chr($codepoint & 0x3f | 0x80);
124 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
125 chr($codepoint >> 6 & 0x3f | 0x80) .
126 chr($codepoint & 0x3f | 0x80);
127 if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this
128 chr($codepoint >> 12 & 0x3f | 0x80) .
129 chr($codepoint >> 6 & 0x3f | 0x80) .
130 chr($codepoint & 0x3f | 0x80);
131 # Doesn't yet handle outside the BMP
132 return "&#$codepoint;";
133 }
134
135 function wfMungeToUtf8($string) {
136 global $wgInputEncoding; # This is debatable
137 #$string = iconv($wgInputEncoding, "UTF-8", $string);
138 $string = preg_replace ( '/&#([0-9]+);/e', 'wfUtf8Sequence($1)', $string );
139 $string = preg_replace ( '/&#x([0-9a-f]+);/ie', 'wfUtf8Sequence(0x$1)', $string );
140 # Should also do named entities here
141 return $string;
142 }
143
144 function wfDebug( $text, $logonly = false )
145 {
146 global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly;
147
148 if ( $wgDebugComments && !$logonly ) {
149 $wgOut->debug( $text );
150 }
151 if ( "" != $wgDebugLogFile && !$wgProfileOnly ) {
152 error_log( $text, 3, $wgDebugLogFile );
153 }
154 }
155
156 function logProfilingData()
157 {
158 global $wgRequestTime, $wgDebugLogFile;
159 global $wgProfiling, $wgProfileStack, $wgProfileLimit, $wgUser;
160 list( $usec, $sec ) = explode( " ", microtime() );
161 $now = (float)$sec + (float)$usec;
162
163 list( $usec, $sec ) = explode( " ", $wgRequestTime );
164 $start = (float)$sec + (float)$usec;
165 $elapsed = $now - $start;
166 if ( "" != $wgDebugLogFile ) {
167 $prof = wfGetProfilingOutput( $start, $elapsed );
168 $forward = "";
169 if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
170 $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR'];
171 if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
172 $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP'];
173 if( !empty( $_SERVER['HTTP_FROM'] ) )
174 $forward .= " from " . $_SERVER['HTTP_FROM'];
175 if( $forward )
176 $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
177 if($wgUser->getId() == 0)
178 $forward .= " anon";
179 $log = sprintf( "%s\t%04.3f\t%s\n",
180 gmdate( "YmdHis" ), $elapsed,
181 urldecode( $_SERVER['REQUEST_URI'] . $forward ) );
182 error_log( $log . $prof, 3, $wgDebugLogFile );
183 }
184 }
185
186
187 function wfReadOnly()
188 {
189 global $wgReadOnlyFile;
190
191 if ( "" == $wgReadOnlyFile ) { return false; }
192 return is_file( $wgReadOnlyFile );
193 }
194
195 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
196
197 # Get a message from anywhere
198 function wfMsg( $key ) {
199 $args = func_get_args();
200 if ( count( $args ) ) {
201 array_shift( $args );
202 }
203 return wfMsgReal( $key, $args, true );
204 }
205
206 # Get a message from the language file
207 function wfMsgNoDB( $key ) {
208 $args = func_get_args();
209 if ( count( $args ) ) {
210 array_shift( $args );
211 }
212 return wfMsgReal( $key, $args, false );
213 }
214
215 # Really get a message
216 function wfMsgReal( $key, $args, $useDB ) {
217 global $wgReplacementKeys, $wgMessageCache, $wgLang;
218
219 $fname = "wfMsg";
220 wfProfileIn( $fname );
221 if ( $wgMessageCache ) {
222 $message = $wgMessageCache->get( $key, $useDB );
223 } elseif ( $wgLang ) {
224 $message = $wgLang->getMessage( $key );
225 } else {
226 wfDebug( "No language object when getting $key\n" );
227 $message = "&lt;$key&gt;";
228 }
229
230 # Replace arguments
231 if( count( $args ) ) {
232 $message = str_replace( $wgReplacementKeys, $args, $message );
233 }
234 wfProfileOut( $fname );
235 return $message;
236 }
237
238 function wfCleanFormFields( $fields )
239 {
240 global $HTTP_POST_VARS;
241 global $wgInputEncoding, $wgOutputEncoding, $wgEditEncoding, $wgLang;
242
243 if ( get_magic_quotes_gpc() ) {
244 foreach ( $fields as $fname ) {
245 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
246 $HTTP_POST_VARS[$fname] = stripslashes(
247 $HTTP_POST_VARS[$fname] );
248 }
249 global ${$fname};
250 if ( isset( ${$fname} ) ) {
251 ${$fname} = stripslashes( ${$fname} );
252 }
253 }
254 }
255 $enc = $wgOutputEncoding;
256 if( $wgEditEncoding != "") $enc = $wgEditEncoding;
257 if ( $enc != $wgInputEncoding ) {
258 foreach ( $fields as $fname ) {
259 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
260 $HTTP_POST_VARS[$fname] = $wgLang->iconv(
261 $wgOutputEncoding, $wgInputEncoding,
262 $HTTP_POST_VARS[$fname] );
263 }
264 global ${$fname};
265 if ( isset( ${$fname} ) ) {
266 ${$fname} = $wgLang->iconv(
267 $enc, $wgInputEncoding, ${$fname} );
268 }
269 }
270 }
271 }
272
273 function wfMungeQuotes( $in )
274 {
275 $out = str_replace( "%", "%25", $in );
276 $out = str_replace( "'", "%27", $out );
277 $out = str_replace( "\"", "%22", $out );
278 return $out;
279 }
280
281 function wfDemungeQuotes( $in )
282 {
283 $out = str_replace( "%22", "\"", $in );
284 $out = str_replace( "%27", "'", $out );
285 $out = str_replace( "%25", "%", $out );
286 return $out;
287 }
288
289 function wfCleanQueryVar( $var )
290 {
291 global $wgLang;
292 if ( get_magic_quotes_gpc() ) {
293 $var = stripslashes( $var );
294 }
295 return $wgLang->recodeInput( $var );
296 }
297
298 function wfSpecialPage()
299 {
300 global $wgUser, $wgOut, $wgTitle, $wgLang;
301
302 /* FIXME: this list probably shouldn't be language-specific, per se */
303 $validSP = $wgLang->getValidSpecialPages();
304 $sysopSP = $wgLang->getSysopSpecialPages();
305 $devSP = $wgLang->getDeveloperSpecialPages();
306
307 $wgOut->setArticleFlag( false );
308 $wgOut->setRobotpolicy( "noindex,follow" );
309
310 $par = NULL;
311 list($t, $par) = split( "/", $wgTitle->getDBkey(), 2 );
312
313 if ( array_key_exists( $t, $validSP ) ||
314 ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) ||
315 ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) {
316 if($par !== NULL)
317 $wgTitle = Title::makeTitle( Namespace::getSpecial(), $t );
318
319 $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) );
320
321 $inc = "Special" . $t . ".php";
322 include_once( $inc );
323 $call = "wfSpecial" . $t;
324 $call( $par );
325 } else if ( array_key_exists( $t, $sysopSP ) ) {
326 $wgOut->sysopRequired();
327 } else if ( array_key_exists( $t, $devSP ) ) {
328 $wgOut->developerRequired();
329 } else {
330 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
331 }
332 }
333
334 function wfSearch( $s )
335 {
336 $se = new SearchEngine( wfCleanQueryVar( $s ) );
337 $se->showResults();
338 }
339
340 function wfGo( $s )
341 { # pick the nearest match
342 $se = new SearchEngine( wfCleanQueryVar( $s ) );
343 $se->goResult();
344 }
345
346 # Just like exit() but makes a note of it.
347 function wfAbruptExit(){
348 static $called = false;
349 if ( $called ){
350 exit();
351 }
352 $called = true;
353
354 if( function_exists( "debug_backtrace" ) ){ // PHP >= 4.3
355 $bt = debug_backtrace();
356 for($i = 0; $i < count($bt) ; $i++){
357 $file = $bt[$i]["file"];
358 $line = $bt[$i]["line"];
359 wfDebug("WARNING: Abrupt exit in $file at line $line\n");
360 }
361 } else {
362 wfDebug("WARNING: Abrupt exit\n");
363 }
364 exit();
365 }
366
367 function wfNumberOfArticles()
368 {
369 global $wgNumberOfArticles;
370
371 wfLoadSiteStats();
372 return $wgNumberOfArticles;
373 }
374
375 /* private */ function wfLoadSiteStats()
376 {
377 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
378 if ( -1 != $wgNumberOfArticles ) return;
379
380 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
381 "FROM site_stats WHERE ss_row_id=1";
382 $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" );
383
384 if ( 0 == wfNumRows( $res ) ) { return; }
385 else {
386 $s = wfFetchObject( $res );
387 $wgTotalViews = $s->ss_total_views;
388 $wgTotalEdits = $s->ss_total_edits;
389 $wgNumberOfArticles = $s->ss_good_articles;
390 }
391 }
392
393 function wfEscapeHTML( $in )
394 {
395 return str_replace(
396 array( "&", "\"", ">", "<" ),
397 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
398 $in );
399 }
400
401 function wfEscapeHTMLTagsOnly( $in ) {
402 return str_replace(
403 array( "\"", ">", "<" ),
404 array( "&quot;", "&gt;", "&lt;" ),
405 $in );
406 }
407
408 function wfUnescapeHTML( $in )
409 {
410 $in = str_replace( "&lt;", "<", $in );
411 $in = str_replace( "&gt;", ">", $in );
412 $in = str_replace( "&quot;", "\"", $in );
413 $in = str_replace( "&amp;", "&", $in );
414 return $in;
415 }
416
417 function wfImageDir( $fname )
418 {
419 global $wgUploadDirectory;
420
421 $hash = md5( $fname );
422 $oldumask = umask(0);
423 $dest = $wgUploadDirectory . "/" . $hash{0};
424 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
425 $dest .= "/" . substr( $hash, 0, 2 );
426 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
427
428 umask( $oldumask );
429 return $dest;
430 }
431
432 function wfImageArchiveDir( $fname )
433 {
434 global $wgUploadDirectory;
435
436 $hash = md5( $fname );
437 $oldumask = umask(0);
438 $archive = "{$wgUploadDirectory}/archive";
439 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
440 $archive .= "/" . $hash{0};
441 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
442 $archive .= "/" . substr( $hash, 0, 2 );
443 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
444
445 umask( $oldumask );
446 return $archive;
447 }
448
449 function wfRecordUpload( $name, $oldver, $size, $desc )
450 {
451 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
452 global $wgUseCopyrightUpload , $wpUploadCopyStatus , $wpUploadSource ;
453
454 $fname = "wfRecordUpload";
455
456 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
457 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
458 $res = wfQuery( $sql, DB_READ, $fname );
459
460 $now = wfTimestampNow();
461 $won = wfInvertTimestamp( $now );
462 $size = IntVal( $size );
463
464 if ( $wgUseCopyrightUpload )
465 {
466 $textdesc = "== " . wfMsg ( "filedesc" ) . " ==\n" . $desc . "\n" .
467 "== " . wfMsg ( "filestatus" ) . " ==\n" . $wpUploadCopyStatus . "\n" .
468 "== " . wfMsg ( "filesource" ) . " ==\n" . $wpUploadSource ;
469 }
470 else $textdesc = $desc ;
471
472 $now = wfTimestampNow();
473 $won = wfInvertTimestamp( $now );
474
475 if ( 0 == wfNumRows( $res ) ) {
476 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
477 "img_description,img_user,img_user_text) VALUES ('" .
478 wfStrencode( $name ) . "',$size,'{$now}','" .
479 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
480 "', '" . wfStrencode( $wgUser->getName() ) . "')";
481 wfQuery( $sql, DB_WRITE, $fname );
482
483 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
484 Namespace::getImage() . " AND cur_title='" .
485 wfStrencode( $name ) . "'";
486 $res = wfQuery( $sql, DB_READ, $fname );
487 if ( 0 == wfNumRows( $res ) ) {
488 $common =
489 Namespace::getImage() . ",'" .
490 wfStrencode( $name ) . "','" .
491 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
492 wfStrencode( $wgUser->getName() ) . "','" . $now .
493 "',1";
494 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
495 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
496 "cur_text,inverse_timestamp,cur_touched) VALUES (" .
497 $common .
498 ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')";
499 wfQuery( $sql, DB_WRITE, $fname );
500 $id = wfInsertId() or 0; # We should throw an error instead
501 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
502 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
503 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
504 wfQuery( $sql, DB_WRITE, $fname );
505 $u = new SearchUpdate( $id, $name, $desc );
506 $u->doUpdate();
507 }
508 } else {
509 $s = wfFetchObject( $res );
510
511 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
512 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
513 wfStrencode( $s->img_name ) . "','" .
514 wfStrencode( $oldver ) .
515 "',{$s->img_size},'{$s->img_timestamp}','" .
516 wfStrencode( $s->img_description ) . "','" .
517 wfStrencode( $s->img_user ) . "','" .
518 wfStrencode( $s->img_user_text) . "')";
519 wfQuery( $sql, DB_WRITE, $fname );
520
521 $sql = "UPDATE image SET img_size={$size}," .
522 "img_timestamp='" . wfTimestampNow() . "',img_user='" .
523 $wgUser->getID() . "',img_user_text='" .
524 wfStrencode( $wgUser->getName() ) . "', img_description='" .
525 wfStrencode( $desc ) . "' WHERE img_name='" .
526 wfStrencode( $name ) . "'";
527 wfQuery( $sql, DB_WRITE, $fname );
528
529 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" .
530 Namespace::getImage() . " AND cur_title='" .
531 wfStrencode( $name ) . "'";
532 wfQuery( $sql, DB_WRITE, $fname );
533 }
534
535 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
536 $da = wfMsg( "uploadedimage", "[[:" . $wgLang->getNsText(
537 Namespace::getImage() ) . ":{$name}|{$name}]]" );
538 $ta = wfMsg( "uploadedimage", $name );
539 $log->addEntry( $da, $desc, $ta );
540 }
541
542
543 /* Some generic result counters, pulled out of SearchEngine */
544
545 function wfShowingResults( $offset, $limit )
546 {
547 return wfMsg( "showingresults", $limit, $offset+1 );
548 }
549
550 function wfShowingResultsNum( $offset, $limit, $num )
551 {
552 return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
553 }
554
555 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
556 {
557 global $wgUser;
558 $prev = wfMsg( "prevn", $limit );
559 $next = wfMsg( "nextn", $limit );
560
561 $sk = $wgUser->getSkin();
562 if ( 0 != $offset ) {
563 $po = $offset - $limit;
564 if ( $po < 0 ) { $po = 0; }
565 $q = "limit={$limit}&offset={$po}";
566 if ( "" != $query ) { $q .= "&{$query}"; }
567 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
568 } else { $plink = $prev; }
569
570 $no = $offset + $limit;
571 $q = "limit={$limit}&offset={$no}";
572 if ( "" != $query ) { $q .= "&{$query}"; }
573
574 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
575 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
576 wfNumLink( $offset, 50, $link, $query ) . " | " .
577 wfNumLink( $offset, 100, $link, $query ) . " | " .
578 wfNumLink( $offset, 250, $link, $query ) . " | " .
579 wfNumLink( $offset, 500, $link, $query );
580
581 return wfMsg( "viewprevnext", $plink, $nlink, $nums );
582 }
583
584 function wfNumLink( $offset, $limit, $link, $query = "" )
585 {
586 global $wgUser;
587 if ( "" == $query ) { $q = ""; }
588 else { $q = "{$query}&"; }
589 $q .= "limit={$limit}&offset={$offset}";
590
591 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
592 return $s;
593 }
594
595 function wfClientAcceptsGzip() {
596 global $wgUseGzip;
597 if( $wgUseGzip ) {
598 # FIXME: we may want to blacklist some broken browsers
599 if( preg_match(
600 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
601 $_SERVER["HTTP_ACCEPT_ENCODING"],
602 $m ) ) {
603 if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
604 wfDebug( " accepts gzip\n" );
605 return true;
606 }
607 }
608 return false;
609 }
610
611 # Yay, more global functions!
612 function wfCheckLimits( $deflimit = 50, $optionname = "rclimit" ) {
613 global $wgUser;
614
615 $limit = (int)$_REQUEST['limit'];
616 if( $limit < 0 ) $limit = 0;
617 if( ( $limit == 0 ) && ( $optionname != "" ) ) {
618 $limit = (int)$wgUser->getOption( $optionname );
619 }
620 if( $limit <= 0 ) $limit = $deflimit;
621 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
622
623 $offset = (int)$_REQUEST['offset'];
624 $offset = (int)$offset;
625 if( $offset < 0 ) $offset = 0;
626 if( $offset > 65000 ) $offset = 65000; # do we need a max? what?
627
628 return array( $limit, $offset );
629 }
630
631 # Escapes the given text so that it may be output using addWikiText()
632 # without any linking, formatting, etc. making its way through. This
633 # is achieved by substituting certain characters with HTML entities.
634 # As required by the callers, <nowiki> is not used. It currently does
635 # not filter out characters which have special meaning only at the
636 # start of a line, such as "*".
637 function wfEscapeWikiText( $text )
638 {
639 $text = str_replace(
640 array( '[', '|', "'", 'ISBN ' , '://' , "\n=" ),
641 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
642 htmlspecialchars($text) );
643 return $text;
644 }
645
646 function wfQuotedPrintable( $string, $charset = "" )
647 {
648 # Probably incomplete; see RFC 2045
649 if( empty( $charset ) ) {
650 global $wgInputEncoding;
651 $charset = $wgInputEncoding;
652 }
653 $charset = strtoupper( $charset );
654 $charset = str_replace( "ISO-8859", "ISO8859", $charset ); // ?
655
656 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
657 $replace = $illegal . '\t ?_';
658 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
659 $out = "=?$charset?Q?";
660 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
661 $out .= "?=";
662 return $out;
663 }
664
665 # Changes the first character to an HTML entity
666 function wfHtmlEscapeFirst( $text ) {
667 $ord = ord($text);
668 $newText = substr($text, 1);
669 return "&#$ord;$newText";
670 }
671
672 function wfSetVar( &$dest, $source )
673 {
674 $temp = $dest;
675 $dest = $source;
676 return $temp;
677 }
678
679 function &wfSetRef( &$dest, $source )
680 {
681 $temp =& $dest;
682 $dest =& $source;
683 return $temp;
684 }
685
686 ?>