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