object-oriented database connections
[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
347 /* private */ $wgAbruptExitCalled = false;
348
349 # Just like exit() but makes a note of it.
350 function wfAbruptExit(){
351 // Safety to avoid infinite recursion in case of (unlikely) bugs somewhere
352 global $wgAbruptExitCalled;
353 if ( $wgAbruptExitCalled ){
354 exit();
355 }
356 $wgAbruptExitCalled = true;
357
358 if( ! function_exists( "debug_backtrace" )){ // for php < 4.3.0
359 wfDebug("WARNING: Abrupt exit from somewhere.\n");
360 exit();
361 }
362
363 wfDebug("WARNING: Abrupt exit. Backtrace follows:\n");
364 $bt = debug_backtrace();
365 for($i = 0; $i < count($bt) ; $i++){
366 $file = $bt[$i]["file"];
367 $line = $bt[$i]["line"];
368 wfDebug("Abrupt exit in $file at line $line\n");
369 }
370 exit();
371 }
372
373 function wfNumberOfArticles()
374 {
375 global $wgNumberOfArticles;
376
377 wfLoadSiteStats();
378 return $wgNumberOfArticles;
379 }
380
381 /* private */ function wfLoadSiteStats()
382 {
383 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
384 if ( -1 != $wgNumberOfArticles ) return;
385
386 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
387 "FROM site_stats WHERE ss_row_id=1";
388 $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" );
389
390 if ( 0 == wfNumRows( $res ) ) { return; }
391 else {
392 $s = wfFetchObject( $res );
393 $wgTotalViews = $s->ss_total_views;
394 $wgTotalEdits = $s->ss_total_edits;
395 $wgNumberOfArticles = $s->ss_good_articles;
396 }
397 }
398
399 function wfEscapeHTML( $in )
400 {
401 return str_replace(
402 array( "&", "\"", ">", "<" ),
403 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
404 $in );
405 }
406
407 function wfEscapeHTMLTagsOnly( $in ) {
408 return str_replace(
409 array( "\"", ">", "<" ),
410 array( "&quot;", "&gt;", "&lt;" ),
411 $in );
412 }
413
414 function wfUnescapeHTML( $in )
415 {
416 $in = str_replace( "&lt;", "<", $in );
417 $in = str_replace( "&gt;", ">", $in );
418 $in = str_replace( "&quot;", "\"", $in );
419 $in = str_replace( "&amp;", "&", $in );
420 return $in;
421 }
422
423 function wfImageDir( $fname )
424 {
425 global $wgUploadDirectory;
426
427 $hash = md5( $fname );
428 $oldumask = umask(0);
429 $dest = $wgUploadDirectory . "/" . $hash{0};
430 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
431 $dest .= "/" . substr( $hash, 0, 2 );
432 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
433
434 umask( $oldumask );
435 return $dest;
436 }
437
438 function wfImageArchiveDir( $fname )
439 {
440 global $wgUploadDirectory;
441
442 $hash = md5( $fname );
443 $oldumask = umask(0);
444 $archive = "{$wgUploadDirectory}/archive";
445 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
446 $archive .= "/" . $hash{0};
447 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
448 $archive .= "/" . substr( $hash, 0, 2 );
449 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
450
451 umask( $oldumask );
452 return $archive;
453 }
454
455 function wfRecordUpload( $name, $oldver, $size, $desc )
456 {
457 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
458 global $wgUseCopyrightUpload , $wpUploadCopyStatus , $wpUploadSource ;
459
460 $fname = "wfRecordUpload";
461
462 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
463 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
464 $res = wfQuery( $sql, DB_READ, $fname );
465
466 $now = wfTimestampNow();
467 $won = wfInvertTimestamp( $now );
468 $size = IntVal( $size );
469
470 if ( $wgUseCopyrightUpload )
471 {
472 $textdesc = "== " . wfMsg ( "filedesc" ) . " ==\n" . $desc . "\n" .
473 "== " . wfMsg ( "filestatus" ) . " ==\n" . $wpUploadCopyStatus . "\n" .
474 "== " . wfMsg ( "filesource" ) . " ==\n" . $wpUploadSource ;
475 }
476 else $textdesc = $desc ;
477
478 $now = wfTimestampNow();
479 $won = wfInvertTimestamp( $now );
480
481 if ( 0 == wfNumRows( $res ) ) {
482 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
483 "img_description,img_user,img_user_text) VALUES ('" .
484 wfStrencode( $name ) . "',$size,'{$now}','" .
485 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
486 "', '" . wfStrencode( $wgUser->getName() ) . "')";
487 wfQuery( $sql, DB_WRITE, $fname );
488
489 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
490 Namespace::getImage() . " AND cur_title='" .
491 wfStrencode( $name ) . "'";
492 $res = wfQuery( $sql, DB_READ, $fname );
493 if ( 0 == wfNumRows( $res ) ) {
494 $common =
495 Namespace::getImage() . ",'" .
496 wfStrencode( $name ) . "','" .
497 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
498 wfStrencode( $wgUser->getName() ) . "','" . $now .
499 "',1";
500 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
501 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
502 "cur_text,inverse_timestamp,cur_touched) VALUES (" .
503 $common .
504 ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')";
505 wfQuery( $sql, DB_WRITE, $fname );
506 $id = wfInsertId() or 0; # We should throw an error instead
507 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
508 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
509 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
510 wfQuery( $sql, DB_WRITE, $fname );
511 $u = new SearchUpdate( $id, $name, $desc );
512 $u->doUpdate();
513 }
514 } else {
515 $s = wfFetchObject( $res );
516
517 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
518 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
519 wfStrencode( $s->img_name ) . "','" .
520 wfStrencode( $oldver ) .
521 "',{$s->img_size},'{$s->img_timestamp}','" .
522 wfStrencode( $s->img_description ) . "','" .
523 wfStrencode( $s->img_user ) . "','" .
524 wfStrencode( $s->img_user_text) . "')";
525 wfQuery( $sql, DB_WRITE, $fname );
526
527 $sql = "UPDATE image SET img_size={$size}," .
528 "img_timestamp='" . wfTimestampNow() . "',img_user='" .
529 $wgUser->getID() . "',img_user_text='" .
530 wfStrencode( $wgUser->getName() ) . "', img_description='" .
531 wfStrencode( $desc ) . "' WHERE img_name='" .
532 wfStrencode( $name ) . "'";
533 wfQuery( $sql, DB_WRITE, $fname );
534
535 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" .
536 Namespace::getImage() . " AND cur_title='" .
537 wfStrencode( $name ) . "'";
538 wfQuery( $sql, DB_WRITE, $fname );
539 }
540
541 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
542 $da = wfMsg( "uploadedimage", "[[:" . $wgLang->getNsText(
543 Namespace::getImage() ) . ":{$name}|{$name}]]" );
544 $ta = wfMsg( "uploadedimage", $name );
545 $log->addEntry( $da, $desc, $ta );
546 }
547
548
549 /* Some generic result counters, pulled out of SearchEngine */
550
551 function wfShowingResults( $offset, $limit )
552 {
553 return wfMsg( "showingresults", $limit, $offset+1 );
554 }
555
556 function wfShowingResultsNum( $offset, $limit, $num )
557 {
558 return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
559 }
560
561 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
562 {
563 global $wgUser;
564 $prev = wfMsg( "prevn", $limit );
565 $next = wfMsg( "nextn", $limit );
566
567 $sk = $wgUser->getSkin();
568 if ( 0 != $offset ) {
569 $po = $offset - $limit;
570 if ( $po < 0 ) { $po = 0; }
571 $q = "limit={$limit}&offset={$po}";
572 if ( "" != $query ) { $q .= "&{$query}"; }
573 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
574 } else { $plink = $prev; }
575
576 $no = $offset + $limit;
577 $q = "limit={$limit}&offset={$no}";
578 if ( "" != $query ) { $q .= "&{$query}"; }
579
580 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
581 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
582 wfNumLink( $offset, 50, $link, $query ) . " | " .
583 wfNumLink( $offset, 100, $link, $query ) . " | " .
584 wfNumLink( $offset, 250, $link, $query ) . " | " .
585 wfNumLink( $offset, 500, $link, $query );
586
587 return wfMsg( "viewprevnext", $plink, $nlink, $nums );
588 }
589
590 function wfNumLink( $offset, $limit, $link, $query = "" )
591 {
592 global $wgUser;
593 if ( "" == $query ) { $q = ""; }
594 else { $q = "{$query}&"; }
595 $q .= "limit={$limit}&offset={$offset}";
596
597 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
598 return $s;
599 }
600
601 function wfClientAcceptsGzip() {
602 global $wgUseGzip;
603 if( $wgUseGzip ) {
604 # FIXME: we may want to blacklist some broken browsers
605 if( preg_match(
606 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
607 $_SERVER["HTTP_ACCEPT_ENCODING"],
608 $m ) ) {
609 if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
610 wfDebug( " accepts gzip\n" );
611 return true;
612 }
613 }
614 return false;
615 }
616
617 # Yay, more global functions!
618 function wfCheckLimits( $deflimit = 50, $optionname = "rclimit" ) {
619 global $wgUser;
620
621 $limit = (int)$_REQUEST['limit'];
622 if( $limit < 0 ) $limit = 0;
623 if( ( $limit == 0 ) && ( $optionname != "" ) ) {
624 $limit = (int)$wgUser->getOption( $optionname );
625 }
626 if( $limit <= 0 ) $limit = $deflimit;
627 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
628
629 $offset = (int)$_REQUEST['offset'];
630 $offset = (int)$offset;
631 if( $offset < 0 ) $offset = 0;
632 if( $offset > 65000 ) $offset = 65000; # do we need a max? what?
633
634 return array( $limit, $offset );
635 }
636
637 # Escapes the given text so that it may be output using addWikiText()
638 # without any linking, formatting, etc. making its way through. This
639 # is achieved by substituting certain characters with HTML entities.
640 # As required by the callers, <nowiki> is not used. It currently does
641 # not filter out characters which have special meaning only at the
642 # start of a line, such as "*".
643 function wfEscapeWikiText( $text )
644 {
645 $text = str_replace(
646 array( '[', '|', "'", 'ISBN ' , '://' , "\n=" ),
647 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
648 htmlspecialchars($text) );
649 return $text;
650 }
651
652 function wfQuotedPrintable( $string, $charset = "" )
653 {
654 # Probably incomplete; see RFC 2045
655 if( empty( $charset ) ) {
656 global $wgInputEncoding;
657 $charset = $wgInputEncoding;
658 }
659 $charset = strtoupper( $charset );
660 $charset = str_replace( "ISO-8859", "ISO8859", $charset ); // ?
661
662 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
663 $replace = $illegal . '\t ?_';
664 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
665 $out = "=?$charset?Q?";
666 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
667 $out .= "?=";
668 return $out;
669 }
670
671 # Changes the first character to an HTML entity
672 function wfHtmlEscapeFirst( $text ) {
673 $ord = ord($text);
674 $newText = substr($text, 1);
675 return "&#$ord;$newText";
676 }
677
678 function wfSetVar( &$dest, $source )
679 {
680 $temp = $dest;
681 $dest = $source;
682 return $temp;
683 }
684
685 function &wfSetRef( &$dest, $source )
686 {
687 $temp =& $dest;
688 $dest =& $source;
689 return $temp;
690 }
691
692 ?>