r29482 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29481‎ | r29482 | r29483 >
Date:07:13, 9 January 2008
Author:tstarling
Status:old
Tags:
Comment:
Added {{#tag:}}: generic adaptor from parser function syntax to XML-style extension tag. Allows any extension tag (including nowiki and pre) to be called with input given by a template expansion. Inspired by [[Extension:TagParser]].
Modified paths:
  • /trunk/phase3/includes/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/CoreParserFunctions.php
@@ -211,5 +211,56 @@
212212 return '';
213213 }
214214 }
 215+
 216+ /**
 217+ * Parser function to extension tag adaptor
 218+ */
 219+ public static function tagObj( $parser, $frame, $args ) {
 220+ $xpath = false;
 221+ if ( !count( $args ) ) {
 222+ return '';
 223+ }
 224+ $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
 225+ $stripList = $parser->getStripList();
 226+ if ( !in_array( $tagName, $stripList ) ) {
 227+ return '<span class="error">' .
 228+ wfMsg( 'unknown_extension_tag', $tagName ) .
 229+ '</span>';
 230+ }
 231+
 232+ $lastNumberedNode = false;
 233+ $attributes = array();
 234+ foreach ( $args as $arg ) {
 235+ if ( !$xpath ) {
 236+ $xpath = new DOMXPath( $arg->ownerDocument );
 237+ }
 238+ $names = $xpath->query( 'name', $arg );
 239+ if ( $names->item( 0 )->hasAttributes() ) {
 240+ $lastNumberedNode = $arg;
 241+ } else {
 242+ $name = $frame->expand( $names->item( 0 ), PPFrame::STRIP_COMMENTS );
 243+ if ( preg_match( '/^\d+$/', $name ) ) {
 244+ // For = suppression syntax {{#tag|thing|1=2=3=4}}
 245+ $lastNumberedNode = $arg;
 246+ } else {
 247+ $values = $xpath->query( 'value', $arg );
 248+ $attributes[$name] = trim( $frame->expand( $values->item( 0 ) ) );
 249+ }
 250+ }
 251+ }
 252+
 253+ if ( !$lastNumberedNode ) {
 254+ $inner = null;
 255+ } else {
 256+ $values = $xpath->query( 'value', $lastNumberedNode );
 257+ $inner = $frame->expand( $values->item( 0 ) );
 258+ }
 259+ $params = array(
 260+ 'name' => $tagName,
 261+ 'inner' => $inner,
 262+ 'attributes' => $attributes
 263+ );
 264+ return $parser->extensionSubstitution( $params, $frame );
 265+ }
215266 }
216267
Index: trunk/phase3/includes/Parser.php
@@ -166,6 +166,7 @@
167167 $this->setFunctionHook( 'special', array( 'CoreParserFunctions', 'special' ) );
168168 $this->setFunctionHook( 'defaultsort', array( 'CoreParserFunctions', 'defaultsort' ), SFH_NO_HASH );
169169 $this->setFunctionHook( 'filepath', array( 'CoreParserFunctions', 'filepath' ), SFH_NO_HASH );
 170+ $this->setFunctionHook( 'tag', array( 'CoreParserFunctions', 'tagObj' ), SFH_OBJECT_ARGS );
170171
171172 if ( $wgAllowDisplayTitle ) {
172173 $this->setFunctionHook( 'displaytitle', array( 'CoreParserFunctions', 'displaytitle' ), SFH_NO_HASH );
@@ -3661,7 +3662,8 @@
36623663 *
36633664 * @param array $params Associative array of parameters:
36643665 * name DOMNode for the tag name
3665 - * attrText DOMNode for unparsed text where tag attributes are thought to be
 3666+ * attr DOMNode for unparsed text where tag attributes are thought to be
 3667+ * attributes Optional associative array of parsed attributes
36663668 * inner Contents of extension element
36673669 * noClose Original text did not have a close tag
36683670 * @param PPFrame $frame
@@ -3671,15 +3673,18 @@
36723674 static $n = 1;
36733675
36743676 $name = $frame->expand( $params['name'] );
3675 - $attrText = is_null( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
3676 - $content = is_null( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
 3677+ $attrText = !isset( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
 3678+ $content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
36773679
36783680 $marker = "{$this->mUniqPrefix}-$name-" . sprintf('%08X', $n++) . $this->mMarkerSuffix;
36793681
36803682 if ( $this->ot['html'] ) {
36813683 $name = strtolower( $name );
36823684
3683 - $params = Sanitizer::decodeTagAttributes( $attrText );
 3685+ $attributes = Sanitizer::decodeTagAttributes( $attrText );
 3686+ if ( isset( $params['attributes'] ) ) {
 3687+ $attributes = $attributes + $params['attributes'];
 3688+ }
36843689 switch ( $name ) {
36853690 case 'html':
36863691 if( $wgRawHtml ) {
@@ -3693,15 +3698,15 @@
36943699 break;
36953700 case 'math':
36963701 $output = $wgContLang->armourMath(
3697 - MathRenderer::renderMath( $content, $params ) );
 3702+ MathRenderer::renderMath( $content, $attributes ) );
36983703 break;
36993704 case 'gallery':
3700 - $output = $this->renderImageGallery( $content, $params );
 3705+ $output = $this->renderImageGallery( $content, $attributes );
37013706 break;
37023707 default:
37033708 if( isset( $this->mTagHooks[$name] ) ) {
37043709 $output = call_user_func_array( $this->mTagHooks[$name],
3705 - array( $content, $params, $this ) );
 3710+ array( $content, $attributes, $this ) );
37063711 } else {
37073712 throw new MWException( "Invalid call hook $name" );
37083713 }
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -335,6 +335,7 @@
336336 'special' => array( 0, 'special', ),
337337 'defaultsort' => array( 1, 'DEFAULTSORT:', 'DEFAULTSORTKEY:', 'DEFAULTCATEGORYSORT:' ),
338338 'filepath' => array( 0, 'FILEPATH:' ),
 339+ 'tag' => array( 0, 'tag' ),
339340 );
340341
341342 /**
@@ -3115,4 +3116,7 @@
31163117 'signature' => '[[{{ns:user}}:$1|$2]]', # don't translate or duplicate this message to other languages
31173118 'signature-anon' => '[[{{#special:Contributions}}/$1|$2]]', # don't translate or duplicate this message to other languages
31183119
 3120+# CoreParserFunctions
 3121+'unknown_extension_tag' => 'Unknown extension tag "$1"',
 3122+
31193123 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r29493* Added Persian translations...huji13:54, 9 January 2008

Status & tagging log