Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.06% covered (warning)
89.06%
114 / 128
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlushUtterances
93.44% covered (success)
93.44%
114 / 122
50.00% covered (danger)
50.00%
1 / 2
40.45
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
53 / 53
100.00% covered (success)
100.00%
1 / 1
1
 execute
88.41% covered (warning)
88.41%
61 / 69
0.00% covered (danger)
0.00%
0 / 1
41.37
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            'messages',
92            'Flush all message utterances.',
93            false,
94            false,
95            'm'
96        );
97        $this->addOption(
98            'force',
99            'Forces flushing in current thread rather than queuing as job.',
100            false,
101            false,
102            'f'
103        );
104    }
105
106    /**
107     * @return bool success
108     */
109    public function execute() {
110        // Non PHP core classes aren't available prior to this point,
111        // i.e. we can't initialize the fields in the constructor,
112        // and we have to be lenient for mocked instances set by tests.
113        if ( !$this->utteranceStore ) {
114            $this->utteranceStore = WikispeechServices::getUtteranceStore();
115        }
116
117        $flushedCount = 0;
118
119        $expire = $this->hasOption( 'expire' );
120        $language = $this->getOption( 'language', null );
121        $voice = $this->getOption( 'voice', null );
122        $pageId = $this->getOption( 'page', null );
123        $consumerUrl = $this->getOption( 'consumerUrl', null );
124        $messages = $this->getOption( 'messages', null );
125        $force = $this->hasOption( 'force' );
126
127        $supportedSetOfOptions = true;
128        if ( !$expire && !$language && !$voice && !$pageId && !$messages ) {
129            $supportedSetOfOptions = false;
130        } elseif ( $expire && ( $language || $voice || $pageId ) ) {
131            $supportedSetOfOptions = false;
132        } elseif ( $language && ( $expire || $pageId ) ) {
133            $supportedSetOfOptions = false;
134        } elseif ( $voice && ( $expire || $pageId ) ) {
135            $supportedSetOfOptions = false;
136        } elseif ( $pageId && ( $expire || $language || $voice ) ) {
137            $supportedSetOfOptions = false;
138        } elseif ( $messages && ( $expire || $language || $voice || $pageId ) ) {
139            $supportedSetOfOptions = false;
140        }
141
142        if ( !$supportedSetOfOptions ) {
143            $this->output( "Unsupported set of options!\n" );
144            $this->showHelp();
145            return false;
146        }
147
148        if ( $expire ) {
149            if ( $force ) {
150                $flushedCount = $this->utteranceStore
151                    ->flushUtterancesByExpirationDate(
152                        $this->utteranceStore->getWikispeechUtteranceExpirationTimestamp()
153                    );
154            } else {
155                if ( !$this->flushUtterancesFromStoreByExpirationJobQueue ) {
156                    $this->flushUtterancesFromStoreByExpirationJobQueue
157                        = new FlushUtterancesFromStoreByExpirationJobQueue();
158                }
159
160                $this->flushUtterancesFromStoreByExpirationJobQueue
161                    ->queueJob();
162            }
163        } elseif ( $language ) {
164            if ( $force ) {
165                $flushedCount = $this->utteranceStore
166                    ->flushUtterancesByLanguageAndVoice( $language, $voice );
167            } else {
168                if ( !$this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue ) {
169                    $this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue
170                        = new FlushUtterancesFromStoreByLanguageAndVoiceJobQueue();
171                }
172
173                $this->flushUtterancesFromStoreByLanguageAndVoiceJobQueue
174                    ->queueJob( $language, $voice );
175            }
176        } elseif ( $pageId ) {
177            if ( $force ) {
178                $flushedCount = $this->utteranceStore
179                    ->flushUtterancesByPage( $consumerUrl, intval( $pageId ) );
180            } else {
181                if ( !$this->flushUtterancesFromStoreByPageIdJobQueue ) {
182                    $this->flushUtterancesFromStoreByPageIdJobQueue
183                        = new FlushUtterancesFromStoreByPageIdJobQueue();
184                }
185
186                $this->flushUtterancesFromStoreByPageIdJobQueue
187                    ->queueJob( intval( $pageId ) );
188            }
189        } elseif ( $messages ) {
190            if ( $force ) {
191                $flushedCount = $this->utteranceStore->flushMessageUtterances();
192            } else {
193                $this->output( 'Non-forced messages flush is not implemented yet.' . "\n" );
194                return false;
195            }
196        } else {
197            // Fallback in case of future bad code in supported set of options.
198            $this->output( "Unsupported set of options! (This might be a developer error.)\n" );
199            $this->showHelp();
200            return false;
201        }
202
203        if ( $force ) {
204            $this->output( "Flushed $flushedCount utterances.\n" );
205        } else {
206            $this->output( 'Flush job has been queued and will be executed ' .
207                "in accordance with your MediaWiki configuration.\n" );
208        }
209
210        return true;
211    }
212
213}
214
215/** @var string This class, required to start via Maintenance. */
216$maintClass = FlushUtterances::class;
217
218require_once RUN_MAINTENANCE_IF_MAIN;