API
authorYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 18 Oct 2006 05:27:43 +0000 (05:27 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 18 Oct 2006 05:27:43 +0000 (05:27 +0000)
* Removed slow result SanitizeData
* Fixed watchlist feed bug (reported by nickj)
* Fixed HTML formatting bug (reported & fixed by nickj)
* clarified HTML intro message

12 files changed:
includes/api/ApiBase.php
includes/api/ApiFeedWatchlist.php
includes/api/ApiFormatBase.php
includes/api/ApiMain.php
includes/api/ApiOpenSearch.php
includes/api/ApiQuery.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiQueryRevisions.php
includes/api/ApiQuerySiteinfo.php
includes/api/ApiQueryWatchlist.php
includes/api/ApiResult.php

index 2bed8a8..b37ed66 100644 (file)
@@ -166,7 +166,7 @@ abstract class ApiBase {
                        $paramsDescription = $this->getParamDescription();
                        $msg = '';
                        $paramPrefix = "\n" . str_repeat(' ', 19);
-                       foreach ($params as $paramName => $paramSettings) {
+                       foreach ($params as $paramName => $paramSettings) {
                                $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';
                                if (is_array($desc))
                                        $desc = implode($paramPrefix, $desc);
index c6dd72d..5a40bc2 100644 (file)
@@ -49,7 +49,8 @@ class ApiFeedWatchlist extends ApiBase {
                        'meta' => 'siteinfo',
                        'siprop' => 'general',
                        'list' => 'watchlist',
-                       'wlstart' => wfTimestamp(TS_MW, time() - intval( 3 * 86400 )), // limit to 3 days
+                       'wlprop' => 'user|comment|timestamp',
+                       'wlstart' => wfTimestamp(TS_MW, time() - intval( 1 * 86400 )), // limit to 1 day
                        'wllimit' => 50
                ));
 
@@ -57,22 +58,20 @@ class ApiFeedWatchlist extends ApiBase {
                $module = new ApiMain($params);
                $module->execute();
 
-               // Get clean data
-               $result = & $module->getResult();
-               $result->SanitizeData();
-               $data = & $result->GetData();
+               // Get data array
+               $data = & $module->getResultData();
 
                $feedItems = array ();
                foreach ($data['query']['watchlist'] as $index => $info) {
                        $title = $info['title'];
                        $titleUrl = Title :: newFromText($title)->getFullUrl();
-                       $feedItems[] = new FeedItem($title, '', $titleUrl, $info['timestamp'], $info['user']);
+                       $feedItems[] = new FeedItem($title, $info['comment'], $titleUrl, $info['timestamp'], $info['user']);
                }
 
                global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
                $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
                $feedUrl = Title :: makeTitle(NS_SPECIAL, 'Watchlist')->getFullUrl();
-               $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist!', $feedUrl);
+               $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist (TODO)!', $feedUrl);
 
                ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
        }
index aba3109..3075fbc 100644 (file)
@@ -98,9 +98,10 @@ abstract class ApiFormatBase extends ApiBase {
 ?>
                        <br/>
                        <small>
-                       This result is being shown in <?=$this->mFormat?> format,
-                       which might not be suitable for your application.<br/>
-                       See <a href='api.php'>API help</a> for more information.<br/>
+                       You are looking at the HTML representation of the <?=$this->mFormat?> format.<br/>
+                       HTML is good for debugging, but probably not suitable for your application.<br/>
+                       Please see "format" parameter documentation at the <a href='api.php'>API help</a>
+                       for more information.<br/>
                        </small>
 <?php
 
@@ -143,7 +144,7 @@ abstract class ApiFormatBase extends ApiBase {
                // encode all tags as safe blue strings
                $text = ereg_replace('\<([^>]+)\>', '<font color=blue>&lt;\1&gt;</font>', $text);
                // identify URLs
-               $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '<a href="\\0">\\0</a>', $text);
+               $text = ereg_replace("[a-zA-Z]+://[^ '\"()<\n]+", '<a href="\\0">\\0</a>', $text);
                // identify requests to api.php
                $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
                // make strings inside * bold
index 3f1ae8b..078b0ad 100644 (file)
@@ -136,6 +136,8 @@ class ApiMain extends ApiBase {
                        // Printer may not be initialized if the extractRequestParams() fails for the main module
                        if (!isset ($this->mPrinter)) {
                                $this->mPrinter = $this->createPrinterByName(self :: API_DEFAULT_FORMAT);
+                               if ($this->mPrinter->getNeedsRawData())
+                                       $this->getResult()->setRawMode();
                        }
                        
                        if ($e instanceof UsageException) {
@@ -167,8 +169,8 @@ class ApiMain extends ApiBase {
 
                        // Reset and print just the error message
                        ob_clean();
-                       $this->mResult->Reset();
-                       $this->mResult->addValue(null, 'error', $errMessage);
+                       $this->getResult()->reset();
+                       $this->getResult()->addValue(null, 'error', $errMessage);
 
                        // If the error occured during printing, do a printer->profileOut()
                        $this->mPrinter->safeProfileOut();
@@ -193,11 +195,13 @@ class ApiMain extends ApiBase {
                        
                        // See if custom printer is used
                        $this->mPrinter = $module->getCustomPrinter();                          
-                       
                        if (is_null($this->mPrinter)) {
                                // Create an appropriate printer
                                $this->mPrinter = $this->createPrinterByName($format);
                        }
+                       
+                       if ($this->mPrinter->getNeedsRawData())
+                               $this->getResult()->setRawMode();
                }
                
                // Execute
@@ -218,8 +222,6 @@ class ApiMain extends ApiBase {
                $printer = $this->mPrinter;
                $printer->profileIn();
                $printer->initPrinter($isError);
-               if (!$printer->getNeedsRawData())
-                       $this->getResult()->SanitizeData();
                $printer->execute();
                $printer->closePrinter();
                $printer->profileOut();
index 2778037..77481d2 100644 (file)
@@ -57,9 +57,7 @@ class ApiOpenSearch extends ApiBase {
                $module->execute();
                
                // Get clean data
-               $result =& $module->getResult();
-               $result->SanitizeData();
-               $data =& $result->GetData();
+               $data =& $module->getResultData();
                
                // Reformat useful data for future printing by JSON engine
                $srchres = array();
index e08eaca..93d60ae 100644 (file)
@@ -167,6 +167,7 @@ class ApiQuery extends ApiBase {
        private function outputGeneralPageInfo() {
 
                $pageSet = $this->getPageSet();
+               $result = $this->getResult();
 
                // Title normalizations
                $normValues = array ();
@@ -178,8 +179,8 @@ class ApiQuery extends ApiBase {
                }
 
                if (!empty ($normValues)) {
-                       ApiResult :: setIndexedTagName($normValues, 'n');
-                       $this->getResult()->addValue('query', 'normalized', $normValues);
+                       $result->setIndexedTagName($normValues, 'n');
+                       $result->addValue('query', 'normalized', $normValues);
                }
 
                // Show redirect information
@@ -192,8 +193,8 @@ class ApiQuery extends ApiBase {
                }
 
                if (!empty ($redirValues)) {
-                       ApiResult :: setIndexedTagName($redirValues, 'r');
-                       $this->getResult()->addValue('query', 'redirects', $redirValues);
+                       $result->setIndexedTagName($redirValues, 'r');
+                       $result->addValue('query', 'redirects', $redirValues);
                }
 
 
@@ -208,8 +209,8 @@ class ApiQuery extends ApiBase {
                                        'revid' => $revid
                                );
                        }
-                       ApiResult :: setIndexedTagName($revids, 'rev');
-                       $this->getResult()->addValue('query', 'badrevids', $revids);
+                       $result->setIndexedTagName($revids, 'rev');
+                       $result->addValue('query', 'badrevids', $revids);
                }
                
                //
@@ -239,8 +240,8 @@ class ApiQuery extends ApiBase {
                }
 
                if (!empty ($pages)) {
-                       ApiResult :: setIndexedTagName($pages, 'page');
-                       $this->getResult()->addValue('query', 'pages', $pages);
+                       $result->setIndexedTagName($pages, 'page');
+                       $result->addValue('query', 'pages', $pages);
                }
        }
 
index 5e01222..e1bc280 100644 (file)
@@ -118,8 +118,9 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                $db->freeResult($res);
 
                if (is_null($resultPageSet)) {
-                       ApiResult :: setIndexedTagName($data, 'p');
-                       $this->getResult()->addValue('query', $this->getModuleName(), $data);
+                       $result = $this->getResult();
+                       $result->setIndexedTagName($data, 'p');
+                       $result->addValue('query', $this->getModuleName(), $data);
                }
        }
 
index 7da11b0..a6f88c3 100644 (file)
@@ -130,7 +130,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                                }
                                
                                if(!empty($params)) {
-                                       ApiResult :: setIndexedTagName($params, 'param');
+                                       $this->getResult()->setIndexedTagName($params, 'param');
                                        $vals = array_merge($vals, $params);
                                }
                        }
@@ -143,7 +143,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                }
                $db->freeResult($res);
 
-               ApiResult :: setIndexedTagName($data, 'item');
+               $this->getResult()->setIndexedTagName($data, 'item');
                $this->getResult()->addValue('query', $this->getModuleName(), $data);
        }
 
index 729b623..5fc5222 100644 (file)
@@ -225,10 +225,11 @@ class ApiQueryRevisions extends ApiQueryBase {
                $db->freeResult($res);
 
                // Ensure that all revisions are shown as '<r>' elements
-               $data = & $this->getResultData();
+               $result = $this->getResult();
+               $data = & $result->getData();
                foreach ($data['query']['pages'] as & $page) {
                        if (is_array($page) && array_key_exists('revisions', $page)) {
-                               ApiResult :: setIndexedTagName($page['revisions'], 'rev');
+                               $result->setIndexedTagName($page['revisions'], 'rev');
                        }
                }
        }
index 3bd1cbd..a49182b 100644 (file)
@@ -65,7 +65,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                                                );
                                                ApiResult :: setContent($data[$ns], $title);
                                        }
-                                       ApiResult :: setIndexedTagName($data, 'ns');
+                                       $this->getResult()->setIndexedTagName($data, 'ns');
                                        $this->getResult()->addValue('query', $p, $data);
                                        break;
 
index 6b1a48f..4524784 100644 (file)
@@ -195,7 +195,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                $db->freeResult($res);
 
                if (is_null($resultPageSet)) {
-                       ApiResult :: setIndexedTagName($data, 'item');
+                       $this->getResult()->setIndexedTagName($data, 'item');
                        $this->getResult()->addValue('query', $this->getModuleName(), $data);
                }
                elseif ($allrev) {
index 7f0a1fb..6bee415 100644 (file)
@@ -31,19 +31,28 @@ if (!defined('MEDIAWIKI')) {
 
 class ApiResult extends ApiBase {
 
-       private $mData;
+       private $mData, $mNeedsRaw;
 
        /**
        * Constructor
        */
        public function __construct($main) {
                parent :: __construct($main, 'result');
-               $this->Reset();
+               $this->mNeedsRaw = false;
+               $this->reset();
        }
 
-       public function Reset() {
+       public function reset() {
                $this->mData = array ();
        }
+       
+       /**
+        * Call this function when special elements such as '_element' 
+        * are needed by the formatter, for example in XML printing. 
+        */
+       public function setRawMode() {
+               $this->mNeedsRaw = true;
+       }
 
        function & getData() {
                return $this->mData;
@@ -97,10 +106,13 @@ class ApiResult extends ApiBase {
         * In case the array contains indexed values (in addition to named),
         * all indexed values will have the given tag name.
         */
-       public static function setIndexedTagName(& $arr, $tag) {
-               // Do not use setElement() as it is ok to call this more than once
+       public function setIndexedTagName(& $arr, $tag) {
+               // In raw mode, add the '_element', otherwise just ignore
+               if (!$this->mNeedsRaw)
+                       return;
                if ($arr === null || $tag === null || !is_array($arr) || is_array($tag))
                        ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
+               // Do not use setElement() as it is ok to call this more than once
                $arr['_element'] = $tag;
        }
 
@@ -130,26 +142,6 @@ class ApiResult extends ApiBase {
                ApiResult :: setElement($data, $name, $value);
        }
 
-       /**
-       * Recursivelly removes any elements from the array that begin with an '_'.
-       * The content element '*' is the only special element that is left.
-       * Use this method when the entire data object gets sent to the user.
-       */
-       public function SanitizeData() {
-               ApiResult :: SanitizeDataInt($this->mData);
-       }
-
-       private static function SanitizeDataInt(& $data) {
-               foreach ($data as $key => & $value) {
-                       if ($key[0] === '_') {
-                               unset ($data[$key]);
-                       }
-                       elseif (is_array($value)) {
-                               ApiResult :: SanitizeDataInt($value);
-                       }
-               }
-       }
-
        public function execute() {
                ApiBase :: dieDebug(__METHOD__, 'execute() is not supported on Result object');
        }