http://www.mediawiki.org/wiki/Special:Code/MediaWiki/71859

Revision: 71859
Author:   jeroendedauw
Date:     2010-08-28 18:23:04 +0000 (Sat, 28 Aug 2010)

Log Message:
-----------
Added new class for parser hooks - will refactor further later on to better 
integrate with the rest of Validator

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.php

Added Paths:
-----------
    trunk/extensions/Validator/ParserHook.php

Added: trunk/extensions/Validator/ParserHook.php
===================================================================
--- trunk/extensions/Validator/ParserHook.php                           (rev 0)
+++ trunk/extensions/Validator/ParserHook.php   2010-08-28 18:23:04 UTC (rev 
71859)
@@ -0,0 +1,168 @@
+<?php
+
+abstract class ParserHook {
+       
+       /**
+        * Gets the name of the parser hook.
+        * 
+        * @since 0.4
+        * 
+        * @return string
+        */
+       protected abstract function getName();
+       
+       /**
+        * Renders and returns the output.
+        * 
+        * @since 0.4
+        * 
+        * @param array $parameters
+        * 
+        * @return string
+        */
+       protected abstract function render( array $parameters );        
+       
+       /**
+        * Function to hook up the coordinate rendering functions to the parser.
+        * 
+        * @since 0.4
+        * 
+        * @param Parser $wgParser
+        * 
+        * @return true
+        */
+       public function init( Parser &$wgParser ) {
+               $wgParser->setHook( $this->getName(), array( $this, 'renderTag' 
) );
+               $wgParser->setFunctionHook( $this->getName(), array( $this, 
'renderFunction' ) );
+               
+               return true;
+       }
+       
+       /**
+        * Function to add the magic word in pre MW 1.16.
+        * 
+        * @since 0.4
+        * 
+        * @param array $magicWords
+        * @param string $langCode
+        * 
+        * @return true
+        */
+       public function magic( array &$magicWords, $langCode ) {
+               $magicWords[$this->getName()] = array( 0, $this->getName() );
+               
+               return true;
+       }       
+       
+       /**
+        * Handler for rendering the tag hook.
+        * 
+        * @since 0.4
+        * 
+        * @param minxed $input string or null
+        * @param array $args
+        * @param Parser $parser
+        * @param PPFrame $frame
+        */
+       public function renderTag( $input, array $args, Parser $parser, PPFrame 
$frame ) {
+               $defaultParam = array_shift( $this->getDefaultParameters() );
+               
+               if ( !is_null( $defaultParam ) ) {
+                       $args[$defaultParam] = $input;
+               }
+               
+               return $this->validateAndRender( $args, true );
+       }
+       
+       /**
+        * Handler for rendering the function hook.
+        * 
+        * @since 0.4
+        * 
+        * @param Parser $parser
+        * ... further arguments ...
+        */
+       public function renderFunction() {
+               $args = func_get_args();
+               
+               // No need for the parser...
+               array_shift( $args );   
+       
+               return array( $this->validateAndRender( $args, false ) );
+       }
+       
+       /**
+        * Takes care of validation and rendering, and returns the output.
+        * 
+        * @since 04
+        * 
+        * @param array $arguments
+        * @param boolean $parsed
+        * 
+        * @return string
+        */
+       public function validateAndRender( array $arguments, $parsed ) {
+               $manager = new ValidatorManager();
+               
+               if ( $parsed ) {
+                       $doRender = $manager->manageParsedParameters(
+                               $arguments,
+                               $this->getParameterInfo(),
+                               $this->getDefaultParameters()
+                       );                      
+               }
+               else {
+                       $doRender = $manager->manageParameters(
+                               $arguments,
+                               $this->getParameterInfo(),
+                               $this->getDefaultParameters()
+                       );
+               }
+               
+               if ( $doRender ) {
+                       $output = $this->render( $manager->getParameters( false 
) );
+               }
+               else {
+                       $output = $this->handleErrors( $manager );
+               }
+               
+               return $output;
+       }
+       
+       protected function handleErrors( ValidatorManager $manager ) {
+               $errorList = $manager->getErrorList();
+
+               $output = '';
+               
+               if ( $errorList != '' ) {
+                       $output .= '<br />' . $errorList;
+               }
+               
+               return $output;
+       }
+       
+       /**
+        * Returns an array containing the parameter info.
+        * Override in deriving classes to add parameter info.
+        * 
+        * @since 0.4
+        * 
+        * @return array
+        */
+       protected function getParameterInfo() {
+               return array();
+       }
+       
+       /**
+        * Returns the list of default parameters.
+        * Override in deriving classes to add default parameters.
+        * 
+        * @since 0.4
+        * 
+        * @return array
+        */
+       protected function getDefaultParameters() {
+               return array();
+       }       
+       
+}
\ No newline at end of file


Property changes on: trunk/extensions/Validator/ParserHook.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-08-28 16:13:54 UTC (rev 
71858)
+++ trunk/extensions/Validator/Validator.php    2010-08-28 18:23:04 UTC (rev 
71859)
@@ -33,13 +33,11 @@
 define( 'Validator_ERRORS_SHOW', 3 );
 define( 'Validator_ERRORS_STRICT', 4 );
 
-$egValidatorDir = dirname( __FILE__ ) . '/';
-
 // Include the settings file.
-require_once( $egValidatorDir . 'Validator_Settings.php' );
+require_once 'Validator_Settings.php';
 
 // Register the internationalization file.
-$wgExtensionMessagesFiles['Validator'] = $egValidatorDir . 
'Validator.i18n.php';
+$wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . 
'/Validator.i18n.php';
 
 $wgExtensionCredits['other'][] = array(
        'path' => __FILE__,
@@ -51,8 +49,9 @@
 );
 
 // Autoload the general classes.
-$wgAutoloadClasses['Validator']                        = $egValidatorDir . 
'Validator.class.php';
-$wgAutoloadClasses['ValidatorFunctions']       = $egValidatorDir . 
'Validator_Functions.php';
-$wgAutoloadClasses['ValidatorFormats']                 = $egValidatorDir . 
'Validator_Formats.php';
-$wgAutoloadClasses['ValidatorManager']                 = $egValidatorDir . 
'Validator_Manager.php';
-$wgAutoloadClasses['TopologicalSort']          = $egValidatorDir . 
'TopologicalSort.php';
\ No newline at end of file
+$wgAutoloadClasses['Validator']                        = dirname( __FILE__ ) . 
'/Validator.class.php';
+$wgAutoloadClasses['ParserHook']                       = dirname( __FILE__ ) . 
'/ParserHook.php';
+$wgAutoloadClasses['ValidatorFunctions']       = dirname( __FILE__ ) . 
'/Validator_Functions.php';
+$wgAutoloadClasses['ValidatorFormats']                 = dirname( __FILE__ ) . 
'/Validator_Formats.php';
+$wgAutoloadClasses['ValidatorManager']                 = dirname( __FILE__ ) . 
'/Validator_Manager.php';
+$wgAutoloadClasses['TopologicalSort']          = dirname( __FILE__ ) . 
'/TopologicalSort.php';
\ No newline at end of file



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to