jenkins-bot has submitted this change and it was merged. Change subject: Wiring up form and service ......................................................................
Wiring up form and service Bug: T146895 Change-Id: I0942e5af84dedaf4532c7bd6ffe988c027c3d78e --- M ElectronPdfService.hooks.php M i18n/en.json M specials/SpecialElectronPdf.php 3 files changed, 123 insertions(+), 26 deletions(-) Approvals: WMDE-Fisch: Looks good to me, approved jenkins-bot: Verified diff --git a/ElectronPdfService.hooks.php b/ElectronPdfService.hooks.php index dc1034f..224f285 100644 --- a/ElectronPdfService.hooks.php +++ b/ElectronPdfService.hooks.php @@ -7,28 +7,90 @@ * @license GPL-2.0+ */ +use MediaWiki\MediaWikiServices; + class ElectronPdfServiceHooks { + /* + * If present, make the "Download as PDF" link in the sidebar point to the selection screen, + * add a new link otherwise + * + * @param Skin $skin + * @param array &$bar + * + * @return bool + */ public static function onSidebarBeforeOutput( Skin $skin, &$bar ) { + $config = MediaWikiServices::getInstance()->getMainConfig(); $title = $skin->getTitle(); if ( is_null( $title ) || !$title->exists() ) { return false; } - $specialPageTitle = SpecialPage::getTitleFor( 'ElectronPdf' ); - - // if the Collection extension is installed, modify their portlet - if ( array_key_exists( 'coll-print_export', $bar ) ) { - // TODO: don't add a new element to the sidebar, but reuse the "Download as PDF" link instead - $bar['coll-print_export'][] = [ + if ( $config->has( 'CollectionFormats' ) ) { + $index = self::getIndexOfDownloadPdfSidebarItem( + $bar['coll-print_export'], + $config->get( 'CollectionFormats' ) + ); + // if Collection extension provides a download-as-pdf link, make it point to the selection screen + if ( $index !== false ) { + $bar['coll-print_export'][$index]['href'] = self::generateSelectionScreenLink( + $title, + $bar['coll-print_export'][$index]['href'] + ); + // if no download-as-pdf link is there, add one and point to the selection screen + } else { + $bar['coll-print_export'][] = [ + 'text' => $skin->msg( 'electronPdfService-sidebar-portlet-print-text' )->escaped(), + 'id' => 'electron-print_pdf', + 'href' => self::generatePdfDownloadLink( $title ) + ]; + } + } else { + // in case Collection is not installed, let's add our own portlet with a direct link to the PDF + $bar['electronPdfService-sidebar-portlet-heading'][] = [ 'text' => $skin->msg( 'electronPdfService-sidebar-portlet-print-text' )->escaped(), 'id' => 'electron-print_pdf', - 'href' => $specialPageTitle->getLocalURL( - [ 'page' => $title->getPrefixedText() ] - ) + 'href' => self::generatePdfDownloadLink( $title ) ]; } return true; } + + private static function getIndexOfDownloadPdfSidebarItem( $portlet, $collectionFormats ) { + $usedPdfLib = array_search( 'PDF', $collectionFormats ); + if ( $usedPdfLib !== false ) { + foreach ( $portlet as $index => $element ) { + if ( $element['id'] === 'coll-download-as-' . $usedPdfLib ) { + return $index; + } + } + } + + return false; + } + + private static function generatePdfDownloadLink( Title $title ) { + $specialPageTitle = SpecialPage::getTitleFor( 'ElectronPdf' ); + + return $specialPageTitle->getLocalURL( + [ + 'page' => $title->getPrefixedText(), + 'action' => 'download-electron-pdf' + ] + ); + } + + private static function generateSelectionScreenLink( Title $title, $collectionUrl ) { + $specialPageTitle = SpecialPage::getTitleFor( 'ElectronPdf' ); + + return $specialPageTitle->getLocalURL( + [ + 'page' => $title->getPrefixedText(), + 'action' => 'show-selection-screen', + 'coll-download-url' => urlencode( $collectionUrl ) + ] + ); + } } diff --git a/i18n/en.json b/i18n/en.json index c878645..7f6348c 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -13,8 +13,8 @@ "electronPdfService-single-column-desc": "Includes tables and templates", "electronPdfService-two-column-desc": "Without tables and templates", "electronPdfService-download-button": "Download", - "electronPdfService-sidebar-portlet-heading": "Print", - "electronPdfService-sidebar-portlet-print-text": "Print PDF", + "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", diff --git a/specials/SpecialElectronPdf.php b/specials/SpecialElectronPdf.php index a0073a4..4eda6b4 100644 --- a/specials/SpecialElectronPdf.php +++ b/specials/SpecialElectronPdf.php @@ -27,6 +27,9 @@ $request = $this->getRequest(); $parts = ( $subPage === '' ) ? [] : explode( '/', $subPage, 2 ); $page = trim( $request->getVal( 'page', isset( $parts[0] ) ? $parts[0] : '' ) ); + $collectionDownloadUrl = trim( + $request->getVal( 'coll-download-url', isset( $parts[0] ) ? $parts[0] : '' ) + ); $title = Title::newFromText( $page ); if ( $title === null ) { @@ -37,13 +40,18 @@ return; } - // TODO: build a propper switch here and consider input if a method was selected - - $this->showRenderModeSelectionPage( $title ); - // $this->renderAndShowPdf( $title, $page ); + switch ( $request->getVal( 'action', '' ) ) { + case 'download-electron-pdf': + $this->renderAndShowPdf( $title ); + return; + case 'redirect-to-collection': + $this->redirectToCollection( $collectionDownloadUrl ); + default: + $this->showRenderModeSelectionPage( $title, $collectionDownloadUrl ); + } } - public function showRenderModeSelectionPage( Title $title ) { + public function showRenderModeSelectionPage( Title $title, $collectionDownloadUrl ) { $this->setHeaders(); $out = $this->getOutput(); @@ -53,7 +61,7 @@ $form = new OOUI\FormLayout( [ 'method' => 'POST', - 'action' => '', + 'action' => $this->getPageTitle()->getLocalURL(), ] ); $form->addClasses( [ 'mw-electronPdfService-selection-form' ] ); @@ -65,14 +73,14 @@ ( new OOUI\Tag() ) ->addClasses( [ 'mw-electronPdfService-selection-body' ] ) ->appendContent( - $this->getLabeledOptionField( 'single', true ), - $this->getLabeledOptionField( 'two' ), + $this->getLabeledOptionField( 'download-electron-pdf', 'single', true ), + $this->getLabeledOptionField( 'redirect-to-collection', 'two' ), + $this->getHiddenField( 'page', $title->getText() ), + $this->getHiddenField( 'coll-download-url', $collectionDownloadUrl ), new OOUI\ButtonGroupWidget( [ 'items' => [ new OOUI\ButtonInputWidget( [ 'type' => 'submit', - 'name' => 'continue', - 'value' => 'go', 'flags' => [ 'primary', 'progressive' ], 'label' => $this->msg( 'electronPdfService-download-button' )->text(), ] ), @@ -84,7 +92,7 @@ $out->addHTML( $form ); } - private function getLabeledOptionField( $name, $selected = false ) { + private function getLabeledOptionField( $action, $name, $selected = false ) { $image = ( new OOUI\Tag() )->addClasses( [ 'mw-electronPdfService-selection-image', 'mw-electronPdfService-selection-' . $name . '-column-image' @@ -93,8 +101,8 @@ $field = ( new OOUI\Tag() )->addClasses( [ 'mw-electronPdfService-selection-field' ] ); $field->appendContent( new OOUI\RadioInputWidget( [ - 'name' => 'column-type', - 'value' => $name, + 'name' => 'action', + 'value' => $action, 'selected' => $selected ] ), ( new OOUI\Tag( 'b' ) )->addClasses( [ 'mw-electronPdfService-selection-label-text' ] ) @@ -110,14 +118,27 @@ return $labelBox; } - public function renderAndShowPdf( Title $title, $page ) { + private function getHiddenField( $name, $value ) { + $element = new OOUI\Tag( 'input' ); + $element->setAttributes( + [ + 'type' => 'hidden', + 'name' => $name, + 'value' => $value + ] + ); + + return $element; + } + + public function renderAndShowPdf( Title $title ) { $this->tempFile = tmpfile(); $request = MWHttpRequest::factory( $this->constructServiceUrl( $title ) ); $request->setCallback( [ $this, 'writeToTempFile' ] ); if ( $request->execute()->isOK() ) { - $this->sendPdfToOutput( $page ); + $this->sendPdfToOutput( $title->getText() ); } else { $this->getOutput()->showErrorPage( 'electronPdfService-page-notfound-title', @@ -176,4 +197,18 @@ fpassthru( $this->tempFile ); $this->getOutput()->disable(); } + + private function redirectToCollection( $collectionDownloadUrl ) { + $queryString = parse_url( + urldecode( $collectionDownloadUrl ), + PHP_URL_QUERY + ); + parse_str( $queryString, $params ); + unset( $params['title'] ); + + $this->getOutput()->redirect( wfAppendQuery( + SkinTemplate::makeSpecialUrl( 'Book' ), + http_build_query( $params ) + ) ); + } } -- To view, visit https://gerrit.wikimedia.org/r/316783 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I0942e5af84dedaf4532c7bd6ffe988c027c3d78e Gerrit-PatchSet: 11 Gerrit-Project: mediawiki/extensions/ElectronPdfService Gerrit-Branch: master Gerrit-Owner: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: WMDE-Fisch <christoph.jau...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits