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
|
|
|
|
2022-04-19 01:45:33 +00:00
|
|
|
use Hooks;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-05-14 18:59:33 +00:00
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
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 {
|
2022-04-20 19:23:45 +00:00
|
|
|
/**
|
|
|
|
* Parser callback for <tabbertransclude> tag
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
2022-04-20 19:23:45 +00:00
|
|
|
* @param string $input
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
public static function parserHook( string $input, array $args, Parser $parser, PPFrame $frame ) {
|
|
|
|
$tabberTransclude = new TabberTransclude();
|
2022-04-20 19:32:19 +00:00
|
|
|
$html = $tabberTransclude->render( $input, $parser, $frame );
|
2022-04-20 19:23:45 +00:00
|
|
|
if ( $input === null ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 21:45:05 +00:00
|
|
|
// Critial rendering styles
|
|
|
|
// See ext.tabberNeue.inline.less
|
|
|
|
$style = sprintf(
|
|
|
|
'<style id="tabber-style">%s</style>',
|
|
|
|
'.tabber__header{height:2.6em;box-shadow:inset 0-1px 0 0;opacity:0.1}.tabber__header:after{position:absolute;width:16ch;height:0.5em;margin-top:1em;margin-left:0.75em;background:#000;border-radius:40px;content:""}.tabber__panel:not(:first-child){display:none}'
|
|
|
|
);
|
|
|
|
$parser->getOutput()->addHeadItem( $style, true );
|
2022-04-20 19:23:45 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue' ] );
|
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.
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2022-04-20 19:23:45 +00:00
|
|
|
public static function render( $input, Parser $parser, PPFrame $frame ) {
|
2022-04-19 01:45:33 +00:00
|
|
|
$selected = true;
|
|
|
|
$arr = explode( "\n", $input );
|
|
|
|
$htmlTabs = '';
|
|
|
|
foreach ( $arr as $tab ) {
|
|
|
|
$htmlTabs .= self::buildTabTransclude( $tab, $parser, $frame, $selected );
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:45:05 +00:00
|
|
|
$noscriptMsg = wfMessage( 'tabberneue-noscript' )->text();
|
2022-04-19 01:45:33 +00:00
|
|
|
$html = '<div class="tabber">' .
|
2022-05-06 21:45:05 +00:00
|
|
|
'<header class="tabber__header"><noscript>' . $noscriptMsg . '</noscript></header>' .
|
|
|
|
'<section class="tabber__section">' . $htmlTabs . '</section></div>';
|
2022-04-19 01:45:33 +00:00
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build individual tab.
|
|
|
|
*
|
|
|
|
* @param string $tab Tab information
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
private static function buildTabTransclude( $tab, Parser $parser, PPFrame $frame, &$selected ) {
|
2022-06-05 19:13:24 +00:00
|
|
|
if ( empty( trim( $tab ) ) ) {
|
2022-04-19 01:45:33 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$tabBody = '';
|
|
|
|
$dataProps = [];
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
|
|
|
list( $pageName, $tabName ) = array_pad( explode( '|', $tab, 2 ), 2, '' );
|
|
|
|
$title = Title::newFromText( trim( $pageName ) );
|
|
|
|
if ( !$title ) {
|
|
|
|
if ( empty( $tabName ) ) {
|
|
|
|
$tabName = $pageName;
|
|
|
|
}
|
|
|
|
$tabBody = sprintf( '<div class="error">Invalid title: %s</div>', $pageName );
|
|
|
|
$pageName = '';
|
|
|
|
} else {
|
|
|
|
$pageName = $title->getPrefixedText();
|
|
|
|
if ( empty( $tabName ) ) {
|
|
|
|
$tabName = $pageName;
|
|
|
|
}
|
|
|
|
$dataProps['page-title'] = $pageName;
|
|
|
|
if ( $selected ) {
|
|
|
|
$tabBody = $parser->recursiveTagParseFully(
|
|
|
|
sprintf( '{{:%s}}', $pageName ),
|
|
|
|
$frame
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Add a link placeholder, as a fallback if JavaScript doesn't execute
|
|
|
|
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
|
|
|
|
$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' ] )
|
|
|
|
);
|
|
|
|
$dataProps['pending-load'] = '1';
|
|
|
|
// 1.37: $currentTitle = $parser->getPage();
|
|
|
|
$currentTitle = $parser->getTitle();
|
|
|
|
$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 )
|
|
|
|
);
|
|
|
|
$dataProps['load-url'] = wfExpandUrl( wfScript( 'api' ) . $query, PROTO_CANONICAL );
|
|
|
|
$oldTabBody = $tabBody;
|
|
|
|
// Allow extensions to update the lazy loaded tab
|
2022-04-19 02:46:40 +00:00
|
|
|
Hooks::run( 'TabberNeueRenderLazyLoadedTab', [ &$tabBody, &$dataProps, $parser, $frame ] );
|
2022-04-19 01:45:33 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tab = '<article class="tabber__panel" title="' . htmlspecialchars( $tabName ) . '"';
|
|
|
|
$tab .= implode( array_map( static function ( $prop, $value ) {
|
|
|
|
return sprintf( ' data-tabber-%s="%s"', $prop, htmlspecialchars( $value ) );
|
|
|
|
}, array_keys( $dataProps ), $dataProps ) );
|
|
|
|
$tab .= '>' . $tabBody . '</article>';
|
|
|
|
$selected = false;
|
|
|
|
|
|
|
|
return $tab;
|
|
|
|
}
|
2019-05-14 18:59:33 +00:00
|
|
|
}
|