Tpt has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/371611 )
Change subject: [WIP] Introduces IndexForPageLookup ...................................................................... [WIP] Introduces IndexForPageLookup Change-Id: Ibf1f16c8fb27073fb6cade19c0d1f4d676a513fc --- M ProofreadPage.body.php M extension.json M includes/Context.php M includes/Pagination/FilePagination.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 A tests/phpunit/page/IndexForPageLookupTest.php M tests/phpunit/page/ProofreadPagePageTest.php 11 files changed, 170 insertions(+), 83 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage refs/changes/11/371611/1 diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php index 56b01ed..5f8eb39 100644 --- a/ProofreadPage.body.php +++ b/ProofreadPage.body.php @@ -793,8 +793,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 042b62b..5fdb723 100644 --- a/extension.json +++ b/extension.json @@ -77,6 +77,7 @@ "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\\Parser\\ParserEntryPoint": "includes/Parser/ParserEntryPoint.php", "ProofreadPage\\Parser\\TagParser": "includes/Parser/TagParser.php", "ProofreadPage\\Parser\\PagelistTagParser": "includes/Parser/PagelistTagParser.php", diff --git a/includes/Context.php b/includes/Context.php index 0bbd60d..78f9da0 100644 --- a/includes/Context.php +++ b/includes/Context.php @@ -3,6 +3,7 @@ namespace ProofreadPage; use ProofreadPage\Index\CustomIndexFieldsParser; +use ProofreadPage\Page\IndexForPageLookup; use ProofreadPage\Pagination\PaginationFactory; use RepoGroup; @@ -38,6 +39,11 @@ private $customIndexFieldsParser; /** + * @var IndexForPageLookup + */ + private $indexForPageLookup; + + /** * @param int $pageNamespaceId * @param int $indexNamespaceId * @param FileProvider $fileProvider @@ -51,6 +57,7 @@ $this->indexNamespaceId = $indexNamespaceId; $this->fileProvider = $fileProvider; $this->customIndexFieldsParser = $customIndexFieldsParser; + $this->indexForPageLookup = new IndexForPageLookup( $indexNamespaceId ); } /** @@ -89,6 +96,13 @@ } /** + * @return IndexForPageLookup + */ + public function getIndexForPageLookup() { + return $this->indexForPageLookup; + } + + /** * @param bool $purgeFileProvider * @return Context */ diff --git a/includes/Pagination/FilePagination.php b/includes/Pagination/FilePagination.php index 48a6e7a..524d85a 100644 --- a/includes/Pagination/FilePagination.php +++ b/includes/Pagination/FilePagination.php @@ -60,8 +60,8 @@ */ public function getPageNumber( ProofreadPagePage $page ) { $pageNumber = $page->getPageNumber(); - $index = $page->getIndex(); - if ( $pageNumber === null || $index === false || !$this->index->equals( $index ) ) { + $index = $this->context->getIndexForPageLookup()->getIndexForPage( $page ); + if ( $pageNumber === null || $index === null || !$this->index->equals( $index ) ) { throw new PageNotInPaginationException( '$page does not belong to the pagination' ); } return $pageNumber; diff --git a/includes/page/IndexForPageLookup.php b/includes/page/IndexForPageLookup.php new file mode 100644 index 0000000..375d158 --- /dev/null +++ b/includes/page/IndexForPageLookup.php @@ -0,0 +1,98 @@ +<?php + +namespace ProofreadPage\Page; + +use ProofreadIndexDbConnector; +use ProofreadIndexPage; +use ProofreadPagePage; +use Title; + +/** + * @licence GNU GPL v2+ + * + * Allows to retrieve the Index: page for a Page: page + */ +class IndexForPageLookup { + + /** + * @var int + */ + private $indexNamespaceId; + + private $cache = []; + + /** + * @param int $indexNamespaceId + */ + public function __construct( $indexNamespaceId ) { + $this->indexNamespaceId = $indexNamespaceId; + } + + /** + * Return index of the page + * @param ProofreadPagePage $page + * @return ProofreadIndexPage|null + */ + 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 = wfFindFile( $imageTitle ); + // if it is multipage, we use the page order of the file + if ( $image && $image->exists() && $image->isMultipage() ) { + return Title::makeTitle( + $this->indexNamespaceId, $image->getTitle()->getText() + ); + } + } + } + return null; + } +} 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 e179544..917b2e0 100644 --- a/includes/page/ProofreadPagePage.php +++ b/includes/page/ProofreadPagePage.php @@ -85,72 +85,4 @@ 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..4d928aa 100644 --- a/tests/phpunit/ContextTest.php +++ b/tests/phpunit/ContextTest.php @@ -39,4 +39,11 @@ '\ProofreadPage\Index\CustomIndexFieldsParser', $context->getCustomIndexFieldsParser() ); } + + public function testIndexForPageLookup() { + $context = new Context( 42, 44, new FileProviderMock( [] ), new CustomIndexFieldsParser() ); + $this->assertInstanceOf( + '\ProofreadPage\Page\IndexForPageLookup', $context->getIndexForPageLookup() + ); + } } diff --git a/tests/phpunit/page/IndexForPageLookupTest.php b/tests/phpunit/page/IndexForPageLookupTest.php new file mode 100644 index 0000000..e75bb39 --- /dev/null +++ b/tests/phpunit/page/IndexForPageLookupTest.php @@ -0,0 +1,40 @@ +<?php + +namespace ProofreadPage\Page; + +use MediaHandler; +use ProofreadIndexPage; +use ProofreadIndexPageTest; +use ProofreadPagePage; +use ProofreadPagePageTest; +use ProofreadPageTestCase; +use ProofreadPage\FileNotFoundException; + +/** + * @group ProofreadPage + * @covers \ProofreadPage\Page\IndexForPageLookup + */ +class IndexForPageLookupTest extends ProofreadPageTestCase { + + /** + * @dataProvider indexForPageProvider + */ + public function testGetIndexForPage( ProofreadPagePage $expected, ProofreadIndexPage $input ) { + $this->assertEquals( + $expected, $this->getContext()->getIndexForPageLookup()->getIndexForPage( $input ) + ); + } + + public function indexForPageProvider() { + return [ + [ + $this->newIndexPage( 'LoremIpsum.djvu' ), + $this->newPagePage( 'LoremIpsum.djvu/2' ) + ], + [ + null, + $this->newPagePage( 'FooBar' ) + ], + ]; + } +} 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/371611 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibf1f16c8fb27073fb6cade19c0d1f4d676a513fc 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