Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.61% covered (warning)
87.61%
99 / 113
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlushUtterances
92.52% covered (success)
92.52%
99 / 107
50.00% covered (danger)
50.00%
1 / 2
32.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
1
 execute
86.89% covered (warning)
86.89%
53 / 61
0.00% covered (danger)
0.00%
0 / 1
33.17
1<?php
2
3namespace MediaWiki\Wikispeech;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use Maintenance;
12use MediaWiki\Wikispeech\Utterance\FlushUtterancesFromStoreByExpirationJobQueue;
13use MediaWiki\Wikispeech\Utterance\FlushUtterancesFromStoreByLanguageAndVoiceJobQueue;
14use MediaWiki\Wikispeech\Utterance\FlushUtterancesFromStoreByPageIdJobQueue;
15use MediaWiki\Wikispeech\Utterance\UtteranceStore;
16
17/** @var string MediaWiki installation path */
18$IP = getenv( 'MW_INSTALL_PATH' );
19if ( $IP === false ) {
20    $IP = __DIR__ . '/../../..';
21}
22require_once "$IP/maintenance/Maintenance.php";
23
24/**
25 * Maintenance script to manually execute
26 * {@link UtteranceStore} flush methods.
27 *
28 * mwscript extensions/Wikispeech/maintenance/flushUtterances.php
29 *
30 * Be aware that you probably need to execute using mwscript, not php,
31 * in order to be executed as user www-data, who has access to deleting files.
32 *
33 * @since 0.1.7
34 */
35class FlushUtterances extends Maintenance {
36
37    /** @var UtteranceStore */
38    private $utteranceStore;
39
40    /** @var FlushUtterancesFromStoreByExpirationJobQueue */
41    private $flushUtterancesFromStoreByExpirationJobQueue;
42
43    /** @var FlushUtterancesFromStoreByLanguageAndVoiceJobQueue */
44    private $flushUtterancesFromStoreByLanguageAndVoiceJobQueue;
45
46    /** @var FlushUtterancesFromStoreByPageIdJobQueue */
47    private $flushUtterancesFromStoreByPageIdJobQueue;
48
49    public function __construct() {
50        parent::__construct();
51
52        $this->requireExtension( 'Wikispeech' );
53        $this->addDescription( 'Flush utterances that expired with age; ' .
54            'by language and optionally voice; or page id.' );
55        $this->addOption(
56            'expire',
57            'Flush all utterances that have expired according to configuration.',
58            false,
59            false,
60            'e'
61        );
62        $this->addOption(
63            'language',
64            'Flush all utterances with this language.',
65            false,
66            true,
67            'l'
68        );
69        $this->addOption(
70            'voice',
71            'Flush all utterances with this voice (language required).',
72            false,
73            true,
74            'v'
75        );
76        $this->addOption(
77            'page',
78            'Flush all utterances for all languages and voices on this page (id).',
79            false,
80            true,
81            'p'
82        );
83        $this->addOption(
84            'consumerUrl',
85            'Flush this page on given remote wiki (page required).',
86            false,
87            true,
88            'c'
89        );
90        $this->addOption(
91            'force',
92            'Forces flushing in current thread rather than queuing as job.',
93            false,
94            false,
95            'f'
96        );
97    }
98
99    /**
100     * @return bool success
101     */
102    public function execute() {
103        // Non PHP core classes aren't available prior to this point,
104        // i.e. we can't initialize the fields in the constructor,
105        // and we have to be lenient for mocked instances set by tests.
106        if ( !$this->utteranceStore ) {
107            $this->utteranceStore = new UtteranceStore();
108        }
109
110        $flushedCount = 0;
111
112        $expire = $this->hasOption( 'expire' );
113        $language = $this->getOption( 'language', null );
114        $voice = $this->getOption( 'voice', null );
115        $pageId = $this->getOption( 'page', null );
116        $consumerUrl = $this->getOption( 'consumerUrl', null );
117        $force = $this->hasOption( 'force' );
118
119        $supportedSetOfOptions = true;
120        if ( !$expire && !$language && !$voice && !$pageId ) {
121            $supportedSetOfOptions = false;
122        } elseif ( $expire && ( $language || $voice || $pageId ) ) {
123            $supportedSetOfOptions = false;
124        } elseif ( $language && ( $expire || $pageId ) ) {
125            $supportedSetOfOptions = false;
126        } elseif ( $voice && ( $expire || $pageId ) ) {
127            $supportedSetOfOptions = false;
128        } elseif ( $pageId && ( $expire || $language || $voice ) ) {
129            $supportedSetOfOptions = false;
130        }
131        if ( !$supportedSetOfOptions ) {
132            $this->output( "Unsupported set of options!\n" );
133            $this->showHelp();
134            return false;
135        }
136
137        if ( $expire ) {
138            if ( $force ) {
139                $flushedCount = $this->utteranceStore
140                    ->flushUtterancesByExpirationDate(
141                        $this->utteranceStore->getWikispeechUtteranceExpirationTimestamp()
142                    );
143            } else {
144                if ( !$this->flushUtterancesFromStoreByExpirationJobQueue ) {
145                    $this->flushUtterancesFromStoreByExpirationJobQueue
146                        = new FlushUtterancesFromStoreByExpirationJobQueue();
147                }
148
149                $this->flushUtterancesFromStoreByExpirationJobQueue
150                    ->queueJob();
151            }
152        } elseif ( $language ) {
153            if ( $force ) {
154                $flushedCount = $this->utteranceStore
155                    ->flushUtterancesByLanguageAndVoice( $language, $voice );
156            } else {
157                if ( !$this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue ) {
158                    $this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue
159                        = new FlushUtterancesFromStoreByLanguageAndVoiceJobQueue();
160                }
161
162                $this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue
163                    ->queueJob( $language, $voice );
164            }
165        } elseif ( $pageId ) {
166            if ( $force ) {
167                $flushedCount = $this->utteranceStore
168                    ->flushUtterancesByPage( $consumerUrl, intval( $pageId ) );
169            } else {
170                if ( !$this->flushUtterancesFromStoreByPageIdJobQueue ) {
171                    $this->flushUtterancesFromStoreByPageIdJobQueue
172                        = new FlushUtterancesFromStoreByPageIdJobQueue();
173                }
174
175                $this->flushUtterancesFromStoreByPageIdJobQueue
176                    ->queueJob( intval( $pageId ) );
177            }
178        } else {
179            // Fallback in case of future bad code in supported set of options.
180            $this->output( "Unsupported set of options! (This might be a developer error.)\n" );
181            $this->showHelp();
182            return false;
183        }
184
185        if ( $force ) {
186            $this->output( "Flushed $flushedCount utterances.\n" );
187        } else {
188            $this->output( 'Flush job has been queued and will be executed ' .
189                "in accordance with your MediaWiki configuration.\n" );
190        }
191
192        return true;
193    }
194
195}
196
197/** @var string This class, required to start via Maintenance. */
198$maintClass = FlushUtterances::class;
199
200require_once RUN_MAINTENANCE_IF_MAIN;