Tpt has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/370809 )
Change subject: Moves function related to display out of PageDisplayHandler ...................................................................... Moves function related to display out of PageDisplayHandler Change-Id: Iac70c5b49616a18cb7a263becde0c6abe642c66b --- M extension.json M includes/page/EditPagePage.php A includes/page/PageDisplayHandler.php M includes/page/PageViewAction.php M includes/page/ProofreadPagePage.php A tests/phpunit/page/PageDisplayHandlerTest.php M tests/phpunit/page/ProofreadPagePageTest.php 7 files changed, 198 insertions(+), 162 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage refs/changes/09/370809/1 diff --git a/extension.json b/extension.json index 12f9ce6..4f95dc1 100644 --- a/extension.json +++ b/extension.json @@ -74,6 +74,7 @@ "ProofreadPage\\Page\\PageSubmitAction": "includes/page/PageSubmitAction.php", "ProofreadPage\\Page\\PageViewAction": "includes/page/PageViewAction.php", "ProofreadPage\\Page\\PageDifferenceEngine": "includes/page/PageDifferenceEngine.php", + "ProofreadPage\\Page\\PageDisplayHandler": "includes/page/PageDisplayHandler.php", "ProofreadPage\\Parser\\ParserEntryPoint": "includes/Parser/ParserEntryPoint.php", "ProofreadPage\\Parser\\TagParser": "includes/Parser/TagParser.php", "ProofreadPage\\Parser\\PagelistTagParser": "includes/Parser/PagelistTagParser.php", diff --git a/includes/page/EditPagePage.php b/includes/page/EditPagePage.php index 94eb774..0458ee5 100644 --- a/includes/page/EditPagePage.php +++ b/includes/page/EditPagePage.php @@ -19,12 +19,17 @@ /** * @var ProofreadPagePage */ - protected $pagePage; + private $pagePage; /** * @var PageContentBuilder */ - protected $pageContentBuilder; + private $pageContentBuilder; + + /** + * @var Context + */ + private $extContext; /** * @param Article $article @@ -37,12 +42,7 @@ $this->pagePage = $pagePage; $this->pageContentBuilder = new PageContentBuilder( $this->context, $context ); - - if ( !$this->isSupportedContentModel( $this->contentModel ) ) { - throw new MWException( - "The content model {$this->contentModel} is not supported" - ); - } + $this->extContext = $context; } /** @@ -76,9 +76,10 @@ */ protected function showContentForm() { $out = $this->context->getOutput(); + $pageDisplayHandler = new PageDisplayHandler( $this->extContext ); // custom CSS for preview - $css = $this->pagePage->getCustomCss(); + $css = $pageDisplayHandler->getCustomCss( $this->pagePage ); if ( $css !== '' ) { $out->addInlineStyle( $css ); } @@ -90,7 +91,7 @@ $content = $this->toEditContent( $this->textbox1 ); - $out->addHTML( $this->pagePage->getPageContainerBegin() ); + $out->addHTML( $pageDisplayHandler->buildPageContainerBegin() ); $this->showEditArea( 'wpHeaderTextbox', 'prp-page-edit-header', @@ -113,7 +114,7 @@ $inputAttributes + [ 'rows' => '2', 'tabindex' => '1' ] ); // the 3 textarea tabindex are set to 1 because summary tabindex is 1 too - $out->addHTML( $this->pagePage->getPageContainerEnd() ); + $out->addHTML( $pageDisplayHandler->buildPageContainerEnd( $this->pagePage ) ); $out->addModules( 'ext.proofreadpage.page.edit' ); $out->addModuleStyles( [ 'ext.proofreadpage.base', 'ext.proofreadpage.page' ] ); diff --git a/includes/page/PageDisplayHandler.php b/includes/page/PageDisplayHandler.php new file mode 100644 index 0000000..b84cc14 --- /dev/null +++ b/includes/page/PageDisplayHandler.php @@ -0,0 +1,135 @@ +<?php + +namespace ProofreadPage\Page; + +use Html; +use ProofreadPage\Context; +use ProofreadPage\FileNotFoundException; +use ProofreadPagePage; +use Sanitizer; + +/** + * @licence GNU GPL v2+ + * + * Utility class to do operations related to Page: page display + */ +class PageDisplayHandler { + + /** + * @var integer default width for scan image + */ + const DEFAULT_IMAGE_WIDTH = 1024; + + /** + * @var Context + */ + private $context; + + /** + * @param Context $context + */ + public function __construct( Context $context ) { + $this->context = $context; + } + + /** + * Return the scan image width for display + * @param ProofreadPagePage $page + * @return int + */ + public function getImageWidth( ProofreadPagePage $page ) { + $index = $page->getIndex(); + if ( $index ) { + $width = $index->getIndexEntry( 'width' ); + if ( $width !== null ) { + $width = $width->getStringValue(); + if ( is_numeric( $width ) ) { + return $width; + } + } + } + return self::DEFAULT_IMAGE_WIDTH; + } + + /** + * Return custom CSS for the page + * Is protected against XSS + * @param ProofreadPagePage $page + * @return string + */ + public function getCustomCss( ProofreadPagePage $page ) { + $index = $page->getIndex(); + if ( $index ) { + $css = $index->getIndexEntry( 'css' ); + if ( $css !== null ) { + return Sanitizer::escapeHtmlAllowEntities( + Sanitizer::checkCss( $css->getStringValue() ) + ); + } + } + return ''; + } + + /** + * Return the part of the page container that is before page content + * @return string + */ + public function buildPageContainerBegin() { + return + Html::openElement( 'div', [ 'class' => 'prp-page-container' ] ) . + Html::openElement( 'div', [ 'class' => 'prp-page-content' ] ); + } + + /** + * Return the part of the page container that after page cnotent + * @return string + */ + public function buildPageContainerEnd( ProofreadPagePage $page ) { + return + Html::closeElement( 'div' ) . + Html::openElement( 'div', [ 'class' => 'prp-page-image' ] ) . + $this->buildImageHtml( $page, [ 'max-width' => $this->getImageWidth( $page ) ] ) . + Html::closeElement( 'div' ) . + Html::closeElement( 'div' ); + } + + /** + * Return HTML for the image + * @param ProofreadPagePage $page + * @param array $options + * @return null|string + */ + private function buildImageHtml( ProofreadPagePage $page, $options ) { + try { + $image = $this->context->getFileProvider()->getForPagePage( $page ); + } catch ( FileNotFoundException $e ) { + return null; + } + if ( !$image->exists() ) { + return null; + } + $width = $image->getWidth(); + if ( isset( $options['max-width'] ) && $width > $options['max-width'] ) { + $width = $options['max-width']; + } + $transformAttributes = [ + 'width' => $width + ]; + + if ( $image->isMultipage() ) { + $pageNumber = $page->getPageNumber(); + if ( $pageNumber !== null ) { + $transformAttributes['page'] = $pageNumber; + } + } + $handler = $image->getHandler(); + if ( !$handler || !$handler->normaliseParams( $image, $transformAttributes ) ) { + return null; + } + $thumbnail = $image->transform( $transformAttributes ); + if ( !$thumbnail ) { + return null; + } + return $thumbnail->toHtml( $options ); + } +} diff --git a/includes/page/PageViewAction.php b/includes/page/PageViewAction.php index 6bee245..1dfd22b 100644 --- a/includes/page/PageViewAction.php +++ b/includes/page/PageViewAction.php @@ -38,12 +38,12 @@ return; } $page = ProofreadPagePage::newFromTitle( $wikiPage->getTitle() ); - $out = $this->getOutput(); + $pageDisplayHandler = new PageDisplayHandler( Context::getDefaultContext() ); // render HTML - $out->addHTML( $page->getPageContainerBegin() ); + $out->addHTML( $pageDisplayHandler->buildPageContainerBegin() ); $this->page->view(); - $out->addHTML( $page->getPageContainerEnd() ); + $out->addHTML( $pageDisplayHandler->buildPageContainerEnd( $page ) ); // add modules $out->addModules( 'ext.proofreadpage.ve.pageTarget.init' ); @@ -53,7 +53,7 @@ ] ); // custom CSS - $css = $page->getCustomCss(); + $css = $pageDisplayHandler->getCustomCss( $page ); if ( $css !== '' ) { $out->addInlineStyle( $css ); } diff --git a/includes/page/ProofreadPagePage.php b/includes/page/ProofreadPagePage.php index aa3cc61..1bbce82 100644 --- a/includes/page/ProofreadPagePage.php +++ b/includes/page/ProofreadPagePage.php @@ -28,11 +28,6 @@ class ProofreadPagePage { /** - * @var integer default width for scan image - */ - const DEFAULT_IMAGE_WIDTH = 1024; - - /** * @var Title */ protected $title; @@ -157,115 +152,5 @@ } } return null; - } - - /** - * @deprecated use FileProvider::getForPagePage - * - * Return image of the page if it exist or false. - * @return File|false - */ - public function getImage() { - try { - return Context::getDefaultContext()->getFileProvider()->getForPagePage( $this ); - } catch ( FileNotFoundException $e ) { - return false; - } - } - - /** - * Return the scan image width for display - * @return integer - */ - public function getImageWidth() { - $index = $this->getIndex(); - if ( $index ) { - $width = $index->getIndexEntry( 'width' ); - if ( $width !== null ) { - $width = $width->getStringValue(); - if ( is_numeric( $width ) ) { - return $width; - } - } - } - - return self::DEFAULT_IMAGE_WIDTH; - } - - /** - * Return custom CSS for the page - * Is protected against XSS - * @return string - */ - public function getCustomCss() { - $index = $this->getIndex(); - if ( $index ) { - $css = $index->getIndexEntry( 'css' ); - if ( $css !== null ) { - return Sanitizer::escapeHtmlAllowEntities( - Sanitizer::checkCss( $css->getStringValue() ) - ); - } - } - - return ''; - } - - /** - * Return HTML for the image - * @param array $options - * @return string|null - */ - public function getImageHtml( $options ) { - $image = $this->getImage(); - if ( !$image || !$image->exists() ) { - return null; - } - $width = $image->getWidth(); - if ( isset( $options['max-width'] ) && $width > $options['max-width'] ) { - $width = $options['max-width']; - } - $transformAttributes = [ - 'width' => $width - ]; - - if ( $image->isMultipage() ) { - $pageNumber = $this->getPageNumber(); - if ( $pageNumber !== null ) { - $transformAttributes['page'] = $pageNumber; - } - } - $handler = $image->getHandler(); - if ( !$handler || !$handler->normaliseParams( $image, $transformAttributes ) ) { - return null; - } - $thumbnail = $image->transform( $transformAttributes ); - if ( !$thumbnail ) { - return null; - } - return $thumbnail->toHtml( $options ); - } - - /** - * Return the part of the page container that is before page content - * @return string - */ - public function getPageContainerBegin() { - return - Html::openElement( 'div', [ 'class' => 'prp-page-container' ] ) . - Html::openElement( 'div', [ 'class' => 'prp-page-content' ] ); - } - - /** - * Return the part of the page container that after page cnotent - * @return string - */ - public function getPageContainerEnd() { - return - Html::closeElement( 'div' ) . - Html::openElement( 'div', [ 'class' => 'prp-page-image' ] ) . - $this->getImageHtml( [ 'max-width' => $this->getImageWidth() ] ) . - Html::closeElement( 'div' ) . - Html::closeElement( 'div' ); } } diff --git a/tests/phpunit/page/PageDisplayHandlerTest.php b/tests/phpunit/page/PageDisplayHandlerTest.php new file mode 100644 index 0000000..6f975e8 --- /dev/null +++ b/tests/phpunit/page/PageDisplayHandlerTest.php @@ -0,0 +1,46 @@ +<?php + +namespace ProofreadPage\Page; + +use ProofreadIndexPageTest; +use ProofreadPagePageTest; +use ProofreadPageTestCase; + +/** + * @group ProofreadPage + * @covers ProofreadPagePage + */ +class PageDisplayHandlerTest extends ProofreadPageTestCase { + + public function testGetImageWidth() { + $handler = new PageDisplayHandler( $this->getContext() ); + + $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|width= 500 \n}}" ); + $page = ProofreadPagePageTest::newPagePage( 'Test.jpg', $index ); + $this->assertEquals( 500, $handler->getImageWidth( $page ) ); + + $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|title=500\n}}" ); + $page = ProofreadPagePageTest::newPagePage( 'Test.jpg', $index ); + $this->assertEquals( PageDisplayHandler::DEFAULT_IMAGE_WIDTH, $handler->getImageWidth( $page ) ); + } + + public function testGetCustomCss() { + $handler = new PageDisplayHandler( $this->getContext() ); + + $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|CSS= width:300px; \n}}" ); + $page = ProofreadPagePageTest::newPagePage( 'Test.jpg', $index ); + $this->assertEquals( 'width:300px;', $handler->getCustomCss( $page ) ); + + $index = ProofreadIndexPageTest::newIndexPage( + 'Test', "{{\n|CSS= background: url('/my-bad-url.jpg');\n}}" + ); + $page = ProofreadPagePageTest::newPagePage( 'Test.jpg', $index ); + $this->assertEquals( '/* insecure input */', $handler->getCustomCss( $page ) ); + + $index = ProofreadIndexPageTest::newIndexPage( + 'Test', "{{\n|CSS= width:300px;<style> \n}}" + ); + $page = ProofreadPagePageTest::newPagePage( 'Test.jpg', $index ); + $this->assertEquals( 'width:300px;<style>', $handler->getCustomCss( $page ) ); + } +} diff --git a/tests/phpunit/page/ProofreadPagePageTest.php b/tests/phpunit/page/ProofreadPagePageTest.php index a425ef0..56f46be 100644 --- a/tests/phpunit/page/ProofreadPagePageTest.php +++ b/tests/phpunit/page/ProofreadPagePageTest.php @@ -48,36 +48,4 @@ $index = ProofreadIndexPageTest::newIndexPage(); $this->assertEquals( $index, self::newPagePage( 'Test.jpg', $index )->getIndex() ); } - - public function testGetImageWidth() { - $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|width= 500 \n}}" ); - $this->assertEquals( 500, self::newPagePage( 'Test.jpg', $index )->getImageWidth() ); - - $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|title=500\n}}" ); - $this->assertEquals( - ProofreadPagePage::DEFAULT_IMAGE_WIDTH, - self::newPagePage( 'Test.jpg', $index )->getImageWidth() - ); - } - - public function testGetCustomCss() { - $index = ProofreadIndexPageTest::newIndexPage( 'Test', "{{\n|CSS= width:300px; \n}}" ); - $this->assertEquals( - 'width:300px;', self::newPagePage( 'Test.jpg', $index )->getCustomCss() - ); - - $index = ProofreadIndexPageTest::newIndexPage( - 'Test', "{{\n|CSS= background: url('/my-bad-url.jpg');\n}}" - ); - $this->assertEquals( - '/* insecure input */', self::newPagePage( 'Test.jpg', $index )->getCustomCss() - ); - - $index = ProofreadIndexPageTest::newIndexPage( - 'Test', "{{\n|CSS= width:300px;<style> \n}}" - ); - $this->assertEquals( - 'width:300px;<style>', self::newPagePage( 'Test.jpg', $index )->getCustomCss() - ); - } } -- To view, visit https://gerrit.wikimedia.org/r/370809 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iac70c5b49616a18cb7a263becde0c6abe642c66b Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ProofreadPage Gerrit-Branch: master Gerrit-Owner: Tpt <thoma...@hotmail.fr> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits