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-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
|
|
|
|
2024-11-16 06:55:25 +00:00
|
|
|
/** @var bool */
|
|
|
|
private static $parseTabName = 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' );
|
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-19 21:20:05 +00:00
|
|
|
$html = self::render( $input, $count, $args, $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
|
|
|
|
2024-11-19 21:04:26 +00:00
|
|
|
$parserOutput->addModuleStyles( [ 'ext.tabberNeue.init.styles' ] );
|
|
|
|
$parserOutput->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
|
2024-11-19 21:20:05 +00:00
|
|
|
* @param array $args
|
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-21 17:42:47 +00:00
|
|
|
public static function render( string $input, int $count, array $args, Parser $parser, PPFrame $frame ): string {
|
2024-11-22 21:02:46 +00:00
|
|
|
$attr = [
|
|
|
|
'id' => "tabber-$count",
|
|
|
|
'class' => 'tabber tabber--init'
|
|
|
|
];
|
|
|
|
|
2024-11-22 21:03:20 +00:00
|
|
|
foreach ( $args as $attribute => $value ) {
|
2024-11-22 21:02:46 +00:00
|
|
|
$attr = Sanitizer::mergeAttributes( $attr, [ $attribute => $value ] );
|
|
|
|
}
|
|
|
|
|
2024-11-19 21:01:11 +00:00
|
|
|
$data = [
|
2024-11-22 21:02:46 +00:00
|
|
|
'array-tabs' => [],
|
|
|
|
'html-attributes' => Sanitizer::safeEncodeTagAttributes( Sanitizer::validateTagAttributes( $attr, 'div' ) )
|
2024-11-19 21:01:11 +00:00
|
|
|
];
|
|
|
|
|
2024-11-22 21:02:46 +00:00
|
|
|
$arr = explode( '|-|', $input );
|
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
|
|
|
|
2024-11-19 21:01:11 +00:00
|
|
|
$data['array-tabs'][] = [
|
|
|
|
'content' => $tabData['content'],
|
|
|
|
'label' => $tabData['label'],
|
|
|
|
'tabId' => "tabber-tab-{$tabData['id']}",
|
|
|
|
'tabpanelId' => self::$useLegacyId ? $tabData['id'] : "tabber-tabpanel-{$tabData['id']}"
|
|
|
|
];
|
2023-07-12 02:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 05:10:15 +00:00
|
|
|
$templateParser = new TemplateParser( __DIR__ . '/templates' );
|
|
|
|
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-19 21:04:26 +00:00
|
|
|
if ( !self::$parseTabName ) {
|
2024-11-15 03:59:52 +00:00
|
|
|
// 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-19 21:04:26 +00:00
|
|
|
// Insert a new line for these characters in wikitext (#151)
|
|
|
|
// Seems like there is no way to get rid of the mw-empty-elt paragraphs sadly
|
2024-11-19 21:05:00 +00:00
|
|
|
$wikitextCharacters = [ '*', '#', ';', ':', '[' ];
|
2024-11-19 21:04:26 +00:00
|
|
|
$needsNewLine = in_array( substr( $content, 0, 1 ), $wikitextCharacters );
|
|
|
|
if ( $needsNewLine ) {
|
|
|
|
$content = "\n$content\n";
|
2024-11-15 05:07:11 +00:00
|
|
|
}
|
2024-11-19 21:04:26 +00:00
|
|
|
return $parser->recursiveTagParse( $content, $frame );
|
2024-11-15 03:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-19 21:01:11 +00:00
|
|
|
$isContentHTML = strpos( $content, '<' ) === 0;
|
|
|
|
if ( $data['content'] && !$isContentHTML ) {
|
|
|
|
// If $content does not have any HTML element (i.e. just a text node), wrap it in <p/>
|
|
|
|
$data['content'] = Html::rawElement( 'p', [], $data['content'] );
|
|
|
|
}
|
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;
|
|
|
|
}
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|