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

Change subject: Moves ProofreaPagePage::getPageNumber to FileProvider
......................................................................

Moves ProofreaPagePage::getPageNumber to FileProvider

Change-Id: I8b2e6d02582961ff1d988d6c6418b15f69db7a96
---
M ProofreadPage.body.php
M extension.json
M includes/FileProvider.php
A includes/PageNumberNotFoundException.php
M includes/Pagination/FilePagination.php
M includes/page/PageContentBuilder.php
M includes/page/PageDisplayHandler.php
M includes/page/ProofreadPagePage.php
M tests/phpunit/FileProviderTest.php
M tests/phpunit/page/ProofreadPagePageTest.php
10 files changed, 81 insertions(+), 41 deletions(-)


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

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 5f8eb39..882b69d 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -21,9 +21,9 @@
 
 use ProofreadPage\Context;
 use ProofreadPage\FileNotFoundException;
-use ProofreadPage\Index\EditIndexPage;
 use ProofreadPage\Page\PageContent;
 use ProofreadPage\Page\PageContentBuilder;
+use ProofreadPage\PageNumberNotFoundException;
 use ProofreadPage\Pagination\PageNotInPaginationException;
 
 /*
@@ -760,16 +760,19 @@
 
                // Image link
                try {
-                       $image = 
Context::getDefaultContext()->getFileProvider()->getForPagePage( $page );
+                       $fileProvider = 
Context::getDefaultContext()->getFileProvider();
+                       $image = $fileProvider->getForPagePage( $page );
                        $imageUrl = null;
                        if ( $image->isMultipage() ) {
                                $transformAttributes = [
                                        'width' => $image->getWidth()
                                ];
-                               $pageNumber = $page->getPageNumber();
-                               if ( $pageNumber !== null ) {
-                                       $transformAttributes['page'] = 
$pageNumber;
+                               try {
+                                       $transformAttributes['page'] = 
$fileProvider->getPageNumberForPagePage( $page );
+                               } catch ( PageNumberNotFoundException $e ) {
+                                       // We do not care
                                }
+
                                $handler = $image->getHandler();
                                if ( $handler && $handler->normaliseParams( 
$image, $transformAttributes ) ) {
                                        $thumbName = $image->thumbName( 
$transformAttributes );
@@ -788,8 +791,7 @@
                                        'text' => wfMessage( 
'proofreadpage_image' )->plain()
                                ];
                        }
-               }
-               catch ( FileNotFoundException $e ) {
+               } catch ( FileNotFoundException $e ) {
                }
 
                // Prev, Next and Index links
diff --git a/extension.json b/extension.json
index 0ecd250..6b79dab 100644
--- a/extension.json
+++ b/extension.json
@@ -45,6 +45,7 @@
                "ProofreadPage\\ProofreadPageInit": 
"includes/ProofreadPageInit.php",
                "ProofreadPage\\DiffFormatterUtils": 
"includes/DiffFormatterUtils.php",
                "ProofreadPage\\FileNotFoundException": 
"includes/FileNotFoundException.php",
+               "ProofreadPage\\PageNumberNotFoundException": 
"includes/PageNumberNotFoundException.php",
                "ProofreadPage\\FileProvider": "includes/FileProvider.php",
                "ProofreadPage\\Link": "includes/Link.php",
                "ProofreadIndexPage": "includes/index/ProofreadIndexPage.php",
diff --git a/includes/FileProvider.php b/includes/FileProvider.php
index dea9d38..be19999 100644
--- a/includes/FileProvider.php
+++ b/includes/FileProvider.php
@@ -63,4 +63,20 @@
                        Title::makeTitle( NS_FILE, strtok( 
$page->getTitle()->getText(), '/' ) )
                );
        }
+
+       /**
+        * @param ProofreadPagePage $page
+        * @return int
+        * @throws PageNumberNotFoundException
+        */
+       public function getPageNumberForPagePage( ProofreadPagePage $page ) {
+               $parts = explode( '/', $page->getTitle()->getText() );
+               if ( count( $parts ) === 1 ) {
+                       throw new PageNumberNotFoundException(
+                               $page->getTitle()->getFullText() . ' does not 
provide a page number.'
+                       );
+               }
+               return (int)$page->getTitle()->getPageLanguage()
+                       ->parseFormattedNumber( $parts[count( $parts ) - 1] );
+       }
 }
diff --git a/includes/PageNumberNotFoundException.php 
b/includes/PageNumberNotFoundException.php
new file mode 100644
index 0000000..342203b
--- /dev/null
+++ b/includes/PageNumberNotFoundException.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace ProofreadPage;
+
+use RuntimeException;
+
+/**
+ * @licence GNU GPL v2+
+ *
+ * An exception thrown if a page number is not found
+ */
+class PageNumberNotFoundException extends RuntimeException {
+}
diff --git a/includes/Pagination/FilePagination.php 
b/includes/Pagination/FilePagination.php
index 0a34c23..d3519e4 100644
--- a/includes/Pagination/FilePagination.php
+++ b/includes/Pagination/FilePagination.php
@@ -6,6 +6,7 @@
 use OutOfBoundsException;
 use ProofreadIndexPage;
 use ProofreadPage\Context;
+use ProofreadPage\PageNumberNotFoundException;
 use ProofreadPagePage;
 use Title;
 
@@ -59,14 +60,19 @@
         * @see ProofreadPagination::getPageNumber
         */
        public function getPageNumber( ProofreadPagePage $page ) {
-               $pageNumber = $page->getPageNumber();
                $index = 
$this->context->getIndexForPageLookup()->getIndexForPage( $page );
-               if ( $pageNumber === null || $index === null || 
!$this->index->equals( $index ) ) {
+               if ( $index === null || !$this->index->equals( $index ) ) {
                        throw new PageNotInPaginationException(
                                $page->getTitle()->getFullText() . ' does not 
belong to the pagination'
                        );
                }
-               return $pageNumber;
+               try {
+                       return 
$this->context->getFileProvider()->getPageNumberForPagePage( $page );
+               } catch ( PageNumberNotFoundException $e ) {
+                       throw new PageNotInPaginationException(
+                               $page->getTitle()->getFullText() . ' does not 
have page numbers'
+                       );
+               }
        }
 
        /**
diff --git a/includes/page/PageContentBuilder.php 
b/includes/page/PageContentBuilder.php
index 9f33b0e..add144d 100644
--- a/includes/page/PageContentBuilder.php
+++ b/includes/page/PageContentBuilder.php
@@ -6,6 +6,7 @@
 use OutOfBoundsException;
 use ProofreadPage\Context;
 use ProofreadPage\FileNotFoundException;
+use ProofreadPage\PageNumberNotFoundException;
 use ProofreadPage\Pagination\PageNotInPaginationException;
 use ProofreadPagePage;
 use WikitextContent;
@@ -74,16 +75,19 @@
 
                // Extract text layer
                try {
-                       $image = 
$this->context->getFileProvider()->getForPagePage( $page );
-                       $pageNumber = $page->getPageNumber();
+                       $fileProvider = $this->context->getFileProvider();
+                       $image = $fileProvider->getForPagePage( $page );
                        if ( $image->exists() ) {
-                               if ( $pageNumber !== null && 
$image->isMultipage() ) {
-                                       $text = 
$image->getHandler()->getPageText( $image, $pageNumber );
-                               } else {
-                                       $text = $image->getHandler()
-                                               ? 
$image->getHandler()->getPageText( $image, 1 )
-                                               : '';
+                               $pageNumber = 1;
+                               if ( $image->isMultipage() ) {
+                                       try {
+                                               $pageNumber = 
$fileProvider->getPageNumberForPagePage( $page );
+                                       } catch ( PageNumberNotFoundException 
$e ) {
+                                       }
                                }
+                               $text = $image->getHandler()
+                                       ? $image->getHandler()->getPageText( 
$image, $pageNumber )
+                                       : '';
                                if ( $text ) {
                                        $text = preg_replace( "/(\\\\n)/", 
"\n", $text );
                                        $body = preg_replace( "/(\\\\\d*)/", 
'', $text );
diff --git a/includes/page/PageDisplayHandler.php 
b/includes/page/PageDisplayHandler.php
index a80f705..98252b7 100644
--- a/includes/page/PageDisplayHandler.php
+++ b/includes/page/PageDisplayHandler.php
@@ -6,6 +6,7 @@
 use OutOfBoundsException;
 use ProofreadPage\Context;
 use ProofreadPage\FileNotFoundException;
+use ProofreadPage\PageNumberNotFoundException;
 use ProofreadPagePage;
 use Sanitizer;
 
@@ -108,8 +109,9 @@
         * @return null|string
         */
        private function buildImageHtml( ProofreadPagePage $page, $options ) {
+               $fileProvider = $this->context->getFileProvider();
                try {
-                       $image = 
$this->context->getFileProvider()->getForPagePage( $page );
+                       $image = $fileProvider->getForPagePage( $page );
                } catch ( FileNotFoundException $e ) {
                        return null;
                }
@@ -125,9 +127,9 @@
                ];
 
                if ( $image->isMultipage() ) {
-                       $pageNumber = $page->getPageNumber();
-                       if ( $pageNumber !== null ) {
-                               $transformAttributes['page'] = $pageNumber;
+                       try {
+                               $transformAttributes['page'] = 
$fileProvider->getPageNumberForPagePage( $page );
+                       } catch ( PageNumberNotFoundException $e ) {
                        }
                }
                $handler = $image->getHandler();
diff --git a/includes/page/ProofreadPagePage.php 
b/includes/page/ProofreadPagePage.php
index 193cd48..230960c 100644
--- a/includes/page/ProofreadPagePage.php
+++ b/includes/page/ProofreadPagePage.php
@@ -65,17 +65,4 @@
        public function getTitle() {
                return $this->title;
        }
-
-       /**
-        * Returns number of the page in the file if it's a multi-page file or 
null
-        * @return integer|null
-        */
-       public function getPageNumber() {
-               $parts = explode( '/', $this->title->getText() );
-               if ( count( $parts ) === 1 ) {
-                       return null;
-               }
-               return (int)$this->title->getPageLanguage()
-                       ->parseFormattedNumber( $parts[count( $parts ) - 1] );
-       }
 }
diff --git a/tests/phpunit/FileProviderTest.php 
b/tests/phpunit/FileProviderTest.php
index 85d5786..fef63c5 100644
--- a/tests/phpunit/FileProviderTest.php
+++ b/tests/phpunit/FileProviderTest.php
@@ -138,4 +138,19 @@
                        ],
                ];
        }
+
+       public function testGetPageNumberForPagePage() {
+               $fileProvider = new FileProviderMock( [] );
+               $this->assertEquals( 1, $fileProvider->getPageNumberForPagePage(
+                       $this->newPagePage( 'Test.djvu/1' )
+               ) );
+       }
+
+       /**
+        * @expectedException \ProofreadPage\PageNumberNotFoundException
+        */
+       public function testGetPageNumberForPagePageNotFound() {
+               $fileProvider = new FileProviderMock( [] );
+               $this->assertNull( $fileProvider->getPageNumberForPagePage( 
$this->newPagePage( 'Test.djvu' ) ) );
+       }
 }
diff --git a/tests/phpunit/page/ProofreadPagePageTest.php 
b/tests/phpunit/page/ProofreadPagePageTest.php
index 236f81b..25a38b0 100644
--- a/tests/phpunit/page/ProofreadPagePageTest.php
+++ b/tests/phpunit/page/ProofreadPagePageTest.php
@@ -21,10 +21,4 @@
                $page = $this->newPagePage( $title );
                $this->assertEquals( $title, $page->getTitle() );
        }
-
-       public function testGetPageNumber() {
-               $this->assertEquals( 1, $this->newPagePage( 'Test.djvu/1' 
)->getPageNumber() );
-
-               $this->assertNull( $this->newPagePage( 'Test.djvu' 
)->getPageNumber() );
-       }
 }

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

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