Patched jquery-1.4.2 to not crash in IE7 when a style property is set with 'null...
[mediawiki.git] / includes / AjaxFunctions.php
blobb564f0a88e87cfa89d4fc69a0d413ddb1e70b1e5
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
11 /**
12 * Function converts an Javascript escaped string back into a string with
13 * specified charset (default is UTF-8).
14 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
16 * @param $source String escaped with Javascript's escape() function
17 * @param $iconv_to String destination character set will be used as second parameter
18 * in the iconv function. Default is UTF-8.
19 * @return string
21 function js_unescape( $source, $iconv_to = 'UTF-8' ) {
22 $decodedStr = '';
23 $pos = 0;
24 $len = strlen ( $source );
26 while ( $pos < $len ) {
27 $charAt = substr ( $source, $pos, 1 );
28 if ( $charAt == '%' ) {
29 $pos++;
30 $charAt = substr ( $source, $pos, 1 );
31 if ( $charAt == 'u' ) {
32 // we got a unicode character
33 $pos++;
34 $unicodeHexVal = substr ( $source, $pos, 4 );
35 $unicode = hexdec ( $unicodeHexVal );
36 $decodedStr .= code2utf( $unicode );
37 $pos += 4;
38 } else {
39 // we have an escaped ascii character
40 $hexVal = substr ( $source, $pos, 2 );
41 $decodedStr .= chr ( hexdec ( $hexVal ) );
42 $pos += 2;
44 } else {
45 $decodedStr .= $charAt;
46 $pos++;
50 if ( $iconv_to != "UTF-8" ) {
51 $decodedStr = iconv( "UTF-8", $iconv_to, $decodedStr );
54 return $decodedStr;
57 /**
58 * Function coverts number of utf char into that character.
59 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
61 * @param $num Integer
62 * @return utf8char
64 function code2utf( $num ) {
65 if ( $num < 128 )
66 return chr( $num );
67 if ( $num < 2048 )
68 return chr( ( $num >> 6 ) + 192 ) . chr( ( $num&63 ) + 128 );
69 if ( $num < 65536 )
70 return chr( ( $num >> 12 ) + 224 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
71 if ( $num < 2097152 )
72 return chr( ( $num >> 18 ) + 240 ) . chr( ( ( $num >> 12 )&63 ) + 128 ) . chr( ( ( $num >> 6 )&63 ) + 128 ) . chr( ( $num&63 ) + 128 );
73 return '';
76 /**
77 * Called in some places (currently just extensions)
78 * to get the thumbnail URL for a given file at a given resolution.
80 function wfAjaxGetThumbnailUrl( $file, $width, $height ) {
81 $file = wfFindFile( $file );
83 if ( !$file || !$file->exists() )
84 return null;
86 $url = $file->getThumbnail( $width, $height )->url;
88 return $url;
91 /**
92 * Called in some places (currently just extensions)
93 * to get the URL for a given file.
95 function wfAjaxGetFileUrl( $file ) {
96 $file = wfFindFile( $file );
98 if ( !$file || !$file->exists() )
99 return null;
101 $url = $file->getUrl();
103 return $url;