URL: http://metabolomics.jp/ */ if( !defined( 'MEDIAWIKI' ) ){ die( 'This file is a MediaWiki extension, see HTMLForm.php.' ); } $wgExtensionFunctions[] = 'efTagFunctionSetup'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'HTMLForm', 'version' => '0.3', 'author' => 'K, Suwa', 'description' => 'Form HTML tags', 'url' => 'http://metabolomics.jp/wiki/Help:Extension/HTMLForm', ); $wgHooks['LanguageGetMagic'][] = 'efTagFunctionsLanguageGetMagic'; class TagFunctions { /** * replace AllowedTag * e.g. {{#formtag:input|type="submit"}} => * {{#formtag:form|action="-"|...}} =>
...
* * @param $parser Parser Parent parser * @param $tag string HTML tag * @param $arg string HTML tag parameter * @param $input string data which be nipped HTML tag * @return string which parse by HTMLForm.php */ function formtag( &$parser, $tag, $arg = '', $input = '' ) { $html = ""; if( strncmp( $tag, 'input', 6) == 0 ){ $html = ''; } else if( strncmp( $tag, 'form', 5 ) == 0 || strncmp( $tag, 'textarea', 9 ) == 0 || strncmp( $tag, 'select', 7 ) == 0 || strncmp( $tag, 'option', 7 ) == 0 || strncmp( $tag, 'optgroup', 9 ) == 0 || strncmp( $tag, 'fieldset', 9 ) == 0 || strncmp( $tag, 'legend', 7 ) == 0 || strncmp( $tag, 'label', 6 ) == 0){ $html = '<' . $tag . ' ' . $arg . '>' . $input . ''; } if( method_exists( $parser, 'preprocessToDom' ) ){ # mw 1.12 or later return array( $parser->preprocessToDom( $html ), 'isChildObj' => true ); } # mw 1.11 or under return $html; } /** * get posted data. * e.g. {{#get:data}} => abcdef * * @param $parser Parser Parent parser * @param $name string data name * @param $sep string separator, if data is array * @return posted data */ function get( &$parser, $name, $sep = ' ' ) { global $wgRequest, $egParamPrefix; if( !isset( $egParamPrefix ) ) $egParamPrefix = "my_"; $parser->disableCache(); if( strpos( $name, '[]' ) === FALSE ) { $data = $wgRequest->getText( "$egParamPrefix$name" ); } else { $name = str_replace( '[]', '', $name ); $tmp = $wgRequest->getArray( "$egParamPrefix$name" ); if( count( $tmp ) > 0 ) $data = implode( $sep, $tmp ); else $data = ""; } return $data; } /** * encode string. * e.g. {{#encode:a%b#c!d=e}} => a%25b%23c%21d%3De * * @param $parser Parser Parent parser * @param $str string * @param $code character code(current support is only 'EUC-JP') * @return posted encoded string */ function strEncode( &$parser, $str, $code = '' ) { $code2 = ""; if( strlen( $code ) == 0 ){ $code = urlencode( $str ); for( $i = 0; $i < strlen( $code ); $i ++ ){ $ch = substr( $code, $i, 1 ); if( strcmp( $ch, "%" ) == 0 ){ $code2 .= substr( $code, $i, 3 ); $i += 2; } else if( strcmp( $ch, "+" ) == 0 ){ $code2 .= "%20"; } else { $code2 .= "%" . bin2hex( $ch ); } } } else if( strcmp( "EUC-JP", $code ) == 0 ){ $code2 = urlencode( mb_convert_encoding( $str, "EUC-JP", "UTF-8" ) ); } return $code2; # return urlencode( $str ); } /** * decode string. * e.g. {{#decode:{{#encode:{{a%25b%23c%21d%3De}}}} => abcde * * @param $parser Parser Parent parser * @param $str encoded string * @return posted decoded string */ function strDecode( &$parser, $str ) { return urldecode( $str ); } function replaceInput( $input, $args, $parser ) { if( !isset( $args['type'] ) ){ return ''; } global $egParamPrefix; if( !isset( $egParamPrefix ) ) $egParamPrefix = "my_"; $html = 'appendParam( $html, $args ); return $html . '>'; } function replaceTextarea( $input, $args, $parser ) { $this->decodeTag( $input ); if( !isset( $args['name'] ) ){ return ''; } global $egParamPrefix; if( !isset( $egParamPrefix ) ) $egParamPrefix = "my_"; $html = ''; } function replaceSelect( $input, $args, $parser ) { $this->decodeTag( $input ); if( !isset( $args['name'] ) ){ return ''; } global $egParamPrefix; if( !isset( $egParamPrefix ) ) $egParamPrefix = "my_"; $html = ''; } function replaceOption( $input, $args, $parser ) { $this->decodeTag( $input ); $html = 'appendParam( $html, $args ); return $html . '>' . $input . ''; } function replaceOptgroup( $input, $args, $parser ) { $this->decodeTag( $input ); if( !isset( $args['label'] ) ){ return ''; } $html = 'appendParam( $html, $args ); return $html . '>' . $input . ''; } function replaceFieldset( $input, $args, $parser ) { $this->decodeTag( $input ); return '
' . $input . '
'; } function replaceLegend( $input, $args, $parser ) { $this->decodeTag( $input ); return '' . $input . ''; } function replaceLabel( $input, $args, $parser ) { $this->decodeTag( $input ); if( !isset( $args['for'] ) ){ return ''; } $html = ''; } function replaceForm( $input, $args, $parser ) { global $wgScriptPath, $wgArticlePath, $wgParser; $this->decodeTag( $input ); # exceptional hasty parse $input = $wgParser->replaceInternalLinks( $input ); $input = $wgParser->replaceExternalLinks( $input ); if( !isset( $args['action'] ) ){ return ''; } $url = $args['action']; // $pos = mb_strpos( $url, 'http://' ); // if( $pos !== false ){ if( ereg( '^[a-zA-Z]*://.*$', $url ) ){ return ''; } $pos = mb_strpos( $url, $wgScriptPath ); if( $pos === false || $pos != 0 ){ if( !isset( $wgArticlePath ) ) return ''; $pos = mb_strpos( $url, str_replace( '$1', '', $wgArticlePath ) ); } if( $pos === false || $pos != 0 ){ return ''; } return '
' . $input . '
'; } function appendParam( &$html, $args ) { if( isset( $args['id']) ){ $html .= ' id="' . $args['id'] . '"'; } if( isset( $args['class'] ) ){ $html .= ' class="' . $args['class'] . '"'; } if( isset( $args['style'] ) ){ $html .= ' style="' . $args['style'] . '"'; } } function decodeTag( &$input ) { $allow_tags = array( 'table', 't[rdh]', 'h[1-6]', '[bh]r', 'p', 'div', 'span', 'su[bp]', 'font', ); $tags = implode( '|', $allow_tags ); $regex1 = "<((\"[^\"]*\"|'[^']'|[^'\">])*)>"; $regex2 = "<((?:(input|textarea|$tags)) (\"[^\"]*\"*|'[^']*'|[^'\";])*)>"; $regex3 = "<([/]?(?:$tags)[ ]*[/]?)>"; $input = mbereg_replace( $regex1, "<\\1>", $input ); $input = mbereg_replace( $regex2, "<\\1>", $input ); $input = mbereg_replace( $regex3, "<\\1>", $input ); } } function efTagFunctionSetup() { global $wgParser; $tagFunctions = new TagFunctions; $wgParser->setHook( 'input', array( &$tagFunctions, 'replaceInput' ) ); $wgParser->setHook( 'textarea', array( &$tagFunctions, 'replaceTextarea' ) ); $wgParser->setHook( 'select', array( &$tagFunctions, 'replaceSelect' ) ); $wgParser->setHook( 'option', array( &$tagFunctions, 'replaceOption' ) ); $wgParser->setHook( 'optgroup', array( &$tagFunctions, 'replaceOptgroup' ) ); $wgParser->setHook( 'fieldset', array( &$tagFunctions, 'replaceFieldset' ) ); $wgParser->setHook( 'legend', array( &$tagFunctions, 'replaceLegend' ) ); $wgParser->setHook( 'label', array( &$tagFunctions, 'replaceLabel' ) ); $wgParser->setHook( 'form', array( &$tagFunctions, 'replaceForm' ) ); $wgParser->setFunctionHook( 'formtag', array( &$tagFunctions, 'formtag' ) ); $wgParser->setFunctionHook( 'get', array( &$tagFunctions, 'get' ) ); $wgParser->setFunctionHook( 'encode', array( &$tagFunctions, 'strEncode' ) ); $wgParser->setFunctionHook( 'decode', array( &$tagFunctions, 'strDecode' ) ); } function efTagFunctionsLanguageGetMagic( &$magicWords, $langCode ) { $magicWords['formtag'] = array( 0, 'formtag' ); $magicWords['get'] = array( 0, 'get' ); $magicWords['encode'] = array( 0, 'encode' ); $magicWords['decode'] = array( 0, 'decode' ); return true; }