2022-04-20 17:50:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TabberNeue
|
|
|
|
* Tabber Class
|
|
|
|
* Implement <tabber> tag
|
|
|
|
*
|
|
|
|
* @package TabberNeue
|
|
|
|
* @author alistair3149, Eric Fortin, Alexia E. Smith, Ciencia Al Poder
|
|
|
|
* @license GPL-3.0-or-later
|
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:TabberNeue
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
2022-06-29 21:22:14 +00:00
|
|
|
namespace MediaWiki\Extension\TabberNeue;
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2024-11-15 22:08:21 +00:00
|
|
|
use Html;
|
2024-11-16 06:56:01 +00:00
|
|
|
use InvalidArgumentException;
|
2023-07-12 02:06:57 +00:00
|
|
|
use JsonException;
|
2023-07-05 21:26:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-04-20 17:50:38 +00:00
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
2024-11-15 22:08:21 +00:00
|
|
|
use Sanitizer;
|
2024-11-16 06:28:07 +00:00
|
|
|
use TemplateParser;
|
2022-04-20 17:50:38 +00:00
|
|
|
|
|
|
|
class Tabber {
|
2024-11-15 22:08:21 +00:00
|
|
|
|
|
|
|
/** @var bool Flag that checks if this is a nested tabber */
|
2023-07-12 02:06:57 +00:00
|
|
|
private static $isNested = false;
|
|
|
|
|
2024-11-16 06:55:25 +00:00
|
|
|
/** @var bool */
|
|
|
|
private static $parseTabName = false;
|
|
|
|
|
2024-11-15 22:08:21 +00:00
|
|
|
/** @var bool */
|
2023-07-12 02:06:57 +00:00
|
|
|
private static $useCodex = false;
|
|
|
|
|
2024-11-15 22:08:21 +00:00
|
|
|
/** @var bool */
|
2024-11-16 06:55:25 +00:00
|
|
|
private static $useLegacyId = false;
|
2024-04-24 19:13:08 +00:00
|
|
|
|
2022-04-20 19:23:45 +00:00
|
|
|
/**
|
|
|
|
* Parser callback for <tabber> tag
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
2023-07-12 02:06:57 +00:00
|
|
|
* @param string|null $input
|
2022-04-20 19:23:45 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
|
|
|
* @return string HTML
|
2022-04-20 19:23:45 +00:00
|
|
|
*/
|
2023-07-12 02:06:57 +00:00
|
|
|
public static function parserHook( ?string $input, array $args, Parser $parser, PPFrame $frame ) {
|
2024-11-16 07:11:34 +00:00
|
|
|
if ( $input === null ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:13:08 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
2024-11-15 22:08:21 +00:00
|
|
|
$parserOutput = $parser->getOutput();
|
|
|
|
|
2024-04-24 19:13:08 +00:00
|
|
|
self::$parseTabName = $config->get( 'TabberNeueParseTabName' );
|
|
|
|
self::$useCodex = $config->get( 'TabberNeueUseCodex' );
|
2024-11-16 06:55:25 +00:00
|
|
|
self::$useLegacyId = $config->get( 'TabberNeueUseLegacyTabIds' );
|
|
|
|
|
2024-11-15 22:44:20 +00:00
|
|
|
$count = count( $parserOutput->getExtensionData( 'tabber-count' ) ?? [] );
|
2023-07-12 02:06:57 +00:00
|
|
|
|
2024-11-16 07:11:34 +00:00
|
|
|
$html = self::render( $input, $count, $parser, $frame );
|
2023-07-05 21:26:33 +00:00
|
|
|
|
2024-11-16 08:08:26 +00:00
|
|
|
$parserOutput->appendExtensionData( 'tabber-count', ++$count );
|
2024-11-15 22:08:21 +00:00
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
if ( self::$useCodex === true ) {
|
2023-07-06 03:00:32 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue.codex' ] );
|
|
|
|
} else {
|
2024-05-25 07:02:18 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( [ 'ext.tabberNeue.init.styles' ] );
|
2024-05-25 04:33:22 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue' ] );
|
2023-07-05 21:26:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 17:08:50 +00:00
|
|
|
$parser->addTrackingCategory( 'tabberneue-tabber-category' );
|
2022-04-20 19:32:19 +00:00
|
|
|
return $html;
|
2022-04-20 19:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* Renders the necessary HTML for a <tabber> tag.
|
|
|
|
*
|
|
|
|
* @param string $input The input URL between the beginning and ending tags.
|
2024-11-15 22:44:20 +00:00
|
|
|
* @param int $count Current Tabber count
|
2022-04-20 17:50:38 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2024-11-15 22:44:20 +00:00
|
|
|
public static function render( string $input, int $count, Parser $parser, PPFrame $frame ): string {
|
2023-07-12 02:06:57 +00:00
|
|
|
$arr = explode( '|-|', $input );
|
2024-11-15 22:08:21 +00:00
|
|
|
$tabs = '';
|
|
|
|
$tabpanels = '';
|
2024-11-15 03:59:52 +00:00
|
|
|
|
2022-04-20 17:50:38 +00:00
|
|
|
foreach ( $arr as $tab ) {
|
2024-11-15 22:44:20 +00:00
|
|
|
$tabData = self::getTabData( $tab, $count, $parser, $frame );
|
2024-11-15 22:08:21 +00:00
|
|
|
if ( $tabData === [] ) {
|
2024-11-15 03:59:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-15 05:07:11 +00:00
|
|
|
|
|
|
|
if ( self::$useCodex && self::$isNested ) {
|
2024-11-15 22:08:21 +00:00
|
|
|
$tabpanels .= self::getCodexNestedTabJSON( $tabData );
|
2024-11-15 05:07:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-15 22:08:21 +00:00
|
|
|
$tabs .= self::getTabHTML( $tabData );
|
|
|
|
$tabpanels .= self::getTabpanelHTML( $tabData );
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
if ( self::$useCodex && self::$isNested ) {
|
2024-11-15 22:08:21 +00:00
|
|
|
$tabpanels = rtrim( implode( '},', explode( '}', $tabpanels ) ), ',' );
|
|
|
|
$tabpanels = strip_tags( html_entity_decode( $tab ) );
|
|
|
|
$tabpanels = str_replace( ',,', ',', $tabpanels );
|
|
|
|
$tabpanels = str_replace( ',]', ']', $tabpanels );
|
|
|
|
return sprintf( '[%s]', $tabpanels );
|
2023-07-12 02:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 05:10:15 +00:00
|
|
|
$templateParser = new TemplateParser( __DIR__ . '/templates' );
|
|
|
|
$data = [
|
|
|
|
'count' => $count,
|
|
|
|
'html-tabs' => $tabs,
|
|
|
|
'html-tabpanels' => $tabpanels
|
|
|
|
];
|
|
|
|
return $templateParser->processTemplate( 'Tabber', $data );
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-11-15 03:59:52 +00:00
|
|
|
* Get parsed tab labels
|
2022-04-20 17:50:38 +00:00
|
|
|
*
|
2024-11-15 03:59:52 +00:00
|
|
|
* @param string $label tab label wikitext
|
2022-04-20 17:50:38 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
*
|
2024-11-15 03:59:52 +00:00
|
|
|
* @return string
|
2022-04-20 17:50:38 +00:00
|
|
|
*/
|
2024-11-15 03:59:52 +00:00
|
|
|
private static function getTabLabel( string $label, Parser $parser ): string {
|
|
|
|
$label = trim( $label );
|
|
|
|
if ( $label === '' ) {
|
2022-06-05 19:13:24 +00:00
|
|
|
return '';
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 03:59:52 +00:00
|
|
|
if ( !self::$parseTabName || self::$useCodex ) {
|
|
|
|
// Only plain text is needed
|
|
|
|
// Use language converter to get variant title and also escape html
|
|
|
|
$label = $parser->getTargetLanguageConverter()->convertHtml( $label );
|
|
|
|
} else {
|
|
|
|
// Might contains HTML
|
|
|
|
$label = $parser->recursiveTagParseFully( $label );
|
|
|
|
$label = $parser->stripOuterParagraph( $label );
|
|
|
|
}
|
|
|
|
return $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parsed tab content
|
|
|
|
*
|
|
|
|
* @param string $content tab content wikitext
|
2024-11-15 05:07:11 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
2024-11-15 03:59:52 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-11-15 05:07:11 +00:00
|
|
|
private static function getTabContent( string $content, Parser $parser, PPFrame $frame ): string {
|
2024-11-15 03:59:52 +00:00
|
|
|
$content = trim( $content );
|
|
|
|
if ( $content === '' ) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-11-15 22:08:21 +00:00
|
|
|
|
2024-11-15 05:07:11 +00:00
|
|
|
if ( !self::$useCodex ) {
|
2024-11-15 22:08:21 +00:00
|
|
|
$wikitextListMarkers = [ '*', '#', ';', ':' ];
|
|
|
|
$isWikitextList = in_array( substr( $content, 0, 1 ), $wikitextListMarkers );
|
|
|
|
if ( $isWikitextList ) {
|
|
|
|
// Fix #151, some wikitext magic
|
|
|
|
// Seems like there is no way to get rid of the mw-empty-elt paragraphs sadly
|
|
|
|
$content = "\n$content\n";
|
2024-11-15 05:07:11 +00:00
|
|
|
}
|
2024-11-15 22:08:21 +00:00
|
|
|
return $parser->recursiveTagParse( $content, $frame );
|
2024-11-15 05:07:11 +00:00
|
|
|
}
|
2024-11-15 22:08:21 +00:00
|
|
|
|
|
|
|
// The outermost tabber that must be parsed fully in codex for correct json
|
|
|
|
if ( strpos( $content, '{{#tag:tabber' ) === false ) {
|
|
|
|
return $parser->recursiveTagParseFully( $content, $frame );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$isNested = true;
|
|
|
|
$content = $parser->recursiveTagParse( $content, $frame );
|
|
|
|
self::$isNested = false;
|
2024-11-15 03:59:52 +00:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get individual tab data from wikitext.
|
|
|
|
*
|
|
|
|
* @param string $tab tab wikitext
|
2024-11-15 22:44:20 +00:00
|
|
|
* @param int $count Current Tabber count
|
2024-11-15 03:59:52 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
2024-11-15 05:07:11 +00:00
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
2024-11-15 03:59:52 +00:00
|
|
|
*
|
2024-11-16 07:46:49 +00:00
|
|
|
* @return array
|
2024-11-16 06:55:25 +00:00
|
|
|
* @throws MWException
|
2024-11-15 03:59:52 +00:00
|
|
|
*/
|
2024-11-15 22:44:20 +00:00
|
|
|
private static function getTabData( string $tab, int $count, Parser $parser, PPFrame $frame ): array {
|
2024-11-15 22:08:21 +00:00
|
|
|
$data = [];
|
2024-11-15 03:59:52 +00:00
|
|
|
if ( empty( trim( $tab ) ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
|
|
|
[ $label, $content ] = array_pad( explode( '=', $tab, 2 ), 2, '' );
|
|
|
|
|
|
|
|
$data['label'] = self::getTabLabel( $label, $parser );
|
|
|
|
// Label is empty, we cannot generate tabber
|
|
|
|
if ( $data['label'] === '' ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2024-11-15 05:07:11 +00:00
|
|
|
$data['content'] = self::getTabContent( $content, $parser, $frame );
|
2024-11-15 22:08:21 +00:00
|
|
|
|
2024-11-16 06:55:25 +00:00
|
|
|
$id = Sanitizer::escapeIdForAttribute( htmlspecialchars( $data['label'] ) );
|
|
|
|
if ( self::$useLegacyId === true ) {
|
|
|
|
$parserOutput = $parser->getOutput();
|
|
|
|
$existingIds = $parserOutput->getExtensionData( 'tabber-ids' ) ?? [];
|
|
|
|
if ( in_array( $id, $existingIds ) ) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'Duplicated Tabber labels is not allowed with $wgTabberNeueUseLegacyTabIds = true.' .
|
|
|
|
'Label was: ' . $label
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$parserOutput->appendExtensionData( 'tabber-ids', $id );
|
|
|
|
} else {
|
|
|
|
$id = "$id-$count";
|
|
|
|
}
|
2024-11-15 22:08:21 +00:00
|
|
|
$data['id'] = $id;
|
2024-11-15 03:59:52 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-11-15 22:08:21 +00:00
|
|
|
* Get the HTML for a tab.
|
2024-11-15 03:59:52 +00:00
|
|
|
*
|
|
|
|
* @param array $tabData Tab data
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2024-11-15 22:08:21 +00:00
|
|
|
private static function getTabHTML( array $tabData ): string {
|
2024-11-16 00:17:08 +00:00
|
|
|
$tabpanelId = "tabber-tabpanel-{$tabData['id']}";
|
2024-11-15 22:08:21 +00:00
|
|
|
return Html::rawElement( 'a', [
|
|
|
|
'class' => 'tabber__tab',
|
|
|
|
'id' => "tabber-tab-{$tabData['id']}",
|
2024-11-16 00:17:08 +00:00
|
|
|
'href' => "#$tabpanelId",
|
2024-11-15 22:08:21 +00:00
|
|
|
'role' => 'tab',
|
2024-11-16 00:17:08 +00:00
|
|
|
'aria-controls' => $tabpanelId
|
2024-11-15 22:08:21 +00:00
|
|
|
], $tabData['label'] );
|
|
|
|
}
|
2024-11-15 05:07:11 +00:00
|
|
|
|
2024-11-15 22:08:21 +00:00
|
|
|
/**
|
|
|
|
* Get the HTML for a tabpanel.
|
|
|
|
*
|
|
|
|
* @param array $tabData Tab data
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
private static function getTabpanelHTML( array $tabData ): string {
|
|
|
|
$content = $tabData['content'];
|
2024-11-15 05:07:11 +00:00
|
|
|
$isContentHTML = strpos( $content, '<' ) === 0;
|
|
|
|
if ( $content && !$isContentHTML ) {
|
|
|
|
// If $content does not have any HTML element (i.e. just a text node), wrap it in <p/>
|
2024-11-15 22:08:21 +00:00
|
|
|
$content = Html::rawElement( 'p', [], $content );
|
2023-07-12 02:06:57 +00:00
|
|
|
}
|
2024-11-15 22:08:21 +00:00
|
|
|
return Html::rawElement( 'article', [
|
|
|
|
'class' => 'tabber__panel',
|
2024-11-16 00:17:08 +00:00
|
|
|
'id' => "tabber-tabpanel-{$tabData['id']}",
|
|
|
|
'role' => 'tabpanel',
|
|
|
|
'tabindex' => 0,
|
|
|
|
'aria-labelledby' => "tabber-tab-{$tabData['id']}"
|
2024-11-15 22:08:21 +00:00
|
|
|
], $content );
|
2024-11-15 05:07:11 +00:00
|
|
|
}
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2024-11-15 05:07:11 +00:00
|
|
|
/**
|
|
|
|
* Get JSON representation of a nested tab for Codex
|
|
|
|
*
|
|
|
|
* @param array $tabData Tab data
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
|
|
|
private static function getCodexNestedTabJSON( array $tabData ): string {
|
|
|
|
// A nested tabber which should return json in codex
|
|
|
|
return json_encode( [
|
|
|
|
'label' => $tabData['label'],
|
|
|
|
'content' => $tabData['content']
|
|
|
|
],
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
);
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
}
|