r16955 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r16954‎ | r16955 | r16956 >
Date:03:15, 12 October 2006
Author:yurik
Status:old
Tags:
Comment:
* allpages module fix
* added exception handling code
Modified paths:
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryAllpages.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -33,6 +33,7 @@
3434
3535 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
3636 private $mPageSet;
 37+ private $mValidNamespaces;
3738
3839 private $mQueryPropModules = array (
3940 'info' => 'ApiQueryInfo',
@@ -69,6 +70,7 @@
7071 $this->mPropModuleNames = array_keys($this->mQueryPropModules);
7172 $this->mListModuleNames = array_keys($this->mQueryListModules);
7273 $this->mMetaModuleNames = array_keys($this->mQueryMetaModules);
 74+ $this->mValidNamespaces = null;
7375
7476 // Allow the entire list of modules at first,
7577 // but during module instantiation check if it can be used as a generator.
@@ -85,6 +87,19 @@
8688 return $this->mPageSet;
8789 }
8890
 91+ public function getValidNamespaces() {
 92+ global $wgContLang;
 93+
 94+ if (is_null($this->mValidNamespaces)) {
 95+ $this->mValidNamespaces = array ();
 96+ foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
 97+ if ($ns >= 0)
 98+ $this->mValidNamespaces[] = $ns; // strval($ns);
 99+ }
 100+ }
 101+ return $this->mValidNamespaces;
 102+ }
 103+
89104 /**
90105 * Query execution happens in the following steps:
91106 * #1 Create a PageSet object with any pages requested by the user
Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -76,7 +76,7 @@
7777 'rev_text_id',
7878 'rev_minor_edit'
7979 );
80 - $conds = array (
 80+ $where = array (
8181 'rev_deleted' => 0
8282 );
8383 $options = array ();
@@ -100,7 +100,7 @@
101101 break;
102102 case 'content' :
103103 $tables[] = 'text';
104 - $conds[] = 'rev_text_id=old_id';
 104+ $where[] = 'rev_text_id=old_id';
105105 $fields[] = 'old_id';
106106 $fields[] = 'old_text';
107107 $fields[] = 'old_flags';
@@ -136,13 +136,13 @@
137137 $after = ($dirNewer ? '>=' : '<=');
138138
139139 if ($startid !== 0)
140 - $conds[] = 'rev_id' . $after . intval($startid);
 140+ $where[] = 'rev_id' . $after . intval($startid);
141141 if ($endid !== 0)
142 - $conds[] = 'rev_id' . $before . intval($endid);
 142+ $where[] = 'rev_id' . $before . intval($endid);
143143 if (isset ($start))
144 - $conds[] = 'rev_timestamp' . $after . $db->addQuotes($start);
 144+ $where[] = 'rev_timestamp' . $after . $db->addQuotes($start);
145145 if (isset ($end))
146 - $conds[] = 'rev_timestamp' . $before . $db->addQuotes($end);
 146+ $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
147147
148148 // must manually initialize unset limit
149149 if (!isset ($limit))
@@ -151,19 +151,19 @@
152152 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
153153
154154 // There is only one ID, use it
155 - $conds['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
 155+ $where['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
156156
157157 }
158158 elseif ($pageCount > 0) {
159159 // When working in multi-page non-enumeration mode,
160160 // limit to the latest revision only
161161 $tables[] = 'page';
162 - $conds[] = 'page_id=rev_page';
163 - $conds[] = 'page_latest=rev_id';
 162+ $where[] = 'page_id=rev_page';
 163+ $where[] = 'page_latest=rev_id';
164164 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
165165
166166 // Get all page IDs
167 - $conds['page_id'] = array_keys($pageSet->getGoodTitles());
 167+ $where['page_id'] = array_keys($pageSet->getGoodTitles());
168168
169169 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
170170 }
@@ -171,7 +171,7 @@
172172 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
173173
174174 // Get all revision IDs
175 - $conds['rev_id'] = array_keys($pageSet->getRevisionIDs());
 175+ $where['rev_id'] = array_keys($pageSet->getRevisionIDs());
176176
177177 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
178178 } else
@@ -180,7 +180,7 @@
181181 $options['LIMIT'] = $limit +1;
182182
183183 $this->profileDBIn();
184 - $res = $db->select($tables, $fields, $conds, __METHOD__, $options);
 184+ $res = $db->select($tables, $fields, $where, __METHOD__, $options);
185185 $this->profileDBOut();
186186
187187 $data = array ();
Index: trunk/phase3/includes/api/ApiQueryAllpages.php
@@ -42,7 +42,7 @@
4343 public function executeGenerator($resultPageSet) {
4444 if ($resultPageSet->isResolvingRedirects())
4545 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
46 -
 46+
4747 $this->run($resultPageSet);
4848 }
4949
@@ -55,11 +55,11 @@
5656 $where = array (
5757 'page_namespace' => $namespace
5858 );
59 -
 59+
6060 if (isset ($from)) {
6161 $where[] = 'page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from));
6262 }
63 -
 63+
6464 if ($filterredir === 'redirects') {
6565 $where['page_is_redirect'] = 1;
6666 }
@@ -77,12 +77,14 @@
7878 $fields = $resultPageSet->getPageTableFields();
7979 }
8080
81 - $this->profileDBIn();
82 - $res = $db->select('page', $fields, $where, __CLASS__ . '::' . __METHOD__, array (
 81+ $options = array (
8382 'USE INDEX' => 'name_title',
8483 'LIMIT' => $limit +1,
8584 'ORDER BY' => 'page_namespace, page_title'
86 - ));
 85+ );
 86+
 87+ $this->profileDBIn();
 88+ $res = $db->select('page', $fields, $where, __METHOD__, $options);
8789 $this->profileDBOut();
8890
8991 $data = array ();
@@ -103,7 +105,10 @@
104106
105107 if (is_null($resultPageSet)) {
106108 $id = intval($row->page_id);
107 - $data[] = $id; // in generator mode, just assemble a list of page IDs.
 109+ $data[$id] = array (
 110+ 'id' => $id,
 111+ 'ns' => $title->getNamespace(),
 112+ 'title' => $title->getPrefixedText());
108113 } else {
109114 $resultPageSet->processDbRow($row);
110115 }
@@ -119,35 +124,25 @@
120125
121126 protected function getAllowedParams() {
122127
123 - global $wgContLang;
124 - $validNamespaces = array ();
125 - foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
126 - if ($ns >= 0)
127 - $validNamespaces[] = $ns; // strval($ns);
128 - }
129 -
130128 return array (
131129 'from' => null,
132130 'namespace' => array (
133131 ApiBase :: PARAM_DFLT => 0,
134 - ApiBase :: PARAM_TYPE => $validNamespaces
135 - ),
 132+ ApiBase :: PARAM_TYPE => $this->getQuery()->getValidNamespaces()),
136133 'filterredir' => array (
137134 ApiBase :: PARAM_DFLT => 'all',
138135 ApiBase :: PARAM_TYPE => array (
139136 'all',
140137 'redirects',
141138 'nonredirects'
142 - )
143 - ),
 139+ )),
144140 'limit' => array (
145141 ApiBase :: PARAM_DFLT => 10,
146142 ApiBase :: PARAM_TYPE => 'limit',
147143 ApiBase :: PARAM_MIN => 1,
148144 ApiBase :: PARAM_MAX1 => 500,
149145 ApiBase :: PARAM_MAX2 => 5000
150 - )
151 - );
 146+ ));
152147 }
153148
154149 protected function getParamDescription() {
Index: trunk/phase3/includes/api/ApiMain.php
@@ -51,6 +51,9 @@
5252 $this->mResult = new ApiResult($this);
5353 $this->mShowVersions = false;
5454 $this->mEnableWrite = $enableWrite;
 55+
 56+ // Initialize Error handler
 57+ set_exception_handler( array($this, 'exceptionHandler') );
5558 }
5659
5760 public function & getResult() {
@@ -107,12 +110,7 @@
108111 $this->printResult(false);
109112
110113 } catch (UsageException $e) {
111 -
112 - // Printer may not be initialized if the extractRequestParams() fails for the main module
113 - if (!isset ($this->mPrinter))
114 - $this->mPrinter = new $this->mFormats[API_DEFAULT_FORMAT] ($this, API_DEFAULT_FORMAT);
115 - $this->printResult(true);
116 -
 114+ $this->printError();
117115 }
118116 $this->profileOut();
119117 }
@@ -130,6 +128,13 @@
131129 $printer->closePrinter();
132130 $printer->profileOut();
133131 }
 132+
 133+ private function printError() {
 134+ // Printer may not be initialized if the extractRequestParams() fails for the main module
 135+ if (!isset ($this->mPrinter))
 136+ $this->mPrinter = new $this->mFormats[API_DEFAULT_FORMAT] ($this, API_DEFAULT_FORMAT);
 137+ $this->printResult(true);
 138+ }
134139
135140 protected function getDescription() {
136141 return array (
@@ -141,22 +146,29 @@
142147 }
143148
144149 public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
145 - $this->mResult->Reset();
146150 if ($httpRespCode === 0)
147151 header($errorCode, true);
148152 else
149153 header($errorCode, true, $httpRespCode);
150154
 155+ $this->makeErrorMessage($description, $errorCode);
 156+
 157+ throw new UsageException($description, $errorCode);
 158+ }
 159+
 160+ public function makeErrorMessage($description, $errorCode, $customContent = null) {
 161+ $this->mResult->Reset();
151162 $data = array (
152163 'code' => $errorCode,
153164 'info' => $description
154165 );
155 - ApiResult :: setContent($data, $this->makeHelpMsg());
 166+
 167+ ApiResult :: setContent($data,
 168+ is_null($customContent) ? $this->makeHelpMsg() : $customContent);
 169+
156170 $this->mResult->addValue(null, 'error', $data);
157 -
158 - throw new UsageException($description, $errorCode);
159171 }
160 -
 172+
161173 /**
162174 * Override the parent to generate help messages for all available modules.
163175 */
@@ -188,7 +200,54 @@
189201
190202 return $msg;
191203 }
 204+
 205+ /**
 206+ * Exception handler which simulates the appropriate catch() handling:
 207+ *
 208+ * try {
 209+ * ...
 210+ * } catch ( MWException $e ) {
 211+ * dieUsage()
 212+ * } catch ( Exception $e ) {
 213+ * echo $e->__toString();
 214+ * }
 215+ *
 216+ *
 217+ *
 218+ *
 219+ * !!!!!!!!!!!!! REVIEW needed !!!!!!!!!!!!!!!!!!
 220+ *
 221+ * this method needs to be reviewed/cleaned up
 222+ *
 223+ *
 224+ *
 225+ */
 226+ public function exceptionHandler( $e ) {
 227+ global $wgFullyInitialised;
 228+ if ( is_a( $e, 'MWException' ) ) {
 229+ try {
 230+ $msg = "Exception Caught: {$e->getMessage()}";
 231+ $this->makeErrorMessage($msg, 'internal_error', "\n\n{$e->getTraceAsString()}\n\n");
 232+ $this->printError();
 233+ } catch (Exception $e2) {
 234+ echo $e->__toString();
 235+ }
 236+ } else {
 237+ echo $e->__toString();
 238+ }
192239
 240+ // Final cleanup, similar to wfErrorExit()
 241+ if ( $wgFullyInitialised ) {
 242+ try {
 243+ wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
 244+ } catch ( Exception $e ) {}
 245+ }
 246+
 247+ // Exit value should be nonzero for the benefit of shell jobs
 248+ exit( 1 );
 249+ }
 250+
 251+
193252 private $mIsBot = null;
194253 public function isBot() {
195254 if (!isset ($this->mIsBot)) {