r17074 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17073‎ | r17074 | r17075 >
Date:05:27, 18 October 2006
Author:yurik
Status:old
Tags:
Comment:
API
* Removed slow result SanitizeData
* Fixed watchlist feed bug (reported by nickj)
* Fixed HTML formatting bug (reported & fixed by nickj)
* clarified HTML intro message
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFeedWatchlist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiOpenSearch.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryAllpages.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryLogEvents.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuerySiteinfo.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryWatchlist.php (modified) (history)
  • /trunk/phase3/includes/api/ApiResult.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -167,6 +167,7 @@
168168 private function outputGeneralPageInfo() {
169169
170170 $pageSet = $this->getPageSet();
 171+ $result = $this->getResult();
171172
172173 // Title normalizations
173174 $normValues = array ();
@@ -178,8 +179,8 @@
179180 }
180181
181182 if (!empty ($normValues)) {
182 - ApiResult :: setIndexedTagName($normValues, 'n');
183 - $this->getResult()->addValue('query', 'normalized', $normValues);
 183+ $result->setIndexedTagName($normValues, 'n');
 184+ $result->addValue('query', 'normalized', $normValues);
184185 }
185186
186187 // Show redirect information
@@ -192,8 +193,8 @@
193194 }
194195
195196 if (!empty ($redirValues)) {
196 - ApiResult :: setIndexedTagName($redirValues, 'r');
197 - $this->getResult()->addValue('query', 'redirects', $redirValues);
 197+ $result->setIndexedTagName($redirValues, 'r');
 198+ $result->addValue('query', 'redirects', $redirValues);
198199 }
199200
200201
@@ -208,8 +209,8 @@
209210 'revid' => $revid
210211 );
211212 }
212 - ApiResult :: setIndexedTagName($revids, 'rev');
213 - $this->getResult()->addValue('query', 'badrevids', $revids);
 213+ $result->setIndexedTagName($revids, 'rev');
 214+ $result->addValue('query', 'badrevids', $revids);
214215 }
215216
216217 //
@@ -239,8 +240,8 @@
240241 }
241242
242243 if (!empty ($pages)) {
243 - ApiResult :: setIndexedTagName($pages, 'page');
244 - $this->getResult()->addValue('query', 'pages', $pages);
 244+ $result->setIndexedTagName($pages, 'page');
 245+ $result->addValue('query', 'pages', $pages);
245246 }
246247 }
247248
Index: trunk/phase3/includes/api/ApiQuerySiteinfo.php
@@ -65,7 +65,7 @@
6666 );
6767 ApiResult :: setContent($data[$ns], $title);
6868 }
69 - ApiResult :: setIndexedTagName($data, 'ns');
 69+ $this->getResult()->setIndexedTagName($data, 'ns');
7070 $this->getResult()->addValue('query', $p, $data);
7171 break;
7272
Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -225,10 +225,11 @@
226226 $db->freeResult($res);
227227
228228 // Ensure that all revisions are shown as '<r>' elements
229 - $data = & $this->getResultData();
 229+ $result = $this->getResult();
 230+ $data = & $result->getData();
230231 foreach ($data['query']['pages'] as & $page) {
231232 if (is_array($page) && array_key_exists('revisions', $page)) {
232 - ApiResult :: setIndexedTagName($page['revisions'], 'rev');
 233+ $result->setIndexedTagName($page['revisions'], 'rev');
233234 }
234235 }
235236 }
Index: trunk/phase3/includes/api/ApiBase.php
@@ -166,7 +166,7 @@
167167 $paramsDescription = $this->getParamDescription();
168168 $msg = '';
169169 $paramPrefix = "\n" . str_repeat(' ', 19);
170 - foreach ($params as $paramName => & $paramSettings) {
 170+ foreach ($params as $paramName => $paramSettings) {
171171 $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';
172172 if (is_array($desc))
173173 $desc = implode($paramPrefix, $desc);
Index: trunk/phase3/includes/api/ApiResult.php
@@ -31,19 +31,28 @@
3232
3333 class ApiResult extends ApiBase {
3434
35 - private $mData;
 35+ private $mData, $mNeedsRaw;
3636
3737 /**
3838 * Constructor
3939 */
4040 public function __construct($main) {
4141 parent :: __construct($main, 'result');
42 - $this->Reset();
 42+ $this->mNeedsRaw = false;
 43+ $this->reset();
4344 }
4445
45 - public function Reset() {
 46+ public function reset() {
4647 $this->mData = array ();
4748 }
 49+
 50+ /**
 51+ * Call this function when special elements such as '_element'
 52+ * are needed by the formatter, for example in XML printing.
 53+ */
 54+ public function setRawMode() {
 55+ $this->mNeedsRaw = true;
 56+ }
4857
4958 function & getData() {
5059 return $this->mData;
@@ -97,10 +106,13 @@
98107 * In case the array contains indexed values (in addition to named),
99108 * all indexed values will have the given tag name.
100109 */
101 - public static function setIndexedTagName(& $arr, $tag) {
102 - // Do not use setElement() as it is ok to call this more than once
 110+ public function setIndexedTagName(& $arr, $tag) {
 111+ // In raw mode, add the '_element', otherwise just ignore
 112+ if (!$this->mNeedsRaw)
 113+ return;
103114 if ($arr === null || $tag === null || !is_array($arr) || is_array($tag))
104115 ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
 116+ // Do not use setElement() as it is ok to call this more than once
105117 $arr['_element'] = $tag;
106118 }
107119
@@ -130,26 +142,6 @@
131143 ApiResult :: setElement($data, $name, $value);
132144 }
133145
134 - /**
135 - * Recursivelly removes any elements from the array that begin with an '_'.
136 - * The content element '*' is the only special element that is left.
137 - * Use this method when the entire data object gets sent to the user.
138 - */
139 - public function SanitizeData() {
140 - ApiResult :: SanitizeDataInt($this->mData);
141 - }
142 -
143 - private static function SanitizeDataInt(& $data) {
144 - foreach ($data as $key => & $value) {
145 - if ($key[0] === '_') {
146 - unset ($data[$key]);
147 - }
148 - elseif (is_array($value)) {
149 - ApiResult :: SanitizeDataInt($value);
150 - }
151 - }
152 - }
153 -
154146 public function execute() {
155147 ApiBase :: dieDebug(__METHOD__, 'execute() is not supported on Result object');
156148 }
Index: trunk/phase3/includes/api/ApiQueryAllpages.php
@@ -118,8 +118,9 @@
119119 $db->freeResult($res);
120120
121121 if (is_null($resultPageSet)) {
122 - ApiResult :: setIndexedTagName($data, 'p');
123 - $this->getResult()->addValue('query', $this->getModuleName(), $data);
 122+ $result = $this->getResult();
 123+ $result->setIndexedTagName($data, 'p');
 124+ $result->addValue('query', $this->getModuleName(), $data);
124125 }
125126 }
126127
Index: trunk/phase3/includes/api/ApiFormatBase.php
@@ -98,9 +98,10 @@
9999 ?>
100100 <br/>
101101 <small>
102 - This result is being shown in <?=$this->mFormat?> format,
103 - which might not be suitable for your application.<br/>
104 - See <a href='api.php'>API help</a> for more information.<br/>
 102+ You are looking at the HTML representation of the <?=$this->mFormat?> format.<br/>
 103+ HTML is good for debugging, but probably not suitable for your application.<br/>
 104+ Please see "format" parameter documentation at the <a href='api.php'>API help</a>
 105+ for more information.<br/>
105106 </small>
106107 <?php
107108
@@ -143,7 +144,7 @@
144145 // encode all tags as safe blue strings
145146 $text = ereg_replace('\<([^>]+)\>', '<font color=blue>&lt;\1&gt;</font>', $text);
146147 // identify URLs
147 - $text = ereg_replace("[a-zA-Z]+://[^ '()<\n]+", '<a href="\\0">\\0</a>', $text);
 148+ $text = ereg_replace("[a-zA-Z]+://[^ '\"()<\n]+", '<a href="\\0">\\0</a>', $text);
148149 // identify requests to api.php
149150 $text = ereg_replace("api\\.php\\?[^ ()<\n\t]+", '<a href="\\0">\\0</a>', $text);
150151 // make strings inside * bold
Index: trunk/phase3/includes/api/ApiMain.php
@@ -136,6 +136,8 @@
137137 // Printer may not be initialized if the extractRequestParams() fails for the main module
138138 if (!isset ($this->mPrinter)) {
139139 $this->mPrinter = $this->createPrinterByName(self :: API_DEFAULT_FORMAT);
 140+ if ($this->mPrinter->getNeedsRawData())
 141+ $this->getResult()->setRawMode();
140142 }
141143
142144 if ($e instanceof UsageException) {
@@ -167,8 +169,8 @@
168170
169171 // Reset and print just the error message
170172 ob_clean();
171 - $this->mResult->Reset();
172 - $this->mResult->addValue(null, 'error', $errMessage);
 173+ $this->getResult()->reset();
 174+ $this->getResult()->addValue(null, 'error', $errMessage);
173175
174176 // If the error occured during printing, do a printer->profileOut()
175177 $this->mPrinter->safeProfileOut();
@@ -193,11 +195,13 @@
194196
195197 // See if custom printer is used
196198 $this->mPrinter = $module->getCustomPrinter();
197 -
198199 if (is_null($this->mPrinter)) {
199200 // Create an appropriate printer
200201 $this->mPrinter = $this->createPrinterByName($format);
201202 }
 203+
 204+ if ($this->mPrinter->getNeedsRawData())
 205+ $this->getResult()->setRawMode();
202206 }
203207
204208 // Execute
@@ -218,8 +222,6 @@
219223 $printer = $this->mPrinter;
220224 $printer->profileIn();
221225 $printer->initPrinter($isError);
222 - if (!$printer->getNeedsRawData())
223 - $this->getResult()->SanitizeData();
224226 $printer->execute();
225227 $printer->closePrinter();
226228 $printer->profileOut();
Index: trunk/phase3/includes/api/ApiFeedWatchlist.php
@@ -49,7 +49,8 @@
5050 'meta' => 'siteinfo',
5151 'siprop' => 'general',
5252 'list' => 'watchlist',
53 - 'wlstart' => wfTimestamp(TS_MW, time() - intval( 3 * 86400 )), // limit to 3 days
 53+ 'wlprop' => 'user|comment|timestamp',
 54+ 'wlstart' => wfTimestamp(TS_MW, time() - intval( 1 * 86400 )), // limit to 1 day
5455 'wllimit' => 50
5556 ));
5657
@@ -57,22 +58,20 @@
5859 $module = new ApiMain($params);
5960 $module->execute();
6061
61 - // Get clean data
62 - $result = & $module->getResult();
63 - $result->SanitizeData();
64 - $data = & $result->GetData();
 62+ // Get data array
 63+ $data = & $module->getResultData();
6564
6665 $feedItems = array ();
6766 foreach ($data['query']['watchlist'] as $index => $info) {
6867 $title = $info['title'];
6968 $titleUrl = Title :: newFromText($title)->getFullUrl();
70 - $feedItems[] = new FeedItem($title, '', $titleUrl, $info['timestamp'], $info['user']);
 69+ $feedItems[] = new FeedItem($title, $info['comment'], $titleUrl, $info['timestamp'], $info['user']);
7170 }
7271
7372 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
7473 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
7574 $feedUrl = Title :: makeTitle(NS_SPECIAL, 'Watchlist')->getFullUrl();
76 - $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist!', $feedUrl);
 75+ $feed = new $wgFeedClasses[$feedformat] ($feedTitle, '!Watchlist (TODO)!', $feedUrl);
7776
7877 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
7978 }
Index: trunk/phase3/includes/api/ApiOpenSearch.php
@@ -57,9 +57,7 @@
5858 $module->execute();
5959
6060 // Get clean data
61 - $result =& $module->getResult();
62 - $result->SanitizeData();
63 - $data =& $result->GetData();
 61+ $data =& $module->getResultData();
6462
6563 // Reformat useful data for future printing by JSON engine
6664 $srchres = array();
Index: trunk/phase3/includes/api/ApiQueryWatchlist.php
@@ -195,7 +195,7 @@
196196 $db->freeResult($res);
197197
198198 if (is_null($resultPageSet)) {
199 - ApiResult :: setIndexedTagName($data, 'item');
 199+ $this->getResult()->setIndexedTagName($data, 'item');
200200 $this->getResult()->addValue('query', $this->getModuleName(), $data);
201201 }
202202 elseif ($allrev) {
Index: trunk/phase3/includes/api/ApiQueryLogEvents.php
@@ -130,7 +130,7 @@
131131 }
132132
133133 if(!empty($params)) {
134 - ApiResult :: setIndexedTagName($params, 'param');
 134+ $this->getResult()->setIndexedTagName($params, 'param');
135135 $vals = array_merge($vals, $params);
136136 }
137137 }
@@ -143,7 +143,7 @@
144144 }
145145 $db->freeResult($res);
146146
147 - ApiResult :: setIndexedTagName($data, 'item');
 147+ $this->getResult()->setIndexedTagName($data, 'item');
148148 $this->getResult()->addValue('query', $this->getModuleName(), $data);
149149 }
150150