Move table of contents code to SkinVector22

This avoids the RuntimeException in old Vector, and means
we can safely enable this on the beta cluster

Note, for any users that are in migration mode e.g. skin is
set to 'vector' and the skin version is set to 2 they will
not see a table of contents in the sidebar or article.

This won't be a problem in production provided T299104
is resolved before we enable.

Change-Id: I942a1cb933e7364600fe1af7491aca20546545e5
This commit is contained in:
Jon Robson 2022-02-18 16:53:42 -08:00 committed by Jdlrobson
parent 29e40714bc
commit ed4b45f44a
2 changed files with 33 additions and 37 deletions

View File

@ -154,18 +154,6 @@ class SkinVector extends SkinMustache {
*/
private const OPT_OUT_LINK_TRACKING_CODE = 'vctw1';
/**
* Updates the constructor to conditionally disable table of contents in article
* body.
* Note, the constructor can only check feature flags that do not vary on whether the
* user is logged in e.g. features with the 'default' key set.
* @inheritDoc
*/
public function __construct( array $options ) {
$options['toc'] = !$this->isTableOfContentsVisibleInSidebar();
parent::__construct( $options );
}
/**
* @param string $icon the name of the icon without wikimedia- prefix.
* @return string
@ -455,12 +443,6 @@ class SkinVector extends SkinMustache {
public function generateHTML() {
if ( $this->isLegacy() ) {
$this->options['template'] = SkinVectorLegacy::getTemplateOption();
if ( $this->isTableOfContentsVisibleInSidebar() ) {
throw new RuntimeException(
'The table of contents flag cannot safely be applied without ' .
'breaking legacy Vector. Please fix T291098 before applying.'
);
}
}
return parent::generateHTML();
}
@ -565,21 +547,6 @@ class SkinVector extends SkinMustache {
$shouldShowOnMainPage;
}
/**
* Determines if the Table of Contents should be visible.
* TOC is visible on main namespaces except for the Main Page
* when the feature flag is on.
*
* @return bool
*/
private function isTableOfContentsVisibleInSidebar(): bool {
$featureManager = VectorServices::getFeatureManager();
$title = $this->getTitle();
$isMainNS = $title ? $title->inNamespaces( 0 ) : false;
$isMainPage = $title ? $title->isMainPage() : false;
return $featureManager->isFeatureEnabled( Constants::FEATURE_TABLE_OF_CONTENTS ) && $isMainNS && !$isMainPage;
}
/**
* @inheritDoc
*/
@ -641,10 +608,6 @@ class SkinVector extends SkinMustache {
) : false,
] );
if ( !$this->isTableOfContentsVisibleInSidebar() ) {
unset( $commonSkinData['data-toc'] );
}
if ( $skin->getUser()->isRegistered() ) {
$migrationMode = $this->getConfig()->get( 'VectorSkinMigrationMode' );
$query = $migrationMode ? 'useskin=vector&' : '';

View File

@ -1,4 +1,8 @@
<?php
use Vector\Constants;
use Vector\VectorServices;
/**
* @ingroup Skins
* @package Vector
@ -7,6 +11,7 @@
class SkinVector22 extends SkinVector {
/**
* @inheritDoc
* Updates the constructor to conditionally disable table of contents in article body.
*/
public function __construct( $options = [] ) {
$options += [
@ -14,9 +19,26 @@ class SkinVector22 extends SkinVector {
'scripts' => self::getScriptsOption(),
'styles' => self::getStylesOption(),
];
$options['toc'] = !$this->isTableOfContentsVisibleInSidebar();
parent::__construct( $options );
}
/**
* Determines if the Table of Contents should be visible.
* TOC is visible on main namespaces except for the Main Page
* when the feature flag is on.
*
* @return bool
*/
private function isTableOfContentsVisibleInSidebar(): bool {
$featureManager = VectorServices::getFeatureManager();
$title = $this->getTitle();
$isMainNS = $title ? $title->inNamespaces( 0 ) : false;
$isMainPage = $title ? $title->isMainPage() : false;
return $featureManager->isFeatureEnabled( Constants::FEATURE_TABLE_OF_CONTENTS ) && $isMainNS && !$isMainPage;
}
/**
* Temporary static function while we deprecate SkinVector class.
*
@ -53,4 +75,15 @@ class SkinVector22 extends SkinVector {
'mediawiki.ui.icon',
];
}
/**
* @return array
*/
public function getTemplateData(): array {
$data = parent::getTemplateData();
if ( !$this->isTableOfContentsVisibleInSidebar() ) {
unset( $data['data-toc'] );
}
return $data;
}
}