Index: trunk/extensions/Validator/includes/parserHooks/Validator_Describe.php |
— | — | @@ -0,0 +1,164 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for the 'describe' parser hooks. |
| 6 | + * |
| 7 | + * @since 0.4.3 |
| 8 | + * |
| 9 | + * @file Validator_Describe.php |
| 10 | + * @ingroup Validator |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 or later |
| 13 | + * |
| 14 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 15 | + */ |
| 16 | +class ValidatorDescribe extends ParserHook { |
| 17 | + |
| 18 | + /** |
| 19 | + * No LST in pre-5.3 PHP *sigh*. |
| 20 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
| 21 | + */ |
| 22 | + public static function staticMagic( array &$magicWords, $langCode ) { |
| 23 | + $className = __CLASS__; |
| 24 | + $instance = new $className(); |
| 25 | + return $instance->magic( $magicWords, $langCode ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * No LST in pre-5.3 PHP *sigh*. |
| 30 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
| 31 | + */ |
| 32 | + public static function staticInit( Parser &$wgParser ) { |
| 33 | + $className = __CLASS__; |
| 34 | + $instance = new $className(); |
| 35 | + return $instance->init( $wgParser ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets the name of the parser hook. |
| 40 | + * @see ParserHook::getName |
| 41 | + * |
| 42 | + * @since 0.4.3 |
| 43 | + * |
| 44 | + * @return string |
| 45 | + */ |
| 46 | + protected function getName() { |
| 47 | + return 'describe'; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns an array containing the parameter info. |
| 52 | + * @see ParserHook::getParameterInfo |
| 53 | + * |
| 54 | + * @since 0.4.3 |
| 55 | + * |
| 56 | + * @return array of Parameter |
| 57 | + */ |
| 58 | + protected function getParameterInfo( $type ) { |
| 59 | + $params = array(); |
| 60 | + |
| 61 | + $params['hooks'] = new ListParameter( 'hooks' ); |
| 62 | + |
| 63 | + return $params; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the list of default parameters. |
| 68 | + * @see ParserHook::getDefaultParameters |
| 69 | + * |
| 70 | + * @since 0.4.3 |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + protected function getDefaultParameters( $type ) { |
| 75 | + return array( 'hooks' ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Renders and returns the output. |
| 80 | + * @see ParserHook::render |
| 81 | + * |
| 82 | + * @since 0.4.3 |
| 83 | + * |
| 84 | + * @param array $parameters |
| 85 | + * |
| 86 | + * @return string |
| 87 | + */ |
| 88 | + public function render( array $parameters ) { |
| 89 | + $parts = array(); |
| 90 | + |
| 91 | + foreach ( $parameters['hooks'] as $hookName ) { |
| 92 | + $parserHook = $this->getParserHookInstance( $hookName ); |
| 93 | + |
| 94 | + if ( $parserHook === false ) { |
| 95 | + $parts[] = wfMsgExt( 'validator-describe-notfound', 'parsemag', $hookName ); |
| 96 | + } |
| 97 | + else { |
| 98 | + $parts[] = $this->getParserHookDescription( $parserHook ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return implode( "\n\n", $parts ); |
| 103 | + } |
| 104 | + |
| 105 | + protected function getParserHookDescription( ParserHook $parserHook ) { |
| 106 | + $descriptionData = $parserHook->getDescriptionData( ParserHook::TYPE_TAG ); // TODO |
| 107 | + |
| 108 | + $tableRows = array(); |
| 109 | + |
| 110 | + foreach ( $descriptionData['parameters'] as $parameter ) { |
| 111 | + $tableRows[] = $this->getDescriptionRow( $parameter ); |
| 112 | + } |
| 113 | + |
| 114 | + if ( count( $tableRows ) > 0 ) { |
| 115 | + $tableRows = array_merge( array( '! Parameter |
| 116 | +! Aliases |
| 117 | +! Default |
| 118 | +! Usage' ), $tableRows ); |
| 119 | + |
| 120 | + $table = implode( "\n|-\n", $tableRows ); |
| 121 | + |
| 122 | + $table = <<<EOT |
| 123 | +{| class="wikitable sortable"' |
| 124 | +{$table} |
| 125 | +|} |
| 126 | +EOT; |
| 127 | + } |
| 128 | + |
| 129 | + //return $table; // TODO |
| 130 | + return '<pre>' . $table . '</pre>'; |
| 131 | + } |
| 132 | + |
| 133 | + protected function getDescriptionRow( Parameter $parameter ) { |
| 134 | + $aliases = $parameter->getAliases(); |
| 135 | + $aliases = count( $aliases ) > 0 ? implode( ', ', $aliases ) : '-'; |
| 136 | + |
| 137 | + $default = $parameter->isRequired() ? "''required''" : $parameter->getDefault(); |
| 138 | + if ( $default == '' ) $default = "''empty''"; |
| 139 | + |
| 140 | + // TODO |
| 141 | + |
| 142 | + return <<<EOT |
| 143 | +| {$parameter->getName()} |
| 144 | +| {$aliases} |
| 145 | +| {$default} |
| 146 | +| Description be here. |
| 147 | +EOT; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns an instance of the class handling the specified parser hook, |
| 152 | + * or false if there is none. |
| 153 | + * |
| 154 | + * @since 0.4.3 |
| 155 | + * |
| 156 | + * @param string $parserHookName |
| 157 | + * |
| 158 | + * @return mixed ParserHook or false |
| 159 | + */ |
| 160 | + protected function getParserHookInstance( $parserHookName ) { |
| 161 | + $className = ParserHook::getHookClassName( $parserHookName ); |
| 162 | + return $className !== false && class_exists( $className ) ? new $className() : false; |
| 163 | + } |
| 164 | + |
| 165 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/Validator/includes/parserHooks/Validator_Describe.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 166 | + native |
Index: trunk/extensions/Validator/includes/ParserHook.php |
— | — | @@ -17,6 +17,39 @@ |
18 | 18 | const TYPE_FUNCTION = 1; |
19 | 19 | |
20 | 20 | /** |
| 21 | + * @since 0.4.3 |
| 22 | + * |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + protected static $registeredHooks = array(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns an array of registered parser hooks (keys) and their handling |
| 29 | + * ParserHook deriving class names (values). |
| 30 | + * |
| 31 | + * @since 0.4.3 |
| 32 | + * |
| 33 | + * @return array |
| 34 | + */ |
| 35 | + public static function getRegisteredParserHooks() { |
| 36 | + return self::$registeredHooks; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the name of the ParserHook deriving class that defines a certain parser hook, |
| 41 | + * or false if there is none. |
| 42 | + * |
| 43 | + * @since 0.4.3 |
| 44 | + * |
| 45 | + * @param string $hookName |
| 46 | + * |
| 47 | + * @return mixed string or false |
| 48 | + */ |
| 49 | + public static function getHookClassName( $hookName ) { |
| 50 | + return array_key_exists( $hookName, self::$registeredHooks ) ? self::$registeredHooks[$hookName] : false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
21 | 54 | * @since 0.4 |
22 | 55 | * |
23 | 56 | * @var Validator |
— | — | @@ -90,6 +123,8 @@ |
91 | 124 | $className = get_class( $this ); |
92 | 125 | |
93 | 126 | foreach ( $this->getNames() as $name ) { |
| 127 | + self::$registeredHooks[$name] = $className; |
| 128 | + |
94 | 129 | if ( $this->forTagExtensions ) { |
95 | 130 | $wgParser->setHook( |
96 | 131 | $this->getTagName( $name ), |
— | — | @@ -362,8 +397,24 @@ |
363 | 398 | */ |
364 | 399 | protected function getDefaultParameters( $type ) { |
365 | 400 | return array(); |
366 | | - } |
| 401 | + } |
367 | 402 | |
| 403 | + /** |
| 404 | + * Returns the data needed to describe the parser hook. |
| 405 | + * |
| 406 | + * @since 0.4.3 |
| 407 | + * |
| 408 | + * @param integer $type Item of the ParserHook::TYPE_ enum |
| 409 | + * |
| 410 | + * @return array |
| 411 | + */ |
| 412 | + public function getDescriptionData( $type ) { |
| 413 | + return array( |
| 414 | + 'parameters' => $this->getParameterInfo( $type ), |
| 415 | + 'defaults' => $this->getDefaultParameters( $type ), |
| 416 | + ); |
| 417 | + } |
| 418 | + |
368 | 419 | } |
369 | 420 | |
370 | 421 | /** |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -688,6 +688,17 @@ |
689 | 689 | } |
690 | 690 | |
691 | 691 | /** |
| 692 | + * Returns the default value. |
| 693 | + * |
| 694 | + * @since 0.4.3 |
| 695 | + * |
| 696 | + * @return mixed |
| 697 | + */ |
| 698 | + public function getDefault() { |
| 699 | + return $this->default; |
| 700 | + } |
| 701 | + |
| 702 | + /** |
692 | 703 | * Set if the parameter manipualations should be applied to the default value. |
693 | 704 | * |
694 | 705 | * @since 0.4 |
Index: trunk/extensions/Validator/Validator.i18n.php |
— | — | @@ -41,6 +41,9 @@ |
42 | 42 | 'validator-listerrors-high' => 'High', |
43 | 43 | 'validator-listerrors-fatal' => 'Fatal', |
44 | 44 | |
| 45 | + // Describe |
| 46 | + 'validator-describe-notfound' => 'There is no parser hook that handles "$1".', |
| 47 | + |
45 | 48 | // Criteria errors for single values |
46 | 49 | 'validator_error_empty_argument' => 'Parameter $1 can not have an empty value.', |
47 | 50 | 'validator_error_must_be_number' => 'Parameter $1 can only be a number.', |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -75,6 +75,7 @@ |
76 | 76 | $wgAutoloadClasses['ParamManipulationImplode'] = $incDir . 'manipulations/ParamManipulationImplode.php'; |
77 | 77 | $wgAutoloadClasses['ParamManipulationInteger'] = $incDir . 'manipulations/ParamManipulationInteger.php'; |
78 | 78 | |
| 79 | +$wgAutoloadClasses['ValidatorDescribe'] = $incDir . 'parserHooks/Validator_Describe.php'; |
79 | 80 | $wgAutoloadClasses['ValidatorListErrors'] = $incDir . 'parserHooks/Validator_ListErrors.php'; |
80 | 81 | unset( $incDir ); |
81 | 82 | |
Index: trunk/extensions/Validator/Validator_Settings.php |
— | — | @@ -21,6 +21,10 @@ |
22 | 22 | $wgHooks['ParserFirstCallInit'][] = 'ValidatorListErrors::staticInit'; |
23 | 23 | $wgHooks['LanguageGetMagic'][] = 'ValidatorListErrors::staticMagic'; |
24 | 24 | |
| 25 | +# Registration of the describe parser hooks. |
| 26 | +$wgHooks['ParserFirstCallInit'][] = 'ValidatorDescribe::staticInit'; |
| 27 | +$wgHooks['LanguageGetMagic'][] = 'ValidatorDescribe::staticMagic'; |
| 28 | + |
25 | 29 | # Maps actions to error severity. |
26 | 30 | # ACTION_LOG will cause the error to be logged |
27 | 31 | # ACTION_WARN will cause a notice that there is an error to be shown inline |