* added missing setIndexedTagName for some upload api array results
[lhc/web/wiklou.git] / includes / api / ApiFormatJson.php
index 98c8e72..1d7e3dc 100644 (file)
@@ -29,7 +29,7 @@ if (!defined('MEDIAWIKI')) {
 }
 
 /**
- * @addtogroup API
+ * @ingroup API
  */
 class ApiFormatJson extends ApiFormatBase {
 
@@ -41,12 +41,17 @@ class ApiFormatJson extends ApiFormatBase {
        }
 
        public function getMimeType() {
-               return 'application/json';
+               return 'text/javascript';
        }
 
        public function getNeedsRawData() {
                return $this->mIsRaw;
        }
+       
+       public function getWantsHelp() {
+               // Help is always ugly in JSON
+               return false;
+       }
 
        public function execute() {
                $prefix = $suffix = "";
@@ -58,11 +63,22 @@ class ApiFormatJson extends ApiFormatBase {
                        $suffix = ")";
                }
 
-               if (!function_exists('json_encode') || $this->getIsHtml()) {
+               // Some versions of PHP have a broken json_encode, see PHP bug 
+               // 46944. Test encoding an affected character (U+20000) to 
+               // avoid this.
+               $this->printText( $prefix . $this->getJsonEncode($this->getResultData(),   $this->getIsHtml() )  . $suffix);            
+       }       
+       /*
+        * static to support static calls to json output (instead of json_encode function) 
+        * @param array $results  the results array to output as a json string
+        * @parm isHTML if the output is html
+        */     
+       public static function getJsonEncode($value, $isHtml=false){
+               if (!function_exists('json_encode') || $isHtml || strtolower(json_encode("\xf0\xa0\x80\x80")) != '\ud840\udc00') {
                        $json = new Services_JSON();
-                       $this->printText($prefix . $json->encode($this->getResultData(), $this->getIsHtml()) . $suffix);
+                       return $json->encode($value, $isHtml) ;
                } else {
-                       $this->printText($prefix . json_encode($this->getResultData()) . $suffix);
+                       return json_encode($value);
                }
        }