2019-05-14 18:59:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2021-06-21 23:48:07 +00:00
|
|
|
* TabberNeue
|
2022-04-20 17:50:38 +00:00
|
|
|
* TabberTransclude Class
|
|
|
|
* Implement <tabbertransclude> tag
|
2019-05-14 18:59:33 +00:00
|
|
|
*
|
2021-06-21 23:48:07 +00:00
|
|
|
* @package TabberNeue
|
2022-04-19 01:45:33 +00:00
|
|
|
* @author alistair3149, Eric Fortin, Alexia E. Smith, Ciencia Al Poder
|
2021-06-21 23:48:07 +00:00
|
|
|
* @license GPL-3.0-or-later
|
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:TabberNeue
|
2021-06-21 23:56:28 +00:00
|
|
|
*/
|
2019-05-14 18:59:33 +00:00
|
|
|
|
2021-10-01 16:20:00 +00:00
|
|
|
declare( strict_types=1 );
|
|
|
|
|
2022-06-29 21:22:14 +00:00
|
|
|
namespace MediaWiki\Extension\TabberNeue;
|
2019-05-14 18:59:33 +00:00
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
use Exception;
|
2022-04-19 01:45:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-05-14 18:59:33 +00:00
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
2024-11-16 07:11:34 +00:00
|
|
|
use Sanitizer;
|
|
|
|
use TemplateParser;
|
2022-04-19 01:45:33 +00:00
|
|
|
use Title;
|
2019-05-14 18:59:33 +00:00
|
|
|
|
2022-04-20 17:50:38 +00:00
|
|
|
class TabberTransclude {
|
2024-11-19 21:01:11 +00:00
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private static $useLegacyId = false;
|
|
|
|
|
2022-04-20 19:23:45 +00:00
|
|
|
/**
|
|
|
|
* Parser callback for <tabbertransclude> 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 ) {
|
2022-04-20 19:23:45 +00:00
|
|
|
if ( $input === null ) {
|
2023-07-12 02:06:57 +00:00
|
|
|
return '';
|
2022-04-20 19:23:45 +00:00
|
|
|
}
|
2023-07-05 21:26:33 +00:00
|
|
|
|
2024-11-19 21:01:11 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
2024-11-16 07:11:34 +00:00
|
|
|
$parserOutput = $parser->getOutput();
|
|
|
|
|
2024-11-19 21:01:11 +00:00
|
|
|
self::$useLegacyId = $config->get( 'TabberNeueUseLegacyTabIds' );
|
|
|
|
|
2024-11-16 07:11:34 +00:00
|
|
|
$count = count( $parserOutput->getExtensionData( 'tabber-count' ) ?? [] );
|
|
|
|
|
2024-11-19 21:20:05 +00:00
|
|
|
$html = self::render( $input, $count, $args, $parser, $frame );
|
2024-11-16 07:11:34 +00:00
|
|
|
|
2024-05-25 07:02:18 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( [ 'ext.tabberNeue.init.styles' ] );
|
2024-11-16 07:11:34 +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-tabbertransclude-category' );
|
2022-04-20 19:32:19 +00:00
|
|
|
return $html;
|
2022-04-20 19:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 01:45:33 +00:00
|
|
|
/**
|
|
|
|
* Renders the necessary HTML for a <tabbertransclude> tag.
|
|
|
|
*
|
|
|
|
* @param string $input The input URL between the beginning and ending tags.
|
2024-11-16 07:11:34 +00:00
|
|
|
* @param int $count Current Tabber count
|
2024-11-19 21:20:05 +00:00
|
|
|
* @param array $args
|
2022-04-19 01:45:33 +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 {
|
2022-04-19 01:45:33 +00:00
|
|
|
$selected = true;
|
|
|
|
$arr = explode( "\n", $input );
|
2024-11-19 21:01:11 +00:00
|
|
|
$data = [
|
2024-11-19 21:20:05 +00:00
|
|
|
'id' => isset( $args['id'] ) ? $args['id'] : "tabber-$count",
|
|
|
|
'class' => isset( $args['class'] ) ? $args['class'] : '',
|
2024-11-19 21:01:11 +00:00
|
|
|
'array-tabs' => []
|
|
|
|
];
|
2024-11-16 07:11:34 +00:00
|
|
|
|
2022-04-19 01:45:33 +00:00
|
|
|
foreach ( $arr as $tab ) {
|
2024-11-15 03:59:52 +00:00
|
|
|
$tabData = self::getTabData( $tab );
|
2024-11-15 22:08:21 +00:00
|
|
|
if ( $tabData === [] ) {
|
2024-11-15 03:59:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-19 21:01:11 +00:00
|
|
|
|
|
|
|
$tabpanelHtml = '';
|
2023-07-12 02:06:57 +00:00
|
|
|
try {
|
2024-11-19 21:01:11 +00:00
|
|
|
$tabpanelHtml = self::buildTabTransclude( $tabData, $parser, $frame, $selected );
|
2023-07-12 02:06:57 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
// This can happen if a $currentTitle is null
|
|
|
|
continue;
|
|
|
|
}
|
2024-11-19 21:01:11 +00:00
|
|
|
|
|
|
|
$data['array-tabs'][] = [
|
|
|
|
'html-tabpanel' => $tabpanelHtml,
|
|
|
|
'label' => $tabData['label'],
|
|
|
|
'tabId' => "tabber-tab-{$tabData['id']}",
|
|
|
|
'tabpanelId' => self::$useLegacyId ? $tabData['id'] : "tabber-tabpanel-{$tabData['id']}"
|
|
|
|
];
|
2022-04-19 01:45:33 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 07:11:34 +00:00
|
|
|
$templateParser = new TemplateParser( __DIR__ . '/templates' );
|
|
|
|
return $templateParser->processTemplate( 'Tabber', $data );
|
2022-04-19 01:45:33 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 03:59:52 +00:00
|
|
|
/**
|
|
|
|
* Get individual tab data from wikitext.
|
|
|
|
*
|
|
|
|
* @param string $tab tab wikitext
|
|
|
|
*
|
2024-11-16 07:46:49 +00:00
|
|
|
* @return array
|
2024-11-15 03:59:52 +00:00
|
|
|
*/
|
|
|
|
private static function getTabData( string $tab ): array {
|
2024-11-15 22:08:21 +00:00
|
|
|
$data = [];
|
2024-11-15 03:59:52 +00:00
|
|
|
if ( empty( trim( $tab ) ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
// Transclude uses a different syntax: Page name|Tab label
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
|
|
|
[ $content, $label ] = array_pad( explode( '|', $tab, 2 ), 2, '' );
|
2024-11-16 07:11:34 +00:00
|
|
|
|
2024-11-15 03:59:52 +00:00
|
|
|
$data['label'] = trim( $label );
|
2024-11-16 07:11:34 +00:00
|
|
|
// Label is empty, we cannot generate tabber
|
|
|
|
if ( $data['label'] === '' ) {
|
|
|
|
return $data;
|
|
|
|
}
|
2024-11-15 03:59:52 +00:00
|
|
|
$data['content'] = trim( $content );
|
2024-11-16 07:11:34 +00:00
|
|
|
$data['id'] = Sanitizer::escapeIdForAttribute( htmlspecialchars( $data['label'] ) );
|
2024-11-15 03:59:52 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2022-04-19 01:45:33 +00:00
|
|
|
/**
|
|
|
|
* Build individual tab.
|
|
|
|
*
|
2024-11-15 03:59:52 +00:00
|
|
|
* @param array $tabData Tab data
|
2022-04-19 01:45:33 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
2022-06-05 19:14:03 +00:00
|
|
|
* @param bool &$selected The tab is the selected one
|
2022-04-19 01:45:33 +00:00
|
|
|
*
|
|
|
|
* @return string HTML
|
2023-07-12 02:06:57 +00:00
|
|
|
* @throws Exception
|
2022-04-19 01:45:33 +00:00
|
|
|
*/
|
2024-11-15 03:59:52 +00:00
|
|
|
private static function buildTabTransclude( array $tabData, Parser $parser, PPFrame $frame, bool &$selected ): string {
|
|
|
|
$tabName = $tabData['label'];
|
|
|
|
$pageName = $tabData['content'];
|
2022-04-19 01:45:33 +00:00
|
|
|
|
|
|
|
$dataProps = [];
|
|
|
|
$title = Title::newFromText( trim( $pageName ) );
|
|
|
|
if ( !$title ) {
|
|
|
|
if ( empty( $tabName ) ) {
|
|
|
|
$tabName = $pageName;
|
|
|
|
}
|
|
|
|
$tabBody = sprintf( '<div class="error">Invalid title: %s</div>', $pageName );
|
|
|
|
} else {
|
|
|
|
$pageName = $title->getPrefixedText();
|
|
|
|
if ( empty( $tabName ) ) {
|
|
|
|
$tabName = $pageName;
|
|
|
|
}
|
|
|
|
$dataProps['page-title'] = $pageName;
|
|
|
|
if ( $selected ) {
|
|
|
|
$tabBody = $parser->recursiveTagParseFully(
|
|
|
|
sprintf( '{{:%s}}', $pageName ),
|
|
|
|
$frame
|
|
|
|
);
|
|
|
|
} else {
|
2024-11-16 07:13:53 +00:00
|
|
|
$service = MediaWikiServices::getInstance();
|
2022-04-19 01:45:33 +00:00
|
|
|
// Add a link placeholder, as a fallback if JavaScript doesn't execute
|
2024-11-16 07:13:53 +00:00
|
|
|
$linkRenderer = $service->getLinkRenderer();
|
2022-04-19 01:45:33 +00:00
|
|
|
$tabBody = sprintf(
|
2022-04-19 02:46:40 +00:00
|
|
|
'<div class="tabber__transclusion">%s</div>',
|
2022-04-19 01:45:33 +00:00
|
|
|
$linkRenderer->makeLink( $title, null, [ 'rel' => 'nofollow' ] )
|
|
|
|
);
|
2023-07-05 22:14:52 +00:00
|
|
|
$currentTitle = $parser->getPage();
|
2022-04-19 01:45:33 +00:00
|
|
|
$query = sprintf(
|
|
|
|
'?action=parse&format=json&formatversion=2&title=%s&text={{:%s}}&redirects=1&prop=text&disablelimitreport=1&disabletoc=1&wrapoutputclass=',
|
|
|
|
urlencode( $currentTitle->getPrefixedText() ),
|
|
|
|
urlencode( $pageName )
|
|
|
|
);
|
2023-07-12 02:06:57 +00:00
|
|
|
|
2024-11-16 07:13:53 +00:00
|
|
|
$utils = $service->getUrlUtils();
|
2024-04-24 19:13:26 +00:00
|
|
|
$utils->expand( wfScript( 'api' ) . $query, PROTO_CANONICAL );
|
2023-07-12 02:06:57 +00:00
|
|
|
|
2024-04-24 19:13:26 +00:00
|
|
|
$dataProps['load-url'] = $utils->expand( wfScript( 'api' ) . $query, PROTO_CANONICAL );
|
2022-04-19 01:45:33 +00:00
|
|
|
$oldTabBody = $tabBody;
|
|
|
|
// Allow extensions to update the lazy loaded tab
|
2024-11-16 07:13:53 +00:00
|
|
|
$service->getHookContainer()->run(
|
2023-07-12 02:06:57 +00:00
|
|
|
'TabberNeueRenderLazyLoadedTab',
|
|
|
|
[ &$tabBody, &$dataProps, $parser, $frame ]
|
|
|
|
);
|
|
|
|
if ( $oldTabBody !== $tabBody ) {
|
2022-04-19 02:46:40 +00:00
|
|
|
$parser->getOutput()->recordOption( 'tabberneuelazyupdated' );
|
2022-04-19 01:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Register as a template
|
|
|
|
$revRecord = $parser->fetchCurrentRevisionRecordOfTitle( $title );
|
|
|
|
$parser->getOutput()->addTemplate(
|
|
|
|
$title,
|
|
|
|
$title->getArticleId(),
|
|
|
|
$revRecord ? $revRecord->getId() : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-16 07:11:34 +00:00
|
|
|
$tab = '<article id="tabber-tabpanel-' . $tabData['id'] . '" class="tabber__panel" data-mw-tabber-title="' . htmlspecialchars( $tabName ) . '"';
|
2022-04-19 01:45:33 +00:00
|
|
|
$tab .= implode( array_map( static function ( $prop, $value ) {
|
2024-05-25 04:33:22 +00:00
|
|
|
return sprintf( ' data-mw-tabber-%s="%s"', $prop, htmlspecialchars( $value ) );
|
2022-04-19 01:45:33 +00:00
|
|
|
}, array_keys( $dataProps ), $dataProps ) );
|
|
|
|
$tab .= '>' . $tabBody . '</article>';
|
|
|
|
$selected = false;
|
|
|
|
|
|
|
|
return $tab;
|
|
|
|
}
|
2019-05-14 18:59:33 +00:00
|
|
|
}
|