r110713 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110712‎ | r110713 | r110714 >
Date:03:01, 5 February 2012
Author:bawolff
Status:deferred
Tags:
Comment:
Make this extension use a tag hook instead of a parser function
Modified paths:
  • /trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php (modified) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions.php (modified) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PreviewFunctions/PreviewFunctions.php
@@ -32,7 +32,7 @@
3333 'path' => __FILE__,
3434 'name' => 'PreviewFunctions',
3535 'descriptionmsg' => 'previewfunctions-desc',
36 - 'version' => 1,
 36+ 'version' => 2,
3737 'url' => 'https://mediawiki.org/wiki/Extension:PreviewFunctions',
3838 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]',
3939 );
Index: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php
@@ -3,5 +3,4 @@
44
55 $magicWords['en'] = array(
66 'ifpreview' => array( 0, 'ifpreview', 'onpreview' ),
7 - 'previewwarning' => array( 0, 'addpreviewwarning' ),
87 );
Index: trunk/extensions/PreviewFunctions/PreviewFunctions_body.php
@@ -7,7 +7,7 @@
88 */
99 public static function register( Parser $parser ) {
1010 $parser->setFunctionHook( 'ifpreview', 'PreviewFunctions::ifPreview', Parser::SFH_OBJECT_ARGS );
11 - $parser->setFunctionHook( 'previewwarning', 'PreviewFunctions::addWarning' );
 11+ $parser->setHook( 'previewwarning', 'PreviewFunctions::addWarning' );
1212 return true;
1313 }
1414
@@ -21,32 +21,42 @@
2222 }
2323 }
2424
25 - public static function addWarning( $parser, $warning ) {
 25+ /**
 26+ * Syntax: <previewwarning>Warning here</previewwarning>
 27+ * or: <previewwarning nodiv="true">Warning here</previewwarning>
 28+ * or: {{#tag:previewwarning|warning here}}
 29+ *
 30+ * Tried to do this as a parserfunction, but just couldn't get
 31+ * original wikitext back (even with PPFrame::RECOVER_ORIG )
 32+ * so doing a tag hook.
 33+ *
 34+ * This could also potentially record images/links used, but i think its
 35+ * better that it doesn't (undecided on that).
 36+ */
 37+ public static function addWarning( $warning, $args, $parser, $frame ) {
2638 if ( $warning === '' ) return '';
2739
28 - // Not 100% sure if doing this right.
2940 // Note, EditPage.php parses such warnings
3041 // (but with a different parser object)
3142
32 - // Even with something like PPFrame::RECOVER_ORIG - tag extensions are still expanded.
33 -
34 - // Not doing:
35 - // $warning = $parser->mStripState->unstripGeneral( $warning );
36 - // Because double parses things that should be treated as html.
37 -
38 - // Based on a line in CoreParserFunctions::displaytitle.
39 - // Can't just substitute a <nowiki>$1</nowiki> and then unstripNoWiki, since that doesn't work
40 - // for other extensions that put nowiki content (Since some like $wgRawHtml's <html>
41 - // would not like the tag escaping done by <nowiki>.
42 - // I suppose I could look for the nowiki part of "UNIQ7d3e18c0572c78c3-nowiki-00000031-QINU"
43 - // but thats super hacky. So just delete the strip items.
 43+ // To make it work when using {{#tag:...}} syntax. (or i suppose fail better...)
4444 $warning = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
4545 . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/',
4646 '',
4747 $warning
4848 );
4949
50 - $warning = Html::rawElement( 'div', array( 'class' => 'error mw-previewfunc-warning' ), $warning );
 50+
 51+ // Could do something complicated here to expand {{{1}}} but not anything else
 52+ // (aka new parser instance and then do Parser::preprocess on it, i think.
 53+ // Don't want to do Parser::recursivePreprocess as that does extensions too)
 54+ // but not doing that (at least for now).
 55+
 56+
 57+ if ( !isset( $args['nodiv'] ) ) {
 58+ $warning = Html::rawElement( 'div', array( 'class' => 'error mw-previewfunc-warning' ), $warning );
 59+ }
5160 $parser->getOutput()->addWarning( $warning );
 61+ return '';
5262 }
5363 }

Status & tagging log