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

Change subject: Introduces IndexForPageLookup
......................................................................

Introduces IndexForPageLookup

Change-Id: I3cea1f364e36c1809a76579f64bbb83b900b0284
---
M ProofreadPage.body.php
M extension.json
M includes/Context.php
M includes/Pagination/FilePagination.php
M includes/Pagination/PaginationFactory.php
A includes/page/DatabaseIndexForPageLookup.php
A includes/page/IndexForPageLookup.php
M includes/page/PageContentBuilder.php
M includes/page/PageDisplayHandler.php
M includes/page/ProofreadPagePage.php
M tests/phpunit/ContextTest.php
M tests/phpunit/Pagination/FilePaginationTest.php
M tests/phpunit/Pagination/PagePaginationTest.php
M tests/phpunit/Pagination/PaginationFactoryTest.php
M tests/phpunit/ProofreadPageTestCase.php
A tests/phpunit/page/DatabaseIndexForPageLookupTest.php
A tests/phpunit/page/IndexForPageLookupMock.php
M tests/phpunit/page/PageContentBuilderTest.php
M tests/phpunit/page/PageDisplayHandlerTest.php
M tests/phpunit/page/ProofreadPagePageTest.php
20 files changed, 346 insertions(+), 190 deletions(-)


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

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 2b78412..c771bed 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -741,8 +741,8 @@
                }
 
                // Prev, Next and Index links
-               $indexPage = $page->getIndex();
-               if ( $indexPage ) {
+               $indexPage = 
Context::getDefaultContext()->getIndexForPageLookup()->getIndexForPage( $page );
+               if ( $indexPage !== null ) {
                        $pagination = Context::getDefaultContext()
                                
->getPaginationFactory()->getPaginationForIndexPage( $indexPage );
                        try {
diff --git a/extension.json b/extension.json
index 30fff6c..ae1ab61 100644
--- a/extension.json
+++ b/extension.json
@@ -82,6 +82,8 @@
                "ProofreadPage\\Page\\PageViewAction": 
"includes/page/PageViewAction.php",
                "ProofreadPage\\Page\\PageDifferenceEngine": 
"includes/page/PageDifferenceEngine.php",
                "ProofreadPage\\Page\\PageDisplayHandler": 
"includes/page/PageDisplayHandler.php",
+               "ProofreadPage\\Page\\IndexForPageLookup": 
"includes/page/IndexForPageLookup.php",
+               "ProofreadPage\\Page\\DatabaseIndexForPageLookup": 
"includes/page/DatabaseIndexForPageLookup.php",
                "ProofreadPage\\Parser\\ParserEntryPoint": 
"includes/Parser/ParserEntryPoint.php",
                "ProofreadPage\\Parser\\TagParser": 
"includes/Parser/TagParser.php",
                "ProofreadPage\\Parser\\PagelistTagParser": 
"includes/Parser/PagelistTagParser.php",
@@ -92,6 +94,7 @@
                "ApiQueryProofread": "ApiQueryProofread.php",
                "ApiQueryProofreadInfo": "ApiQueryProofreadInfo.php",
                "ProofreadPage\\FileProviderMock": 
"tests/phpunit/FileProviderMock.php",
+               "ProofreadPage\\Page\\IndexForPageLookupMock": 
"tests/phpunit/page/IndexForPageLookupMock.php",
                "ProofreadPageTestCase": 
"tests/phpunit/ProofreadPageTestCase.php",
                "FixProofreadPagePagesContentModel": 
"maintenance/fixProofreadPagePagesContentModel.php",
                "FixProofreadIndexPagesContentModel": 
"maintenance/fixProofreadIndexPagesContentModel.php"
diff --git a/includes/Context.php b/includes/Context.php
index 0bbd60d..4644bfb 100644
--- a/includes/Context.php
+++ b/includes/Context.php
@@ -3,6 +3,8 @@
 namespace ProofreadPage;
 
 use ProofreadPage\Index\CustomIndexFieldsParser;
+use ProofreadPage\Page\DatabaseIndexForPageLookup;
+use ProofreadPage\Page\IndexForPageLookup;
 use ProofreadPage\Pagination\PaginationFactory;
 use RepoGroup;
 
@@ -38,19 +40,26 @@
        private $customIndexFieldsParser;
 
        /**
+        * @var IndexForPageLookup
+        */
+       private $indexForPageLookup;
+
+       /**
         * @param int $pageNamespaceId
         * @param int $indexNamespaceId
         * @param FileProvider $fileProvider
         * @param CustomIndexFieldsParser $customIndexFieldsParser
+        * @param IndexForPageLookup $indexForPageLookup
         */
        public function __construct(
                $pageNamespaceId, $indexNamespaceId, FileProvider $fileProvider,
-               CustomIndexFieldsParser $customIndexFieldsParser
+               CustomIndexFieldsParser $customIndexFieldsParser, 
IndexForPageLookup $indexForPageLookup
        ) {
                $this->pageNamespaceId = $pageNamespaceId;
                $this->indexNamespaceId = $indexNamespaceId;
                $this->fileProvider = $fileProvider;
                $this->customIndexFieldsParser = $customIndexFieldsParser;
+               $this->indexForPageLookup = $indexForPageLookup;
        }
 
        /**
@@ -89,22 +98,28 @@
        }
 
        /**
-        * @param bool $purgeFileProvider
+        * @return IndexForPageLookup
+        */
+       public function getIndexForPageLookup() {
+               return $this->indexForPageLookup;
+       }
+
+       /**
+        * @param bool $purge
         * @return Context
         */
-       public static function getDefaultContext( $purgeFileProvider = false ) {
+       public static function getDefaultContext( $purge = false ) {
                static $defaultContext;
 
-               if ( $defaultContext === null ) {
-                       $defaultContext = new self(
-                               ProofreadPageInit::getNamespaceId( 'page' ),
-                               ProofreadPageInit::getNamespaceId( 'index' ),
-                               new FileProvider( RepoGroup::singleton() ),
-                               new CustomIndexFieldsParser()
+               if ( $defaultContext === null || $purge ) {
+                       $repoGroup = RepoGroup::singleton();
+                       $pageNamespaceId = ProofreadPageInit::getNamespaceId( 
'page' );
+                       $indexNamespaceId = ProofreadPageInit::getNamespaceId( 
'index' );
+                       $defaultContext = new self( $pageNamespaceId, 
$indexNamespaceId,
+                               new FileProvider( $repoGroup ),
+                               new CustomIndexFieldsParser(),
+                               new DatabaseIndexForPageLookup( 
$indexNamespaceId, $repoGroup )
                        );
-               }
-               if ( $purgeFileProvider ) {
-                       $defaultContext->fileProvider = new FileProvider( 
RepoGroup::singleton() );
                }
 
                return $defaultContext;
diff --git a/includes/Pagination/FilePagination.php 
b/includes/Pagination/FilePagination.php
index 48a6e7a..0a34c23 100644
--- a/includes/Pagination/FilePagination.php
+++ b/includes/Pagination/FilePagination.php
@@ -60,9 +60,11 @@
         */
        public function getPageNumber( ProofreadPagePage $page ) {
                $pageNumber = $page->getPageNumber();
-               $index = $page->getIndex();
-               if ( $pageNumber === null || $index === false || 
!$this->index->equals( $index ) ) {
-                       throw new PageNotInPaginationException( '$page does not 
belong to the pagination' );
+               $index = 
$this->context->getIndexForPageLookup()->getIndexForPage( $page );
+               if ( $pageNumber === null || $index === null || 
!$this->index->equals( $index ) ) {
+                       throw new PageNotInPaginationException(
+                               $page->getTitle()->getFullText() . ' does not 
belong to the pagination'
+                       );
                }
                return $pageNumber;
        }
@@ -115,11 +117,11 @@
                if ( $i18nNumber !== $pageNumber && !$title->exists() ) {
                        $arabicTitle = $this->buildPageTitleFromPageNumber( 
$pageNumber );
                        if ( $arabicTitle->exists() ) {
-                               return new ProofreadPagePage( $arabicTitle, 
$this->index );
+                               return ProofreadPagePage::newFromTitle( 
$arabicTitle );
                        }
                }
 
-               return new ProofreadPagePage( $title, $this->index );
+               return ProofreadPagePage::newFromTitle( $title );
        }
 
        /**
diff --git a/includes/Pagination/PaginationFactory.php 
b/includes/Pagination/PaginationFactory.php
index 6e62a3e..0faee37 100644
--- a/includes/Pagination/PaginationFactory.php
+++ b/includes/Pagination/PaginationFactory.php
@@ -67,7 +67,7 @@
                        $pages = [];
                        $pageNumbers = [];
                        foreach ( $links as $link ) {
-                               $pages[] = new ProofreadPagePage( 
$link->getTarget(), $indexPage );
+                               $pages[] = ProofreadPagePage::newFromTitle( 
$link->getTarget() );
                                $pageNumbers[] = new PageNumber( 
$link->getLabel() );
                        }
                        return new PagePagination( $indexPage, $pages, 
$pageNumbers );
diff --git a/includes/page/DatabaseIndexForPageLookup.php 
b/includes/page/DatabaseIndexForPageLookup.php
new file mode 100644
index 0000000..a918611
--- /dev/null
+++ b/includes/page/DatabaseIndexForPageLookup.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace ProofreadPage\Page;
+
+use ProofreadIndexDbConnector;
+use ProofreadIndexPage;
+use ProofreadPagePage;
+use RepoGroup;
+use Title;
+
+/**
+ * @licence GNU GPL v2+
+ *
+ * Allows to retrieve the Index: page for a Page: page
+ */
+class DatabaseIndexForPageLookup implements IndexForPageLookup {
+
+       /**
+        * @var int
+        */
+       private $indexNamespaceId;
+
+       /**
+        * @var RepoGroup
+        */
+       private $repoGroup;
+
+       private $cache = [];
+
+       /**
+        * @param int $indexNamespaceId
+        * @param RepoGroup $repoGroup
+        */
+       public function __construct( $indexNamespaceId, RepoGroup $repoGroup ) {
+               $this->indexNamespaceId = $indexNamespaceId;
+               $this->repoGroup = $repoGroup;
+       }
+
+       /**
+        * @see IndexForPageLookup::getIndexForPage
+        */
+       public function getIndexForPage( ProofreadPagePage $page ) {
+               $cacheKey = $page->getTitle()->getDBkey();
+
+               if ( !array_key_exists( $cacheKey, $this->cache ) ) {
+                       $indexTitle = $this->findIndexTitle( $page->getTitle() 
);
+                       if ( $indexTitle === null ) {
+                               $this->cache[$cacheKey] = null;
+                       } else {
+                               $this->cache[$cacheKey] = 
ProofreadIndexPage::newFromTitle( $indexTitle );
+                       }
+               }
+               return $this->cache[$cacheKey];
+       }
+
+       private function findIndexTitle( Title $pageTitle ) {
+               $possibleIndexTitle = $this->findPossibleIndexTitleBasedOnName( 
$pageTitle );
+
+               // Try to find links from Index: pages
+               $result = ProofreadIndexDbConnector::getRowsFromTitle( 
$pageTitle );
+               $indexesThatLinksHere = [];
+               foreach ( $result as $x ) {
+                       $refTitle = Title::makeTitle( $x->page_namespace, 
$x->page_title );
+                       if ( $refTitle !== null &&
+                               $refTitle->inNamespace( $this->indexNamespaceId 
)
+                       ) {
+                               if ( $possibleIndexTitle !== null &&
+                                       // It is the same as the linked file, 
we know it's this Index:
+                                       $refTitle->equals( $possibleIndexTitle )
+                               ) {
+                                       return $refTitle;
+                               }
+                               $indexesThatLinksHere[] = $refTitle;
+                       }
+               }
+               if ( !empty( $indexesThatLinksHere ) ) {
+                       // TODO: what should we do if there are more than 1 
possible index?
+                       return reset( $indexesThatLinksHere );
+               }
+
+               return $possibleIndexTitle;
+       }
+
+       /**
+        * @return Title|null the index page based on the name of the Page: 
page and the existence
+        *   of a file with the same name
+        */
+       private function findPossibleIndexTitleBasedOnName( Title $pageTitle ) {
+               $m = explode( '/', $pageTitle->getText(), 2 );
+               if ( isset( $m[1] ) ) {
+                       $imageTitle = Title::makeTitleSafe( NS_FILE, $m[0] );
+                       if ( $imageTitle !== null ) {
+                               $image = $this->repoGroup->findFile( 
$imageTitle );
+                               // if it is multipage, we use the page order of 
the file
+                               if ( $image->exists() && $image->isMultipage() 
) {
+                                       return Title::makeTitle(
+                                               $this->indexNamespaceId, 
$image->getTitle()->getText()
+                                       );
+                               }
+                       }
+               }
+               return null;
+       }
+}
diff --git a/includes/page/IndexForPageLookup.php 
b/includes/page/IndexForPageLookup.php
new file mode 100644
index 0000000..4dce31a
--- /dev/null
+++ b/includes/page/IndexForPageLookup.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace ProofreadPage\Page;
+
+use ProofreadIndexPage;
+use ProofreadPagePage;
+
+/**
+ * @licence GNU GPL v2+
+ *
+ * Allows to retrieve the Index: page for a Page: page
+ */
+interface IndexForPageLookup {
+
+       /**
+        * Return index of the page
+        * @param ProofreadPagePage $page
+        * @return ProofreadIndexPage|null
+        */
+       public function getIndexForPage( ProofreadPagePage $page );
+}
diff --git a/includes/page/PageContentBuilder.php 
b/includes/page/PageContentBuilder.php
index 0755e74..9f33b0e 100644
--- a/includes/page/PageContentBuilder.php
+++ b/includes/page/PageContentBuilder.php
@@ -41,11 +41,11 @@
         * @return PageContent
         */
        public function buildDefaultContentForPage( ProofreadPagePage $page ) {
-               $index = $page->getIndex();
+               $index = 
$this->context->getIndexForPageLookup()->getIndexForPage( $page );
                $body = '';
 
                // default header and footer
-               if ( $index ) {
+               if ( $index !== null ) {
                        $params = [];
                        try {
                                $pagination = 
$this->context->getPaginationFactory()
diff --git a/includes/page/PageDisplayHandler.php 
b/includes/page/PageDisplayHandler.php
index 9f76a17..a80f705 100644
--- a/includes/page/PageDisplayHandler.php
+++ b/includes/page/PageDisplayHandler.php
@@ -39,8 +39,8 @@
         * @return int
         */
        public function getImageWidth( ProofreadPagePage $page ) {
-               $index = $page->getIndex();
-               if ( $index ) {
+               $index = 
$this->context->getIndexForPageLookup()->getIndexForPage( $page );
+               if ( $index !== null ) {
                        try {
                                $width = 
$this->context->getCustomIndexFieldsParser()->parseCustomIndexField(
                                        $index->getContent(), 'width'
@@ -62,8 +62,8 @@
         * @return string
         */
        public function getCustomCss( ProofreadPagePage $page ) {
-               $index = $page->getIndex();
-               if ( !$index ) {
+               $index = 
$this->context->getIndexForPageLookup()->getIndexForPage( $page );
+               if ( $index === null ) {
                        return '';
                }
                try {
diff --git a/includes/page/ProofreadPagePage.php 
b/includes/page/ProofreadPagePage.php
index c2f1cb5..5877690 100644
--- a/includes/page/ProofreadPagePage.php
+++ b/includes/page/ProofreadPagePage.php
@@ -30,17 +30,10 @@
        protected $title;
 
        /**
-        * @var ProofreadIndexPage|null index related to the page
-        */
-       protected $index;
-
-       /**
         * @param Title $title Reference to a Title object
-        * @param ProofreadIndexPage $index index related to the page
         */
-       public function __construct( Title $title, ProofreadIndexPage $index = 
null ) {
+       public function __construct( Title $title ) {
                $this->title = $title;
-               $this->index = $index;
        }
 
        /**
@@ -81,73 +74,5 @@
                }
                return (int)$this->title->getPageLanguage()
                        ->parseFormattedNumber( $parts[count( $parts ) - 1] );
-       }
-
-       /**
-        * Return index of the page if it exist or false.
-        * @return ProofreadIndexPage|false
-        */
-       public function getIndex() {
-               if ( $this->index !== null ) {
-                       return $this->index;
-               }
-
-               $indexTitle = $this->findIndexTitle();
-               if ( $indexTitle === null ) {
-                       $this->index = false;
-                       return false;
-               } else {
-                       $this->index = ProofreadIndexPage::newFromTitle( 
$indexTitle );
-                       return $this->index;
-               }
-       }
-
-       private function findIndexTitle() {
-               $possibleIndexTitle = 
$this->findPossibleIndexTitleBasedOnName();
-
-               // Try to find links from Index: pages
-               $result = ProofreadIndexDbConnector::getRowsFromTitle( 
$this->title );
-               $indexesThatLinksHere = [];
-               foreach ( $result as $x ) {
-                       $refTitle = Title::makeTitle( $x->page_namespace, 
$x->page_title );
-                       if ( $refTitle !== null &&
-                               $refTitle->inNamespace( 
ProofreadPage::getIndexNamespaceId() )
-                       ) {
-                               if ( $possibleIndexTitle !== null &&
-                                       // It is the same as the linked file, 
we know it's this Index:
-                                       $refTitle->equals( $possibleIndexTitle )
-                               ) {
-                                       return $refTitle;
-                               }
-                               $indexesThatLinksHere[] = $refTitle;
-                       }
-               }
-               if ( !empty( $indexesThatLinksHere ) ) {
-                       // TODO: what should we do if there are more than 1 
possible index?
-                       return reset( $indexesThatLinksHere );
-               }
-
-               return $possibleIndexTitle;
-       }
-
-       /**
-        * @return Title|null the index page based on the name of the Page: 
page and the existence
-        *   of a file with the same name
-        */
-       private function findPossibleIndexTitleBasedOnName() {
-               $m = explode( '/', $this->title->getText(), 2 );
-               if ( isset( $m[1] ) ) {
-                       $imageTitle = Title::makeTitleSafe( NS_FILE, $m[0] );
-                       if ( $imageTitle !== null ) {
-                               $image = wfFindFile( $imageTitle );
-                               // if it is multipage, we use the page order of 
the file
-                               if ( $image && $image->exists() && 
$image->isMultipage() ) {
-                                       return Title::makeTitle(
-                                               
ProofreadPage::getIndexNamespaceId(), $image->getTitle()->getText()
-                                       );
-                               }
-                       }
-               }
-               return null;
        }
 }
diff --git a/tests/phpunit/ContextTest.php b/tests/phpunit/ContextTest.php
index 84b2dbd..76ba53d 100644
--- a/tests/phpunit/ContextTest.php
+++ b/tests/phpunit/ContextTest.php
@@ -3,6 +3,7 @@
 namespace ProofreadPage;
 
 use ProofreadPage\Index\CustomIndexFieldsParser;
+use ProofreadPage\Page\IndexForPageLookupMock;
 use ProofreadPageTestCase;
 
 /**
@@ -12,31 +13,44 @@
 class ContextTest extends ProofreadPageTestCase {
 
        public function testGetPageNamespaceId() {
-               $context = new Context( 42, 44, new FileProviderMock( [] ), new 
CustomIndexFieldsParser() );
-               $this->assertEquals( 42, $context->getPageNamespaceId() );
+               $this->assertEquals( 42, 
$this->buildDummyContext()->getPageNamespaceId() );
        }
 
        public function testGetIndexNamespaceId() {
-               $context = new Context( 42, 44, new FileProviderMock( [] ), new 
CustomIndexFieldsParser() );
-               $this->assertEquals( 44, $context->getIndexNamespaceId() );
+               $this->assertEquals( 44, 
$this->buildDummyContext()->getIndexNamespaceId() );
        }
 
        public function testGetFileProvider() {
-               $context = new Context( 42, 44, new FileProviderMock( [] ), new 
CustomIndexFieldsParser() );
-               $this->assertInstanceOf( '\ProofreadPage\FileProvider', 
$context->getFileProvider() );
+               $this->assertInstanceOf(
+                       '\ProofreadPage\FileProvider',
+                       $this->buildDummyContext()->getFileProvider()
+               );
        }
 
        public function testGetPaginationFactory() {
-               $context = new Context( 42, 44, new FileProviderMock( [] ), new 
CustomIndexFieldsParser() );
                $this->assertInstanceOf(
-                       '\ProofreadPage\Pagination\PaginationFactory', 
$context->getPaginationFactory()
+                       '\ProofreadPage\Pagination\PaginationFactory',
+                       $this->buildDummyContext()->getPaginationFactory()
                );
        }
 
        public function testCustomIndexFieldsParser() {
-               $context = new Context( 42, 44, new FileProviderMock( [] ), new 
CustomIndexFieldsParser() );
                $this->assertInstanceOf(
-                       '\ProofreadPage\Index\CustomIndexFieldsParser', 
$context->getCustomIndexFieldsParser()
+                       '\ProofreadPage\Index\CustomIndexFieldsParser',
+                       $this->buildDummyContext()->getCustomIndexFieldsParser()
+               );
+       }
+
+       public function testIndexForPageLookup() {
+               $this->assertInstanceOf(
+                       '\ProofreadPage\Page\IndexForPageLookup',
+                       $this->buildDummyContext()->getIndexForPageLookup()
+               );
+       }
+
+       private function buildDummyContext() {
+               return new Context( 42, 44,
+                       new FileProviderMock( [] ), new 
CustomIndexFieldsParser(), new IndexForPageLookupMock( [] )
                );
        }
 }
diff --git a/tests/phpunit/Pagination/FilePaginationTest.php 
b/tests/phpunit/Pagination/FilePaginationTest.php
index dadd19a..8c0a320 100644
--- a/tests/phpunit/Pagination/FilePaginationTest.php
+++ b/tests/phpunit/Pagination/FilePaginationTest.php
@@ -17,16 +17,19 @@
 
        public function testGetPageNumber() {
                $index = $this->newIndexPage( 'LoremIpsum.djvu' );
+               $context = $this->getContext( [
+                       'LoremIpsum.djvu/2' => $index
+               ] );
                $pagination = new FilePagination(
                        $index,
                        new PageList( [] ),
-                       
$this->getContext()->getFileProvider()->getFileFromTitle(
+                       $context->getFileProvider()->getFileFromTitle(
                                Title::makeTitle( NS_MEDIA, 'LoremIpsum.djvu' )
                        ),
-                       $this->getContext()
+                       $context
                );
                $this->assertEquals( 2, $pagination->getPageNumber(
-                       $this->newPagePage( 'LoremIpsum.djvu/2', $index )
+                       $this->newPagePage( 'LoremIpsum.djvu/2' )
                ) );
        }
 
@@ -126,7 +129,7 @@
                        $this->markTestSkipped( 'There is no support for DjVu 
files, please enable it.' );
                }
                $index = $this->newIndexPage( 'LoremIpsum.djvu' );
-               $page = $this->newPagePage( 'LoremIpsum.djvu/2', $index );
+               $page = $this->newPagePage( 'LoremIpsum.djvu/2' );
                $pagination = new FilePagination(
                        $index, new PageList( [] ),
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
@@ -160,8 +163,8 @@
                        $this->markTestSkipped( 'There is no support for DjVu 
files, please enable it.' );
                }
                $index = $this->newIndexPage( 'LoremIpsum.djvu' );
-               $page1 = $this->newPagePage( 'LoremIpsum.djvu/1', $index );
-               $page2 = $this->newPagePage( 'LoremIpsum.djvu/2', $index );
+               $page1 = $this->newPagePage( 'LoremIpsum.djvu/1' );
+               $page2 = $this->newPagePage( 'LoremIpsum.djvu/2' );
                $pagination = new FilePagination(
                        $index, new PageList( [] ),
                        
$this->getContext()->getFileProvider()->getFileFromTitle(
diff --git a/tests/phpunit/Pagination/PagePaginationTest.php 
b/tests/phpunit/Pagination/PagePaginationTest.php
index 677e8cc..ffdbe12 100644
--- a/tests/phpunit/Pagination/PagePaginationTest.php
+++ b/tests/phpunit/Pagination/PagePaginationTest.php
@@ -4,9 +4,7 @@
 
 use InvalidArgumentException;
 use OutOfBoundsException;
-use ProofreadPagePage;
 use ProofreadPageTestCase;
-use Title;
 
 /**
  * @group ProofreadPage
@@ -16,13 +14,13 @@
 
        public function testGetPageNumber() {
                $index = $this->newIndexPage();
-               $page = new ProofreadPagePage( Title::newFromText( 'Page:Test 
2.tiff' ), $index );
+               $page = $this->newPagePage( 'Test 2.tiff' );
                $pagination = new PagePagination(
                        $index,
                        [
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 1.jpg' ), $index ),
+                               $this->newPagePage( 'Test 1.jpg' ),
                                $page,
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test:3.png' ), $index )
+                               $this->newPagePage( 'Test:3.png' )
                        ],
                        [
                                new PageNumber( 'TOC' ),
@@ -31,7 +29,7 @@
                        ]
                );
                $this->assertEquals( 2, $pagination->getPageNumber(
-                       new ProofreadPagePage( Title::newFromText( 'Page:Test 
2.tiff' ), $index )
+                       $this->newPagePage( 'Test 2.tiff' )
                ) );
        }
 
@@ -42,7 +40,7 @@
                $index = $this->newIndexPage();
                $pagination = new PagePagination( $index, [], [] );
                $pagination->getPageNumber(
-                       new ProofreadPagePage( Title::newFromText( 'Page:Test 
2.tiff' ), $index )
+                       $this->newPagePage( 'Test 2.tiff' )
                );
        }
 
@@ -51,7 +49,7 @@
                $pageNumber = new PageNumber( 'TOC' );
                $pagination = new PagePagination(
                        $index,
-                       [ new ProofreadPagePage( Title::newFromText( 'Page:Test 
1.jpg' ), $index ) ],
+                       [ $this->newPagePage( 'Test 1.jpg' ) ],
                        [ $pageNumber ]
                );
                $this->assertEquals( $pageNumber, 
$pagination->getDisplayedPageNumber( 1 ) );
@@ -71,9 +69,9 @@
                $pagination = new PagePagination(
                        $index,
                        [
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 1.jpg' ), $index ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 2.jpg' ), $index ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test:3.png' ), $index )
+                               $this->newPagePage( 'Test 1.jpg' ),
+                               $this->newPagePage( 'Test 2.jpg' ),
+                               $this->newPagePage( 'Test:3.png' )
                        ],
                        [
                                new PageNumber( 'TOC' ),
@@ -86,13 +84,13 @@
 
        public function testGetPage() {
                $index = $this->newIndexPage();
-               $page = new ProofreadPagePage( Title::newFromText( 'Page:Test 
1.jpg' ), $index );
+               $page = $this->newPagePage( 'Test 1.jpg' );
                $pagination = new PagePagination(
                        $index,
                        [
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 1.jpg' ), $index ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 2.tiff' ), $index ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test:3.png' ), $index )
+                               $this->newPagePage( 'Test 1.jpg' ),
+                               $this->newPagePage( 'Test 2.tiff' ),
+                               $this->newPagePage( 'Test:3.png' )
                        ],
                        [
                                new PageNumber( 'TOC' ),
@@ -114,8 +112,8 @@
 
        public function testIterator() {
                $index = $this->newIndexPage();
-               $page1 = new ProofreadPagePage( Title::newFromText( 'Page:Test 
1.jpg' ), $index );
-               $page2 = new ProofreadPagePage( Title::newFromText( 'Page:Test 
2.jpg' ), $index );
+               $page1 = $this->newPagePage( 'Test 1.jpg' );
+               $page2 = $this->newPagePage( 'Test 2.jpg' );
                $pagination = new PagePagination(
                        $index,
                        [ $page1, $page2 ],
diff --git a/tests/phpunit/Pagination/PaginationFactoryTest.php 
b/tests/phpunit/Pagination/PaginationFactoryTest.php
index ddf0627..f3cb7bc 100644
--- a/tests/phpunit/Pagination/PaginationFactoryTest.php
+++ b/tests/phpunit/Pagination/PaginationFactoryTest.php
@@ -3,7 +3,6 @@
 namespace ProofreadPage\Pagination;
 
 use MediaHandler;
-use ProofreadPagePage;
 use ProofreadPageTestCase;
 use Title;
 
@@ -37,17 +36,17 @@
        }
 
        public function testGetPaginationWithoutPagelist() {
-               $page = $this->newIndexPage(
+               $index = $this->newIndexPage(
                        'Test',
                        "{{\n|Pages=[[Page:Test 1.jpg|TOC]] [[Page:Test 
2.tiff|1]] " .
                        "[[Page:Test:3.png|2]]\n|Author=[[Author:Me]]\n}}"
                );
                $pagination = new PagePagination(
-                       $page,
+                       $index,
                        [
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 1.jpg' ), $page ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test 2.tiff' ), $page ),
-                               new ProofreadPagePage( Title::newFromText( 
'Page:Test:3.png' ), $page )
+                               $this->newPagePage( Title::newFromText( 
'Page:Test 1.jpg' ) ),
+                               $this->newPagePage( Title::newFromText( 
'Page:Test 2.tiff' ) ),
+                               $this->newPagePage( Title::newFromText( 
'Page:Test:3.png' ) )
                        ],
                        [
                                new PageNumber( 'TOC' ),
@@ -57,7 +56,11 @@
                );
                $this->assertEquals(
                        $pagination,
-                       
$this->getContext()->getPaginationFactory()->getPaginationForIndexPage( $page )
+                       $this->getContext( [
+                               'Page:Test_1.jpg' => $index,
+                               'Page:Test_2.tiff' => $index,
+                               'Page:Test:3.png' => $index,
+                       ] )->getPaginationFactory()->getPaginationForIndexPage( 
$index )
                );
        }
 }
diff --git a/tests/phpunit/ProofreadPageTestCase.php 
b/tests/phpunit/ProofreadPageTestCase.php
index ae74ecb..5c50cf0 100644
--- a/tests/phpunit/ProofreadPageTestCase.php
+++ b/tests/phpunit/ProofreadPageTestCase.php
@@ -5,6 +5,7 @@
 use ProofreadPage\FileProviderMock;
 use ProofreadPage\Index\CustomIndexFieldsParser;
 use ProofreadPage\Index\IndexContent;
+use ProofreadPage\Page\IndexForPageLookupMock;
 use ProofreadPage\ProofreadPageInit;
 
 /**
@@ -99,15 +100,15 @@
        }
 
        /**
+        * @param ProofreadIndexPage[] $indexForPage
         * @return Context
         */
-       protected function getContext() {
+       protected function getContext( $indexForPage = [] ) {
                if ( $this->context === null ) {
-                       $this->context = new Context(
-                               ProofreadPageInit::getNamespaceId( 'page' ),
-                               ProofreadPageInit::getNamespaceId( 'index' ),
+                       $this->context = new Context( 
$this->getPageNamespaceId(), $this->getIndexNamespaceId(),
                                $this->getFileProvider(),
-                               new CustomIndexFieldsParser( 
self::$customIndexFieldsConfiguration )
+                               new CustomIndexFieldsParser( 
self::$customIndexFieldsConfiguration ),
+                               new IndexForPageLookupMock( $indexForPage )
                        );
                }
 
@@ -117,14 +118,13 @@
        /**
         * Constructor of a new ProofreadPagePage
         * @param Title|string $title
-        * @param ProofreadIndexPage|null $index
         * @return ProofreadPagePage
         */
-       public function newPagePage( $title = 'test.jpg', ProofreadIndexPage 
$index = null ) {
+       public function newPagePage( $title = 'test.jpg' ) {
                if ( is_string( $title ) ) {
                        $title = Title::makeTitle( $this->getPageNamespaceId(), 
$title );
                }
-               return new ProofreadPagePage( $title, $index );
+               return ProofreadPagePage::newFromTitle( $title );
        }
 
        /**
@@ -170,7 +170,7 @@
        /**
         * @return File[]
         */
-       private function buildFileList() {
+       protected function buildFileList() {
                $backend = new FSFileBackend( [
                        'name' => 'localtesting',
                        'wikiId' => wfWikiID(),
diff --git a/tests/phpunit/page/DatabaseIndexForPageLookupTest.php 
b/tests/phpunit/page/DatabaseIndexForPageLookupTest.php
new file mode 100644
index 0000000..1a8656b
--- /dev/null
+++ b/tests/phpunit/page/DatabaseIndexForPageLookupTest.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace ProofreadPage\Page;
+
+use ProofreadPageTestCase;
+use RepoGroup;
+
+/**
+ * @group ProofreadPage
+ * @covers \ProofreadPage\Page\DatabaseIndexForPageLookup
+ */
+class DatabaseIndexForPageLookupTest extends ProofreadPageTestCase {
+
+       public function testGetIndexForPage() {
+               $repoGroupMock = $this->getMockBuilder( '\RepoGroup' 
)->disableOriginalConstructor()->getMock();
+               $repoGroupMock->expects( $this->once() )
+                       ->method( 'findFile' )
+                       ->willReturn( $this->buildFileList()[0] );
+               $lookup = new DatabaseIndexForPageLookup(
+                       $this->getIndexNamespaceId(),
+                       $repoGroupMock
+               );
+               $this->assertEquals(
+                       $this->newIndexPage( 'LoremIpsum.djvu' ),
+                       $lookup->getIndexForPage( $this->newPagePage( 
'LoremIpsum.djvu/2' ) )
+               );
+       }
+
+       public function testGetIndexForPageNotFound() {
+               $lookup = new DatabaseIndexForPageLookup( 
$this->getIndexNamespaceId(), RepoGroup::singleton() );
+               $this->assertNull( $lookup->getIndexForPage( 
$this->newPagePage( 'FooBar' ) ) );
+       }
+}
diff --git a/tests/phpunit/page/IndexForPageLookupMock.php 
b/tests/phpunit/page/IndexForPageLookupMock.php
new file mode 100644
index 0000000..8a015eb
--- /dev/null
+++ b/tests/phpunit/page/IndexForPageLookupMock.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace ProofreadPage\Page;
+
+use ProofreadPagePage;
+
+/**
+ * @licence GNU GPL v2+
+ *
+ * Provide a FileProviderMock mock based on a mapping
+ */
+class IndexForPageLookupMock implements IndexForPageLookup {
+
+       private $indexForPage = [];
+
+       public function __construct( $indexForPage ) {
+               $this->indexForPage = $indexForPage;
+       }
+
+       /**
+        * @see IndexForPageLookup::getIndexForPage
+        */
+       public function getIndexForPage( ProofreadPagePage $page ) {
+               if ( !array_key_exists( $page->getTitle()->getDBkey(), 
$this->indexForPage ) ) {
+                       return null;
+               }
+               return $this->indexForPage[ $page->getTitle()->getDBkey() ];
+       }
+}
diff --git a/tests/phpunit/page/PageContentBuilderTest.php 
b/tests/phpunit/page/PageContentBuilderTest.php
index 3202914..a6aa5d1 100644
--- a/tests/phpunit/page/PageContentBuilderTest.php
+++ b/tests/phpunit/page/PageContentBuilderTest.php
@@ -4,6 +4,7 @@
 
 use IContextSource;
 use MediaHandler;
+use ProofreadIndexPage;
 use ProofreadPage\FileNotFoundException;
 use ProofreadPagePage;
 use ProofreadPageTestCase;
@@ -42,10 +43,13 @@
         * @dataProvider buildDefaultContentForPageProvider
         */
        public function testBuildDefaultContentForPage(
-               ProofreadPagePage $page, PageContent $defaultContent
+               ProofreadPagePage $page, ProofreadIndexPage $index = null, 
PageContent $defaultContent
        ) {
+               $context = $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] );
                try {
-                       $image = 
$this->getContext()->getFileProvider()->getForPagePage( $page );
+                       $image = $context->getFileProvider()->getForPagePage( 
$page );
                } catch ( FileNotFoundException $e ) {
                        $image = false;
                }
@@ -53,7 +57,7 @@
                if ( $image && MediaHandler::getHandler( 'image/vnd.djvu' ) === 
false ) {
                        $this->markTestSkipped( 'There is no support for DjVu 
files, please enable it.' );
                }
-               $contentBuilder = new PageContentBuilder( $this->context, 
$this->getContext() );
+               $contentBuilder = new PageContentBuilder( $this->context, 
$context );
                $this->assertEquals(
                        $defaultContent, 
$contentBuilder->buildDefaultContentForPage( $page )
                );
@@ -62,38 +66,28 @@
        public function buildDefaultContentForPageProvider() {
                return [
                        [
-                               $this->newPagePage(
-                                       'Test.djvu/1',
-                                       $this->newIndexPage(
-                                               'Test.djvu', "{{\n|Title=Test 
book\n|Header={{{title}}}\n}}"
-                                       )
-                               ),
+                               $this->newPagePage( 'Test.djvu/1' ),
+                               $this->newIndexPage( 'Test.djvu', 
"{{\n|Title=Test book\n|Header={{{title}}}\n}}" ),
                                self::newContent( 'Test book', '', '<references 
/>', 1 ),
                        ],
                        [
-                               $this->newPagePage(
-                                       'LoremIpsum.djvu/2'
-                               ),
+                               $this->newPagePage( 'LoremIpsum.djvu/2' ),
+                               null,
                                self::newContent( '', "Lorem ipsum \n2 \n", 
'<references/>', 1 ),
                        ],
                        [
-                               $this->newPagePage(
-                                       'LoremIpsum.djvu/2',
-                                       $this->newIndexPage(
-                                               'LoremIpsum.djvu',
-                                               "{{\n|Title=Test 
book\n|Pages=<pagelist/>\n|Header={{{pagenum}}}\n}}"
-                                       )
+                               $this->newPagePage( 'LoremIpsum.djvu/2' ),
+                               $this->newIndexPage( 'LoremIpsum.djvu',
+                                       "{{\n|Title=Test 
book\n|Pages=<pagelist/>\n|Header={{{pagenum}}}\n}}"
                                ),
                                self::newContent( '2', "Lorem ipsum \n2 \n", 
'<references />', 1 ),
                        ],
                        [
-                               $this->newPagePage(
-                                       'LoremIpsum.djvu/2',
-                                       $this->newIndexPage(
-                                               'LoremIpsum.djvu',
-                                               "{{\n|Title=Test 
book\n|Pages=<pagelist 1to5=roman />\n" .
-                                                       
"|Header={{{pagenum}}}\n}}"
-                                       )
+                               $this->newPagePage( 'LoremIpsum.djvu/2' ),
+                               $this->newIndexPage(
+                                       'LoremIpsum.djvu',
+                                       "{{\n|Title=Test book\n|Pages=<pagelist 
1to5=roman />\n" .
+                                       "|Header={{{pagenum}}}\n}}"
                                ),
                                self::newContent( 'ii', "Lorem ipsum \n2 \n", 
'<references />', 1 ),
                        ],
diff --git a/tests/phpunit/page/PageDisplayHandlerTest.php 
b/tests/phpunit/page/PageDisplayHandlerTest.php
index 9a0983a..71f2804 100644
--- a/tests/phpunit/page/PageDisplayHandlerTest.php
+++ b/tests/phpunit/page/PageDisplayHandlerTest.php
@@ -11,34 +11,51 @@
 class PageDisplayHandlerTest extends ProofreadPageTestCase {
 
        public function testGetImageWidth() {
-               $handler = new PageDisplayHandler( $this->getContext() );
-
                $index = $this->newIndexPage( 'Test', "{{\n|width= 500 \n}}" );
-               $page = $this->newPagePage( 'Test.jpg', $index );
+               $page = $this->newPagePage( 'Test.jpg' );
+               $handler = new PageDisplayHandler( $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] ) );
                $this->assertEquals( 500, $handler->getImageWidth( $page ) );
+       }
 
+       public function testGetImageWidthWithDefault() {
                $index = $this->newIndexPage( 'Test', "{{\n|title=500\n}}" );
-               $page = $this->newPagePage( 'Test.jpg', $index );
+               $page = $this->newPagePage( 'Test.jpg' );
+               $handler = new PageDisplayHandler( $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] ) );
                $this->assertEquals( PageDisplayHandler::DEFAULT_IMAGE_WIDTH, 
$handler->getImageWidth( $page ) );
        }
 
        public function testGetCustomCss() {
-               $handler = new PageDisplayHandler( $this->getContext() );
-
                $index = $this->newIndexPage( 'Test', "{{\n|CSS= width:300px; 
\n}}" );
-               $page = $this->newPagePage( 'Test.jpg', $index );
+               $page = $this->newPagePage( 'Test.jpg' );
+               $handler = new PageDisplayHandler( $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] ) );
                $this->assertEquals( 'width:300px;', $handler->getCustomCss( 
$page ) );
+       }
 
+       public function testGetCustomCssWithInsecureInput() {
                $index = $this->newIndexPage(
                        'Test', "{{\n|CSS= background: 
url('/my-bad-url.jpg');\n}}"
                );
-               $page = $this->newPagePage( 'Test.jpg', $index );
+               $page = $this->newPagePage( 'Test.jpg' );
+               $handler = new PageDisplayHandler( $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] ) );
                $this->assertEquals( '/* insecure input */', 
$handler->getCustomCss( $page ) );
+       }
 
+       public function testGetCustomCssWithEscaping() {
                $index = $this->newIndexPage(
                        'Test', "{{\n|CSS= width:300px;<style> \n}}"
                );
-               $page = $this->newPagePage( 'Test.jpg', $index );
+               $page = $this->newPagePage( 'Test.jpg' );
+               $handler = new PageDisplayHandler( $this->getContext( [
+                       $page->getTitle()->getDBkey() => $index
+               ] ) );
                $this->assertEquals( 'width:300px;&lt;style&gt;', 
$handler->getCustomCss( $page ) );
        }
 }
diff --git a/tests/phpunit/page/ProofreadPagePageTest.php 
b/tests/phpunit/page/ProofreadPagePageTest.php
index 86b8d34..236f81b 100644
--- a/tests/phpunit/page/ProofreadPagePageTest.php
+++ b/tests/phpunit/page/ProofreadPagePageTest.php
@@ -27,9 +27,4 @@
 
                $this->assertNull( $this->newPagePage( 'Test.djvu' 
)->getPageNumber() );
        }
-
-       public function testGetIndex() {
-               $index = $this->newIndexPage();
-               $this->assertEquals( $index, $this->newPagePage( 'Test.jpg', 
$index )->getIndex() );
-       }
 }

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

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