* API: JSON format now allows callback parameter
authorYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 15 May 2007 18:45:20 +0000 (18:45 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 15 May 2007 18:45:20 +0000 (18:45 +0000)
includes/api/ApiFormatJson.php

index 99979d2..8184c8c 100644 (file)
@@ -49,14 +49,35 @@ class ApiFormatJson extends ApiFormatBase {
        }
 
        public function execute() {
+               $prefix = $suffix = "";
+
+               $params = $this->extractRequestParams();
+               $callback = $params['callback'];
+               if(!is_null($callback)) {
+                       $prefix = ereg_replace("[^_A-Za-z0-9]", "", $callback ) . "(";
+                       $suffix = ")";
+               }
+
                if (!function_exists('json_encode') || $this->getIsHtml()) {
                        $json = new Services_JSON();
-                       $this->printText($json->encode($this->getResultData(), $this->getIsHtml()));
+                       $this->printText($prefix . $json->encode($this->getResultData(), $this->getIsHtml()) . $suffix);
                } else {
-                       $this->printText(json_encode($this->getResultData()));
+                       $this->printText($prefix . json_encode($this->getResultData()) . $suffix);
                }
        }
 
+       protected function getAllowedParams() {
+               return array (
+                       'callback' => null
+               );
+       }
+
+       protected function getParamDescription() {
+               return array (
+                       'callback' => 'If specified, wraps the output into a given function call',
+               );
+       }
+
        protected function getDescription() {
                if ($this->mIsRaw)
                        return 'Output data with the debuging elements in JSON format' . parent :: getDescription();