r17020 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17019‎ | r17020 | r17021 >
Date:07:43, 15 October 2006
Author:yurik
Status:old
Tags:
Comment:
* API query optimizations
* API allow modules to have custom printers
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatJson.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatXml.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFormatYaml.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiOpenSearch.php (modified) (history)
  • /trunk/phase3/includes/api/ApiPageSet.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/ApiFormatXml.php
@@ -43,7 +43,7 @@
4444 return true;
4545 }
4646
47 - public function executePrinter() {
 47+ public function execute() {
4848 $xmlindent = null;
4949 extract($this->extractRequestParams());
5050
Index: trunk/phase3/includes/api/ApiBase.php
@@ -91,6 +91,15 @@
9292 }
9393
9494 /**
 95+ * If the module may only be used with a certain format module,
 96+ * it should override this method to return an instance of that formatter.
 97+ * A value of null means the default format will be used.
 98+ */
 99+ public function getCustomFormatModule() {
 100+ return null;
 101+ }
 102+
 103+ /**
95104 * Generates help message for this module, or false if there is no description
96105 */
97106 public function makeHelpMsg() {
Index: trunk/phase3/includes/api/ApiFormatYaml.php
@@ -39,7 +39,7 @@
4040 return 'application/yaml';
4141 }
4242
43 - public function executePrinter() {
 43+ public function execute() {
4444 $this->printText(Spyc :: YAMLDump($this->getResultData()));
4545 }
4646
Index: trunk/phase3/includes/api/ApiResult.php
@@ -105,7 +105,7 @@
106106
107107 $data = & $this->getData();
108108
109 - if (isset ($path)) {
 109+ if (!is_null($path)) {
110110 if (is_array($path)) {
111111 foreach ($path as $p) {
112112 if (!isset ($data[$p]))
Index: trunk/phase3/includes/api/ApiFormatJson.php
@@ -39,7 +39,7 @@
4040 return 'application/json';
4141 }
4242
43 - public function executePrinter() {
 43+ public function execute() {
4444 $json = new Services_JSON();
4545 $this->printText($json->encode($this->getResultData(), true));
4646 }
Index: trunk/phase3/includes/api/ApiFormatBase.php
@@ -54,15 +54,6 @@
5555 */
5656 public abstract function getMimeType();
5757
58 - public function execute() {
59 - ApiBase :: dieDebug(__METHOD__, 'This is not an executable module');
60 - }
61 -
62 - /**
63 - * Format modules must override this method to implement actual printing
64 - */
65 - public abstract function executePrinter();
66 -
6758 public function getNeedsRawData() {
6859 return false;
6960 }
Index: trunk/phase3/includes/api/ApiMain.php
@@ -101,6 +101,10 @@
102102 $this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
103103 'statement is included in the site\'s LocalSettings.php file', 'readonly');
104104 }
 105+
 106+ public function createPrinterByName($format) {
 107+ return new $this->mFormats[$format] ($this, $format);
 108+ }
105109
106110 public function execute() {
107111 $this->profileIn();
@@ -128,8 +132,7 @@
129133
130134 // Printer may not be initialized if the extractRequestParams() fails for the main module
131135 if (!isset ($this->mPrinter)) {
132 - $format = self :: API_DEFAULT_FORMAT;
133 - $this->mPrinter = new $this->mFormats[$format] ($this, $format);
 136+ $this->mPrinter = $this->createPrinterByName(self :: API_DEFAULT_FORMAT);
134137 }
135138
136139 if ($e instanceof UsageException) {
@@ -183,12 +186,13 @@
184187 $module = new $this->mModules[$action] ($this, $action);
185188
186189 if (!$this->mInternalMode) {
187 - if ($module instanceof ApiFormatBase) {
188 - // The requested module will print data in its own format
189 - $this->mPrinter = $module;
190 - } else {
 190+
 191+ // See if custom printer is used
 192+ $this->mPrinter = $module->getCustomFormatModule();
 193+
 194+ if (is_null($this->mPrinter)) {
191195 // Create an appropriate printer
192 - $this->mPrinter = new $this->mFormats[$format] ($this, $format);
 196+ $this->mPrinter = $this->createPrinterByName($format);
193197 }
194198 }
195199
@@ -212,7 +216,7 @@
213217 $printer->initPrinter($isError);
214218 if (!$printer->getNeedsRawData())
215219 $this->getResult()->SanitizeData();
216 - $printer->executePrinter();
 220+ $printer->execute();
217221 $printer->closePrinter();
218222 $printer->profileOut();
219223 }
@@ -268,9 +272,9 @@
269273 }
270274
271275 $msg .= "\n$astriks Formats $astriks\n\n";
272 - foreach ($this->mFormats as $moduleName => $moduleClass) {
273 - $msg .= "* format=$moduleName *";
274 - $module = new $this->mFormats[$moduleName] ($this, $moduleName);
 276+ foreach ($this->mFormats as $formatName => $moduleClass) {
 277+ $msg .= "* format=$formatName *";
 278+ $module = $this->createPrinterByName($formatName);
275279 $msg2 = $module->makeHelpMsg();
276280 if ($msg2 !== false)
277281 $msg .= $msg2;
Index: trunk/phase3/includes/api/ApiOpenSearch.php
@@ -29,16 +29,14 @@
3030 require_once ("ApiFormatBase.php");
3131 }
3232
33 -class ApiOpenSearch extends ApiFormatBase {
 33+class ApiOpenSearch extends ApiBase {
3434
35 - private $mResult = array();
36 -
3735 public function __construct($main, $action) {
3836 parent :: __construct($main, $action);
3937 }
4038
41 - public function getMimeType() {
42 - return 'application/json';
 39+ public function getCustomFormatModule() {
 40+ return $this->getMain()->createPrinterByName('json');
4341 }
4442
4543 public function execute() {
@@ -63,20 +61,18 @@
6462 $result->SanitizeData();
6563 $data = $result->GetData();
6664
67 - // Reformat useful data for future printing
68 - $result = array();
 65+ // Reformat useful data for future printing by JSON engine
 66+ $srchres = array();
6967 foreach ($data['query']['allpages'] as $pageid => &$pageinfo) {
70 - $result[] = $pageinfo['title'];
 68+ $srchres[] = $pageinfo['title'];
7169 }
7270
73 - $this->mResult = array($command, $result);
 71+ // Set top level elements
 72+ $result = $this->getResult();
 73+ $result->addValue(null, 0, $command);
 74+ $result->addValue(null, 1, $srchres);
7475 }
7576
76 - public function executePrinter() {
77 - $json = new Services_JSON();
78 - $this->printText($json->encode($this->mResult, true));
79 - }
80 -
8177 protected function GetAllowedParams() {
8278 return array (
8379 'command' => null
Index: trunk/phase3/includes/api/ApiPageSet.php
@@ -385,9 +385,9 @@
386386 $pageids = array();
387387 $remaining = array_flip($revids);
388388
389 - $tables = array('page', 'revision');
 389+ $tables = array('revision');
390390 $fields = array('rev_id','rev_page');
391 - $where = array( 'rev_deleted' => 0, 'rev_id' => $revids );
 391+ $where = array('rev_deleted' => 0, 'rev_id' => $revids);
392392
393393 // Get pageIDs data from the `page` table
394394 $this->profileDBIn();
Index: trunk/phase3/includes/api/ApiQueryWatchlist.php
@@ -66,8 +66,7 @@
6767
6868 $options = array (
6969 'LIMIT' => $limit +1,
70 - 'ORDER BY' => 'rc_timestamp' . ($dirNewer ? '' : ' DESC'),
71 - 'USE INDEX' => 'rc_timestamp');
 70+ 'ORDER BY' => 'rc_timestamp' . ($dirNewer ? '' : ' DESC'));
7271
7372 if (is_null($resultPageSet)) {
7473 $fields = array (
@@ -111,10 +110,15 @@
112111 $where[] = 'rc_this_oldid=page_latest';
113112 if (isset ($namespace))
114113 $where['wl_namespace'] = $namespace;
 114+
115115 if (isset ($start))
116 - $where[] = 'rev_timestamp' . $after . $db->addQuotes($start);
 116+ $where[] = 'rc_timestamp' . $after . $db->addQuotes($start);
 117+
117118 if (isset ($end))
118 - $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
 119+ $where[] = 'rc_timestamp' . $before . $db->addQuotes($end);
 120+
 121+ if (!isset ($start) && !isset ($end))
 122+ $where[] = "rc_timestamp > ''";
119123
120124 $this->profileDBIn();
121125 $res = $db->select($tables, $fields, $where, __METHOD__, $options);