r114428 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114427‎ | r114428 | r114429 >
Date:19:00, 22 March 2012
Author:danwe
Status:new
Tags:
Comment:
"?!" parser function and moved parser hook files into sub directory
Modified paths:
  • /trunk/extensions/SemanticExpressiveness/SemanticExpressiveness.php (modified) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/SemExExpressiveStringPF.php (deleted) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/SemExQueryPF.php (deleted) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/parserhooks (added) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExExpressiveStringPF.php (added) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExPlainQueryPF.php (added) (history)
  • /trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExQueryPF.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticExpressiveness/SemanticExpressiveness.php
@@ -50,7 +50,6 @@
5151 $wgAutoloadClasses['SemExExpressiveStringPieceSQResult'] = $incDir . 'SemExExpressiveStringPieceSQResult.php';
5252 $wgAutoloadClasses['SemExExpressiveStringPieceWikiLink'] = $incDir . 'SemExExpressiveStringPieceWikiLink.php';
5353 $wgAutoloadClasses['SemExExpressiveStringOutputOptions'] = $incDir . 'SemExExpressiveStringOutputOptions.php';
54 -$wgAutoloadClasses['SemExQueryPF' ] = $incDir . 'SemExQueryPF.php';
5554 $wgAutoloadClasses['SemExShortQuery' ] = $incDir . 'SemExShortQuery.php';
5655 $wgAutoloadClasses['SemExShortQueryProcessor' ] = $incDir . 'SemExShortQueryProcessor.php';
5756 $wgAutoloadClasses['SemExShortQueryResult' ] = $incDir . 'SemExShortQueryResult.php';
@@ -65,8 +64,9 @@
6665 $wgAutoloadClasses['SemExParamManipulationQuerySource'] = $incDir . 'validation/SemExParamManipulationQuerySource.php';
6766
6867 // Parser function initializations:
69 -$wgAutoloadClasses['SemExQueryPF' ] = $incDir . 'SemExQueryPF.php';
70 -$wgAutoloadClasses['SemExExpressiveStringPF' ] = $incDir . 'SemExExpressiveStringPF.php';
 68+$wgAutoloadClasses['SemExQueryPF' ] = $incDir . 'parserhooks/SemExQueryPF.php';
 69+$wgAutoloadClasses['SemExPlainQueryPF' ] = $incDir . 'parserhooks/SemExPlainQueryPF.php';
 70+$wgAutoloadClasses['SemExExpressiveStringPF'] = $incDir . 'parserhooks/SemExExpressiveStringPF.php';
7171
7272 $wgHooks['ParserFirstCallInit'][] = 'SemExExpressiveStringPF::staticInit';
7373
@@ -85,8 +85,8 @@
8686
8787 static function init( &$parser ) {
8888 $parser->setFunctionHook( '?', array( 'SemExQueryPF', 'render' ), SFH_NO_HASH );
89 - //$parser->setFunctionHook( '?!', array( 'SemExPlainQueryPF', 'render' ), SFH_NO_HASH );
90 - $parser->setFunctionHook( '?to?!', __CLASS__ . '::parserFunc_QueryToPlainQuery', SFH_NO_HASH );
 89+ $parser->setFunctionHook( '?!', array( 'SemExPlainQueryPF', 'render' ), SFH_NO_HASH );
 90+ //$parser->setFunctionHook( '?to?!', array( __CLASS__, 'parserFunc_QueryToPlainQuery' ), SFH_NO_HASH );
9191 return true;
9292 }
9393
Index: trunk/extensions/SemanticExpressiveness/includes/SemExQueryPF.php
@@ -1,90 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class for '?' short query parser function
6 - *
7 - * @since 0.1
8 - *
9 - * @file SemExQueryPF.php
10 - * @ingroup SemanticExpressiveness
11 - *
12 - * @author Daniel Werner < danweetz@web.de >
13 - */
14 -
15 -class SemExQueryPF {
16 -
17 - public static function render( Parser &$parser ) {
18 - global $smwgQEnabled, $smwgIQRunningNumber;
19 -
20 - if ( $smwgQEnabled ) {
21 - $smwgIQRunningNumber++;
22 -
23 - $params = func_get_args();
24 - array_shift( $params ); // remove $parser
25 - $params = static::validateParams( $params );
26 -
27 - $query = SemExShortQuery::newFromValidatedParams( $params );
28 - $options = SemExShortQueryOutputOptions::newFromValidatedParams( $params );
29 -
30 - if( ! $query || ! $options ) {
31 - // @ToDo: real error message (anyway, in what case can this happen?)
32 - return 'FALSE';
33 - }
34 -
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 {
47 - $result = smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
48 - }
49 -
50 - return $result;
51 - }
52 -
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 -
65 - /**
66 - * Returns a description of all allowed function Parameters.
67 - *
68 - * @return array
69 - */
70 - public static function getParameters() {
71 - $params = array();
72 -
73 - $params['intro'] = new Parameter( 'intro' );
74 - $params['intro']->setDefault( '' );
75 -
76 - $params['outro'] = new Parameter( 'outro' );
77 - $params['outro']->setDefault( '' );
78 -
79 - $params['default'] = new Parameter( 'default' );
80 - $params['default']->setDefault( '' );
81 -
82 - // add function parameters describing the querry and its options:
83 - $params = array_merge(
84 - $params,
85 - SemExShortQuery::getPFParams(),
86 - SemExShortQueryOutputOptions::getPFParams()
87 - );
88 -
89 - return $params;
90 - }
91 -}
Index: trunk/extensions/SemanticExpressiveness/includes/SemExExpressiveStringPF.php
@@ -1,132 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class for the '?to?!' parser function, basically a limited converter for expressive strings.
6 - *
7 - * @since 0.1
8 - *
9 - * @file SemExExpressiveStringPF.php
10 - * @ingroup SemanticExpressiveness
11 - *
12 - * @author Daniel Werner < danweetz@web.de >
13 - */
14 -class SemExExpressiveStringPF extends ParserHook {
15 -
16 - public function __construct() {
17 - // make this a parser function extension (no tag extension) only:
18 - parent::__construct( false, true, ParserHook::FH_NO_HASH );
19 - }
20 -
21 - /**
22 - * No LSB in pre-5.3 PHP, to be refactored later
23 - */
24 - public static function staticInit( Parser &$parser ) {
25 - $instance = new self;
26 - $instance->init( $parser );
27 - return true;
28 - }
29 -
30 - /**
31 - * Gets the name of the parser hook.
32 - * @see ParserHook::getName
33 - *
34 - * @return string
35 - */
36 - protected function getName() {
37 - return '?to?!';
38 - }
39 -
40 - /**
41 - * Returns an array containing the parameter info.
42 - * @see ParserHook::getParameterInfo
43 - *
44 - * @return array
45 - */
46 - protected function getParameterInfo( $type ) {
47 - $params = SemExExpressiveStringOutputOptions::getPFParams();
48 -
49 - # input text.
50 - # since 0.1
51 - $params['text'] = new Parameter( 'text' );
52 -
53 - $pieceTypesCriteria = new CriterionInArray(
54 - array_values( SemExExpressiveString::getRegisteredPieceTypeNames() )
55 - );
56 -
57 - $params['detect'] = new ListParameter( 'detect' );
58 - $params['detect']->addCriteria( $pieceTypesCriteria );
59 - $params['detect']->setDefault( array( '' ), false );
60 -
61 - $params['ignore'] = new ListParameter( 'ignore' );
62 - $params['ignore']->addCriteria( $pieceTypesCriteria );
63 - $params['ignore']->setDefault( array(), false );
64 -
65 - return $params;
66 - }
67 -
68 - /**
69 - * Returns the list of default parameters.
70 - * @see ParserHook::getDefaultParameters
71 - *
72 - * @return array
73 - */
74 - protected function getDefaultParameters( $type ) {
75 - return array(
76 - array( 'text', Validator::PARAM_UNNAMED ),
77 - );
78 - }
79 -
80 - /**
81 - * Returns the parser function options.
82 - * @see ParserHook::getFunctionOptions
83 - *
84 - * @return array
85 - */
86 - protected function getFunctionOptions() {
87 - return array(
88 - 'noparse' => true,
89 - 'isHTML' => false
90 - );
91 - }
92 -
93 - /**
94 - * Renders and returns the output.
95 - * @see ParserHook::renderTag
96 - *
97 - * @param array $parameters
98 - * @return string
99 - */
100 - public function render( array $parameters ) {
101 - // get all types that should be handled by this
102 - $enabledTypes = array();
103 -
104 - if( implode( '', $parameters['detect'] ) !== '' ) { // '' counts as if parameter not set
105 - foreach( $parameters['detect'] as $typeName ) {
106 - $type = SemExExpressiveString::getRegisteredPieceTypeByName( $typeName );
107 - if( $type !== null ) {
108 - $enabledTypes[] = $type;
109 - }
110 - }
111 - } elseif( empty( $parameters['ignore'] ) ) {
112 - $enabledTypes = null; // same as next but constructor will process this faster
113 - } else {
114 - $enabledTypes = SemExExpressiveString::getRegisteredPieceTypeNames();
115 - }
116 -
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 );
123 - }
124 -
125 - // build expressive string from input with enabled types:
126 - $exprString = new SemExExpressiveString( $parameters['text'], $this->parser, $enabledTypes );
127 -
128 - /** @ToDo: Make it possible to define options per piece type per parameter prefixes */
129 - $options = SemExExpressiveStringOutputOptions::newFromValidatedParams( $parameters );
130 - return $exprString->getOutput( $options );
131 - }
132 -
133 -}
Index: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExExpressiveStringPF.php
@@ -0,0 +1,132 @@
 2+<?php
 3+
 4+/**
 5+ * Class for the '?to?!' parser function, basically a limited converter for expressive strings.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SemExExpressiveStringPF.php
 10+ * @ingroup SemanticExpressiveness
 11+ *
 12+ * @author Daniel Werner < danweetz@web.de >
 13+ */
 14+class SemExExpressiveStringPF extends ParserHook {
 15+
 16+ public function __construct() {
 17+ // make this a parser function extension (no tag extension) only:
 18+ parent::__construct( false, true, ParserHook::FH_NO_HASH );
 19+ }
 20+
 21+ /**
 22+ * No LSB in pre-5.3 PHP, to be refactored later
 23+ */
 24+ public static function staticInit( Parser &$parser ) {
 25+ $instance = new self;
 26+ $instance->init( $parser );
 27+ return true;
 28+ }
 29+
 30+ /**
 31+ * Gets the name of the parser hook.
 32+ * @see ParserHook::getName
 33+ *
 34+ * @return string
 35+ */
 36+ protected function getName() {
 37+ return '?to?!';
 38+ }
 39+
 40+ /**
 41+ * Returns an array containing the parameter info.
 42+ * @see ParserHook::getParameterInfo
 43+ *
 44+ * @return array
 45+ */
 46+ protected function getParameterInfo( $type ) {
 47+ $params = SemExExpressiveStringOutputOptions::getPFParams();
 48+
 49+ # input text.
 50+ # since 0.1
 51+ $params['text'] = new Parameter( 'text' );
 52+
 53+ $pieceTypesCriteria = new CriterionInArray(
 54+ array_values( SemExExpressiveString::getRegisteredPieceTypeNames() )
 55+ );
 56+
 57+ $params['detect'] = new ListParameter( 'detect' );
 58+ $params['detect']->addCriteria( $pieceTypesCriteria );
 59+ $params['detect']->setDefault( array( '' ), false );
 60+
 61+ $params['ignore'] = new ListParameter( 'ignore' );
 62+ $params['ignore']->addCriteria( $pieceTypesCriteria );
 63+ $params['ignore']->setDefault( array(), false );
 64+
 65+ return $params;
 66+ }
 67+
 68+ /**
 69+ * Returns the list of default parameters.
 70+ * @see ParserHook::getDefaultParameters
 71+ *
 72+ * @return array
 73+ */
 74+ protected function getDefaultParameters( $type ) {
 75+ return array(
 76+ array( 'text', Validator::PARAM_UNNAMED ),
 77+ );
 78+ }
 79+
 80+ /**
 81+ * Returns the parser function options.
 82+ * @see ParserHook::getFunctionOptions
 83+ *
 84+ * @return array
 85+ */
 86+ protected function getFunctionOptions() {
 87+ return array(
 88+ 'noparse' => true,
 89+ 'isHTML' => false
 90+ );
 91+ }
 92+
 93+ /**
 94+ * Renders and returns the output.
 95+ * @see ParserHook::renderTag
 96+ *
 97+ * @param array $parameters
 98+ * @return string
 99+ */
 100+ public function render( array $parameters ) {
 101+ // get all types that should be handled by this
 102+ $enabledTypes = array();
 103+
 104+ if( implode( '', $parameters['detect'] ) !== '' ) { // '' counts as if parameter not set
 105+ foreach( $parameters['detect'] as $typeName ) {
 106+ $type = SemExExpressiveString::getRegisteredPieceTypeByName( $typeName );
 107+ if( $type !== null ) {
 108+ $enabledTypes[] = $type;
 109+ }
 110+ }
 111+ } elseif( empty( $parameters['ignore'] ) ) {
 112+ $enabledTypes = null; // same as next but constructor will process this faster
 113+ } else {
 114+ $enabledTypes = SemExExpressiveString::getRegisteredPieceTypeNames();
 115+ }
 116+
 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 );
 123+ }
 124+
 125+ // build expressive string from input with enabled types:
 126+ $exprString = new SemExExpressiveString( $parameters['text'], $this->parser, $enabledTypes );
 127+
 128+ /** @ToDo: Make it possible to define options per piece type per parameter prefixes */
 129+ $options = SemExExpressiveStringOutputOptions::newFromValidatedParams( $parameters );
 130+ return $exprString->getOutput( $options );
 131+ }
 132+
 133+}
Property changes on: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExExpressiveStringPF.php
___________________________________________________________________
Added: svn:eol-style
1134 + native
Index: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExQueryPF.php
@@ -0,0 +1,89 @@
 2+<?php
 3+
 4+/**
 5+ * Class for '?' short query parser function
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SemExQueryPF.php
 10+ * @ingroup SemanticExpressiveness
 11+ *
 12+ * @author Daniel Werner < danweetz@web.de >
 13+ */
 14+class SemExQueryPF {
 15+
 16+ public static function render( Parser &$parser ) {
 17+ global $smwgQEnabled, $smwgIQRunningNumber;
 18+
 19+ if ( $smwgQEnabled ) {
 20+ $smwgIQRunningNumber++;
 21+
 22+ $params = func_get_args();
 23+ array_shift( $params ); // remove $parser
 24+ $params = static::validateParams( $params );
 25+
 26+ $query = SemExShortQuery::newFromValidatedParams( $params );
 27+ $options = SemExShortQueryOutputOptions::newFromValidatedParams( $params );
 28+
 29+ if( ! $query || ! $options ) {
 30+ // @ToDo: real error message (anyway, in what case can this happen?)
 31+ return 'FALSE';
 32+ }
 33+
 34+ $result = SemExShortQueryProcessor::getResultFromQuery( $parser, $query, $options );
 35+
 36+ if( $result === '' ) {
 37+ $result = $params['default'];
 38+ } else {
 39+ $result = // allow ' ' in form of '_' around the result
 40+ preg_replace( '/_$/', ' ', $params['intro'] ) .
 41+ $result .
 42+ preg_replace( '/^_/', ' ', $params['outro'] );
 43+ }
 44+ }
 45+ else {
 46+ $result = smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
 47+ }
 48+
 49+ return $result;
 50+ }
 51+
 52+ protected static function validateParams( array $rawParams, &$validator = null ) {
 53+ $validator = new Validator();
 54+ $validator->setFunctionParams(
 55+ $rawParams,
 56+ static::getParameters(),
 57+ array( 'property', Validator::PARAM_UNNAMED ) // 'property' is parameter 1
 58+ );
 59+ $validator->validateParameters();
 60+
 61+ return $validator->getParameterValues();
 62+ }
 63+
 64+ /**
 65+ * Returns a description of all allowed function Parameters.
 66+ *
 67+ * @return array
 68+ */
 69+ public static function getParameters() {
 70+ $params = array();
 71+
 72+ $params['intro'] = new Parameter( 'intro' );
 73+ $params['intro']->setDefault( '' );
 74+
 75+ $params['outro'] = new Parameter( 'outro' );
 76+ $params['outro']->setDefault( '' );
 77+
 78+ $params['default'] = new Parameter( 'default' );
 79+ $params['default']->setDefault( '' );
 80+
 81+ // add function parameters describing the querry and its options:
 82+ $params = array_merge(
 83+ $params,
 84+ SemExShortQuery::getPFParams(),
 85+ SemExShortQueryOutputOptions::getPFParams()
 86+ );
 87+
 88+ return $params;
 89+ }
 90+}
Property changes on: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExQueryPF.php
___________________________________________________________________
Added: svn:eol-style
191 + native
Index: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExPlainQueryPF.php
@@ -0,0 +1,19 @@
 2+<?php
 3+
 4+/**
 5+ * Class for ?!' short query parser function
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SemExPlainQueryPF.php
 10+ * @ingroup SemanticExpressiveness
 11+ *
 12+ * @author Daniel Werner < danweetz@web.de >
 13+ */
 14+class SemExPlainQueryPF extends SemExQueryPF {
 15+ public static function getParameters() {
 16+ $params = parent::getParameters();
 17+ $params['format']->setDefault( 'raw' );
 18+ return $params;
 19+ }
 20+}
Property changes on: trunk/extensions/SemanticExpressiveness/includes/parserhooks/SemExPlainQueryPF.php
___________________________________________________________________
Added: svn:eol-style
121 + native

Status & tagging log