r17096 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17095‎ | r17096 | r17097 >
Date:23:49, 18 October 2006
Author:yurik
Status:old
Tags:
Comment:
API *
* OpenSearch support for namespaces
* Minor watchlist feed cleanup
Modified paths:
  • /trunk/phase3/includes/api/ApiFeedWatchlist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiOpenSearch.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryAllpages.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryBase.php
@@ -76,13 +76,10 @@
7777
7878 /**
7979 * This is a very simplistic utility function
80 - * to convert a title string to a db key.
81 - * It will replace all ' ' with '_', and make first letter uppercase
 80+ * to convert a non-namespaced title string to a db key.
 81+ * It will replace all ' ' with '_'
8282 */
8383 public static function titleToKey($title) {
84 - global $wgContLang, $wgCapitalLinks;
85 - if ($wgCapitalLinks)
86 - $title = $wgContLang->ucfirst( $title );
8784 return str_replace(' ', '_', $title);
8885 }
8986
Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -142,7 +142,7 @@
143143 $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
144144
145145 // must manually initialize unset limit
146 - if (!!is_null($limit))
 146+ if (is_null($limit))
147147 $limit = 10;
148148
149149 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
Index: trunk/phase3/includes/api/ApiQueryAllpages.php
@@ -59,7 +59,7 @@
6060 if (isset ($from)) {
6161 $where[] = 'page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from));
6262 }
63 -
 63+
6464 if (isset ($prefix)) {
6565 $where[] = "page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'";
6666 }
@@ -108,8 +108,7 @@
109109 $id = intval($row->page_id);
110110 $data[$id] = array (
111111 'id' => $id,
112 - 'ns' => $title->getNamespace(),
113 - 'title' => $title->getPrefixedText());
 112+ 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText());
114113 } else {
115114 $resultPageSet->processDbRow($row);
116115 }
@@ -125,27 +124,30 @@
126125 }
127126
128127 protected function getAllowedParams() {
129 -
 128+ $namespaces = $this->getQuery()->getValidNamespaces();
130129 return array (
131130 'from' => null,
132131 'prefix' => null,
133132 'namespace' => array (
134133 ApiBase :: PARAM_DFLT => 0,
135 - ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
 134+ ApiBase :: PARAM_TYPE => $namespaces
 135+ ),
136136 'filterredir' => array (
137137 ApiBase :: PARAM_DFLT => 'all',
138138 ApiBase :: PARAM_TYPE => array (
139139 'all',
140140 'redirects',
141141 'nonredirects'
142 - )),
 142+ )
 143+ ),
143144 'limit' => array (
144145 ApiBase :: PARAM_DFLT => 10,
145146 ApiBase :: PARAM_TYPE => 'limit',
146147 ApiBase :: PARAM_MIN => 1,
147148 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
148149 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
149 - ));
 150+ )
 151+ );
150152 }
151153
152154 protected function getParamDescription() {
Index: trunk/phase3/includes/api/ApiFeedWatchlist.php
@@ -43,6 +43,9 @@
4444 $feedformat = null;
4545 extract($this->extractRequestParams());
4646
 47+ // limit to 1 day
 48+ $startTime = wfTimestamp(TS_MW, time() - intval(1 * 86400));
 49+
4750 // Prepare nested request
4851 $params = new FauxRequest(array (
4952 'action' => 'query',
@@ -50,10 +53,10 @@
5154 'siprop' => 'general',
5255 'list' => 'watchlist',
5356 'wlprop' => 'user|comment|timestamp',
54 - 'wlstart' => wfTimestamp(TS_MW, time() - intval( 1 * 86400 )), // limit to 1 day
 57+ 'wlstart' => $startTime,
5558 'wllimit' => 50
5659 ));
57 -
 60+
5861 // Execute
5962 $module = new ApiMain($params);
6063 $module->execute();
@@ -63,20 +66,34 @@
6467
6568 $feedItems = array ();
6669 foreach ($data['query']['watchlist'] as $index => $info) {
67 - $title = $info['title'];
68 - $titleUrl = Title :: newFromText($title)->getFullUrl();
69 - $feedItems[] = new FeedItem($title, $info['comment'], $titleUrl, $info['timestamp'], $info['user']);
 70+ $feedItems[] = $this->createFeedItem($info);
7071 }
7172
7273 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
7374 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
7475 $feedUrl = Title :: makeTitle(NS_SPECIAL, 'Watchlist')->getFullUrl();
75 - $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist (TODO)!', $feedUrl);
7676
 77+ $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
 78+
7779 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
7880 }
7981
80 - protected function GetAllowedParams() {
 82+ private function createFeedItem($info) {
 83+ global $wgUser;
 84+
 85+ $titleStr = $info['title'];
 86+ $title = Title :: newFromText($titleStr);
 87+ $titleUrl = $title->getFullUrl();
 88+ $comment = $info['comment'];
 89+ $timestamp = $info['timestamp'];
 90+ $user = $info['user'];
 91+
 92+ $completeText = "$comment ($user)";
 93+
 94+ return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
 95+ }
 96+
 97+ protected function getAllowedParams() {
8198 global $wgFeedClasses;
8299 $feedFormatNames = array_keys($wgFeedClasses);
83100 return array (
@@ -87,17 +104,17 @@
88105 );
89106 }
90107
91 - protected function GetParamDescription() {
 108+ protected function getParamDescription() {
92109 return array (
93110 'feedformat' => 'The format of the feed'
94111 );
95112 }
96113
97 - protected function GetDescription() {
 114+ protected function getDescription() {
98115 return 'This module returns a watchlist feed';
99116 }
100117
101 - protected function GetExamples() {
 118+ protected function getExamples() {
102119 return array (
103120 'api.php?action=feedwatchlist'
104121 );
Index: trunk/phase3/includes/api/ApiOpenSearch.php
@@ -42,54 +42,58 @@
4343 public function execute() {
4444 $search = null;
4545 extract($this->ExtractRequestParams());
46 -
 46+
 47+ $title = Title :: newFromText($search);
 48+ if(!$title)
 49+ return; // Return empty result
 50+
4751 // Prepare nested request
4852 $params = new FauxRequest(array (
4953 'action' => 'query',
5054 'list' => 'allpages',
51 - 'apnamespace' => 0,
 55+ 'apnamespace' => $title->getNamespace(),
5256 'aplimit' => 10,
53 - 'apprefix' => $search
 57+ 'apprefix' => $title->getDBkey()
5458 ));
55 -
 59+
5660 // Execute
5761 $module = new ApiMain($params);
5862 $module->execute();
59 -
 63+
6064 // Get clean data
61 - $data =& $module->getResultData();
62 -
 65+ $data = & $module->getResultData();
 66+
6367 // Reformat useful data for future printing by JSON engine
64 - $srchres = array();
65 - foreach ($data['query']['allpages'] as $pageid => &$pageinfo) {
 68+ $srchres = array ();
 69+ foreach ($data['query']['allpages'] as $pageid => & $pageinfo) {
6670 // Note: this data will no be printable by the xml engine
6771 // because it does not support lists of unnamed items
6872 $srchres[] = $pageinfo['title'];
6973 }
70 -
 74+
7175 // Set top level elements
7276 $result = $this->getResult();
7377 $result->addValue(null, 0, $search);
7478 $result->addValue(null, 1, $srchres);
7579 }
76 -
77 - protected function GetAllowedParams() {
 80+
 81+ protected function getAllowedParams() {
7882 return array (
7983 'search' => null
8084 );
8185 }
8286
83 - protected function GetParamDescription() {
 87+ protected function getParamDescription() {
8488 return array (
8589 'search' => 'Search string'
8690 );
8791 }
8892
89 - protected function GetDescription() {
 93+ protected function getDescription() {
9094 return 'This module implements OpenSearch protocol';
9195 }
9296
93 - protected function GetExamples() {
 97+ protected function getExamples() {
9498 return array (
9599 'api.php?action=opensearch&search=Te'
96100 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r39936Follow-up for r39935: re-fix bug 14651 by making the first letter uppercase i...catrope05:51, 25 August 2008