* API: Watchlist feed allows 'hours' parameter of how many hours to go back
authorYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 19 May 2007 18:08:36 +0000 (18:08 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 19 May 2007 18:08:36 +0000 (18:08 +0000)
RELEASE-NOTES
includes/api/ApiBase.php
includes/api/ApiFeedWatchlist.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryBacklinks.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryRevisions.php
includes/api/ApiQueryUserContributions.php
includes/api/ApiQueryWatchlist.php

index 1854489..0522900 100644 (file)
@@ -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 ==
 
index 0450f88..9b7b78b 100644 (file)
@@ -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)
index 55b5681..598e95d 100644 (file)
@@ -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'
                );
        }
 
index 5e2fb0a..9d52cb1 100644 (file)
@@ -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
                        )
                );
index c3d5042..518041e 100644 (file)
@@ -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
                        )
                );
index 3142c9e..69af898 100644 (file)
@@ -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
                        )
                );
index cec0fb5..a66788c 100644 (file)
@@ -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
                        )
                );
index ef5d794..bf9d6d1 100644 (file)
@@ -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 (
index e928698..75a7edd 100644 (file)
@@ -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 (
index 1b7b72d..66d53e7 100644 (file)
@@ -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 (