The purpose of this modification is to ensure that every normal request
[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 if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
169 $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR'];
170 if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
171 $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP'];
172 if( !empty( $_SERVER['HTTP_FROM'] ) )
173 $forward .= " from " . $_SERVER['HTTP_FROM'];
174 if( $forward )
175 $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
176 if($wgUser->getId() == 0)
177 $forward .= " anon";
178 $log = sprintf( "%s\t%04.3f\t%s\n",
179 gmdate( "YmdHis" ), $elapsed,
180 urldecode( $_SERVER['REQUEST_URI'] . $forward ) );
181 error_log( "TJOHEJ". $log . $prof, 3, $wgDebugLogFile );
182 }
183 }
184
185
186 function wfReadOnly()
187 {
188 global $wgReadOnlyFile;
189
190 if ( "" == $wgReadOnlyFile ) { return false; }
191 return is_file( $wgReadOnlyFile );
192 }
193
194 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
195
196 # Get a message from anywhere
197 function wfMsg( $key ) {
198 $args = func_get_args();
199 if ( count( $args ) ) {
200 array_shift( $args );
201 }
202 return wfMsgReal( $key, $args, true );
203 }
204
205 # Get a message from the language file
206 function wfMsgNoDB( $key ) {
207 $args = func_get_args();
208 if ( count( $args ) ) {
209 array_shift( $args );
210 }
211 return wfMsgReal( $key, $args, false );
212 }
213
214 # Really get a message
215 function wfMsgReal( $key, $args, $useDB ) {
216 global $wgLang, $wgReplacementKeys, $wgMemc, $wgDBname;
217 global $wgUseDatabaseMessages, $wgUseMemCached, $wgOut;
218 global $wgAllMessagesEn, $wgLanguageCode;
219
220 $fname = "wfMsg";
221 wfProfileIn( $fname );
222
223 static $messageCache = false;
224 $memcKey = "$wgDBname:messages";
225 $fname = "wfMsg";
226 $message = false;
227
228 # newFromText is too slow!
229 $title = ucfirst( $key );
230 if ( $messageCache ) {
231 $message = $messageCache[$title];
232 } elseif ( !$wgUseDatabaseMessages || !$useDB ) {
233 $message = $wgLang->getMessage( $key );
234 }
235
236 if ( !$message && $wgUseMemCached ) {
237 # Try memcached
238 if ( !$messageCache ) {
239 $messageCache = $wgMemc->get( $memcKey );
240 }
241
242 # If there's nothing in memcached, load all the messages from the database
243 # This should only happen on server reset -- ordinary changes should update
244 # memcached in editUpdates()
245 if ( !$messageCache ) {
246 # Other threads don't need to load the messages if another thread is doing it.
247 $wgMemc->set( $memcKey, "loading", time() + 60 );
248 $messageCache = wfLoadAllMessages();
249 # Save in memcached
250 $wgMemc->set( $memcKey, $messageCache, time() + 3600 );
251
252
253 }
254 if ( is_array( $messageCache ) && array_key_exists( $title, $messageCache ) ) {
255 $message = $messageCache[$title];
256 } elseif ( $messageCache == "loading" ) {
257 $messageCache = false;
258 }
259 }
260
261 # If there was no MemCached, load each message from the DB individually
262 if ( !$message ) {
263 if ( $useDB ) {
264 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
265 " AND cur_title='$title'";
266 $res = wfQuery( $sql, DB_READ, $fname );
267
268 if ( wfNumRows( $res ) ) {
269 $obj = wfFetchObject( $res );
270 $message = $obj->cur_text;
271 wfFreeResult( $res );
272 }
273 }
274 }
275
276 # Try the array in $wgLang
277 if ( !$message ) {
278 $message = $wgLang->getMessage( $key );
279 }
280
281 # Try the English array
282 if ( !$message && $wgLanguageCode != "en" ) {
283 $message = Language::getMessage( $key );
284 }
285
286 # Replace arguments
287 if( count( $args ) ) {
288 $message = str_replace( $wgReplacementKeys, $args, $message );
289 }
290 wfProfileOut( $fname );
291 if ( !$message ) {
292 # Failed, message does not exist
293 return "&lt;$key&gt;";
294 }
295 return $message;
296 }
297
298 function wfCleanFormFields( $fields )
299 {
300 global $HTTP_POST_VARS;
301 global $wgInputEncoding, $wgOutputEncoding, $wgEditEncoding, $wgLang;
302
303 if ( get_magic_quotes_gpc() ) {
304 foreach ( $fields as $fname ) {
305 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
306 $HTTP_POST_VARS[$fname] = stripslashes(
307 $HTTP_POST_VARS[$fname] );
308 }
309 global ${$fname};
310 if ( isset( ${$fname} ) ) {
311 ${$fname} = stripslashes( ${$fname} );
312 }
313 }
314 }
315 $enc = $wgOutputEncoding;
316 if( $wgEditEncoding != "") $enc = $wgEditEncoding;
317 if ( $enc != $wgInputEncoding ) {
318 foreach ( $fields as $fname ) {
319 if ( isset( $HTTP_POST_VARS[$fname] ) ) {
320 $HTTP_POST_VARS[$fname] = $wgLang->iconv(
321 $wgOutputEncoding, $wgInputEncoding,
322 $HTTP_POST_VARS[$fname] );
323 }
324 global ${$fname};
325 if ( isset( ${$fname} ) ) {
326 ${$fname} = $wgLang->iconv(
327 $enc, $wgInputEncoding, ${$fname} );
328 }
329 }
330 }
331 }
332
333 function wfMungeQuotes( $in )
334 {
335 $out = str_replace( "%", "%25", $in );
336 $out = str_replace( "'", "%27", $out );
337 $out = str_replace( "\"", "%22", $out );
338 return $out;
339 }
340
341 function wfDemungeQuotes( $in )
342 {
343 $out = str_replace( "%22", "\"", $in );
344 $out = str_replace( "%27", "'", $out );
345 $out = str_replace( "%25", "%", $out );
346 return $out;
347 }
348
349 function wfCleanQueryVar( $var )
350 {
351 global $wgLang;
352 if ( get_magic_quotes_gpc() ) {
353 $var = stripslashes( $var );
354 }
355 return $wgLang->recodeInput( $var );
356 }
357
358 function wfSpecialPage()
359 {
360 global $wgUser, $wgOut, $wgTitle, $wgLang;
361
362 /* FIXME: this list probably shouldn't be language-specific, per se */
363 $validSP = $wgLang->getValidSpecialPages();
364 $sysopSP = $wgLang->getSysopSpecialPages();
365 $devSP = $wgLang->getDeveloperSpecialPages();
366
367 $wgOut->setArticleFlag( false );
368 $wgOut->setRobotpolicy( "noindex,follow" );
369
370 $par = NULL;
371 list($t, $par) = split( "/", $wgTitle->getDBkey(), 2 );
372
373 if ( array_key_exists( $t, $validSP ) ||
374 ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) ||
375 ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) {
376 if($par !== NULL)
377 $wgTitle = Title::makeTitle( Namespace::getSpecial(), $t );
378
379 $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) );
380
381 $inc = "Special" . $t . ".php";
382 include_once( $inc );
383 $call = "wfSpecial" . $t;
384 $call( $par );
385 } else if ( array_key_exists( $t, $sysopSP ) ) {
386 $wgOut->sysopRequired();
387 } else if ( array_key_exists( $t, $devSP ) ) {
388 $wgOut->developerRequired();
389 } else {
390 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
391 }
392 }
393
394 function wfSearch( $s )
395 {
396 $se = new SearchEngine( wfCleanQueryVar( $s ) );
397 $se->showResults();
398 }
399
400 function wfGo( $s )
401 { # pick the nearest match
402 $se = new SearchEngine( wfCleanQueryVar( $s ) );
403 $se->goResult();
404 }
405
406 /* private */ $wgAbruptExitCalled = false;
407
408 # Just like exit() but makes a note of it.
409 function wfAbruptExit(){
410 // Safety to avoid infinite recursion in case of (unlikely) bugs somewhere
411 global $wgAbruptExitCalled;
412 if ( $wgAbruptExitCalled ){
413 exit();
414 }
415 $wgAbruptExitCalled = true;
416
417 $bt = debug_backtrace();
418 for($i = 0; $i < count($bt) ; $i++){
419 $file = $bt[$i]["file"];
420 $line = $bt[$i]["line"];
421 wfDebug("WARNING: Abrupt exit in $file at line $line\n");
422 }
423 exit();
424 }
425
426 function wfNumberOfArticles()
427 {
428 global $wgNumberOfArticles;
429
430 wfLoadSiteStats();
431 return $wgNumberOfArticles;
432 }
433
434 /* private */ function wfLoadSiteStats()
435 {
436 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
437 if ( -1 != $wgNumberOfArticles ) return;
438
439 $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " .
440 "FROM site_stats WHERE ss_row_id=1";
441 $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" );
442
443 if ( 0 == wfNumRows( $res ) ) { return; }
444 else {
445 $s = wfFetchObject( $res );
446 $wgTotalViews = $s->ss_total_views;
447 $wgTotalEdits = $s->ss_total_edits;
448 $wgNumberOfArticles = $s->ss_good_articles;
449 }
450 }
451
452 function wfEscapeHTML( $in )
453 {
454 return str_replace(
455 array( "&", "\"", ">", "<" ),
456 array( "&amp;", "&quot;", "&gt;", "&lt;" ),
457 $in );
458 }
459
460 function wfEscapeHTMLTagsOnly( $in ) {
461 return str_replace(
462 array( "\"", ">", "<" ),
463 array( "&quot;", "&gt;", "&lt;" ),
464 $in );
465 }
466
467 function wfUnescapeHTML( $in )
468 {
469 $in = str_replace( "&lt;", "<", $in );
470 $in = str_replace( "&gt;", ">", $in );
471 $in = str_replace( "&quot;", "\"", $in );
472 $in = str_replace( "&amp;", "&", $in );
473 return $in;
474 }
475
476 function wfImageDir( $fname )
477 {
478 global $wgUploadDirectory;
479
480 $hash = md5( $fname );
481 $oldumask = umask(0);
482 $dest = $wgUploadDirectory . "/" . $hash{0};
483 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
484 $dest .= "/" . substr( $hash, 0, 2 );
485 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
486
487 umask( $oldumask );
488 return $dest;
489 }
490
491 function wfImageArchiveDir( $fname )
492 {
493 global $wgUploadDirectory;
494
495 $hash = md5( $fname );
496 $oldumask = umask(0);
497 $archive = "{$wgUploadDirectory}/archive";
498 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
499 $archive .= "/" . $hash{0};
500 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
501 $archive .= "/" . substr( $hash, 0, 2 );
502 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
503
504 umask( $oldumask );
505 return $archive;
506 }
507
508 function wfRecordUpload( $name, $oldver, $size, $desc )
509 {
510 global $wgUser, $wgLang, $wgTitle, $wgOut, $wgDeferredUpdateList;
511 global $wgUseCopyrightUpload , $wpUploadCopyStatus , $wpUploadSource ;
512
513 $fname = "wfRecordUpload";
514
515 $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," .
516 "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'";
517 $res = wfQuery( $sql, DB_READ, $fname );
518
519 $now = wfTimestampNow();
520 $won = wfInvertTimestamp( $now );
521 $size = IntVal( $size );
522
523 if ( $wgUseCopyrightUpload )
524 {
525 $textdesc = "== " . wfMsg ( "filedesc" ) . " ==\n" . $desc . "\n" .
526 "== " . wfMsg ( "filestatus" ) . " ==\n" . $wpUploadCopyStatus . "\n" .
527 "== " . wfMsg ( "filesource" ) . " ==\n" . $wpUploadSource ;
528 }
529 else $textdesc = $desc ;
530
531 $now = wfTimestampNow();
532 $won = wfInvertTimestamp( $now );
533
534 if ( 0 == wfNumRows( $res ) ) {
535 $sql = "INSERT INTO image (img_name,img_size,img_timestamp," .
536 "img_description,img_user,img_user_text) VALUES ('" .
537 wfStrencode( $name ) . "',$size,'{$now}','" .
538 wfStrencode( $desc ) . "', '" . $wgUser->getID() .
539 "', '" . wfStrencode( $wgUser->getName() ) . "')";
540 wfQuery( $sql, DB_WRITE, $fname );
541
542 $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" .
543 Namespace::getImage() . " AND cur_title='" .
544 wfStrencode( $name ) . "'";
545 $res = wfQuery( $sql, DB_READ, $fname );
546 if ( 0 == wfNumRows( $res ) ) {
547 $common =
548 Namespace::getImage() . ",'" .
549 wfStrencode( $name ) . "','" .
550 wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
551 wfStrencode( $wgUser->getName() ) . "','" . $now .
552 "',1";
553 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
554 "cur_comment,cur_user,cur_user_text,cur_timestamp,cur_is_new," .
555 "cur_text,inverse_timestamp,cur_touched) VALUES (" .
556 $common .
557 ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')";
558 wfQuery( $sql, DB_WRITE, $fname );
559 $id = wfInsertId() or 0; # We should throw an error instead
560 $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
561 rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new,
562 rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')";
563 wfQuery( $sql, DB_WRITE, $fname );
564 $u = new SearchUpdate( $id, $name, $desc );
565 $u->doUpdate();
566 }
567 } else {
568 $s = wfFetchObject( $res );
569
570 $sql = "INSERT INTO oldimage (oi_name,oi_archive_name,oi_size," .
571 "oi_timestamp,oi_description,oi_user,oi_user_text) VALUES ('" .
572 wfStrencode( $s->img_name ) . "','" .
573 wfStrencode( $oldver ) .
574 "',{$s->img_size},'{$s->img_timestamp}','" .
575 wfStrencode( $s->img_description ) . "','" .
576 wfStrencode( $s->img_user ) . "','" .
577 wfStrencode( $s->img_user_text) . "')";
578 wfQuery( $sql, DB_WRITE, $fname );
579
580 $sql = "UPDATE image SET img_size={$size}," .
581 "img_timestamp='" . wfTimestampNow() . "',img_user='" .
582 $wgUser->getID() . "',img_user_text='" .
583 wfStrencode( $wgUser->getName() ) . "', img_description='" .
584 wfStrencode( $desc ) . "' WHERE img_name='" .
585 wfStrencode( $name ) . "'";
586 wfQuery( $sql, DB_WRITE, $fname );
587
588 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" .
589 Namespace::getImage() . " AND cur_title='" .
590 wfStrencode( $name ) . "'";
591 wfQuery( $sql, DB_WRITE, $fname );
592 }
593
594 $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) );
595 $da = wfMsg( "uploadedimage", "[[:" . $wgLang->getNsText(
596 Namespace::getImage() ) . ":{$name}|{$name}]]" );
597 $ta = wfMsg( "uploadedimage", $name );
598 $log->addEntry( $da, $desc, $ta );
599 }
600
601
602 /* Some generic result counters, pulled out of SearchEngine */
603
604 function wfShowingResults( $offset, $limit )
605 {
606 return wfMsg( "showingresults", $limit, $offset+1 );
607 }
608
609 function wfShowingResultsNum( $offset, $limit, $num )
610 {
611 return wfMsg( "showingresultsnum", $limit, $offset+1, $num );
612 }
613
614 function wfViewPrevNext( $offset, $limit, $link, $query = "" )
615 {
616 global $wgUser;
617 $prev = wfMsg( "prevn", $limit );
618 $next = wfMsg( "nextn", $limit );
619
620 $sk = $wgUser->getSkin();
621 if ( 0 != $offset ) {
622 $po = $offset - $limit;
623 if ( $po < 0 ) { $po = 0; }
624 $q = "limit={$limit}&offset={$po}";
625 if ( "" != $query ) { $q .= "&{$query}"; }
626 $plink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$prev}</a>";
627 } else { $plink = $prev; }
628
629 $no = $offset + $limit;
630 $q = "limit={$limit}&offset={$no}";
631 if ( "" != $query ) { $q .= "&{$query}"; }
632
633 $nlink = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$next}</a>";
634 $nums = wfNumLink( $offset, 20, $link , $query ) . " | " .
635 wfNumLink( $offset, 50, $link, $query ) . " | " .
636 wfNumLink( $offset, 100, $link, $query ) . " | " .
637 wfNumLink( $offset, 250, $link, $query ) . " | " .
638 wfNumLink( $offset, 500, $link, $query );
639
640 return wfMsg( "viewprevnext", $plink, $nlink, $nums );
641 }
642
643 function wfNumLink( $offset, $limit, $link, $query = "" )
644 {
645 global $wgUser;
646 if ( "" == $query ) { $q = ""; }
647 else { $q = "{$query}&"; }
648 $q .= "limit={$limit}&offset={$offset}";
649
650 $s = "<a href=\"" . wfLocalUrlE( $link, $q ) . "\">{$limit}</a>";
651 return $s;
652 }
653
654 function wfClientAcceptsGzip() {
655 global $wgUseGzip;
656 if( $wgUseGzip ) {
657 # FIXME: we may want to blacklist some broken browsers
658 if( preg_match(
659 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
660 $_SERVER["HTTP_ACCEPT_ENCODING"],
661 $m ) ) {
662 if( ( $m[1] == "q" ) && ( $m[2] == 0 ) ) return false;
663 wfDebug( " accepts gzip\n" );
664 return true;
665 }
666 }
667 return false;
668 }
669
670 # Yay, more global functions!
671 function wfCheckLimits( $deflimit = 50, $optionname = "rclimit" ) {
672 global $wgUser;
673
674 $limit = (int)$_REQUEST['limit'];
675 if( $limit < 0 ) $limit = 0;
676 if( ( $limit == 0 ) && ( $optionname != "" ) ) {
677 $limit = (int)$wgUser->getOption( $optionname );
678 }
679 if( $limit <= 0 ) $limit = $deflimit;
680 if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
681
682 $offset = (int)$_REQUEST['offset'];
683 $offset = (int)$offset;
684 if( $offset < 0 ) $offset = 0;
685 if( $offset > 65000 ) $offset = 65000; # do we need a max? what?
686
687 return array( $limit, $offset );
688 }
689
690 # Escapes the given text so that it may be output using addWikiText()
691 # without any linking, formatting, etc. making its way through. This
692 # is achieved by substituting certain characters with HTML entities.
693 # As required by the callers, <nowiki> is not used. It currently does
694 # not filter out characters which have special meaning only at the
695 # start of a line, such as "*".
696 function wfEscapeWikiText( $text )
697 {
698 $text = str_replace(
699 array( '[', "'", 'ISBN ' , '://' , "\n=" ),
700 array( '&#91;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
701 htmlspecialchars($text) );
702 return $text;
703 }
704
705 # Loads the entire MediaWiki namespace, returns the array
706 function wfLoadAllMessages()
707 {
708 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
709 $res = wfQuery( $sql, DB_READ, $fname );
710
711 $messages = array();
712 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
713 $messages[$row->cur_title] = $row->cur_text;
714 }
715 wfFreeResult( $res );
716 return $messages;
717 }
718
719 function wfQuotedPrintable( $string, $charset = "" )
720 {
721 # Probably incomplete; see RFC 2045
722 if( empty( $charset ) ) {
723 global $wgInputEncoding;
724 $charset = $wgInputEncoding;
725 }
726 $charset = strtoupper( $charset );
727 $charset = str_replace( "ISO-8859", "ISO8859", $charset ); // ?
728
729 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
730 $replace = $illegal . '\t ?_';
731 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
732 $out = "=?$charset?Q?";
733 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
734 $out .= "?=";
735 return $out;
736 }
737
738
739 ?>