r79190 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79189‎ | r79190 | r79191 >
Date:17:11, 29 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added describe parser hook for automatic creation of parser hook documentation.
Modified paths:
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Settings.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/parserHooks/Validator_Describe.php (added) (history)

Diff [purge]

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
1166 + native
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -17,6 +17,39 @@
1818 const TYPE_FUNCTION = 1;
1919
2020 /**
 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+ /**
2154 * @since 0.4
2255 *
2356 * @var Validator
@@ -90,6 +123,8 @@
91124 $className = get_class( $this );
92125
93126 foreach ( $this->getNames() as $name ) {
 127+ self::$registeredHooks[$name] = $className;
 128+
94129 if ( $this->forTagExtensions ) {
95130 $wgParser->setHook(
96131 $this->getTagName( $name ),
@@ -362,8 +397,24 @@
363398 */
364399 protected function getDefaultParameters( $type ) {
365400 return array();
366 - }
 401+ }
367402
 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+
368419 }
369420
370421 /**
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -688,6 +688,17 @@
689689 }
690690
691691 /**
 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+ /**
692703 * Set if the parameter manipualations should be applied to the default value.
693704 *
694705 * @since 0.4
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -41,6 +41,9 @@
4242 'validator-listerrors-high' => 'High',
4343 'validator-listerrors-fatal' => 'Fatal',
4444
 45+ // Describe
 46+ 'validator-describe-notfound' => 'There is no parser hook that handles "$1".',
 47+
4548 // Criteria errors for single values
4649 'validator_error_empty_argument' => 'Parameter $1 can not have an empty value.',
4750 'validator_error_must_be_number' => 'Parameter $1 can only be a number.',
Index: trunk/extensions/Validator/Validator.php
@@ -75,6 +75,7 @@
7676 $wgAutoloadClasses['ParamManipulationImplode'] = $incDir . 'manipulations/ParamManipulationImplode.php';
7777 $wgAutoloadClasses['ParamManipulationInteger'] = $incDir . 'manipulations/ParamManipulationInteger.php';
7878
 79+$wgAutoloadClasses['ValidatorDescribe'] = $incDir . 'parserHooks/Validator_Describe.php';
7980 $wgAutoloadClasses['ValidatorListErrors'] = $incDir . 'parserHooks/Validator_ListErrors.php';
8081 unset( $incDir );
8182
Index: trunk/extensions/Validator/Validator_Settings.php
@@ -21,6 +21,10 @@
2222 $wgHooks['ParserFirstCallInit'][] = 'ValidatorListErrors::staticInit';
2323 $wgHooks['LanguageGetMagic'][] = 'ValidatorListErrors::staticMagic';
2424
 25+# Registration of the describe parser hooks.
 26+$wgHooks['ParserFirstCallInit'][] = 'ValidatorDescribe::staticInit';
 27+$wgHooks['LanguageGetMagic'][] = 'ValidatorDescribe::staticMagic';
 28+
2529 # Maps actions to error severity.
2630 # ACTION_LOG will cause the error to be logged
2731 # ACTION_WARN will cause a notice that there is an error to be shown inline

Follow-up revisions

RevisionCommit summaryAuthorDate
r79192Follow up to r79190jeroendedauw17:36, 29 December 2010

Status & tagging log