Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.67% |
25 / 60 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PreSynthesizeMessages | |
46.30% |
25 / 54 |
|
66.67% |
2 / 3 |
25.49 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| synthesizeErrorMessage | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | namespace MediaWiki\Wikispeech; |
| 3 | |
| 4 | /** |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @license GPL-2.0-or-later |
| 8 | */ |
| 9 | use Exception; |
| 10 | use InvalidArgumentException; |
| 11 | use Maintenance; |
| 12 | use MediaWiki\Wikispeech\Segment\SegmentMessagesFactory; |
| 13 | use MediaWiki\Wikispeech\Utterance\UtteranceGenerator; |
| 14 | |
| 15 | /** @var string MediaWiki installation path */ |
| 16 | $IP = getenv( 'MW_INSTALL_PATH' ); |
| 17 | if ( $IP === false ) { |
| 18 | $IP = __DIR__ . '/../../..'; |
| 19 | } |
| 20 | require_once "$IP/maintenance/Maintenance.php"; |
| 21 | /** |
| 22 | * Maintenance script to pre synthesize error messages |
| 23 | * |
| 24 | * @since 0.1.13 |
| 25 | */ |
| 26 | class PreSynthesizeMessages extends Maintenance { |
| 27 | /** @var UtteranceGenerator */ |
| 28 | private $utteranceGenerator; |
| 29 | |
| 30 | /** @var SegmentMessagesFactory */ |
| 31 | private $segmentMessagesFactory; |
| 32 | |
| 33 | public function __construct() { |
| 34 | parent::__construct(); |
| 35 | $this->requireExtension( 'Wikispeech' ); |
| 36 | $this->addDescription( 'Pre synthesize error messages' ); |
| 37 | $this->addOption( |
| 38 | 'language', |
| 39 | 'Pre synthesize error messages with this language.', |
| 40 | true, |
| 41 | true, |
| 42 | 'l' |
| 43 | ); |
| 44 | $this->addOption( |
| 45 | 'voice', |
| 46 | 'Pre synthesize error messages with this voice (language required).', |
| 47 | false, |
| 48 | true, |
| 49 | 'v' |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return bool success |
| 55 | */ |
| 56 | public function execute() { |
| 57 | $language = $this->getOption( 'language', null ); |
| 58 | $voice = $this->getOption( 'voice', null ); |
| 59 | $this->utteranceGenerator = WikispeechServices::getUtteranceGenerator(); |
| 60 | $this->segmentMessagesFactory = WikispeechServices::getSegmentMessagesFactory(); |
| 61 | |
| 62 | // @todo These messages are arbitrary to show that it works. |
| 63 | // In the future we probably want to generate a list of messages |
| 64 | $errorMessageKeys = [ 'wikispeech-error-loading-audio-title', 'wikispeech-error-generate-preview-title' ]; |
| 65 | foreach ( $errorMessageKeys as $messageKey ) { |
| 66 | $this->synthesizeErrorMessage( $messageKey, $language, $voice ); |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Synthesize the error messages |
| 73 | * |
| 74 | * @param string $messageKey |
| 75 | * @param string $language |
| 76 | * @param string $voice |
| 77 | */ |
| 78 | public function synthesizeErrorMessage( $messageKey, $language, $voice ) { |
| 79 | try { |
| 80 | |
| 81 | $message = wfMessage( $messageKey ); |
| 82 | if ( !$message->exists() ) { |
| 83 | throw new InvalidArgumentException( "Message key does not exist: $messageKey" ); |
| 84 | } |
| 85 | |
| 86 | $segmentResponse = $this->segmentMessagesFactory->segmentMessage( $messageKey, $language ); |
| 87 | $segmentList = $segmentResponse->getSegments(); |
| 88 | if ( !$voice ) { |
| 89 | $voiceHandler = WikispeechServices::getVoiceHandler(); |
| 90 | $voice = $voiceHandler->getDefaultVoice( $language ); |
| 91 | if ( !$voice ) { |
| 92 | throw new InvalidArgumentException( |
| 93 | "No default voice found for language: $language" |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | foreach ( $segmentList->getSegments() as $segment ) { |
| 98 | $segmentHash = $segment->getHash(); |
| 99 | |
| 100 | if ( $segmentHash === null ) { |
| 101 | throw new InvalidArgumentException( |
| 102 | "Segment hash is null for message key: $messageKey" |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | $this->utteranceGenerator->getUtterance( |
| 107 | null, |
| 108 | $voice, |
| 109 | $language, |
| 110 | 0, |
| 111 | $segment, |
| 112 | $messageKey |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | $this->output( "Successfully pre-synthesized message with message key: $messageKey\n" ); |
| 117 | |
| 118 | } catch ( Exception $e ) { |
| 119 | $this->output( "Error synthesizing message with message key: $messageKey " . $e->getMessage() . "\n" ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | /** @var string This class, required to start via Maintenance. */ |
| 124 | $maintClass = PreSynthesizeMessages::class; |
| 125 | require_once RUN_MAINTENANCE_IF_MAIN; |