Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/extending/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ and frontend registries independently.
### Frontend module

Register frontend extension points from a ResourceLoader module that depends on `ext.neowiki`. The
`NeoWikiGetFrontendModules` hook loads it wherever NeoWiki's UI loads; on pages of your own, load it as any module.
`NeoWikiGetFrontendModules` hook loads it wherever NeoWiki's UI loads, which on a content page is only where the page
shows a View, its Main Subject in full or a `{{#create_subject}}` button, or the viewer may edit it. On pages of your
own, load it as any module.

```json
"ResourceModules": {
Expand Down
37 changes: 29 additions & 8 deletions src/EntryPoints/NeoWikiHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function onBeforePageDisplay( OutputPage $out, Skin $skin ): void
$carriesCreateSubjectButton = self::carriesCreateSubjectButton( $out );

if ( self::isContentPage( $out ) ) {
self::handleContentPage( $out, $skin );
self::handleContentPage( $out, $skin, $carriesCreateSubjectButton );
} elseif ( self::isActionOnContentPage( $out ) ) {
self::nameActionPageByMainSubject( $out );
} elseif ( self::isSchemaPage( $out ) && $out->isArticle() ) {
Expand Down Expand Up @@ -138,20 +138,41 @@ private static function isInContentNamespace( OutputPage $out ): bool {
return MediaWikiServices::getInstance()->getNamespaceInfo()->isContent( $out->getTitle()->getNamespace() );
}

private static function handleContentPage( OutputPage $out, Skin $skin ): void {
private static function handleContentPage( OutputPage $out, Skin $skin, bool $carriesCreateSubjectButton ): void {
self::warnAboutHalfConfiguredNeo4j();
self::addRdfAutodiscoveryLinks( $out );
self::showMainSubject( $out );

$extension = NeoWikiExtension::getInstance();
$permissionHints = $extension->newSubjectPermissionHints( $out->getAuthority() );
$canEditPageSubjects = $permissionHints->canEditSubject( new PageId( $out->getTitle()->getArticleID() ) );

$extension->newFrontendModuleLoader()->loadWithSubjectPermissions(
$out,
$skin,
$permissionHints->canEditSubject( new PageId( $out->getTitle()->getArticleID() ) )
);
if ( !self::needsFrontend( $out, $canEditPageSubjects, $carriesCreateSubjectButton ) ) {
return;
}

$extension->newFrontendModuleLoader()->loadWithSubjectPermissions( $out, $skin, $canEditPageSubjects );
$out->addHtml( self::getNeoWikiAppHtml( $out, $permissionHints ) );
self::addRdfAutodiscoveryLinks( $out );
}

/**
* A reader of a page that shows no Subject and no button would download the frontend for nothing.
*/
private static function needsFrontend(
OutputPage $out,
bool $canEditPageSubjects,
bool $carriesCreateSubjectButton
): bool {
$html = $out->getHTML();

return $canEditPageSubjects
|| $carriesCreateSubjectButton
|| ViewHtmlBuilder::containsMainSubjectInFull( $html )
|| ViewHtmlBuilder::containsView( $html );
}

private static function showMainSubject( OutputPage $out ): void {
$extension = NeoWikiExtension::getInstance();
$revisionId = self::revisionIdUnlessLatest( $out );
$builder = $extension->newViewHtmlBuilder();

Expand Down
8 changes: 8 additions & 0 deletions src/Presentation/ViewHtmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public static function containsMainSubjectInFull( string $html ): bool {
return preg_match( '/<div id="ext-neowiki-main-subject"(?: [\w-]+="[^"]*")* data-mw-neowiki-subject-id="/', $html ) === 1;
}

/**
* Whether the HTML holds a placeholder made by viewPlaceholderHtml(), also after a parser such as Parsoid added
* attributes of its own.
*/
public static function containsView( string $html ): bool {
return preg_match( '/\sclass="(?:[^"]*\s)?ext-neowiki-view[\s"]/', $html ) === 1;
}

public static function viewPlaceholderHtml( string $subjectId, ?string $layoutName = null ): string {
$attributes = [
'class' => 'ext-neowiki-view',
Expand Down
10 changes: 9 additions & 1 deletion tests/phpunit/EntryPoints/AutoRenderMainSubjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ public function testAutoRendersMainSubjectAtTheTopOfThePageByDefault(): void {
$this->assertStringContainsString( self::viewOf( self::SUBJECT_ID ), self::htmlAboveThePageText( $html ) );
}

public function testRendersAppContainerWhenAutoRenderDisabled(): void {
public function testLoadsTheFrontendForAReaderOfAPageShowingItsMainSubjectAutomatically(): void {
$this->createPageWithMainSubjectAndText();
$this->setGroupPermissions( '*', 'edit', false );

$this->assertContains( 'ext.neowiki', $this->viewPage( [] )->getModules() );
}

public function testRendersAppContainerForAViewerWhoMayEditWhenAutoRenderDisabled(): void {
$this->createPageWithMainSubjectAndText();
$this->overrideConfigValue( 'NeoWikiAutoRenderMainSubject', false );
$this->setGroupPermissions( '*', 'edit', true );

$html = $this->renderRequest( [] );

Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/EntryPoints/ContentPageFrontendLoadingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare( strict_types = 1 );

namespace ProfessionalWiki\NeoWiki\Tests\EntryPoints;

use MediaWiki\Context\RequestContext;
use MediaWiki\Output\OutputPage;
use MediaWiki\Permissions\Authority;
use MediaWiki\Title\Title;
use ProfessionalWiki\NeoWiki\EntryPoints\NeoWikiHooks;
use ProfessionalWiki\NeoWiki\Presentation\ViewHtmlBuilder;
use ProfessionalWiki\NeoWiki\Tests\NeoWikiIntegrationTestCase;
use ProfessionalWiki\NeoWiki\Tests\NeoWikiMockAuthorityTrait;

/**
* The database holds the page, whose rights decide who may edit it.
*
* @covers \ProfessionalWiki\NeoWiki\EntryPoints\NeoWikiHooks::onBeforePageDisplay
* @group Database
*/
class ContentPageFrontendLoadingTest extends NeoWikiIntegrationTestCase {

use NeoWikiMockAuthorityTrait;

private const string SUBJECT_ID = 's1demo1aaaaaaa1';

public function testLoadsNoFrontendForAReaderOfAPageWithoutNeoWikiUi(): void {
$out = $this->displayToAReader( '<p>Paris is the capital of France.</p>' );

$this->assertNotContains( 'ext.neowiki', $out->getModules() );
$this->assertNotContains( 'ext.neowiki.styles', $out->getModuleStyles() );
}

public function testLoadsTheFrontendForAReaderOfAPageShowingAView(): void {
$out = $this->displayToAReader( '<p>Paris</p>' . ViewHtmlBuilder::viewPlaceholderHtml( self::SUBJECT_ID ) );

$this->assertContains( 'ext.neowiki', $out->getModules() );
}

public function testLoadsTheFrontendForAReaderOfAPagePlacingItsMainSubject(): void {
$out = $this->displayToAReader( ViewHtmlBuilder::mainSubjectInFullPlaceholderHtml( self::SUBJECT_ID ) );

$this->assertContains( 'ext.neowiki', $out->getModules() );
}

public function testLoadsTheFrontendForAViewerWhoMayEditThePage(): void {
$out = $this->display( '<p>Paris is the capital of France.</p>', $this->getTestSysop()->getAuthority() );

$this->assertContains( 'ext.neowiki', $out->getModules() );
$this->assertStringContainsString( 'id="ext-neowiki-app"', $out->getHTML() );
}

private function displayToAReader( string $html ): OutputPage {
return $this->display( $html, $this->authorityThatCannotEditPageId( $this->title()->getArticleID() ) );
}

private function display( string $html, Authority $authority ): OutputPage {
$context = new RequestContext();
$context->setTitle( $this->title() );
$context->setAuthority( $authority );

$out = $context->getOutput();
$out->setArticleFlag( true );
$out->setRevisionId( $this->title()->getLatestRevID() );
$out->addHTML( $html );

NeoWikiHooks::onBeforePageDisplay( $out, $context->getSkin() );

return $out;
}

private function title(): Title {
return $this->getExistingTestPage( 'ContentPageFrontendLoadingTest' )->getTitle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function testAddsTheDeniedReasonToAContentPageCarryingAButton(): void {
$this->assertArrayHasKey( 'wgNeoWikiCreateSubjectPageDeniedReason', $out->getJsConfigVars() );
}

public function testAddsTheFrontendConfigVarsToAContentPageCarryingAButton(): void {
$out = $this->displayPage( Title::makeTitle( NS_MAIN, 'Carries a button' ), requestsTheFrontend: true );

$this->assertArrayHasKey( 'wgNeoWikiValidationDebounceMs', $out->getJsConfigVars() );
}

public function testAddsNoDeniedReasonToAContentPageWithoutAButton(): void {
$out = $this->displayPage( Title::makeTitle( NS_MAIN, 'Carries nothing' ), requestsTheFrontend: false );

Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/EntryPoints/RdfAutodiscoveryLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public function testAdvertisesTurtleAndTrigExportsForPageWithSubjects(): void {
$this->assertStringStartsWith( 'http', $trig );
}

public function testAdvertisesTurtleAndTrigExportsForPageWithoutSubjects(): void {
public function testAdvertisesTurtleAndTrigExportsToAReaderOfAPageWithoutSubjects(): void {
$this->editPage( 'Autodiscovery without subjects', 'Plain page, no Subjects.' );
$this->setGroupPermissions( '*', 'edit', false );
$title = Title::newFromText( 'Autodiscovery without subjects' );

$out = $this->renderContentPage( 'Autodiscovery without subjects', $title->getLatestRevID() );
Expand Down
5 changes: 5 additions & 0 deletions tests/phpunit/EntryPoints/SubjectEditPermissionHintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MediaWiki\Permissions\Authority;
use MediaWiki\Title\Title;
use ProfessionalWiki\NeoWiki\EntryPoints\NeoWikiHooks;
use ProfessionalWiki\NeoWiki\Presentation\ViewHtmlBuilder;
use ProfessionalWiki\NeoWiki\Tests\NeoWikiIntegrationTestCase;
use ProfessionalWiki\NeoWiki\Tests\NeoWikiMockAuthorityTrait;

Expand Down Expand Up @@ -60,6 +61,9 @@ public function testOffersTheSubjectCreatorOnlyToAViewerWhoMayEditThePage(): voi
$this->assertStringNotContainsString( 'data-mw-neowiki-create-subject', $protected->getHTML() );
}

/**
* The page shows a View, so the frontend loads whether or not the viewer may edit.
*/
private function renderContentPage( Title $title, Authority $authority ): OutputPage {
$context = new RequestContext();
$context->setTitle( $title );
Expand All @@ -68,6 +72,7 @@ private function renderContentPage( Title $title, Authority $authority ): Output
$out = $context->getOutput();
$out->setArticleFlag( true );
$out->setRevisionId( $title->getLatestRevID() );
$out->addHTML( ViewHtmlBuilder::viewPlaceholderHtml( 's1demo1aaaaaaa1' ) );

NeoWikiHooks::onBeforePageDisplay( $out, $context->getSkin() );

Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/Presentation/ViewHtmlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,37 @@ public static function wikitextMarkupProvider(): iterable {
];
}

/**
* @dataProvider htmlWithViewProvider
*/
public function testRecognizesAView( string $html ): void {
$this->assertTrue( ViewHtmlBuilder::containsView( $html ) );
}

public static function htmlWithViewProvider(): iterable {
yield 'a placeholder' => [ '<p>Text</p>' . ViewHtmlBuilder::viewPlaceholderHtml( self::TITLING_SUBJECT_ID ) ];
yield 'a placeholder naming its Layout' => [
ViewHtmlBuilder::viewPlaceholderHtml( self::TITLING_SUBJECT_ID, 'Infobox' )
];
// Parsoid adds attributes of its own to the elements a parser function returns.
yield 'a placeholder a parser rewrote' => [
'<div about="#mwt1" typeof="mw:Transclusion" class="ext-neowiki-view mw-extra" data-mw-neowiki-subject-id="s1"></div>'
];
}

/**
* @dataProvider htmlWithoutViewProvider
*/
public function testFindsNoViewInHtmlWithoutOne( string $html ): void {
$this->assertFalse( ViewHtmlBuilder::containsView( $html ) );
}

public static function htmlWithoutViewProvider(): iterable {
yield 'plain text' => [ '<p>Paris is the capital of France.</p>' ];
yield 'the mount for the Main Subject in full' => [
ViewHtmlBuilder::mainSubjectInFullPlaceholderHtml( self::TITLING_SUBJECT_ID )
];
yield 'a class that only starts like it' => [ '<div class="ext-neowiki-view-schema"></div>' ];
}

}
Loading