Combine JavaScript and JSON encoding logic
[lhc/web/wiklou.git] / includes / api / ApiFormatJson.php
index 19c5e5f..342a580 100644 (file)
@@ -56,36 +56,38 @@ class ApiFormatJson extends ApiFormatBase {
        }
 
        public function execute() {
-               $prefix = $suffix = '';
-
                $params = $this->extractRequestParams();
+               $json = FormatJson::encode(
+                       $this->getResultData(),
+                       $this->getIsHtml(),
+                       $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK
+               );
                $callback = $params['callback'];
-               if ( !is_null( $callback ) ) {
-                       $prefix = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback ) . '(';
-                       $suffix = ')';
+               if ( $callback !== null ) {
+                       $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback );
+                       $this->printText( "$callback($json)" );
+               } else {
+                       $this->printText( $json );
                }
-               $this->printText(
-                       $prefix .
-                       FormatJson::encode( $this->getResultData(), $this->getIsHtml() ) .
-                       $suffix
-               );
        }
 
        public function getAllowedParams() {
                return array(
-                       'callback'  => null,
+                       'callback' => null,
+                       'utf8' => false,
                );
        }
 
        public function getParamDescription() {
                return array(
                        'callback' => 'If specified, wraps the output into a given function call. For safety, all user-specific data will be restricted.',
+                       'utf8' => 'If specified, encodes most (but not all) non-ASCII characters as UTF-8 instead of replacing them with hexadecimal escape sequences.',
                );
        }
 
        public function getDescription() {
                if ( $this->mIsRaw ) {
-                       return 'Output data with the debuging elements in JSON format' . parent::getDescription();
+                       return 'Output data with the debugging elements in JSON format' . parent::getDescription();
                } else {
                        return 'Output data in JSON format' . parent::getDescription();
                }