Tpt has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/371484 )
Change subject: Refactors TagParsers ...................................................................... Refactors TagParsers Change-Id: I5234dd05280af661c5ca7e00b3116eb8d4415e15 --- M ProofreadPage.body.php M extension.json M includes/Context.php M includes/Pagination/FilePagination.php M includes/Pagination/PaginationFactory.php M includes/Parser/PagelistTagParser.php M includes/Parser/PagequalityTagParser.php M includes/Parser/PagesTagParser.php D includes/Parser/ParserEntryPoint.php D includes/Parser/TagParser.php M tests/phpunit/Pagination/FilePaginationTest.php M tests/phpunit/Pagination/PaginationFactoryTest.php 12 files changed, 170 insertions(+), 160 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage refs/changes/84/371484/1 diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php index 56b01ed..7571830 100644 --- a/ProofreadPage.body.php +++ b/ProofreadPage.body.php @@ -21,10 +21,12 @@ use ProofreadPage\Context; use ProofreadPage\FileNotFoundException; -use ProofreadPage\Index\EditIndexPage; use ProofreadPage\Page\PageContent; use ProofreadPage\Page\PageContentBuilder; use ProofreadPage\Pagination\PageNotInPaginationException; +use ProofreadPage\Parser\PagelistTagParser; +use ProofreadPage\Parser\PagequalityTagParser; +use ProofreadPage\Parser\PagesTagParser; /* @todo : @@ -107,13 +109,25 @@ * @return bool hook return value */ public static function onParserFirstCallInit( Parser $parser ) { - $parser->setHook( - 'pagelist', [ 'ProofreadPage\Parser\ParserEntryPoint', 'renderPagelistTag' ] - ); - $parser->setHook( 'pages', [ 'ProofreadPage\Parser\ParserEntryPoint', 'renderPagesTag' ] ); - $parser->setHook( - 'pagequality', [ 'ProofreadPage\Parser\ParserEntryPoint', 'renderPagequalityTag' ] - ); + $context = Context::getDefaultContext( true ); + $parser->setHook( 'pagelist', function ( $input, array $args, Parser $parser ) use ( $context ) { + $tagParser = new PagelistTagParser( $parser, + $context->getPageNamespaceId(), $context->getIndexNamespaceId(), $context->getFileProvider() + ); + return $tagParser->render( $input, $args ); + } ); + $parser->setHook( 'pages', function ( $input, array $args, Parser $parser ) use ( $context ) { + $tagParser = new PagesTagParser( $parser, + $context->getPageNamespaceId(), $context->getIndexNamespaceId(), + $context->getPaginationFactory(), $context->getCustomIndexFieldsParser() + ); + return $tagParser->render( $input, $args ); + } ); + $parser->setHook( 'pagequality', + function ( $input, array $args, Parser $parser ) use ( $context ) { + $tagParser = new PagequalityTagParser(); + return $tagParser->render( $input, $args ); + } ); return true; } diff --git a/extension.json b/extension.json index 266871c..aef8046 100644 --- a/extension.json +++ b/extension.json @@ -76,8 +76,6 @@ "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", "ProofreadPage\\Parser\\PagesTagParser": "includes/Parser/PagesTagParser.php", "ProofreadPage\\Parser\\PagequalityTagParser": "includes/Parser/PagequalityTagParser.php", diff --git a/includes/Context.php b/includes/Context.php index 0bbd60d..71d7dce 100644 --- a/includes/Context.php +++ b/includes/Context.php @@ -33,6 +33,11 @@ private $fileProvider; /** + * @var PaginationFactory + */ + private $paginationFactory; + + /** * @var CustomIndexFieldsParser */ private $customIndexFieldsParser; @@ -50,6 +55,7 @@ $this->pageNamespaceId = $pageNamespaceId; $this->indexNamespaceId = $indexNamespaceId; $this->fileProvider = $fileProvider; + $this->paginationFactory = new PaginationFactory( $pageNamespaceId, $fileProvider ); $this->customIndexFieldsParser = $customIndexFieldsParser; } @@ -78,7 +84,7 @@ * @return PaginationFactory */ public function getPaginationFactory() { - return new PaginationFactory( $this ); + return $this->paginationFactory; } /** diff --git a/includes/Pagination/FilePagination.php b/includes/Pagination/FilePagination.php index 48a6e7a..76e0c10 100644 --- a/includes/Pagination/FilePagination.php +++ b/includes/Pagination/FilePagination.php @@ -5,7 +5,6 @@ use File; use OutOfBoundsException; use ProofreadIndexPage; -use ProofreadPage\Context; use ProofreadPagePage; use Title; @@ -32,23 +31,23 @@ private $pages = []; /** - * @var Context + * @var int */ - private $context; + private $pageNamespaceId; /** * @param ProofreadIndexPage $index * @param PageList $pageList representation of the <pagelist> tag that configure page numbers * @param File $file the pagination file - * @param Context $context the current context + * @param int $pageNamespaceId */ public function __construct( - ProofreadIndexPage $index, PageList $pageList, File $file, Context $context + ProofreadIndexPage $index, PageList $pageList, File $file, $pageNamespaceId ) { parent::__construct( $index ); $this->pageList = $pageList; - $this->context = $context; + $this->pageNamespaceId = $pageNamespaceId; if ( $file->isMultipage() ) { $this->numberOfPages = $file->pageCount(); @@ -128,7 +127,7 @@ */ private function buildPageTitleFromPageNumber( $pageNumber ) { return Title::makeTitle( - $this->context->getPageNamespaceId(), + $this->pageNamespaceId, $this->index->getTitle()->getText() . '/' . $pageNumber ); } diff --git a/includes/Pagination/PaginationFactory.php b/includes/Pagination/PaginationFactory.php index 6e62a3e..b4abcec 100644 --- a/includes/Pagination/PaginationFactory.php +++ b/includes/Pagination/PaginationFactory.php @@ -3,8 +3,8 @@ namespace ProofreadPage\Pagination; use ProofreadIndexPage; -use ProofreadPage\Context; use ProofreadPage\FileNotFoundException; +use ProofreadPage\FileProvider; use ProofreadPagePage; /** @@ -13,17 +13,23 @@ class PaginationFactory { /** - * @var Context + * @var int */ - private $context; + private $pageNamespaceId; + + /** + * @var FileProvider + */ + private $fileProvider; /** * @var Pagination[] */ private $paginations = []; - public function __construct( Context $context ) { - $this->context = $context; + public function __construct( $pageNamespaceId, FileProvider $fileProvider ) { + $this->pageNamespaceId = $pageNamespaceId; + $this->fileProvider = $fileProvider; } /** @@ -46,7 +52,7 @@ */ private function buildPaginationForIndexPage( ProofreadIndexPage $indexPage ) { try { - $file = $this->context->getFileProvider()->getForIndexPage( $indexPage ); + $file = $this->fileProvider->getForIndexPage( $indexPage ); } catch ( FileNotFoundException $e ) { $file = false; } @@ -58,11 +64,11 @@ $indexPage, $pagelist, $file, - $this->context + $this->pageNamespaceId ); } else { $links = $indexPage->getContent()->getLinksToNamespace( - Context::getDefaultContext()->getPageNamespaceId() + $this->pageNamespaceId ); $pages = []; $pageNumbers = []; diff --git a/includes/Parser/PagelistTagParser.php b/includes/Parser/PagelistTagParser.php index 7800b55..f27154c 100644 --- a/includes/Parser/PagelistTagParser.php +++ b/includes/Parser/PagelistTagParser.php @@ -2,8 +2,10 @@ namespace ProofreadPage\Parser; +use Parser; use ProofreadIndexPage; use ProofreadPage\FileNotFoundException; +use ProofreadPage\FileProvider; use ProofreadPage\Pagination\FilePagination; use ProofreadPage\Pagination\PageList; use ProofreadPage\Pagination\PageNumber; @@ -13,20 +15,53 @@ * * Parser for the <pagelist> tag */ -class PagelistTagParser extends TagParser { +class PagelistTagParser { /** - * @see TagParser::render + * @var Parser + */ + protected $parser; + + /** + * @var int + */ + private $pageNamespaceId; + + /** + * @var int + */ + private $indexNamespaceId; + + /** + * @var FileProvider + */ + private $fileProvider; + + public function __construct( + Parser $parser, $pageNamespaceId, $indexNamespaceId, FileProvider $fileProvider + ) { + $this->parser = $parser; + $this->pageNamespaceId = $pageNamespaceId; + $this->indexNamespaceId = $indexNamespaceId; + $this->fileProvider = $fileProvider; + } + + /** + * Render a <pagelist> tag + * + * @param string $input the content between opening and closing tags + * @param array $args tags arguments + * @return string */ public function render( $input, array $args ) { $title = $this->parser->getTitle(); - if ( !$title->inNamespace( $this->context->getIndexNamespaceId() ) ) { + if ( !$title->inNamespace( $this->indexNamespaceId ) ) { return ''; } $index = ProofreadIndexPage::newFromTitle( $title ); $pageList = new PageList( $args ); try { - $image = $this->context->getFileProvider()->getForIndexPage( $index ); + $image = $this->fileProvider->getForIndexPage( $index ); } catch ( FileNotFoundException $e ) { return $this->formatError( 'proofreadpage_nosuch_file' ); } @@ -34,7 +69,7 @@ return $this->formatError( 'proofreadpage_nosuch_file' ); } - $pagination = new FilePagination( $index, $pageList, $image, $this->context ); + $pagination = new FilePagination( $index, $pageList, $image, $this->pageNamespaceId ); $count = $pagination->getNumberOfPages(); $return = ''; @@ -94,4 +129,13 @@ return trim( $this->parser->recursiveTagParse( $return ) ); } + + /** + * @param string $errorMsg + * @return string + */ + private function formatError( $errorMsg ) { + return '<strong class="error">' . wfMessage( $errorMsg )->inContentLanguage()->escaped() . + '</strong>'; + } } diff --git a/includes/Parser/PagequalityTagParser.php b/includes/Parser/PagequalityTagParser.php index 84b312c..92fcbcd 100644 --- a/includes/Parser/PagequalityTagParser.php +++ b/includes/Parser/PagequalityTagParser.php @@ -9,10 +9,14 @@ * * Parser for the <pagequality> tag */ -class PagequalityTagParser extends TagParser { +class PagequalityTagParser { /** - * @see TagParser::render + * Render a <pagequality> tag + * + * @param string $input the content between opening and closing tags + * @param array $args tags arguments + * @return string */ public function render( $input, array $args ) { if ( !array_key_exists( 'level', $args ) || !is_numeric( $args['level'] ) || diff --git a/includes/Parser/PagesTagParser.php b/includes/Parser/PagesTagParser.php index e7f5053..366e116 100644 --- a/includes/Parser/PagesTagParser.php +++ b/includes/Parser/PagesTagParser.php @@ -3,9 +3,11 @@ namespace ProofreadPage\Parser; use OutOfBoundsException; +use Parser; use ProofreadIndexPage; -use ProofreadPage\Context; +use ProofreadPage\Index\CustomIndexFieldsParser; use ProofreadPage\Pagination\FilePagination; +use ProofreadPage\Pagination\PaginationFactory; use ProofreadPageDbConnector; use ProofreadPagePage; use Title; @@ -15,10 +17,45 @@ * * Parser for the <pages> tag */ -class PagesTagParser extends TagParser { +class PagesTagParser { /** - * @see TagParser::render + * @var int + */ + private $pageNamespaceId; + + /** + * @var int + */ + private $indexNamespaceId; + + /** + * @var PaginationFactory + */ + private $paginationFactory; + + /** + * @var CustomIndexFieldsParser + */ + private $customIndexFieldsParser; + + public function __construct( + Parser $parser, $pageNamespaceId, $indexNamespaceId, + PaginationFactory $paginationFactory, CustomIndexFieldsParser $customIndexFieldsParser + ) { + $this->parser = $parser; + $this->pageNamespaceId = $pageNamespaceId; + $this->indexNamespaceId = $indexNamespaceId; + $this->paginationFactory = $paginationFactory; + $this->customIndexFieldsParser = $customIndexFieldsParser; + } + + /** + * Render a <pages> tag + * + * @param string $input the content between opening and closing tags + * @param array $args tags arguments + * @return string */ public function render( $input, array $args ) { // abort if this is nested <pages> call @@ -43,8 +80,8 @@ // abort if the tag is on an index or a page page if ( - $pageTitle->inNamespace( $this->context->getIndexNamespaceId() ) || - $pageTitle->inNamespace( $this->context->getPageNamespaceId() ) + $pageTitle->inNamespace( $this->indexNamespaceId ) || + $pageTitle->inNamespace( $this->pageNamespaceId ) ) { return ''; } @@ -58,14 +95,13 @@ return $this->formatError( 'proofreadpage_index_expected' ); } - $indexTitle = Title::makeTitleSafe( $this->context->getIndexNamespaceId(), $index ); + $indexTitle = Title::makeTitleSafe( $this->indexNamespaceId, $index ); if ( $indexTitle === null || !$indexTitle->exists() ) { return $this->formatError( 'proofreadpage_nosuch_index' ); } $indexPage = ProofreadIndexPage::newFromTitle( $indexTitle ); $indexContent = $indexPage->getContent(); - $pagination = $this->context->getPaginationFactory() - ->getPaginationForIndexPage( $indexPage ); + $pagination = $this->paginationFactory->getPaginationForIndexPage( $indexPage ); $language = $this->parser->getTargetLanguage(); $this->parser->getOutput()->addTemplate( $indexTitle, $indexTitle->getArticleID(), $indexTitle->getLatestRevID() @@ -159,9 +195,7 @@ $fromPage = null; if ( $from ) { - $fromTitle = Title::makeTitleSafe( - $this->context->getPageNamespaceId(), $from - ); + $fromTitle = Title::makeTitleSafe( $this->pageNamespaceId, $from ); if ( $fromTitle !== null ) { $fromPage = ProofreadPagePage::newFromTitle( $fromTitle ); $adding = false; @@ -170,7 +204,7 @@ $toPage = null; if ( $to ) { - $toTitle = Title::makeTitleSafe( $this->context->getPageNamespaceId(), $to ); + $toTitle = Title::makeTitleSafe( $this->pageNamespaceId, $to ); if ( $toTitle !== null ) { $toPage = ProofreadPagePage::newFromTitle( $toTitle ); } @@ -258,7 +292,7 @@ ); } catch ( OutOfBoundsException $e ) { - } // if the first page does not exists + } // if the first page does not exists } if ( $header ) { @@ -312,8 +346,7 @@ if ( isset( $to_pagenum ) ) { $h_out .= "|to=$to_pagenum"; } - $attributes = $this->context->getCustomIndexFieldsParser() - ->parseCustomIndexFieldsForHeader( $indexContent ); + $attributes = $this->customIndexFieldsParser->parseCustomIndexFieldsForHeader( $indexContent ); foreach ( $attributes as $attribute ) { $key = strtolower( $attribute->getKey() ); if ( array_key_exists( $key, $args ) ) { @@ -365,4 +398,13 @@ } return $nums; } + + /** + * @param string $errorMsg + * @return string + */ + private function formatError( $errorMsg ) { + return '<strong class="error">' . wfMessage( $errorMsg )->inContentLanguage()->escaped() . + '</strong>'; + } } diff --git a/includes/Parser/ParserEntryPoint.php b/includes/Parser/ParserEntryPoint.php deleted file mode 100644 index ac32809..0000000 --- a/includes/Parser/ParserEntryPoint.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php - -namespace ProofreadPage\Parser; - -use Parser; -use ProofreadPage\Context; - -/** - * @licence GNU GPL v2+ - * - * Entry point for parser hooks - */ -class ParserEntryPoint { - - /** - * Parser hook for <pagelist> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @param Parser $parser the current parser - * @return string - */ - public static function renderPagelistTag( $input, array $args, Parser $parser ) { - $tagParser = new PagelistTagParser( $parser, Context::getDefaultContext( true ) ); - return $tagParser->render( $input, $args ); - } - - /** - * Parser hook for <pages> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @param Parser $parser the current parser - * @return string - */ - public static function renderPagesTag( $input, array $args, Parser $parser ) { - $tagParser = new PagesTagParser( $parser, Context::getDefaultContext( true ) ); - return $tagParser->render( $input, $args ); - } - - /** - * Parser hook for <pagequality> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @param Parser $parser the current parser - * @return string - */ - public static function renderPagequalityTag( $input, array $args, Parser $parser ) { - $tagParser = new PagequalityTagParser( $parser, Context::getDefaultContext( true ) ); - return $tagParser->render( $input, $args ); - } -} diff --git a/includes/Parser/TagParser.php b/includes/Parser/TagParser.php deleted file mode 100644 index e68821e..0000000 --- a/includes/Parser/TagParser.php +++ /dev/null @@ -1,50 +0,0 @@ -<?php - -namespace ProofreadPage\Parser; - -use Parser; -use ProofreadPage\Context; - -/** - * @licence GNU GPL v2+ - * - * Abstract structure for a tag parser - */ -abstract class TagParser { - - /** - * @var Parser - */ - protected $parser; - - /** - * @var Context - */ - protected $context; - - /** - * @param Parser $parser the current parser - */ - public function __construct( Parser $parser, Context $context ) { - $this->parser = $parser; - $this->context = $context; - } - - /** - * Render a <pagelist> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @return string - */ - abstract public function render( $input, array $args ); - - /** - * @param string $errorMsg - * @return string - */ - protected function formatError( $errorMsg ) { - return '<strong class="error">' . wfMessage( $errorMsg )->inContentLanguage()->escaped() . - '</strong>'; - } -} diff --git a/tests/phpunit/Pagination/FilePaginationTest.php b/tests/phpunit/Pagination/FilePaginationTest.php index dadd19a..57ad44b 100644 --- a/tests/phpunit/Pagination/FilePaginationTest.php +++ b/tests/phpunit/Pagination/FilePaginationTest.php @@ -23,7 +23,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( 2, $pagination->getPageNumber( $this->newPagePage( 'LoremIpsum.djvu/2', $index ) @@ -47,7 +47,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); return [ [ @@ -76,7 +76,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( $pageNumber, $pagination->getDisplayedPageNumber( 1 ) ); } @@ -92,7 +92,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( $pageNumber, $pagination->getDisplayedPageNumber( 1 ) ); } @@ -116,7 +116,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( 5, $pagination->getNumberOfPages() ); } @@ -132,7 +132,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( $page, $pagination->getPage( 2 ) ); } @@ -150,7 +150,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $pagination->getPage( 42 ); } @@ -167,7 +167,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( 1, $pagination->key() ); diff --git a/tests/phpunit/Pagination/PaginationFactoryTest.php b/tests/phpunit/Pagination/PaginationFactoryTest.php index ddf0627..0cef949 100644 --- a/tests/phpunit/Pagination/PaginationFactoryTest.php +++ b/tests/phpunit/Pagination/PaginationFactoryTest.php @@ -28,7 +28,7 @@ $this->getContext()->getFileProvider()->getFileFromTitle( Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' ) ), - $this->getContext() + $this->getPageNamespaceId() ); $this->assertEquals( $pagination, -- To view, visit https://gerrit.wikimedia.org/r/371484 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5234dd05280af661c5ca7e00b3116eb8d4415e15 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