API *
authorYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 18 Oct 2006 23:49:09 +0000 (23:49 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 18 Oct 2006 23:49:09 +0000 (23:49 +0000)
* OpenSearch support for namespaces
* Minor watchlist feed cleanup

includes/api/ApiFeedWatchlist.php
includes/api/ApiOpenSearch.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryRevisions.php

index 6dd5f98..b26948f 100644 (file)
@@ -43,6 +43,9 @@ class ApiFeedWatchlist extends ApiBase {
                $feedformat = null;
                extract($this->extractRequestParams());
 
+               // limit to 1 day
+               $startTime = wfTimestamp(TS_MW, time() - intval(1 * 86400));
+
                // Prepare nested request
                $params = new FauxRequest(array (
                        'action' => 'query',
@@ -50,10 +53,10 @@ class ApiFeedWatchlist extends ApiBase {
                        'siprop' => 'general',
                        'list' => 'watchlist',
                        'wlprop' => 'user|comment|timestamp',
-                       'wlstart' => wfTimestamp(TS_MW, time() - intval( 1 * 86400 )), // limit to 1 day
+                       'wlstart' => $startTime,
                        'wllimit' => 50
                ));
-               
+
                // Execute
                $module = new ApiMain($params);
                $module->execute();
@@ -63,20 +66,34 @@ class ApiFeedWatchlist extends ApiBase {
 
                $feedItems = array ();
                foreach ($data['query']['watchlist'] as $index => $info) {
-                       $title = $info['title'];
-                       $titleUrl = Title :: newFromText($title)->getFullUrl();
-                       $feedItems[] = new FeedItem($title, $info['comment'], $titleUrl, $info['timestamp'], $info['user']);
+                       $feedItems[] = $this->createFeedItem($info);
                }
 
                global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
                $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
                $feedUrl = Title :: makeTitle(NS_SPECIAL, 'Watchlist')->getFullUrl();
-               $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist (TODO)!', $feedUrl);
+
+               $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
 
                ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
        }
 
-       protected function GetAllowedParams() {
+       private function createFeedItem($info) {
+               global $wgUser;
+
+               $titleStr = $info['title'];
+               $title = Title :: newFromText($titleStr);
+               $titleUrl = $title->getFullUrl();
+               $comment = $info['comment'];
+               $timestamp = $info['timestamp'];
+               $user = $info['user'];
+
+               $completeText = "$comment ($user)";
+
+               return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
+       }
+
+       protected function getAllowedParams() {
                global $wgFeedClasses;
                $feedFormatNames = array_keys($wgFeedClasses);
                return array (
@@ -87,17 +104,17 @@ class ApiFeedWatchlist extends ApiBase {
                );
        }
 
-       protected function GetParamDescription() {
+       protected function getParamDescription() {
                return array (
                        'feedformat' => 'The format of the feed'
                );
        }
 
-       protected function GetDescription() {
+       protected function getDescription() {
                return 'This module returns a watchlist feed';
        }
 
-       protected function GetExamples() {
+       protected function getExamples() {
                return array (
                        'api.php?action=feedwatchlist'
                );
index 77481d2..8b24af9 100644 (file)
@@ -42,54 +42,58 @@ class ApiOpenSearch extends ApiBase {
        public function execute() {
                $search = null;
                extract($this->ExtractRequestParams());
-               
+
+               $title = Title :: newFromText($search);
+               if(!$title)
+                       return; // Return empty result
+                       
                // Prepare nested request
                $params = new FauxRequest(array (
                        'action' => 'query',
                        'list' => 'allpages',
-                       'apnamespace' => 0,
+                       'apnamespace' => $title->getNamespace(),
                        'aplimit' => 10,
-                       'apprefix' => $search
+                       'apprefix' => $title->getDBkey()
                ));
-               
+
                // Execute
                $module = new ApiMain($params);
                $module->execute();
-               
+
                // Get clean data
-               $data =& $module->getResultData();
-               
+               $data = & $module->getResultData();
+
                // Reformat useful data for future printing by JSON engine
-               $srchres = array();
-               foreach ($data['query']['allpages'] as $pageid => &$pageinfo) {
+               $srchres = array ();
+               foreach ($data['query']['allpages'] as $pageid => & $pageinfo) {
                        // Note: this data will no be printable by the xml engine
                        // because it does not support lists of unnamed items
                        $srchres[] = $pageinfo['title'];
                }
-               
+
                // Set top level elements
                $result = $this->getResult();
                $result->addValue(null, 0, $search);
                $result->addValue(null, 1, $srchres);
        }
-       
-       protected function GetAllowedParams() {
+
+       protected function getAllowedParams() {
                return array (
                        'search' => null
                );
        }
 
-       protected function GetParamDescription() {
+       protected function getParamDescription() {
                return array (
                        'search' => 'Search string'
                );
        }
 
-       protected function GetDescription() {
+       protected function getDescription() {
                return 'This module implements OpenSearch protocol';
        }
 
-       protected function GetExamples() {
+       protected function getExamples() {
                return array (
                        'api.php?action=opensearch&search=Te'
                );
index e1bc280..117cfa7 100644 (file)
@@ -59,7 +59,7 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                if (isset ($from)) {
                        $where[] = 'page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from));
                }
-               
+
                if (isset ($prefix)) {
                        $where[] = "page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'";
                }
@@ -108,8 +108,7 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                                        $id = intval($row->page_id);
                                        $data[$id] = array (
                                                'id' => $id,
-                                               'ns' => $title->getNamespace(),
-                                               'title' => $title->getPrefixedText());
+                                       'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText());
                                } else {
                                        $resultPageSet->processDbRow($row);
                                }
@@ -125,27 +124,30 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
        }
 
        protected function getAllowedParams() {
-
+               $namespaces = $this->getQuery()->getValidNamespaces();
                return array (
                        'from' => null,
                        'prefix' => null,
                        'namespace' => array (
                                ApiBase :: PARAM_DFLT => 0,
-                               ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
+                               ApiBase :: PARAM_TYPE => $namespaces
+                       ),
                        'filterredir' => array (
                                ApiBase :: PARAM_DFLT => 'all',
                                ApiBase :: PARAM_TYPE => array (
                                        'all',
                                        'redirects',
                                        'nonredirects'
-                               )),
+                               )
+                       ),
                        'limit' => array (
                                ApiBase :: PARAM_DFLT => 10,
                                ApiBase :: PARAM_TYPE => 'limit',
                                ApiBase :: PARAM_MIN => 1,
                                ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
                                ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
-               ));
+                       )
+               );
        }
 
        protected function getParamDescription() {
index c5655f1..4963927 100644 (file)
@@ -76,13 +76,10 @@ abstract class ApiQueryBase extends ApiBase {
 
        /**
         * This is a very simplistic utility function
-        * to convert a title string to a db key.
-        * It will replace all ' ' with '_', and make first letter uppercase
+        * to convert a non-namespaced title string to a db key.
+        * It will replace all ' ' with '_'
         */
        public static function titleToKey($title) {
-               global $wgContLang, $wgCapitalLinks;
-               if ($wgCapitalLinks)
-                       $title = $wgContLang->ucfirst( $title );
                return str_replace(' ', '_', $title);
        }
 
index 8e075bf..6b51c48 100644 (file)
@@ -142,7 +142,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                                $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
 
                        // must manually initialize unset limit
-                       if (!!is_null($limit))
+                       if (is_null($limit))
                                $limit = 10;
 
                        $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);