Physikerwelt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181995

Change subject: Add batch import scripts for admins
......................................................................

Add batch import scripts for admins

Change-Id: I15ce779f74e01e4fa5d355aad5bfe6222c64e351
---
M db/math_wmc_runs.sql
M includes/ImportCsv.php
A maintenance/BatchImport.php
3 files changed, 104 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MathSearch 
refs/changes/95/181995/1

diff --git a/db/math_wmc_runs.sql b/db/math_wmc_runs.sql
index 6f66b8b..7b2319d 100644
--- a/db/math_wmc_runs.sql
+++ b/db/math_wmc_runs.sql
@@ -1,6 +1,6 @@
 CREATE TABLE math_wmc_runs
 (
-    runId INT PRIMARY KEY NOT NULL,
+    runId INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
     runName VARCHAR(45),
     userId INT UNSIGNED,
     isDraft TINYINT NOT NULL,
diff --git a/includes/ImportCsv.php b/includes/ImportCsv.php
index 1a3470a..376f01f 100644
--- a/includes/ImportCsv.php
+++ b/includes/ImportCsv.php
@@ -12,7 +12,7 @@
         */
        private static $columnHeaders = array( 'queryId', 'formulaId' );
        /**
-        * @var bool
+        * @var int
         */
        protected $runId = false;
        /**
@@ -52,7 +52,7 @@
                $this->overwrite = $overwrite;
                $runId = $this->validateRunId( $runId );
                if ( $runId !== false ) {
-                       $this->importFromFile( $csvFile );
+                       return $this->importFromFile( $csvFile );
                }
        }
 
@@ -66,21 +66,22 @@
                }
                $dbw = wfGetDB( DB_MASTER );
                $uID = $this->getUser()->getId();
-               $res =
-                       $dbw->selectField( 'math_wmc_runs', 'runName',
+               if ( is_int($run) ){
+                       $runId = $dbw->selectField( 'math_wmc_runs', 'runId',
                                array( 'isDraft' => true, 'userID' => $uID, 
'runId' => $run ) );
-               if ( !$res ) {
-                       $exists =
-                               $dbw->selectField( 'math_wmc_runs', 'runId',
+               } else {
+                       $runId = $dbw->selectField( 'math_wmc_runs', 'runId',
+                               array( 'isDraft' => true, 'userID' => $uID, 
'runName' => $run ) );
+               }
+               if ( !$runId ) {
+                       $exists = $dbw->selectField( 'math_wmc_runs', 'runId',
                                        array( 'userID' => $uID, 'runName' => 
$run ) );
                        if ( !$exists ) {
-                               $success =
-                                       $dbw->insert( 'math_wmc_runs',
+                               $success = $dbw->insert( 'math_wmc_runs',
                                                array( 'isDraft' => true, 
'userID' => $uID, 'runName' => $run ) );
                                if ( $success ) {
                                        $this->runId = $dbw->insertId();
-                                       $this->warnings[] =
-                                               wfMessage( 'math-wmc-RunAdded', 
$run, $this->runId )->text();
+                                       $this->warnings[] = wfMessage( 
'math-wmc-RunAdded', $run, $this->runId )->text();
                                } else {
                                        $this->runId = false;
                                        $this->warnings[] = wfMessage( 
'math-wmc-RunAddError', $run )->text();
@@ -90,7 +91,7 @@
                                $this->runId = false;
                        }
                } else {
-                       $this->runId = $run;
+                       $this->runId = $runId;
                }
                return $this->runId;
        }
@@ -115,7 +116,7 @@
         */
        public function importFromFile( $csv_file ) {
                if ( is_null( $csv_file ) ) {
-                       return wfMessage( 'emptyfile' )->text();
+                       $this->warnings[] = wfMessage( 'emptyfile' )->text();
                }
 
                $table = array();
@@ -137,7 +138,6 @@
                        // they didn't get removed, so remove them now.
                        $table[0][0] = trim( $table[0][0], '"' );
                }
-
                return $this->importFromArray( $table );
 
        }
@@ -201,7 +201,7 @@
                                }
                        }
                }
-               return null;
+               return true;
        }
 
        /**
@@ -312,4 +312,11 @@
                return $this->results;
        }
 
+       /**
+        * @return int
+        */
+       public function getRunId() {
+               return $this->runId;
+       }
+
 }
\ No newline at end of file
diff --git a/maintenance/BatchImport.php b/maintenance/BatchImport.php
new file mode 100644
index 0000000..63fb79f
--- /dev/null
+++ b/maintenance/BatchImport.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @ingroup Maintenance
+ */
+
+require_once( __DIR__ . '/../../../maintenance/Maintenance.php' );
+
+class BatchImport extends Maintenance {
+       private $dir;
+       private $overwrite;
+
+       /**
+        * @var DatabaseBase
+        */
+       private $db;
+       /**
+        *
+        */
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "Batch imports submissions from a folder. 
\n Processes CSV files that follow the naming convention: \n 
\$userName-\$runName.csv";
+               $this->addArg( "dir", "The directory to be read", true );
+               $this->addOption( "overwrite" , "Overwrite existing runs with 
the same name.", false, false, "o" );
+       }
+
+       /**
+        *
+        */
+       public function execute() {
+               $this->dir = $this->getArg( 0 );
+               $this->overwrite = $this->getOption( 'overwrite' );
+               if( $this->overwrite ){
+                       $this->output( "Loaded with option overwrite enabled 
.\n" );
+               }
+               if ( ! is_dir($this->dir) ){
+                       $this->output("{$this->dir} is not a directory.\n");
+                       exit(1);
+               }
+               $files = new GlobIterator($this->dir."/*-*.csv");
+               foreach ( $files as $file ) {
+                       $fn = $file->getFilename();
+                       if ( preg_match( 
"/(?P<user>.*?)-(?P<runName>.*?)\\.csv/", $fn,$matches) ){
+                               $user = User::newFromName( $matches['user'] );
+                               if( $user->getId() > 0 ){
+                                       $this->output("Importing filename $fn 
for userId {$user->getId()}.\n");
+                                       $importer = new ImportCsv($user);
+                                       $result = $importer->execute( 
fopen($file,'r'), $matches['runName'], $this->overwrite );
+                                       foreach( $importer->getWarnings() as 
$warning){
+                                               $this->output("warning: 
$warning \n");
+                                       }
+                                       if ( $result !== true ){
+                                               $this->output("$result\n");
+                                       } else {
+                                               $this->output( "File $fn 
imported as {$importer->getRunId()} \n" );
+                                       }
+                               } else {
+                                       $this->output("User {$matches['user']} 
is invalid. Skipping file $fn.\n");
+                               }
+                       }
+               }
+       }
+}
+
+$maintClass = "BatchImport";
+require_once( RUN_MAINTENANCE_IF_MAIN );

-- 
To view, visit https://gerrit.wikimedia.org/r/181995
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I15ce779f74e01e4fa5d355aad5bfe6222c64e351
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to