From ed4b45f44a62707e43e4a895f12ffcda90e95e12 Mon Sep 17 00:00:00 2001 From: Jon Robson Date: Fri, 18 Feb 2022 16:53:42 -0800 Subject: [PATCH] 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 --- includes/SkinVector.php | 37 ------------------------------------- includes/SkinVector22.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/includes/SkinVector.php b/includes/SkinVector.php index 4590b294d..0df24c469 100644 --- a/includes/SkinVector.php +++ b/includes/SkinVector.php @@ -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&' : ''; diff --git a/includes/SkinVector22.php b/includes/SkinVector22.php index 30397cd59..4ffbb7a54 100644 --- a/includes/SkinVector22.php +++ b/includes/SkinVector22.php @@ -1,4 +1,8 @@ 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; + } }