r114427 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114426‎ | r114427 | r114428 >
Date:18:17, 22 March 2012
Author:danwe
Status:new
Tags:
Comment:
intro/outro/default parameters for "?" parser function implemented
Modified paths:
  • /trunk/extensions/SemanticExpressiveness/includes/SemExExpressiveStringPF.php (modified) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/SemExQueryPF.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticExpressiveness/includes/SemExQueryPF.php
@@ -20,27 +20,47 @@
2121 $smwgIQRunningNumber++;
2222
2323 $params = func_get_args();
24 - array_shift( $params ); // remove $parser
 24+ array_shift( $params ); // remove $parser
 25+ $params = static::validateParams( $params );
2526
26 - $query = SemExShortQuery::newFromPFParams(
27 - $params,
28 - array( 'property', Validator::PARAM_UNNAMED )
29 - );
30 - $options = SemExShortQueryOutputOptions::newFromPFParams( $params );
 27+ $query = SemExShortQuery::newFromValidatedParams( $params );
 28+ $options = SemExShortQueryOutputOptions::newFromValidatedParams( $params );
3129
3230 if( ! $query || ! $options ) {
3331 // @ToDo: real error message (anyway, in what case can this happen?)
3432 return 'FALSE';
3533 }
3634
37 - $result = SemExShortQueryProcessor::getResultFromQuery( $parser, $query, $options );
38 - } else {
 35+ $result = SemExShortQueryProcessor::getResultFromQuery( $parser, $query, $options );
 36+
 37+ if( $result === '' ) {
 38+ $result = $params['default'];
 39+ } else {
 40+ $result = // allow ' ' in form of '_' around the result
 41+ preg_replace( '/_$/', ' ', $params['intro'] ) .
 42+ $result .
 43+ preg_replace( '/^_/', ' ', $params['outro'] );
 44+ }
 45+ }
 46+ else {
3947 $result = smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
4048 }
4149
4250 return $result;
4351 }
4452
 53+ protected static function validateParams( array $rawParams, &$validator = null ) {
 54+ $validator = new Validator();
 55+ $validator->setFunctionParams(
 56+ $rawParams,
 57+ static::getParameters(),
 58+ array( 'property', Validator::PARAM_UNNAMED ) // 'property' is parameter 1
 59+ );
 60+ $validator->validateParameters();
 61+
 62+ return $validator->getParameterValues();
 63+ }
 64+
4565 /**
4666 * Returns a description of all allowed function Parameters.
4767 *
@@ -50,8 +70,13 @@
5171 $params = array();
5272
5373 $params['intro'] = new Parameter( 'intro' );
 74+ $params['intro']->setDefault( '' );
 75+
5476 $params['outro'] = new Parameter( 'outro' );
 77+ $params['outro']->setDefault( '' );
 78+
5579 $params['default'] = new Parameter( 'default' );
 80+ $params['default']->setDefault( '' );
5681
5782 // add function parameters describing the querry and its options:
5883 $params = array_merge(
Index: trunk/extensions/SemanticExpressiveness/includes/SemExExpressiveStringPF.php
@@ -43,7 +43,7 @@
4444 * @return array
4545 */
4646 protected function getParameterInfo( $type ) {
47 - $params = array();
 47+ $params = SemExExpressiveStringOutputOptions::getPFParams();
4848
4949 # input text.
5050 # since 0.1
@@ -55,13 +55,11 @@
5656
5757 $params['detect'] = new ListParameter( 'detect' );
5858 $params['detect']->addCriteria( $pieceTypesCriteria );
59 - $params['detect']->setDefault( false, false );
 59+ $params['detect']->setDefault( array( '' ), false );
6060
6161 $params['ignore'] = new ListParameter( 'ignore' );
6262 $params['ignore']->addCriteria( $pieceTypesCriteria );
6363 $params['ignore']->setDefault( array(), false );
64 -
65 - $params = array_merge( $params, SemExExpressiveStringOutputOptions::getPFParams() );
6664
6765 return $params;
6866 }
@@ -102,7 +100,7 @@
103101 // get all types that should be handled by this
104102 $enabledTypes = array();
105103
106 - if( $parameters['detect'] !== false ) {
 104+ if( implode( '', $parameters['detect'] ) !== '' ) { // '' counts as if parameter not set
107105 foreach( $parameters['detect'] as $typeName ) {
108106 $type = SemExExpressiveString::getRegisteredPieceTypeByName( $typeName );
109107 if( $type !== null ) {
@@ -115,11 +113,13 @@
116114 $enabledTypes = SemExExpressiveString::getRegisteredPieceTypeNames();
117115 }
118116
119 - $enabledTypes = array_flip( $enabledTypes );
120 - foreach( $parameters['ignore'] as $typeName ) {
121 - unset( $enabledTypes[ SemExExpressiveString::getRegisteredPieceTypeByName( $typeName ) ] );
 117+ if( $enabledTypes !== null ) {
 118+ $enabledTypes = array_flip( $enabledTypes );
 119+ foreach( $parameters['ignore'] as $typeName ) {
 120+ unset( $enabledTypes[ SemExExpressiveString::getRegisteredPieceTypeByName( $typeName ) ] );
 121+ }
 122+ $enabledTypes = array_flip( $enabledTypes );
122123 }
123 - $enabledTypes = array_flip( $enabledTypes );
124124
125125 // build expressive string from input with enabled types:
126126 $exprString = new SemExExpressiveString( $parameters['text'], $this->parser, $enabledTypes );

Status & tagging log