Introduce SkinComponentTableOfContents

- Moves Skin::getSectionsData and related methods into new TOC component.
- Update SkinMustacheTest for toc getTemplateData method.
- Add test for new TOC component.

Bug: T301523
Change-Id: I29dda96f1e91da6892840d38a80c6102d425d0f7
This commit is contained in:
Clare Ming 2022-02-11 16:43:45 -07:00
parent 20e2c5ec32
commit e70633b3b2
7 changed files with 285 additions and 240 deletions

View file

@ -1094,6 +1094,7 @@ $wgAutoloadLocalClasses = [
'MediaWiki\\Skin\\SkinComponent' => __DIR__ . '/includes/skins/components/SkinComponent.php',
'MediaWiki\\Skin\\SkinComponentLogo' => __DIR__ . '/includes/skins/components/SkinComponentLogo.php',
'MediaWiki\\Skin\\SkinComponentRegistry' => __DIR__ . '/includes/skins/components/SkinComponentRegistry.php',
'MediaWiki\\Skin\\SkinComponentTableOfContents' => __DIR__ . '/includes/skins/components/SkinComponentTableOfContents.php',
'MediaWiki\\Skins\\Hook\\SkinAfterPortletHook' => __DIR__ . '/includes/skins/Hook/SkinAfterPortletHook.php',
'MediaWiki\\Skins\\Hook\\SkinPageReadyConfigHook' => __DIR__ . '/includes/skins/Hook/SkinPageReadyConfigHook.php',
'MediaWiki\\Special\\SpecialPageFactory' => __DIR__ . '/includes/specialpage/SpecialPageFactory.php',

View file

@ -100,70 +100,6 @@ abstract class Skin extends ContextSource {
return self::VERSION_MAJOR;
}
/**
* Enriches section data by nesting child elements within parent elements
* such that the table of contents can be rendered in Mustache.
*
* Example Mustache template code:
* <ul>
* {{#array-sections}}
* <li>{{number}} {{line}}</li>
* {{#array-subsections}}
* {{>TableOfContents__subsection}}
* {{/array-subsections}}
* {{/array-sections}}
* </ul>
*
* @return array
*/
private function getSectionsData(): array {
$sections = $this->getOutput()->getSections();
$data = $this->getSectionsDataInternal( $sections, 1 );
return $data;
}
/**
* Nests child sections within their parent sections.
*
* @param array $sections
* @param int $toclevel
* @return array
*/
private function getSectionsDataInternal( $sections, $toclevel = 1 ) {
$data = [];
foreach ( $sections as $i => $section ) {
// Set all the parent sections at the current top level.
if ( $section['toclevel'] === $toclevel ) {
$data[] = $section + [
'array-sections' => $this->getSectionsDataInternal(
array_slice( $sections, $i + 1 ),
$toclevel + 1
)
];
}
// Child section belongs to a higher parent.
if ( $section['toclevel'] < $toclevel ) {
return $data;
}
}
return $data;
}
/**
* Get table of contents template data
*
* @return array|null
*/
private function getTOCData() {
// Return data only if TOC present T298796.
if ( !$this->getOutput()->isTOCEnabled() ) {
return null;
}
return [
'array-sections' => $this->getSectionsData(),
];
}
/**
* Subclasses may extend this method to add additional
* template data.
@ -226,8 +162,6 @@ abstract class Skin extends ContextSource {
$htmlTitle
),
'html-title' => $htmlTitle,
// Array values
'data-toc' => $this->getTOCData(),
// Boolean values
'is-title-blank' => $blankedHeading, // @since 1.38
'is-anon' => $user->isAnon(),

View file

@ -88,6 +88,11 @@ class SkinComponentRegistry {
$this->skin->getOutput()->getTitle()
);
break;
case 'toc':
$component = new SkinComponentTableOfContents(
$this->skin->getOutput()
);
break;
default:
throw new RuntimeException( 'Unknown component: ' . $name );
}
@ -99,5 +104,6 @@ class SkinComponentRegistry {
*/
private function registerComponents() {
$this->registerComponent( 'logos' );
$this->registerComponent( 'toc' );
}
}

View file

@ -0,0 +1,106 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
namespace MediaWiki\Skin;
use OutputPage;
/**
* @internal for use inside Skin and SkinTemplate classes only
* @unstable
*/
class SkinComponentTableOfContents implements SkinComponent {
/** @var OutputPage */
private $output;
/**
* @param OutputPage $output
*/
public function __construct( OutputPage $output ) {
$this->output = $output;
}
/**
* Enriches section data by nesting child elements within parent elements
* such that the table of contents can be rendered in Mustache.
*
* Example Mustache template code:
* <ul>
* {{#array-sections}}
* <li>{{number}} {{line}}</li>
* {{#array-subsections}}
* {{>TableOfContents__subsection}}
* {{/array-subsections}}
* {{/array-sections}}
* </ul>
*
* @return array
*/
private function getSectionsData(): array {
return $this->getSectionsDataInternal( $this->output->getSections(), 1 );
}
/**
* Nests child sections within their parent sections.
*
* @param array $sections
* @param int $toclevel
* @return array
*/
private function getSectionsDataInternal( array $sections, int $toclevel = 1 ): array {
$data = [];
foreach ( $sections as $i => $section ) {
// Set all the parent sections at the current top level.
if ( $section['toclevel'] === $toclevel ) {
$data[] = $section + [
'array-sections' => $this->getSectionsDataInternal(
array_slice( $sections, $i + 1 ),
$toclevel + 1
)
];
}
// Child section belongs to a higher parent.
if ( $section['toclevel'] < $toclevel ) {
return $data;
}
}
return $data;
}
/**
* Get table of contents template data
*
* @return array
*/
private function getTOCData(): array {
// Return data only if TOC present T298796.
if ( !$this->output->isTOCEnabled() ) {
return [];
}
return [
'array-sections' => $this->getSectionsData(),
];
}
/**
* @inheritDoc
*/
public function getTemplateData(): array {
return $this->getTOCData();
}
}

View file

@ -39,6 +39,10 @@ class SkinMustacheTest extends MediaWikiIntegrationTestCase {
->willReturn( [] );
$mock->method( 'getCSP' )
->willReturn( $mockContentSecurityPolicy );
$mock->method( 'isTOCEnabled' )
->willReturn( true );
$mock->method( 'getSections' )
->willReturn( [] );
return $mock;
}
@ -86,8 +90,9 @@ class SkinMustacheTest extends MediaWikiIntegrationTestCase {
}
/**
* @covers SkinTemplate::getTemplateData
* @covers Skin::getTemplateData
* @covers MediaWiki\Skin\SkinComponentLogo::getTemplateData
* @covers MediaWiki\Skin\SkinComponentTableOfContents::getTemplateData
*/
public function testGetTemplateData() {
$config = $this->getServiceContainer()->getMainConfig();

View file

@ -205,179 +205,6 @@ class SkinTemplateTest extends MediaWikiIntegrationTestCase {
);
}
public function provideGetSectionsData(): array {
// byteoffset and fromtitle are redacted from this test.
$SECTION_1 = [
'toclevel' => 1,
'line' => 'Section 1',
'anchor' => 'section_1',
];
$SECTION_1_1 = [
'toclevel' => 2,
'line' => 'Section 1.1',
'anchor' => 'section_1_1',
];
$SECTION_1_2 = [
'toclevel' => 2,
'line' => 'Section 1.2',
'anchor' => 'section_1_2',
];
$SECTION_1_2_1 = [
'toclevel' => 3,
'line' => 'Section 1.2.1',
'anchor' => 'section_1_2_1',
];
$SECTION_1_3 = [
'toclevel' => 2,
'line' => 'Section 1.3',
'anchor' => 'section_1_3',
];
$SECTION_2 = [
'toclevel' => 1,
'line' => 'Section 2',
'anchor' => 'section_2',
];
return [
[
// sections data
[],
[]
],
[
[
$SECTION_1,
$SECTION_2
],
[
$SECTION_1 + [
'array-sections' => [],
],
$SECTION_2 + [
'array-sections' => [],
]
]
],
[
[
$SECTION_1,
$SECTION_1_1,
$SECTION_2,
],
[
$SECTION_1 + [
'array-sections' => [
$SECTION_1_1 + [
'array-sections' => [],
]
]
],
$SECTION_2 + [
'array-sections' => [],
]
]
],
[
[
$SECTION_1,
$SECTION_1_1,
$SECTION_1_2,
$SECTION_1_2_1,
$SECTION_1_3,
$SECTION_2,
],
[
$SECTION_1 + [
'array-sections' => [
$SECTION_1_1 + [
'array-sections' => [],
],
$SECTION_1_2 + [
'array-sections' => [
$SECTION_1_2_1 + [
'array-sections' => [],
]
]
],
$SECTION_1_3 + [
'array-sections' => [],
]
]
],
$SECTION_2 + [
'array-sections' => [],
]
]
]
];
}
/**
* @covers Skin::getSectionsData
* @dataProvider provideGetSectionsData
*
* @param array $sectionsData
* @param array $expected
*/
public function testGetSectionsData( $sectionsData, $expected ) {
$skin = new SkinTemplate();
$context = new DerivativeContext( $skin->getContext() );
$mock = $this->createMock( OutputPage::class );
$mock->expects( $this->any() )
->method( 'getSections' )
->willReturn( $sectionsData );
$reflectionMethod = new ReflectionMethod( Skin::class, 'getSectionsData' );
$reflectionMethod->setAccessible( true );
$context->setOutput( $mock );
$skin->setContext( $context );
$data = $reflectionMethod->invoke(
$skin
);
$this->assertEquals( $expected, $data );
}
public function provideGetTOCData() {
return [
[
false,
null,
'Data not provided when TOC is disabled'
],
[
true,
[
'array-sections' => []
],
'Data not provided when TOC is enabled'
],
];
}
/**
* @covers Skin::getTOCData
* @dataProvider provideGetTOCData
*/
public function testGetTOCData( $isTOCEnabled, $expected ) {
$skin = new SkinTemplate();
$context = new DerivativeContext( $skin->getContext() );
$mock = $this->createMock( OutputPage::class );
$mock->expects( $this->any() )
->method( 'isTOCEnabled' )
->willReturn( $isTOCEnabled );
$reflectionMethod = new ReflectionMethod( Skin::class, 'getTOCData' );
$reflectionMethod->setAccessible( true );
$context->setOutput( $mock );
$skin->setContext( $context );
$data = $reflectionMethod->invoke(
$skin
);
$this->assertEquals( $expected, $data );
}
public function provideContentNavigation(): array {
return [
'No userpage set' => [

View file

@ -0,0 +1,166 @@
<?php
use MediaWiki\Skin\SkinComponentTableOfContents;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Skin\SkinComponentTableOfContents
*
* @group Output
*/
class SkinComponentTableOfContentsTest extends MediaWikiUnitTestCase {
public function provideGetSectionsData(): array {
// byteoffset and fromtitle are redacted from this test.
$SECTION_1 = [
'toclevel' => 1,
'line' => 'Section 1',
'anchor' => 'section_1',
];
$SECTION_1_1 = [
'toclevel' => 2,
'line' => 'Section 1.1',
'anchor' => 'section_1_1',
];
$SECTION_1_2 = [
'toclevel' => 2,
'line' => 'Section 1.2',
'anchor' => 'section_1_2',
];
$SECTION_1_2_1 = [
'toclevel' => 3,
'line' => 'Section 1.2.1',
'anchor' => 'section_1_2_1',
];
$SECTION_1_3 = [
'toclevel' => 2,
'line' => 'Section 1.3',
'anchor' => 'section_1_3',
];
$SECTION_2 = [
'toclevel' => 1,
'line' => 'Section 2',
'anchor' => 'section_2',
];
return [
[
// sections data
[],
[]
],
[
[
$SECTION_1,
$SECTION_2
],
[
$SECTION_1 + [
'array-sections' => [],
],
$SECTION_2 + [
'array-sections' => [],
]
]
],
[
[
$SECTION_1,
$SECTION_1_1,
$SECTION_2,
],
[
$SECTION_1 + [
'array-sections' => [
$SECTION_1_1 + [
'array-sections' => [],
]
]
],
$SECTION_2 + [
'array-sections' => [],
]
]
],
[
[
$SECTION_1,
$SECTION_1_1,
$SECTION_1_2,
$SECTION_1_2_1,
$SECTION_1_3,
$SECTION_2,
],
[
$SECTION_1 + [
'array-sections' => [
$SECTION_1_1 + [
'array-sections' => [],
],
$SECTION_1_2 + [
'array-sections' => [
$SECTION_1_2_1 + [
'array-sections' => [],
]
]
],
$SECTION_1_3 + [
'array-sections' => [],
]
]
],
$SECTION_2 + [
'array-sections' => [],
]
]
]
];
}
/**
* @covers \MediaWiki\Skin\SkinComponentTableOfContents::getSectionsData
* @dataProvider provideGetSectionsData
*
* @param array $sectionsData
* @param array $expected
*/
public function testGetSectionsData( $sectionsData, $expected ) {
$mockOutput = $this->createMock( OutputPage::class );
$mockOutput->method( 'getSections' )->willReturn( $sectionsData );
$skinComponent = new SkinComponentTableOfContents( $mockOutput );
$data = TestingAccessWrapper::newFromObject( $skinComponent )->getSectionsData();
$this->assertEquals( $expected, $data );
}
public function provideGetTOCData() {
return [
[
false,
[],
'Data not provided when TOC is disabled'
],
[
true,
[
'array-sections' => []
],
'Data not provided when TOC is enabled'
],
];
}
/**
* @covers \MediaWiki\Skin\SkinComponentTableOfContents::getTOCData
* @dataProvider provideGetTOCData
*/
public function testGetTOCData( $isTOCEnabled, $expected ) {
$mockOutput = $this->createMock( OutputPage::class );
$mockOutput->method( 'isTOCEnabled' )->willReturn( $isTOCEnabled );
$skinComponent = new SkinComponentTableOfContents( $mockOutput );
$data = TestingAccessWrapper::newFromObject( $skinComponent )->getTOCData();
$this->assertEquals( $expected, $data );
}
}