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