r71859 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71858‎ | r71859 | r71860 >
Date:18:23, 28 August 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added new class for parser hooks - will refactor further later on to better integrate with the rest of Validator
Modified paths:
  • /trunk/extensions/Validator/ParserHook.php (added) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/ParserHook.php
@@ -0,0 +1,168 @@
 2+<?php
 3+
 4+abstract class ParserHook {
 5+
 6+ /**
 7+ * Gets the name of the parser hook.
 8+ *
 9+ * @since 0.4
 10+ *
 11+ * @return string
 12+ */
 13+ protected abstract function getName();
 14+
 15+ /**
 16+ * Renders and returns the output.
 17+ *
 18+ * @since 0.4
 19+ *
 20+ * @param array $parameters
 21+ *
 22+ * @return string
 23+ */
 24+ protected abstract function render( array $parameters );
 25+
 26+ /**
 27+ * Function to hook up the coordinate rendering functions to the parser.
 28+ *
 29+ * @since 0.4
 30+ *
 31+ * @param Parser $wgParser
 32+ *
 33+ * @return true
 34+ */
 35+ public function init( Parser &$wgParser ) {
 36+ $wgParser->setHook( $this->getName(), array( $this, 'renderTag' ) );
 37+ $wgParser->setFunctionHook( $this->getName(), array( $this, 'renderFunction' ) );
 38+
 39+ return true;
 40+ }
 41+
 42+ /**
 43+ * Function to add the magic word in pre MW 1.16.
 44+ *
 45+ * @since 0.4
 46+ *
 47+ * @param array $magicWords
 48+ * @param string $langCode
 49+ *
 50+ * @return true
 51+ */
 52+ public function magic( array &$magicWords, $langCode ) {
 53+ $magicWords[$this->getName()] = array( 0, $this->getName() );
 54+
 55+ return true;
 56+ }
 57+
 58+ /**
 59+ * Handler for rendering the tag hook.
 60+ *
 61+ * @since 0.4
 62+ *
 63+ * @param minxed $input string or null
 64+ * @param array $args
 65+ * @param Parser $parser
 66+ * @param PPFrame $frame
 67+ */
 68+ public function renderTag( $input, array $args, Parser $parser, PPFrame $frame ) {
 69+ $defaultParam = array_shift( $this->getDefaultParameters() );
 70+
 71+ if ( !is_null( $defaultParam ) ) {
 72+ $args[$defaultParam] = $input;
 73+ }
 74+
 75+ return $this->validateAndRender( $args, true );
 76+ }
 77+
 78+ /**
 79+ * Handler for rendering the function hook.
 80+ *
 81+ * @since 0.4
 82+ *
 83+ * @param Parser $parser
 84+ * ... further arguments ...
 85+ */
 86+ public function renderFunction() {
 87+ $args = func_get_args();
 88+
 89+ // No need for the parser...
 90+ array_shift( $args );
 91+
 92+ return array( $this->validateAndRender( $args, false ) );
 93+ }
 94+
 95+ /**
 96+ * Takes care of validation and rendering, and returns the output.
 97+ *
 98+ * @since 04
 99+ *
 100+ * @param array $arguments
 101+ * @param boolean $parsed
 102+ *
 103+ * @return string
 104+ */
 105+ public function validateAndRender( array $arguments, $parsed ) {
 106+ $manager = new ValidatorManager();
 107+
 108+ if ( $parsed ) {
 109+ $doRender = $manager->manageParsedParameters(
 110+ $arguments,
 111+ $this->getParameterInfo(),
 112+ $this->getDefaultParameters()
 113+ );
 114+ }
 115+ else {
 116+ $doRender = $manager->manageParameters(
 117+ $arguments,
 118+ $this->getParameterInfo(),
 119+ $this->getDefaultParameters()
 120+ );
 121+ }
 122+
 123+ if ( $doRender ) {
 124+ $output = $this->render( $manager->getParameters( false ) );
 125+ }
 126+ else {
 127+ $output = $this->handleErrors( $manager );
 128+ }
 129+
 130+ return $output;
 131+ }
 132+
 133+ protected function handleErrors( ValidatorManager $manager ) {
 134+ $errorList = $manager->getErrorList();
 135+
 136+ $output = '';
 137+
 138+ if ( $errorList != '' ) {
 139+ $output .= '<br />' . $errorList;
 140+ }
 141+
 142+ return $output;
 143+ }
 144+
 145+ /**
 146+ * Returns an array containing the parameter info.
 147+ * Override in deriving classes to add parameter info.
 148+ *
 149+ * @since 0.4
 150+ *
 151+ * @return array
 152+ */
 153+ protected function getParameterInfo() {
 154+ return array();
 155+ }
 156+
 157+ /**
 158+ * Returns the list of default parameters.
 159+ * Override in deriving classes to add default parameters.
 160+ *
 161+ * @since 0.4
 162+ *
 163+ * @return array
 164+ */
 165+ protected function getDefaultParameters() {
 166+ return array();
 167+ }
 168+
 169+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/ParserHook.php
___________________________________________________________________
Added: svn:eol-style
1170 + native
Index: trunk/extensions/Validator/Validator.php
@@ -33,13 +33,11 @@
3434 define( 'Validator_ERRORS_SHOW', 3 );
3535 define( 'Validator_ERRORS_STRICT', 4 );
3636
37 -$egValidatorDir = dirname( __FILE__ ) . '/';
38 -
3937 // Include the settings file.
40 -require_once( $egValidatorDir . 'Validator_Settings.php' );
 38+require_once 'Validator_Settings.php';
4139
4240 // Register the internationalization file.
43 -$wgExtensionMessagesFiles['Validator'] = $egValidatorDir . 'Validator.i18n.php';
 41+$wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php';
4442
4543 $wgExtensionCredits['other'][] = array(
4644 'path' => __FILE__,
@@ -51,8 +49,9 @@
5250 );
5351
5452 // Autoload the general classes.
55 -$wgAutoloadClasses['Validator'] = $egValidatorDir . 'Validator.class.php';
56 -$wgAutoloadClasses['ValidatorFunctions'] = $egValidatorDir . 'Validator_Functions.php';
57 -$wgAutoloadClasses['ValidatorFormats'] = $egValidatorDir . 'Validator_Formats.php';
58 -$wgAutoloadClasses['ValidatorManager'] = $egValidatorDir . 'Validator_Manager.php';
59 -$wgAutoloadClasses['TopologicalSort'] = $egValidatorDir . 'TopologicalSort.php';
\ No newline at end of file
 53+$wgAutoloadClasses['Validator'] = dirname( __FILE__ ) . '/Validator.class.php';
 54+$wgAutoloadClasses['ParserHook'] = dirname( __FILE__ ) . '/ParserHook.php';
 55+$wgAutoloadClasses['ValidatorFunctions'] = dirname( __FILE__ ) . '/Validator_Functions.php';
 56+$wgAutoloadClasses['ValidatorFormats'] = dirname( __FILE__ ) . '/Validator_Formats.php';
 57+$wgAutoloadClasses['ValidatorManager'] = dirname( __FILE__ ) . '/Validator_Manager.php';
 58+$wgAutoloadClasses['TopologicalSort'] = dirname( __FILE__ ) . '/TopologicalSort.php';
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r71860Follow up to r71859jeroendedauw18:23, 28 August 2010

Status & tagging log