Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PhonosIPAFilePersistJob
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\Phonos\Job;
22
23use GenericParameterJob;
24use Job;
25use MediaWiki\Extension\Phonos\Engine\AudioParams;
26use MediaWiki\Extension\Phonos\Exception\PhonosException;
27use MediaWiki\Logger\LoggerFactory;
28use MediaWiki\MediaWikiServices;
29use Psr\Log\LoggerInterface;
30
31/**
32 * Generate audio file using text to speech engine
33 *
34 * This job will reach out to an external service and generate
35 * pronunciation audio files based on IPA notation
36 *
37 * Some external services like Google have a rate limit and so this job
38 * should be throttled accordingly using $wgJobBackoffThrottling
39 */
40class PhonosIPAFilePersistJob extends Job implements GenericParameterJob {
41    /** @var LoggerInterface */
42    protected $logger;
43
44    /**
45     * @inheritDoc
46     */
47    public function __construct( array $params ) {
48        parent::__construct( 'phonosIPAFilePersist', $params );
49        $this->removeDuplicates = true;
50        $this->logger = LoggerFactory::getInstance( 'Phonos' );
51        $this->logger->info(
52            __METHOD__ . ' Job created',
53            [
54                'params' => $params
55            ]
56        );
57    }
58
59    /**
60     * @inheritDoc
61     */
62    public function run() {
63        $this->logger->info(
64            __METHOD__ . ' Job being run',
65            [
66                'params' => $this->params
67            ]
68        );
69        $engine = MediaWikiServices::getInstance()->get( 'Phonos.Engine' );
70        $params = new AudioParams(
71            $this->params['ipa'],
72            $this->params['text'],
73            $this->params['lang']
74        );
75
76        try {
77            $engine->getAudioData( $params );
78            $engine->clearError( $params );
79
80        } catch ( PhonosException $e ) {
81            $engine->setError( $params, $e->getMessageKeyAndArgs() );
82
83            $this->logger->error(
84                __METHOD__ . ' Job failed',
85                [
86                    'params' => $this->params,
87                    'exception' => $e
88                ]
89            );
90            $statsdDataFactory = MediaWikiServices::getInstance()->get( 'StatsdDataFactory' );
91            $key = $e->getStatsdKey();
92            $statsdDataFactory->increment( "extension.Phonos.error.$key" );
93
94            throw $e;
95        }
96
97        return true;
98    }
99}