Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/322121

Change subject: Adjust to redirect directly to a RESTbase URL
......................................................................

Adjust to redirect directly to a RESTbase URL

Bug: T150427
Change-Id: I3273ca18aaaf97d47f57769c35e8deb133be5073
---
M extension.json
M i18n/en.json
M specials/SpecialElectronPdf.php
3 files changed, 13 insertions(+), 102 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ElectronPdfService 
refs/changes/21/322121/1

diff --git a/extension.json b/extension.json
index 75e4b5f..8a98267 100644
--- a/extension.json
+++ b/extension.json
@@ -14,13 +14,7 @@
                "SpecialElectronPdf": "specials/SpecialElectronPdf.php"
        },
        "config": {
-               "ElectronPdfService": {
-                       "serviceUrl":"https://pdf-electron.wmflabs.org";,
-                       "format":"pdf",
-                       "key":"secret",
-                       "pageUrl":""
-               },
-               "ElectronPdfServiceMaxDocumentSize": 1073741824
+               "ElectronPdfServiceRESTbaseURL": "/api/rest_v1/page/pdf/"
        },
        "ExtensionMessagesFiles": {
                "ElectronPdfServiceAlias": "ElectronPdfService.i18n.alias.php"
diff --git a/i18n/en.json b/i18n/en.json
index 95a26c6..17ea04c 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -16,7 +16,5 @@
        "electronPdfService-sidebar-portlet-heading": "Print/export",
        "electronPdfService-sidebar-portlet-print-text": "Download as PDF",
        "electronPdfService-invalid-page-title": "Invalid page",
-       "electronPdfService-invalid-page-text": "The specified page is not 
valid.",
-       "electronPdfService-page-notfound-title": "Page not found by service",
-       "electronPdfService-page-notfound-text": "The service was not able to 
resolve the specified link."
+       "electronPdfService-invalid-page-text": "The specified page is not 
valid."
 }
\ No newline at end of file
diff --git a/specials/SpecialElectronPdf.php b/specials/SpecialElectronPdf.php
index 8c5797f..f93a503 100644
--- a/specials/SpecialElectronPdf.php
+++ b/specials/SpecialElectronPdf.php
@@ -58,8 +58,8 @@
                $stats->increment( 'electronpdf.action.' . $action );
 
                switch ( $action ) {
-                       case 'download-electron-pdf':
-                               $this->renderAndShowPdf( $title );
+                       case 'redirect-to-electron':
+                               $this->redirectToElectron( $title );
                                return;
                        case 'redirect-to-collection':
                                $this->redirectToCollection( 
$collectionDownloadUrl );
@@ -95,7 +95,7 @@
                        ( new OOUI\Tag() )
                                ->addClasses( [ 
'mw-electronPdfService-selection-body' ] )
                                ->appendContent(
-                                       $this->getLabeledOptionField( 
'download-electron-pdf', 'single', true ),
+                                       $this->getLabeledOptionField( 
'redirect-to-electron', 'single', true ),
                                        $this->getLabeledOptionField( 
'redirect-to-collection', 'two' ),
                                        $this->getHiddenField( 'page', 
$title->getText() ),
                                        $this->getHiddenField( 
'coll-download-url', $collectionDownloadUrl ),
@@ -164,67 +164,6 @@
                return $element;
        }
 
-       /**
-        * @param Title $title page to download as PDF
-        */
-       public function renderAndShowPdf( Title $title ) {
-               if ( !$this->getRequest()->checkUrlExtension() ) {
-                       $this->getOutput()->showErrorPage(
-                               'electronPdfService-page-notfound-title',
-                               'electronPdfService-page-notfound-text'
-                       );
-                       return;
-               }
-               $tempFile = TempFSFile::factory( 'electron_', 'pdf' );
-               $this->tempFileHandle = fopen( $tempFile->getPath(), 'w+' );
-               $this->totalBytesWritten = 0;
-
-               $request = MWHttpRequest::factory( $this->constructServiceUrl( 
$title ) );
-               $request->setCallback( [ $this, 'writeToTempFile' ] );
-
-               if ( $request->execute()->isOK() ) {
-                       $this->sendPdfToOutput( $title->getPrefixedText() );
-               } else {
-                       $this->getOutput()->showErrorPage(
-                               'electronPdfService-page-notfound-title',
-                               'electronPdfService-page-notfound-text'
-                       );
-               }
-
-               fclose( $this->tempFileHandle );
-               $tempFile->purge();
-               return;
-       }
-
-       /**
-        * Callback used by the MWHttpRequest to the Electron service writing 
the result into a file
-        * If writing fails or the $maxDocumentSize is reached it returns -1 to 
abort the HTTP fetch.
-        *
-        * @param resource $res
-        * @param string $content
-        * @return int
-        */
-       public function writeToTempFile( $res, $content ) {
-               $maxDocumentSize = $this->config->get( 
'ElectronPdfServiceMaxDocumentSize' );
-               $bytes = fwrite(
-                       $this->tempFileHandle,
-                       $content,
-                       $maxDocumentSize - $this->totalBytesWritten + 1
-               );
-
-               if ( $bytes === false ) {
-                       return -1;
-               }
-
-               $this->totalBytesWritten += $bytes;
-
-               if ( $this->totalBytesWritten > $maxDocumentSize ) {
-                       return -1;
-               }
-
-               return $bytes;
-       }
-
        public function setHeaders() {
                parent::setHeaders();
                $this->addModules();
@@ -244,39 +183,19 @@
         * @param Title $title
         * @return string
         */
-       private function constructServiceUrl( Title $title ) {
-               $electronPdfService = $this->config->get( 'ElectronPdfService' 
);
+       private function getServiceUrl( Title $title ) {
+               $restBaseUrl = $this->config->get( 
'ElectronPdfServiceRESTbaseURL' );
 
-               // for testing the functionality on localhost please set
-               // $wgElectronPdfService["pageUrl"] to a publicly accessible 
URL in your LocalSettings.php!
-               if ( !isset( $electronPdfService["pageUrl"] ) ) {
-                       $pageUrl = $title->getCanonicalURL();
-               } else {
-                       $pageUrl = $electronPdfService["pageUrl"];
-               }
-               $serviceUrl =
-                       $electronPdfService["serviceUrl"] . '/' .
-                       $electronPdfService["format"] .
-                       '?accessKey=' . $electronPdfService["key"] .
-                       '&url=' . urlencode( $pageUrl );
-
-               return $serviceUrl;
+               return $restBaseUrl . urlencode( $title->getPrefixedText() );
        }
 
        /**
-        * @param string $page
+        * @param Title $title
         */
-       private function sendPdfToOutput( $page ) {
-               $fileMetaData = stream_get_meta_data( $this->tempFileHandle );
-               $contentDisposition = FileBackend::makeContentDisposition( 
'inline', $page . '.pdf' );
-
-               $headers = [
-                       'Content-Type:application/pdf',
-                       'Content-Length: ' . filesize( $fileMetaData['uri'] ),
-                       'Content-Disposition: ' . $contentDisposition
-               ];
-
-               StreamFile::stream( $fileMetaData['uri'], $headers );
+       private function redirectToElectron( Title $title ) {
+               $this->getOutput()->redirect(
+                       $this->getServiceUrl( $title )
+               );
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3273ca18aaaf97d47f57769c35e8deb133be5073
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ElectronPdfService
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to