r85382 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85381‎ | r85382 | r85383 >
Date:21:32, 4 April 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
changes for 1.6; implemented Validator style parameter handling in SMWResultPrinter
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/SemanticMediaWiki.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinter.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_CSV.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_DSV.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -55,7 +55,7 @@
5656 $querymode = $printer->getQueryMode( $context );
5757 }
5858
59 - $mainlabel = array_key_exists( 'mainlabel', $params ) ? $params['mainlabel'] : '';
 59+ $mainlabel = array_key_exists( 'mainlabel', $params ) ? $params['mainlabel'] : '';
6060 if ( ( $querymode == SMWQuery::MODE_NONE ) ||
6161 ( ( !$desc->isSingleton() || ( count( $desc->getPrintRequests() ) + count( $extraprintouts ) == 0 ) )
6262 && ( trim( $mainlabel ) != '-' ) ) ) {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinter.php
@@ -1,21 +1,26 @@
22 <?php
33 /**
44 * File with abstract base class for printing query results.
 5+ *
 6+ * @file SMW_QueryPrinter.php
 7+ * @ingroup SMWQuery
 8+ *
 9+ * @licence GNU GPL v2+
510 * @author Markus Krötzsch
6 - * @file
7 - * @ingroup SMWQuery
 11+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
812 */
913
10 -// constants that define how/if headers should be displayed
 14+// Constants that define how/if headers should be displayed.
1115 define( 'SMW_HEADERS_SHOW', 2 );
1216 define( 'SMW_HEADERS_PLAIN', 1 );
13 -define( 'SMW_HEADERS_HIDE', 0 ); // used to be "false" hence use "0" to support extensions that still assume this
 17+define( 'SMW_HEADERS_HIDE', 0 ); // Used to be "false" hence use "0" to support extensions that still assume this.
1418
1519 /**
1620 * Abstract base class for SMW's novel query printing mechanism. It implements
1721 * part of the former functionality of SMWInlineQuery (everything related to
1822 * output formatting and the correspoding parameters) and is subclassed by concrete
1923 * printers that provide the main formatting functionality.
 24+ *
2025 * @ingroup SMWQuery
2126 */
2227 abstract class SMWResultPrinter {
@@ -140,11 +145,13 @@
141146 $this->hasTemplates = false;
142147
143148 if ( $this->useValidator ) {
144 - $params = $this->handleParameters( $params );
 149+ // TODO: retain behaviour of base readParameters
 150+ $this->handleParameters( $this->handleRawParameters( $params ), $outputmode );
145151 }
 152+ else {
 153+ $this->readParameters( $params, $outputmode );
 154+ }
146155
147 - $this->readParameters( $params, $outputmode );
148 -
149156 // Default output for normal printers:
150157 if ( ( $outputmode != SMW_OUTPUT_FILE ) && // not in FILE context,
151158 ( $results->getCount() == 0 ) && // no results,
@@ -176,7 +183,7 @@
177184 $result = $this->getResultText( $results, $outputmode );
178185
179186 if ( $outputmode != SMW_OUTPUT_FILE ) {
180 - $result = $this->handleNonFileResult( $result );
 187+ $result = $this->handleNonFileResult( $result, $results, $outputmode );
181188 }
182189
183190 return $result;
@@ -188,10 +195,12 @@
189196 * @since 1.6
190197 *
191198 * @param string $result
 199+ * @param SMWQueryResult $results
 200+ * @param $outputmode
192201 *
193202 * @return string
194203 */
195 - protected function handleNonFileResult( $result ) {
 204+ protected function handleNonFileResult( $result, SMWQueryResult $results, $outputmode ) {
196205 $result .= $this->getErrorString( $results ); // append errors
197206
198207 if ( ( !$this->isHTML ) && ( $this->hasTemplates ) ) { // preprocess embedded templates if needed
@@ -267,7 +276,7 @@
268277 *
269278 * @return array
270279 */
271 - protected function handleParameters( array $keyValuePairs ) {
 280+ protected function handleRawParameters( array $keyValuePairs ) {
272281 $validator = new Validator();
273282 $validator->setParameters( $keyValuePairs, $this->getParameters() );
274283 $validator->validateParameters();
@@ -277,6 +286,7 @@
278287 throw new Exception( 'Validator: fatal param validation error' );
279288 }
280289 else {
 290+ // TODO: keep track of non-fatal errors to display
281291 return $validator->getParameterValues();
282292 }
283293 }
@@ -285,9 +295,57 @@
286296 * Read an array of parameter values given as key-value-pairs and
287297 * initialise internal member fields accordingly. Possibly overwritten
288298 * (extended) by subclasses.
 299+ *
 300+ * @since 1.6
 301+ *
 302+ * @param array $params
 303+ * @param $outputmode
 304+ */
 305+ protected function handleParameters( array $params, $outputmode ) {
 306+ $this->m_params = $params;
 307+
 308+ if ( array_key_exists( 'intro', $params ) ) { $this->mIntro = $params['intro']; }
 309+ if ( array_key_exists( 'outro', $params ) ) { $this->mOutro = $params['outro']; }
 310+
 311+ if ( array_key_exists( 'searchlabel', $params ) ) {
 312+ $this->mSearchlabel = $params['searchlabel'] === false ? null : $params['searchlabel'];
 313+ }
 314+
 315+ switch ( $params['link'] ) {
 316+ case 'head': case 'subject':
 317+ $this->mLinkFirst = true;
 318+ $this->mLinkOthers = false;
 319+ break;
 320+ case 'all':
 321+ $this->mLinkFirst = true;
 322+ $this->mLinkOthers = true;
 323+ break;
 324+ case 'none':
 325+ $this->mLinkFirst = false;
 326+ $this->mLinkOthers = false;
 327+ break;
 328+ }
 329+
 330+ if ( array_key_exists( 'default', $params ) ) { $this->mDefault = str_replace( '_', ' ', $params['default'] ); }
 331+
 332+ if ( $params['headers'] == 'hide' ) {
 333+ $this->mShowHeaders = SMW_HEADERS_HIDE;
 334+ } elseif ( $params['headers'] == 'plain' ) {
 335+ $this->mShowHeaders = SMW_HEADERS_PLAIN;
 336+ } else {
 337+ $this->mShowHeaders = SMW_HEADERS_SHOW;
 338+ }
 339+ }
 340+
 341+ /**
 342+ * Read an array of parameter values given as key-value-pairs and
 343+ * initialise internal member fields accordingly. Possibly overwritten
 344+ * (extended) by subclasses.
289345 *
290346 * @param array $params
291347 * @param $outputmode
 348+ *
 349+ * @deprecated Use handleParameters instead
292350 */
293351 protected function readParameters( /* array */ $params, $outputmode ) {
294352 $this->m_params = $params;
@@ -465,19 +523,22 @@
466524 *
467525 * @since 1.5.0
468526 *
469 - * @return array
 527+ * @return array of Parameter
470528 */
471529 protected function textDisplayParameters() {
472530 $params = array();
473531
474532 $params['intro'] = new Parameter( 'intro' );
475533 $params['intro']->setDescription( wfMsg( 'smw_paramdesc_intro' ) );
476 -
 534+ $params['intro']->setDefault( '' );
 535+
477536 $params['outro'] = new Parameter( 'outro' );
478537 $params['outro']->setDescription( wfMsg( 'smw_paramdesc_outro' ) );
 538+ $params['outro']->setDefault( '' );
479539
480540 $params['default'] = new Parameter( 'default' );
481541 $params['default']->setDescription( wfMsg( 'smw_paramdesc_default' ) );
 542+ $params['default']->setDefault( '' );
482543
483544 return $params;
484545 }
@@ -497,6 +558,7 @@
498559
499560 $params['searchlabel'] = new Parameter( 'searchlabel' );
500561 $params['searchlabel']->setDescription( wfMsg( 'smw_paramdesc_searchlabel' ) );
 562+ $params['searchlabel']->setDefault( false, false );
501563
502564 return $params;
503565 }
@@ -515,17 +577,21 @@
516578
517579 $params['limit'] = new Parameter( 'limit', Parameter::TYPE_INTEGER );
518580 $params['limit']->setDescription( wfMsg( 'smw_paramdesc_limit' ) );
 581+ $params['limit']->setDefault( 20 );
519582
520583 $params['headers'] = new Parameter( 'headers' );
521584 $params['headers']->setDescription( wfMsg( 'smw_paramdesc_headers' ) );
522585 $params['headers']->addCriteria( new CriterionInArray( 'show', 'hide', 'plain' ) );
 586+ $params['headers']->setDefault( 'show' );
523587
524588 $params['mainlabel'] = new Parameter( 'mainlabel' );
525589 $params['mainlabel']->setDescription( wfMsg( 'smw_paramdesc_mainlabel' ) );
 590+ $params['mainlabel']->setDefault( false, false );
526591
527592 $params['link'] = new Parameter( 'link' );
528593 $params['link']->setDescription( wfMsg( 'smw_paramdesc_link' ) );
529594 $params['link']->addCriteria( new CriterionInArray( 'all', 'subject', 'none' ) );
 595+ $params['link']->setDefault( 'all' );
530596
531597 return $params;
532598 }
Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_CSV.php
@@ -86,7 +86,7 @@
8787 $link->setParameter( 'csv', 'format' );
8888 $link->setParameter( $this->m_sep, 'sep' );
8989
90 - if ( array_key_exists( 'mainlabel', $this->m_params ) ) {
 90+ if ( $this->m_params['mainlabel'] !== false ) {
9191 $link->setParameter( $this->m_params['mainlabel'], 'mainlabel' );
9292 }
9393
Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_DSV.php
@@ -160,7 +160,7 @@
161161 $link->setParameter( 'dsv', 'format' );
162162 $link->setParameter( $this->separator, 'sep' );
163163
164 - if ( array_key_exists( 'mainlabel', $this->m_params ) ) {
 164+ if ( $this->m_params['mainlabel'] !== false ) {
165165 $link->setParameter( $this->m_params['mainlabel'], 'mainlabel' );
166166 }
167167
Index: trunk/extensions/SemanticMediaWiki/SemanticMediaWiki.php
@@ -36,3 +36,6 @@
3737 'url' => 'http://semantic-mediawiki.org',
3838 'descriptionmsg' => 'smw-desc'
3939 );
 40+
 41+// A flag used to indicate SMW supports Validator style parameter definitions and validation in the SMWResultPrinter class.
 42+define( 'SMW_SUPPORTS_VALIDATOR', true );

Follow-up revisions

RevisionCommit summaryAuthorDate
r85383version nr increment to 1.6 alpha; follow up to r85382; testing changes in SM...jeroendedauw21:33, 4 April 2011

Status & tagging log