Skip to content

Commit

Permalink
Create option to disable credits in export form
Browse files Browse the repository at this point in the history
Add option that will exclude/include credits in exported books.

Bug: T274959
  • Loading branch information
cimurah committed Mar 12, 2021
1 parent 331cf87 commit 89338a8
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 27 deletions.
2 changes: 2 additions & 0 deletions i18n/en.json
Expand Up @@ -37,6 +37,8 @@
"no-font-option": "None (use device default)",
"font-field-help": "Choose from $1 available fonts.",
"options-label": "Options:",
"credits-field-label": "Exclude editor credits (faster download)",
"credits-default-message": "This e-book comes from the free online library Wikisource, under the terms of the Creative Commons.",
"images-field-label": "Do not include images",
"nocache-field-label": "Bypass all caching (slower but useful for debugging)",
"export-button": "Export",
Expand Down
2 changes: 2 additions & 0 deletions i18n/qqq.json
Expand Up @@ -39,6 +39,8 @@
"no-font-option": "Font dropdown option to indicate that no font should be used.",
"font-field-help": "Help text for the 'Font' field.\n\nParameter:\n* $1 is the total number of fonts (an integer).",
"options-label": "Form label for the set of 'Options' fields.",
"credits-field-label": "Form field label for the 'do/don't include credits' option field.",
"credits-default-message": "Text shown in place of credits when they are not included in a book.",
"images-field-label": "Form field label for the 'do/don't include images' option field.",
"nocache-field-label": "Form field label for the 'disable caching' option field.",
"export-button": "Button text for the main export form.",
Expand Down
7 changes: 6 additions & 1 deletion src/Controller/ExportController.php
Expand Up @@ -104,6 +104,7 @@ public function home(
}

$font = $this->getFont( $request, $api->getLang(), $fontProvider );
$credits = (bool)$request->get( 'credits', true );
$images = (bool)$request->get( 'images', true );
return $this->render( 'export.html.twig', [
'fonts' => $fontProvider->getAll(),
Expand All @@ -113,6 +114,7 @@ public function home(
'title' => $this->getTitle( $request ),
'langs' => $this->getLangs( $request ),
'lang' => $this->getLang( $request ),
'credits' => $credits,
'images' => $images,
'nocache' => $nocache,
'enableCache' => $this->enableCache,
Expand All @@ -130,6 +132,8 @@ private function export(
$page = $request->get( 'page' );
$format = $this->getFormat( $request );
$font = $this->getFont( $request, $api->getLang(), $fontProvider );
// The `credits` checkbox submits as 'false' to disable, so needs extra filtering.
$credits = filter_var( $request->get( 'credits', true ), FILTER_VALIDATE_BOOL );
// The `images` checkbox submits as 'false' to disable, so needs extra filtering.
$images = filter_var( $request->get( 'images', true ), FILTER_VALIDATE_BOOL );

Expand All @@ -139,7 +143,7 @@ private function export(
}

// Generate ebook.
$options = [ 'images' => $images, 'fonts' => $font ];
$options = [ 'images' => $images, 'fonts' => $font, 'credits' => $credits ];
$creator = BookCreator::forApi( $api, $format, $options, $generatorSelector, $creditRepo );
$creator->create( $page );

Expand Down Expand Up @@ -262,6 +266,7 @@ public function error( Request $request, Throwable $exception, Api $api, FontPro
'title' => $this->getTitle( $request ),
'langs' => $this->getLangs( $request ),
'lang' => $this->getLang( $request ),
'credits' => true,
'images' => true,
'messages' => [
'danger' => [ $message ],
Expand Down
9 changes: 7 additions & 2 deletions src/Generator/ConvertGenerator.php
Expand Up @@ -7,6 +7,7 @@
use App\Util\Api;
use App\Util\Util;
use InvalidArgumentException;
use Krinkle\Intuition\Intuition;
use Symfony\Component\Process\Process;

/**
Expand Down Expand Up @@ -86,12 +87,16 @@ public static function getSupportedTypes() {
/** @var Api */
private $api;

/** @var Intuition */
private $intuition;

/** @var int Command timeout in seconds. */
private $timeout;

public function __construct( FontProvider $fontProvider, Api $api, int $timeout ) {
public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition, int $timeout ) {
$this->fontProvider = $fontProvider;
$this->api = $api;
$this->intuition = $intuition;
$this->timeout = $timeout;
}

Expand Down Expand Up @@ -144,7 +149,7 @@ public function create( Book $book ) {
}

private function createEpub( Book $book ) {
$epubGenerator = new EpubGenerator( $this->fontProvider, $this->api );
$epubGenerator = new EpubGenerator( $this->fontProvider, $this->api, $this->intuition );
return $epubGenerator->create( $book );
}

Expand Down
31 changes: 21 additions & 10 deletions src/Generator/EpubGenerator.php
Expand Up @@ -9,6 +9,7 @@
use App\Util\Util;
use Exception;
use IntlDateFormatter;
use Krinkle\Intuition\Intuition;
use ZipArchive;

/**
Expand All @@ -33,9 +34,13 @@ class EpubGenerator implements FormatGenerator {
/** @var Api */
protected $api;

public function __construct( FontProvider $fontProvider, Api $api ) {
/** @var Intuition */
private $intuition;

public function __construct( FontProvider $fontProvider, Api $api, Intuition $intuition ) {
$this->fontProvider = $fontProvider;
$this->api = $api;
$this->intuition = $intuition;
}

/**
Expand Down Expand Up @@ -398,17 +403,23 @@ private function getXhtmlTitle( Book $book ) {
}

private function getXhtmlAbout( Book $book, $wsUrl ) {
$list = '<ul>';
$listBot = '<ul>';
foreach ( $book->credits as $name => $value ) {
if ( $value[ 'bot' ] !== null ) {
$listBot .= '<li>' . htmlspecialchars( $name, ENT_QUOTES ) . "</li>\n";
} else {
$list .= '<li>' . htmlspecialchars( $name, ENT_QUOTES ) . "</li>\n";
if ( empty( $book->credits ) ) {
$list = $this->intuition->msg( 'credits-default-message' ) ??
'credits-default-message missing';
$listBot = '';
} else {
$list = '<ul>';
$listBot = '<ul>';
foreach ( $book->credits as $name => $value ) {
if ( $value[ 'bot' ] !== null ) {
$listBot .= '<li>' . htmlspecialchars( $name, ENT_QUOTES ) . "</li>\n";
} else {
$list .= '<li>' . htmlspecialchars( $name, ENT_QUOTES ) . "</li>\n";
}
}
$list .= '</ul>';
$listBot .= '</ul>';
}
$list .= '</ul>';
$listBot .= '</ul>';

$about = $this->api->getAboutPage();
if ( $about == '' ) {
Expand Down
9 changes: 7 additions & 2 deletions src/GeneratorSelector.php
Expand Up @@ -8,6 +8,7 @@
use App\Generator\FormatGenerator;
use App\Util\Api;
use Exception;
use Krinkle\Intuition\Intuition;

class GeneratorSelector {

Expand Down Expand Up @@ -43,10 +44,14 @@ class GeneratorSelector {
/** @var ConvertGenerator */
private $convertGenerator;

public function __construct( FontProvider $fontProvider, Api $api, ConvertGenerator $convertGenerator ) {
/** @var Intuition */
private $intuition;

public function __construct( FontProvider $fontProvider, Api $api, ConvertGenerator $convertGenerator, Intuition $intuition ) {
$this->fontProvider = $fontProvider;
$this->api = $api;
$this->convertGenerator = $convertGenerator;
$this->intuition = $intuition;
}

/**
Expand All @@ -69,7 +74,7 @@ public function getGenerator( $format ): FormatGenerator {
$format = self::$aliases[$format];
}
if ( $format === 'epub-3' ) {
return new EpubGenerator( $this->fontProvider, $this->api );
return new EpubGenerator( $this->fontProvider, $this->api, $this->intuition );
} elseif ( in_array( $format, ConvertGenerator::getSupportedTypes() ) ) {
$this->convertGenerator->setFormat( $format );
return $this->convertGenerator;
Expand Down
26 changes: 18 additions & 8 deletions templates/export.html.twig
Expand Up @@ -58,15 +58,25 @@
<label class="col-lg-2 control-label">{{ msg( 'options-label' ) }}</label>

<div class="col-lg-10">
<label class="checkbox-inline">
<input type="checkbox" value="false" {% if not images %}checked="checked"{% endif %} name="images" />
{{ msg( 'images-field-label' ) }}
</label>
{% if enableCache %}
<label class="checkbox-inline">
<input type="checkbox" value="1" {% if nocache %}checked="checked"{% endif %} name="nocache" />
{{ msg( 'nocache-field-label' ) }}
<div class="checkbox">
<label>
<input type="checkbox" value="false" {% if not credits %}checked="checked"{% endif %} name="credits" />
{{ msg( 'credits-field-label' ) }}
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="false" {% if not images %}checked="checked"{% endif %} name="images" />
{{ msg( 'images-field-label' ) }}
</label>
</div>
{% if enableCache %}
<div class="checkbox">
<label class="checkbox">
<input type="checkbox" value="1" {% if nocache %}checked="checked"{% endif %} name="nocache" />
{{ msg( 'nocache-field-label' ) }}
</label>
</div>
{% endif %}
</div>
</div>
Expand Down
9 changes: 7 additions & 2 deletions tests/Book/GeneratorSelectorTest.php
Expand Up @@ -8,6 +8,7 @@
use App\GeneratorSelector;
use App\Util\Api;
use Exception;
use Krinkle\Intuition\Intuition;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

Expand All @@ -25,13 +26,17 @@ class GeneratorSelectorTest extends KernelTestCase {
/** @var GeneratorSelector */
private $generatorSelector;

/** @var Intuition */
private $intuition;

public function setUp(): void {
parent::setUp();
$this->fontProvider = new FontProvider( new ArrayAdapter() );
self::bootKernel();
$this->api = self::$container->get( Api::class );
$convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, 10 );
$this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator );
$this->intuition = self::$container->get( Intuition::class );
$convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10 );
$this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition );
}

public function testGetUnknownGeneratorRaisesException() {
Expand Down
9 changes: 7 additions & 2 deletions tests/BookCreator/BookCreatorIntegrationTest.php
Expand Up @@ -9,6 +9,7 @@
use App\GeneratorSelector;
use App\Repository\CreditRepository;
use App\Util\Api;
use Krinkle\Intuition\Intuition;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

Expand All @@ -33,12 +34,16 @@ class BookCreatorIntegrationTest extends KernelTestCase {
/** @var EpubCheck */
private $epubCheck;

/** @var Intuition */
private $intuition;

public function setUp(): void {
self::bootKernel();
$this->fontProvider = new FontProvider( new ArrayAdapter() );
$this->api = self::$container->get( Api::class );
$convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, 10 );
$this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator );
$this->intuition = self::$container->get( Intuition::class );
$convertGenerator = new ConvertGenerator( $this->fontProvider, $this->api, $this->intuition, 10 );
$this->generatorSelector = new GeneratorSelector( $this->fontProvider, $this->api, $convertGenerator, $this->intuition );
$this->creditRepository = self::$container->get( CreditRepository::class );
$this->epubCheck = self::$container->get( EpubCheck::class );
}
Expand Down

0 comments on commit 89338a8

Please sign in to comment.