Tpt has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/371131 )

Change subject: Refactors TagParsers
......................................................................

Refactors TagParsers

Change-Id: I794beef218568e1db0c627bda0ad8f17e5f7a251
---
M ProofreadPage.body.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
10 files changed, 141 insertions(+), 146 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage 
refs/changes/31/371131/1

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index eda957a..0193fd0 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 boolean 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()
+                       );
+                       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/includes/Pagination/FilePagination.php 
b/includes/Pagination/FilePagination.php
index 48a6e7a..7ef1990 100644
--- a/includes/Pagination/FilePagination.php
+++ b/includes/Pagination/FilePagination.php
@@ -32,23 +32,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 +128,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..3023e3c 100644
--- a/includes/Pagination/PaginationFactory.php
+++ b/includes/Pagination/PaginationFactory.php
@@ -58,7 +58,7 @@
                                $indexPage,
                                $pagelist,
                                $file,
-                               $this->context
+                               $this->context->getPageNamespaceId()
                        );
                } else {
                        $links = $indexPage->getContent()->getLinksToNamespace(
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 5ccf5d2..f1c93ea 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\Pagination\FilePagination;
+use ProofreadPage\Pagination\PaginationFactory;
 use ProofreadPageDbConnector;
 use ProofreadPagePage;
 use Title;
@@ -15,10 +17,38 @@
  *
  * 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;
+
+       public function __construct(
+               Parser $parser, $pageNamespaceId, $indexNamespaceId, 
PaginationFactory $paginationFactory
+       ) {
+               $this->parser = $parser;
+               $this->pageNamespaceId = $pageNamespaceId;
+               $this->indexNamespaceId = $indexNamespaceId;
+               $this->paginationFactory = $paginationFactory;
+       }
+
+       /**
+        * 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 +73,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 +88,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 +188,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 +197,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 +285,7 @@
                                );
                        }
                        catch ( OutOfBoundsException $e ) {
-         } // if the first page does not exists
+                       } // if the first page does not exists
                }
 
                if ( $header ) {
@@ -365,4 +392,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 b17be7f..939a69a 100644
--- a/tests/phpunit/Pagination/FilePaginationTest.php
+++ b/tests/phpunit/Pagination/FilePaginationTest.php
@@ -24,7 +24,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals( 2, $pagination->getPageNumber(
                        new ProofreadPagePage( Title::makeTitle( 250, 
'LoremIpsum.djvu/2' ), $index )
@@ -48,7 +48,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                return [
                        [
@@ -77,7 +77,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals( $pageNumber, 
$pagination->getDisplayedPageNumber( 1 ) );
        }
@@ -93,7 +93,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals( $pageNumber, 
$pagination->getDisplayedPageNumber( 1 ) );
        }
@@ -117,7 +117,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals( 5, $pagination->getNumberOfPages() );
        }
@@ -133,7 +133,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals( $page, $pagination->getPage( 2 ) );
        }
@@ -151,7 +151,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $pagination->getPage( 42 );
        }
@@ -168,7 +168,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
 
                $this->assertEquals( 1, $pagination->key() );
diff --git a/tests/phpunit/Pagination/PaginationFactoryTest.php 
b/tests/phpunit/Pagination/PaginationFactoryTest.php
index 752a5f0..21728be 100644
--- a/tests/phpunit/Pagination/PaginationFactoryTest.php
+++ b/tests/phpunit/Pagination/PaginationFactoryTest.php
@@ -29,7 +29,7 @@
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       250
                );
                $this->assertEquals(
                        $pagination,

-- 
To view, visit https://gerrit.wikimedia.org/r/371131
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I794beef218568e1db0c627bda0ad8f17e5f7a251
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

Reply via email to