follow-up r62498 — use the new functionality of requireOnlyOneParameter of allowing...
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
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
15 *
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
20 */
21 function js_unescape( $source, $iconv_to = 'UTF-8' ) {
22 $decodedStr = '';
23 $pos = 0;
24 $len = strlen ( $source );
25
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;
43 }
44 } else {
45 $decodedStr .= $charAt;
46 $pos++;
47 }
48 }
49
50 if ( $iconv_to != "UTF-8" ) {
51 $decodedStr = iconv( "UTF-8", $iconv_to, $decodedStr );
52 }
53
54 return $decodedStr;
55 }
56
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
60 *
61 * @param $num Integer
62 * @return utf8char
63 */
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 '';
74 }
75
76 /**
77 * Called for AJAX watch/unwatch requests.
78 * @param $pagename Prefixed title string for page to watch/unwatch
79 * @param $watch String 'w' to watch, 'u' to unwatch
80 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
81 * respectively, followed by an HTML message to display in the alert box; or
82 * '<err#>' on error
83 */
84 function wfAjaxWatch( $pagename = "", $watch = "" ) {
85 if ( wfReadOnly() ) {
86 // redirect to action=(un)watch, which will display the database lock
87 // message
88 return '<err#>';
89 }
90
91 if ( 'w' !== $watch && 'u' !== $watch ) {
92 return '<err#>';
93 }
94 $watch = 'w' === $watch;
95
96 $title = Title::newFromDBkey( $pagename );
97 if ( !$title ) {
98 // Invalid title
99 return '<err#>';
100 }
101 $article = new Article( $title );
102 $watching = $title->userIsWatching();
103
104 if ( $watch ) {
105 if ( !$watching ) {
106 $dbw = wfGetDB( DB_MASTER );
107 $dbw->begin();
108 $ok = $article->doWatch();
109 $dbw->commit();
110 }
111 } else {
112 if ( $watching ) {
113 $dbw = wfGetDB( DB_MASTER );
114 $dbw->begin();
115 $ok = $article->doUnwatch();
116 $dbw->commit();
117 }
118 }
119 // Something stopped the change
120 if ( isset( $ok ) && !$ok ) {
121 return '<err#>';
122 }
123 if ( $watch ) {
124 return '<w#>' . wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
125 } else {
126 return '<u#>' . wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
127 }
128 }
129
130 /**
131 * Called in some places (currently just extensions)
132 * to get the thumbnail URL for a given file at a given resolution.
133 */
134 function wfAjaxGetThumbnailUrl( $file, $width, $height ) {
135 $file = wfFindFile( $file );
136
137 if ( !$file || !$file->exists() )
138 return null;
139
140 $url = $file->getThumbnail( $width, $height )->url;
141
142 return $url;
143 }
144
145 /**
146 * Called in some places (currently just extensions)
147 * to get the URL for a given file.
148 */
149 function wfAjaxGetFileUrl( $file ) {
150 $file = wfFindFile( $file );
151
152 if ( !$file || !$file->exists() )
153 return null;
154
155 $url = $file->getUrl();
156
157 return $url;
158 }