Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
BatchImport | |
0.00% |
0 / 32 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 |
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 | * @ingroup Maintenance |
19 | */ |
20 | |
21 | use MediaWiki\User\User; |
22 | |
23 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
24 | |
25 | class BatchImport extends Maintenance { |
26 | |
27 | /** @var string */ |
28 | private $dir; |
29 | /** @var bool */ |
30 | private $overwrite; |
31 | |
32 | public function __construct() { |
33 | parent::__construct(); |
34 | $this->addDescription( "Batch imports submissions from a folder. \n Processes CSV files " . |
35 | "that follow the naming convention: \n \$userName-\$runName.csv" ); |
36 | $this->addArg( 'dir', 'The directory to be read', true ); |
37 | $this->addOption( |
38 | 'overwrite', 'Overwrite existing runs with the same name.', false, false, "o" |
39 | ); |
40 | $this->requireExtension( 'MathSearch' ); |
41 | } |
42 | |
43 | public function execute() { |
44 | $this->dir = $this->getArg( 0 ); |
45 | $this->overwrite = $this->getOption( 'overwrite' ); |
46 | if ( $this->overwrite ) { |
47 | $this->output( "Loaded with option overwrite enabled .\n" ); |
48 | } |
49 | if ( !is_dir( $this->dir ) ) { |
50 | $this->output( "{$this->dir} is not a directory.\n" ); |
51 | exit( 1 ); |
52 | } |
53 | $files = new GlobIterator( $this->dir . '/*-*.csv' ); |
54 | foreach ( $files as $file ) { |
55 | $fn = $file->getFilename(); |
56 | if ( preg_match( '/(?P<user>.*?)-(?P<runName>.*?)\\.csv/', $fn, $matches ) ) { |
57 | $user = User::newFromName( $matches['user'] ); |
58 | if ( $user->getId() > 0 ) { |
59 | $this->output( "Importing filename $fn for userId {$user->getId()}.\n" ); |
60 | $importer = new ImportCsv( $user ); |
61 | $result = |
62 | $importer->execute( fopen( $file, 'r' ), $matches['runName'], |
63 | $this->overwrite ); |
64 | foreach ( $importer->getWarnings() as $warning ) { |
65 | $this->output( "warning: $warning \n" ); |
66 | } |
67 | if ( $result !== true ) { |
68 | $this->output( "$result\n" ); |
69 | } else { |
70 | $this->output( "File $fn imported as {$importer->getRunId()} \n" ); |
71 | } |
72 | } else { |
73 | $this->output( "User {$matches['user']} is invalid. Skipping file $fn.\n" ); |
74 | } |
75 | } |
76 | } |
77 | } |
78 | } |
79 | |
80 | $maintClass = BatchImport::class; |
81 | /** @noinspection PhpIncludeInspection */ |
82 | require_once RUN_MAINTENANCE_IF_MAIN; |