From 550c083a18d0be82ae41db174360c17c687a974e Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 18 Oct 2006 05:27:43 +0000 Subject: [PATCH] API * Removed slow result SanitizeData * Fixed watchlist feed bug (reported by nickj) * Fixed HTML formatting bug (reported & fixed by nickj) * clarified HTML intro message --- includes/api/ApiBase.php | 2 +- includes/api/ApiFeedWatchlist.php | 13 +++++---- includes/api/ApiFormatBase.php | 9 ++++--- includes/api/ApiMain.php | 12 +++++---- includes/api/ApiOpenSearch.php | 4 +-- includes/api/ApiQuery.php | 17 ++++++------ includes/api/ApiQueryAllpages.php | 5 ++-- includes/api/ApiQueryLogEvents.php | 4 +-- includes/api/ApiQueryRevisions.php | 5 ++-- includes/api/ApiQuerySiteinfo.php | 2 +- includes/api/ApiQueryWatchlist.php | 2 +- includes/api/ApiResult.php | 42 ++++++++++++------------------ 12 files changed, 56 insertions(+), 61 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 2bed8a8e52..b37ed66a7d 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -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); diff --git a/includes/api/ApiFeedWatchlist.php b/includes/api/ApiFeedWatchlist.php index c6dd72d404..5a40bc2b7d 100644 --- a/includes/api/ApiFeedWatchlist.php +++ b/includes/api/ApiFeedWatchlist.php @@ -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); } diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index aba3109d8a..3075fbc708 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -98,9 +98,10 @@ abstract class ApiFormatBase extends ApiBase { ?>
- This result is being shown in mFormat?> format, - which might not be suitable for your application.
- See API help for more information.
+ You are looking at the HTML representation of the mFormat?> format.
+ HTML is good for debugging, but probably not suitable for your application.
+ Please see "format" parameter documentation at the API help + for more information.
]+)\>', '<\1>', $text); // identify URLs - $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '\\0', $text); + $text = ereg_replace("[a-zA-Z]+://[^ '\"()<\n]+", '\\0', $text); // identify requests to api.php $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '\\0', $text); // make strings inside * bold diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 3f1ae8b0ca..078b0ad713 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -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(); diff --git a/includes/api/ApiOpenSearch.php b/includes/api/ApiOpenSearch.php index 2778037b9c..77481d2cbd 100644 --- a/includes/api/ApiOpenSearch.php +++ b/includes/api/ApiOpenSearch.php @@ -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(); diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index e08eaca782..93d60aeef1 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -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); } } diff --git a/includes/api/ApiQueryAllpages.php b/includes/api/ApiQueryAllpages.php index 5e01222b84..e1bc280f20 100644 --- a/includes/api/ApiQueryAllpages.php +++ b/includes/api/ApiQueryAllpages.php @@ -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); } } diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index 7da11b02d8..a6f88c390d 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -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); } diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index 729b6237b6..5fc52222a7 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -225,10 +225,11 @@ class ApiQueryRevisions extends ApiQueryBase { $db->freeResult($res); // Ensure that all revisions are shown as '' 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'); } } } diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php index 3bd1cbd7c8..a49182baca 100644 --- a/includes/api/ApiQuerySiteinfo.php +++ b/includes/api/ApiQuerySiteinfo.php @@ -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; diff --git a/includes/api/ApiQueryWatchlist.php b/includes/api/ApiQueryWatchlist.php index 6b1a48fdb0..452478499c 100644 --- a/includes/api/ApiQueryWatchlist.php +++ b/includes/api/ApiQueryWatchlist.php @@ -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) { diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php index 7f0a1fb3e1..6bee415334 100644 --- a/includes/api/ApiResult.php +++ b/includes/api/ApiResult.php @@ -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'); } -- 2.20.1