-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: - Add CSV imports via CLI - Add Import Usecase - Fix typo in Console\Command - Use camelCase for console command actions - Better validation errors Refs T272 Test Plan: - `./bin/ushahidi import list` - to see possible formats. - `./bin/ushahidi import run -f ../primary-schools-2007-top50.csv -m ../primary-schools-map.json --values ../primary-schools-values.json` Where map.json is a json file of source-destination mappings like ``` { "[Id]" : "[values][school-id][0]", "[Name_of_School]": "[title]" } ``` and values.json is a json file of fixed values like ``` { "status":"published", "form":2 } ``` Reviewers: will, lkamau, jasonmule Subscribers: jasonmule, will, rjmackay, lkamau Maniphest Tasks: T272 Differential Revision: https://phabricator.ushahidi.com/D874
- Loading branch information
Showing
22 changed files
with
858 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php defined('SYSPATH') or die('No direct script access'); | ||
|
||
/** | ||
* Ushahidi CSV File Reader | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
use \ArrayIterator; | ||
use League\Csv\Reader; | ||
use Ushahidi\Core\Tool\FileReader; | ||
|
||
class Ushahidi_FileReader_CSV implements FileReader | ||
{ | ||
|
||
protected $limit; | ||
public function setLimit($limit) { | ||
$this->limit = $limit; | ||
} | ||
|
||
protected $offset; | ||
public function setOffset($offset) { | ||
$this->offset = $offset; | ||
} | ||
|
||
public function process($filename) { | ||
// @todo inject factory function to get Reader | ||
$reader = Reader::createFromFileObject(new SplFileObject($filename)); | ||
|
||
// Filter out empty rows | ||
$nbColumns = count($reader->fetchOne()); | ||
$reader->addFilter(function($row) use ($nbColumns) { | ||
return count($row) == $nbColumns; | ||
}); | ||
|
||
if ($this->offset) { | ||
$reader->setOffset($this->offset); | ||
} | ||
if ($this->limit) { | ||
$reader->setLimit($this->limit); | ||
} | ||
|
||
return new ArrayIterator($reader->fetchAssoc()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
application/classes/Ushahidi/Transformer/MappingTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php defined('SYSPATH') or die('No direct script access'); | ||
|
||
/** | ||
* Ushahidi Mapping Transformer | ||
* | ||
* A user defined transform, transforms records based on | ||
* - a source-destination mapping | ||
* - a set of fixed destination values | ||
* | ||
* Uses the MappingStep from ddeboer/import to process | ||
* the transformation | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
use Ushahidi\Core\Tool\MappingTransformer; | ||
use Ddeboer\DataImport\Step\MappingStep; | ||
|
||
class Ushahidi_Transformer_MappingTransformer implements MappingTransformer | ||
{ | ||
protected $map; | ||
// MappingTransformer | ||
public function setMap(Array $map) | ||
{ | ||
$this->map = new MappingStep($map); | ||
} | ||
|
||
protected $fixedValues; | ||
// MappingTransformer | ||
public function setFixedValues(Array $fixedValues) | ||
{ | ||
$this->fixedValues = $fixedValues; | ||
} | ||
|
||
// Tranformer | ||
public function interact(Array $data) | ||
{ | ||
$this->map->process($data); | ||
|
||
$data = array_merge($data, $this->fixedValues); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.