From aab4c9d2050d0b579405af759a9a5b5911749183 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 19 May 2007 20:26:08 +0000 Subject: [PATCH] API: * breaking change: Converted a map of revisions into a list of revisions to allow easier json processing (no need to know map keys) * html formatting now properly links urls ending with a '\n' string (jsonfm) * regression: fixed allpages to return int instead of string for pageid and ns * Added: info now returns page length, counter, and a new flag --- includes/api/ApiFormatBase.php | 4 ++-- includes/api/ApiMain.php | 15 ++++++++++++--- includes/api/ApiQueryAllpages.php | 6 +++--- includes/api/ApiQueryInfo.php | 13 ++++++++++++- includes/api/ApiQueryRevisions.php | 12 +++++++----- includes/api/ApiResult.php | 8 ++++++-- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 082433887f..e420e204b9 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -149,9 +149,9 @@ for more information. $text = ereg_replace('\<([^>]+)\>', '<\1>', $text); // identify URLs $protos = "http|https|ftp|gopher"; - $text = ereg_replace("($protos)://[^ '\"()<\n]+", '\\0', $text); + $text = ereg_replace("($protos)://[^ \\'\"()<\n]+", '\\0', $text); // identify requests to api.php - $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '\\0', $text); + $text = ereg_replace("api\\.php\\?[^ \\()<\n\t]+", '\\0', $text); // make strings inside * bold $text = ereg_replace("\\*[^<>\n]+\\*", '\\0', $text); // make strings inside $ italic diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index df698fcdf7..234bc527cb 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -29,7 +29,16 @@ if (!defined('MEDIAWIKI')) { } /** - * This is the main API class, used for both external and internal processing. + * This is the main API class, used for both external and internal processing. + * When executed, it will create the requested formatter object, + * instantiate and execute an object associated with the needed action, + * and use formatter to print results. + * In case of an exception, an error message will be printed using the same formatter. + * + * To use API from another application, run it using FauxRequest object, in which + * case any internal exceptions will not be handled but passed up to the caller. + * After successful execution, use getResult() for the resulting data. + * * @addtogroup API */ class ApiMain extends ApiBase { @@ -43,11 +52,11 @@ class ApiMain extends ApiBase { * List of available modules: action name => module class */ private static $Modules = array ( - 'help' => 'ApiHelp', // 'login' => 'ApiLogin', // LOGIN is temporarily disabled until it becomes more secure 'query' => 'ApiQuery', 'opensearch' => 'ApiOpenSearch', - 'feedwatchlist' => 'ApiFeedWatchlist' + 'feedwatchlist' => 'ApiFeedWatchlist', + 'help' => 'ApiHelp', ); /** diff --git a/includes/api/ApiQueryAllpages.php b/includes/api/ApiQueryAllpages.php index 9d52cb115c..a6ad4e89a0 100644 --- a/includes/api/ApiQueryAllpages.php +++ b/includes/api/ApiQueryAllpages.php @@ -92,9 +92,9 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase { if (is_null($resultPageSet)) { $title = Title :: makeTitle($row->page_namespace, $row->page_title); if ($title->userCanRead()) { - $data[intval($row->page_id)] = array( - 'pageid' => $row->page_id, - 'ns' => $title->getNamespace(), + $data[] = array( + 'pageid' => intval($row->page_id), + 'ns' => intval($title->getNamespace()), 'title' => $title->getPrefixedText()); } } else { diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index 3014fec12d..5ed79fe000 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -40,8 +40,11 @@ class ApiQueryInfo extends ApiQueryBase { public function requestExtraData() { $pageSet = $this->getPageSet(); $pageSet->requestField('page_is_redirect'); + $pageSet->requestField('page_is_new'); + $pageSet->requestField('page_counter'); $pageSet->requestField('page_touched'); $pageSet->requestField('page_latest'); + $pageSet->requestField('page_len'); } public function execute() { @@ -51,18 +54,26 @@ class ApiQueryInfo extends ApiQueryBase { $result = $this->getResult(); $pageIsRedir = $pageSet->getCustomField('page_is_redirect'); + $pageIsNew = $pageSet->getCustomField('page_is_new'); + $pageCounter = $pageSet->getCustomField('page_counter'); $pageTouched = $pageSet->getCustomField('page_touched'); $pageLatest = $pageSet->getCustomField('page_latest'); + $pageLength = $pageSet->getCustomField('page_len'); foreach ( $titles as $pageid => $unused ) { $pageInfo = array ( 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]), - 'lastrevid' => intval($pageLatest[$pageid]) + 'lastrevid' => intval($pageLatest[$pageid]), + 'counter' => $pageCounter[$pageid], + 'length' => $pageLength[$pageid], ); if ($pageIsRedir[$pageid]) $pageInfo['redirect'] = ''; + if ($pageIsNew[$pageid]) + $pageInfo['new'] = ''; + $result->addValue(array ( 'query', 'pages' diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index bf9d6d19e4..13ae6e56e0 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -176,11 +176,13 @@ class ApiQueryRevisions extends ApiQueryBase { if ($showContent) ApiResult :: setContent($vals, Revision :: getRevisionText($row)); - $this->getResult()->addValue(array ( - 'query', - 'pages', - intval($row->rev_page - ), 'revisions'), intval($row->rev_id), $vals); + $this->getResult()->addValue( + array ( + 'query', + 'pages', + intval($row->rev_page), + 'revisions'), + null, $vals); } } $db->freeResult($res); diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php index 65f17e8b41..8eaf87c581 100644 --- a/includes/api/ApiResult.php +++ b/includes/api/ApiResult.php @@ -125,7 +125,8 @@ class ApiResult extends ApiBase { /** * Add value to the output data at the given path. * Path is an indexed array, each element specifing the branch at which to add the new value - * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value + * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value + * If $name is empty, the $value is added as a next list element data[] = $value */ public function addValue($path, $name, $value) { @@ -145,7 +146,10 @@ class ApiResult extends ApiBase { } } - ApiResult :: setElement($data, $name, $value); + if (empty($name)) + $data[] = $value; // Add list element + else + ApiResult :: setElement($data, $name, $value); // Add named element } public function execute() { -- 2.20.1