Various cleanups to ExternalEdit. Documentation, code style, removed some unused...
[mediawiki.git] / includes / AjaxFunctions.php
blobd3173e4edc8b78a586fd24bdbd611808fe8237a1
1 <?php
2 /**
3 * Handler functions for Ajax requests
5 * @file
6 * @ingroup Ajax
7 */
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 die( 1 );
13 /**
14 * Function converts an Javascript escaped string back into a string with
15 * specified charset (default is UTF-8).
16 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
18 * @param $source String escaped with Javascript's escape() function
19 * @param $iconv_to String destination character set will be used as second parameter
20 * in the iconv function. Default is UTF-8.
21 * @return string
23 function js_unescape( $source, $iconv_to = 'UTF-8' ) {
24 $decodedStr = '';
25 $pos = 0;
26 $len = strlen ( $source );
28 while ( $pos < $len ) {
29 $charAt = substr ( $source, $pos, 1 );
30 if ( $charAt == '%' ) {
31 $pos++;
32 $charAt = substr ( $source, $pos, 1 );
34 if ( $charAt == 'u' ) {
35 // we got a unicode character
36 $pos++;
37 $unicodeHexVal = substr ( $source, $pos, 4 );
38 $unicode = hexdec ( $unicodeHexVal );
39 $decodedStr .= codepointToUtf8( $unicode );
40 $pos += 4;
41 } else {
42 // we have an escaped ascii character
43 $hexVal = substr ( $source, $pos, 2 );
44 $decodedStr .= chr ( hexdec ( $hexVal ) );
45 $pos += 2;
47 } else {
48 $decodedStr .= $charAt;
49 $pos++;
53 if ( $iconv_to != "UTF-8" ) {
54 $decodedStr = iconv( "utf-8", $iconv_to, $decodedStr );
57 return $decodedStr;