WMDE-Fisch has uploaded a new change for review.

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

Change subject: Added doc blocks and improved writeToTempFile callback
......................................................................

Added doc blocks and improved writeToTempFile callback

writeToTempFile now returns -1 on error to abort the HTTP fetch
correctly. It also considers already written bytes when writing
to the temp file.

Addressing comments in Idc14f4472adbc2350e91ebd8ce043a08f9bf1c1f

Change-Id: I8ad8fac88e0d31eed53793a1eeb855aa24630a45
---
M specials/SpecialElectronPdf.php
1 file changed, 45 insertions(+), 3 deletions(-)


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

diff --git a/specials/SpecialElectronPdf.php b/specials/SpecialElectronPdf.php
index 4a9e9f3..dc100fd 100644
--- a/specials/SpecialElectronPdf.php
+++ b/specials/SpecialElectronPdf.php
@@ -23,6 +23,9 @@
         */
        public $totalBytesWritten;
 
+       /**
+        * @var Config $config
+        */
        public $config;
 
        public function __construct() {
@@ -30,6 +33,9 @@
                $this->config = 
MediaWikiServices::getInstance()->getMainConfig();
        }
 
+       /**
+        * @param null|string $subPage
+        */
        public function execute( $subPage ) {
                $request = $this->getRequest();
                $parts = ( $subPage === '' ) ? [] : explode( '/', $subPage, 2 );
@@ -59,6 +65,10 @@
                }
        }
 
+       /**
+        * @param Title $title page to download as PDF
+        * @param string $collectionDownloadUrl URL to the specific download 
page of the Collection extension
+        */
        public function showRenderModeSelectionPage( Title $title, 
$collectionDownloadUrl ) {
                $this->setHeaders();
 
@@ -100,6 +110,12 @@
                $out->addHTML( $form );
        }
 
+       /**
+        * @param string $action
+        * @param string $name
+        * @param boolean $selected
+        * @return OOUI\Tag
+        */
        private function getLabeledOptionField( $action, $name, $selected = 
false ) {
                $image = ( new OOUI\Tag() )->addClasses( [
                        'mw-electronPdfService-selection-image',
@@ -126,6 +142,11 @@
                return $labelBox;
        }
 
+       /**
+        * @param string $name
+        * @param string $value
+        * @return OOUI\Tag
+        */
        private function getHiddenField( $name, $value ) {
                $element = new OOUI\Tag( 'input' );
                $element->setAttributes(
@@ -139,6 +160,9 @@
                return $element;
        }
 
+       /**
+        * @param Title $title page to download as PDF
+        */
        public function renderAndShowPdf( Title $title ) {
                if ( !$this->getRequest()->checkUrlExtension() ) {
                        $this->getOutput()->showErrorPage(
@@ -168,18 +192,26 @@
                return;
        }
 
+       /**
+        * Callback method used by the MWHttpRequest to the Electron service 
writing the result into a temp file.
+        * If writing the file 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 );
+               $bytes = fwrite( $this->tempFileHandle, $content, 
$maxDocumentSize - $this->totalBytesWritten + 1 );
 
                if ( $bytes === false ) {
-                       return false;
+                       return -1;
                }
 
                $this->totalBytesWritten += $bytes;
 
                if ( $this->totalBytesWritten > $maxDocumentSize ) {
-                       return false;
+                       return -1;
                }
 
                return $bytes;
@@ -200,6 +232,10 @@
                }
        }
 
+       /**
+        * @param Title $title
+        * @return string
+        */
        private function constructServiceUrl( Title $title ) {
                $electronPdfService = $this->config->get( 'ElectronPdfService' 
);
 
@@ -219,6 +255,9 @@
                return $serviceUrl;
        }
 
+       /**
+        * @param string $page
+        */
        private function sendPdfToOutput( $page ) {
                $fileMetaData = stream_get_meta_data( $this->tempFileHandle );
                $contentDisposition = FileBackend::makeContentDisposition( 
'inline', $page . '.pdf' );
@@ -232,6 +271,9 @@
                StreamFile::stream( $fileMetaData['uri'], $headers );
        }
 
+       /**
+        * @param string $collectionDownloadUrl
+        */
        private function redirectToCollection( $collectionDownloadUrl ) {
                $queryString = parse_url(
                        urldecode( $collectionDownloadUrl ),

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8ad8fac88e0d31eed53793a1eeb855aa24630a45
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ElectronPdfService
Gerrit-Branch: master
Gerrit-Owner: WMDE-Fisch <christoph.jau...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to