From fea603827172bf1e0953f5f844049080d2dc17f7 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 19 May 2007 18:08:36 +0000 Subject: [PATCH] * API: Watchlist feed allows 'hours' parameter of how many hours to go back --- RELEASE-NOTES | 6 ++- includes/api/ApiBase.php | 53 ++++++++++++++++++---- includes/api/ApiFeedWatchlist.php | 34 ++++++++------ includes/api/ApiQueryAllpages.php | 2 +- includes/api/ApiQueryBacklinks.php | 2 +- includes/api/ApiQueryLogEvents.php | 2 +- includes/api/ApiQueryRecentChanges.php | 2 +- includes/api/ApiQueryRevisions.php | 2 +- includes/api/ApiQueryUserContributions.php | 2 +- includes/api/ApiQueryWatchlist.php | 2 +- 10 files changed, 76 insertions(+), 31 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1854489e2c..052290042d 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -77,13 +77,15 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * New properties: links, templates, images, langlinks, categories, external links * Breaking Change: imagelinks renamed into imageusage (il->iu) * Bug fix: incorrect generator behavior in some cases +* JSON format allows an optional callback function to wrap the result. * Login module disabled until a more secure solution can be implemented * (bug 9938) Querying by revision identifier returns the most recent revision for the corresponding page, rather than the requested revision * (bug 8772) Filter page revision queries by user * (bug 9927) User contributions queries do not accept IP addresses -* Watchlist now reports a proper feed item when the user is not logged in -* Watchlist date bug fixed - automatically shows one last day +* Watchlist Feed now reports a proper feed item when the user is not logged in +* Watchlist Feed date bug fixed - automatically shows one last day +* Watchlist Feed now allows to specify number of hours to monitor == Maintenance script changes since 1.10 == diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 0450f8849a..9b7b78b702 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -43,7 +43,7 @@ abstract class ApiBase { const PARAM_DFLT = 0; const PARAM_ISMULTI = 1; const PARAM_TYPE = 2; - const PARAM_MAX1 = 3; + const PARAM_MAX = 3; const PARAM_MAX2 = 4; const PARAM_MIN = 5; @@ -201,12 +201,33 @@ abstract class ApiBase { if (is_array($type)) { $desc .= $paramPrefix . $prompt . implode(', ', $type); - } - elseif ($type == 'namespace') { - // Special handling because namespaces are type-limited, yet they are not given - $desc .= $paramPrefix . $prompt . implode(', ', ApiBase :: getValidNamespaces()); + } else { + switch ($type) { + case 'namespace': + // Special handling because namespaces are type-limited, yet they are not given + $desc .= $paramPrefix . $prompt . implode(', ', ApiBase :: getValidNamespaces()); + break; + case 'limit': + $desc .= $paramPrefix . "No more than {$paramSettings[self :: PARAM_MAX]} ({$paramSettings[self :: PARAM_MAX2]} for bots) allowed."; + break; + case 'integer': + $hasMin = isset($paramSettings[self :: PARAM_MIN]); + $hasMax = isset($paramSettings[self :: PARAM_MAX]); + if ($hasMin || $hasMax) { + if (!$hasMax) + $intRangeStr = "The value must be no less than {$paramSettings[self :: PARAM_MIN]}"; + elseif (!$hasMin) + $intRangeStr = "The value must be no more than {$paramSettings[self :: PARAM_MAX]}"; + else + $intRangeStr = "The value must be between {$paramSettings[self :: PARAM_MIN]} and {$paramSettings[self :: PARAM_MAX]}"; + + $desc .= $paramPrefix . $intRangeStr; + } + break; + } } } + $default = is_array($paramSettings) ? (isset ($paramSettings[self :: PARAM_DFLT]) ? $paramSettings[self :: PARAM_DFLT] : null) : $paramSettings; if (!is_null($default) && $default !== false) @@ -348,17 +369,33 @@ abstract class ApiBase { break; case 'string' : // nothing to do break; - case 'integer' : // Force everything using intval() + case 'integer' : // Force everything using intval() and optionally validate limits + $value = is_array($value) ? array_map('intval', $value) : intval($value); + $checkMin = isset ($paramSettings[self :: PARAM_MIN]); + $checkMax = isset ($paramSettings[self :: PARAM_MAX]); + + if ($checkMin || $checkMax) { + $min = $checkMin ? $paramSettings[self :: PARAM_MIN] : false; + $max = $checkMax ? $paramSettings[self :: PARAM_MAX] : false; + + $values = is_array($value) ? $value : array($value); + foreach ($values as $v) { + if ($checkMin && $v < $checkMin) + $this->dieUsage("$paramName may not be less than $min (set to $v)", $paramName); + if ($checkMax && $v > $checkMax) + $this->dieUsage("$paramName may not be over $max (set to $v)", $paramName); + } + } break; case 'limit' : - if (!isset ($paramSettings[self :: PARAM_MAX1]) || !isset ($paramSettings[self :: PARAM_MAX2])) + if (!isset ($paramSettings[self :: PARAM_MAX]) || !isset ($paramSettings[self :: PARAM_MAX2])) ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $paramName"); if ($multi) ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0; $value = intval($value); - $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX1], $paramSettings[self :: PARAM_MAX2]); + $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]); break; case 'boolean' : if ($multi) diff --git a/includes/api/ApiFeedWatchlist.php b/includes/api/ApiFeedWatchlist.php index 55b568116c..598e95de24 100644 --- a/includes/api/ApiFeedWatchlist.php +++ b/includes/api/ApiFeedWatchlist.php @@ -42,26 +42,27 @@ class ApiFeedWatchlist extends ApiBase { } public function execute() { - $feedformat = null; - extract($this->extractRequestParams()); - - // limit to 1 day going from now back - $endTime = wfTimestamp(TS_MW, time() - intval(1 * 24 * 60 * 60)); + $params = $this->extractRequestParams(); + + // limit to the number of hours going from now back + $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60)); // Prepare nested request - $params = new FauxRequest(array ( + $fauxReq = new FauxRequest(array ( 'action' => 'query', 'meta' => 'siteinfo', 'siprop' => 'general', 'list' => 'watchlist', 'wlprop' => 'user|comment|timestamp', - 'wldir' => 'older', - 'wlend' => $endTime, + 'wldir' => 'older', // reverse order - from newest to oldest + 'wlend' => $endTime, // stop at this time 'wllimit' => 50 )); // Execute - $module = new ApiMain($params); + $module = new ApiMain($fauxReq); + + global $wgFeedClasses, $wgSitename, $wgContLanguageCode; try { $module->execute(); @@ -74,11 +75,10 @@ class ApiFeedWatchlist extends ApiBase { $feedItems[] = $this->createFeedItem($info); } - global $wgFeedClasses, $wgSitename, $wgContLanguageCode; $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']'; $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl(); - $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl); + $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl); ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems); @@ -87,11 +87,10 @@ class ApiFeedWatchlist extends ApiBase { // Error results should not be cached $this->getMain()->setCacheMaxAge(0); - global $wgFeedClasses, $wgSitename, $wgContLanguageCode; $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']'; $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl(); - $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl); + $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl); if ($e instanceof UsageException) { @@ -127,13 +126,20 @@ class ApiFeedWatchlist extends ApiBase { 'feedformat' => array ( ApiBase :: PARAM_DFLT => 'rss', ApiBase :: PARAM_TYPE => $feedFormatNames + ), + 'hours' => array ( + ApiBase :: PARAM_DFLT => 24, + ApiBase :: PARAM_TYPE => 'integer', + ApiBase :: PARAM_MIN => 1, + ApiBase :: PARAM_MAX => 72, ) ); } protected function getParamDescription() { return array ( - 'feedformat' => 'The format of the feed' + 'feedformat' => 'The format of the feed', + 'hours' => 'List pages modified within this many hours from now' ); } diff --git a/includes/api/ApiQueryAllpages.php b/includes/api/ApiQueryAllpages.php index 5e2fb0abf6..9d52cb115c 100644 --- a/includes/api/ApiQueryAllpages.php +++ b/includes/api/ApiQueryAllpages.php @@ -130,7 +130,7 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ) ); diff --git a/includes/api/ApiQueryBacklinks.php b/includes/api/ApiQueryBacklinks.php index c3d5042323..518041eb78 100644 --- a/includes/api/ApiQueryBacklinks.php +++ b/includes/api/ApiQueryBacklinks.php @@ -306,7 +306,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ) ); diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index 3142c9e096..69af898f1c 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -140,7 +140,7 @@ class ApiQueryLogEvents extends ApiQueryBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ) ); diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php index cec0fb5e46..a66788cd66 100644 --- a/includes/api/ApiQueryRecentChanges.php +++ b/includes/api/ApiQueryRecentChanges.php @@ -151,7 +151,7 @@ class ApiQueryRecentChanges extends ApiQueryBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ) ); diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index ef5d794ed4..bf9d6d19e4 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -211,7 +211,7 @@ class ApiQueryRevisions extends ApiQueryBase { 'limit' => array ( ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_SML1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_SML1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2 ), 'startid' => array ( diff --git a/includes/api/ApiQueryUserContributions.php b/includes/api/ApiQueryUserContributions.php index e92869845e..75a7edd703 100644 --- a/includes/api/ApiQueryUserContributions.php +++ b/includes/api/ApiQueryUserContributions.php @@ -127,7 +127,7 @@ class ApiQueryContributions extends ApiQueryBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ), 'start' => array ( diff --git a/includes/api/ApiQueryWatchlist.php b/includes/api/ApiQueryWatchlist.php index 1b7b72da67..66d53e7ad7 100644 --- a/includes/api/ApiQueryWatchlist.php +++ b/includes/api/ApiQueryWatchlist.php @@ -191,7 +191,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase { ApiBase :: PARAM_DFLT => 10, ApiBase :: PARAM_TYPE => 'limit', ApiBase :: PARAM_MIN => 1, - ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 ), 'prop' => array ( -- 2.20.1