Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
importAccounts.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Account export
4  * @author Anakeen
5  * @package FDL
6 */
7 
8 $usage = new ApiUsage();
9 $usage->setDefinitionText("Import accounts definition");
10 $filename = $usage->addRequiredParameter("file", "the input XML file", function ($values, $argName, ApiUsage $apiusage)
11 {
12  if ($values === ApiUsage::GET_USAGE) {
13  return "";
14  }
15  if (is_file($values) && !is_readable($values)) {
16  $apiusage->exitError(sprintf("Error: file output \"%s\" not readable.", $values));
17  }
18  return '';
19 });
20 $outfile = $usage->addOptionalParameter("report-file", "the output report file", function ($values, $argName, ApiUsage $apiusage)
21 {
22  if ($values === ApiUsage::GET_USAGE) {
23  return "";
24  }
25  if ($values !== "-" && is_file($values) && !is_writable($values)) {
26  $apiusage->exitError(sprintf("Error: file output \"%s\" not writable.", $values));
27  }
28  return '';
29 });
30 $dry = $usage->addEmptyParameter("dry-run", "Analyse file only - no import is proceed");
31 
32 $usage->verify();
33 
34 $import = new \Dcp\Core\ImportAccounts();
35 $import->setFile($filename);
36 $import->setAnalyzeOnly($dry);
37 $import->import();
38 
39 $ext = substr($outfile, strrpos($outfile, '.') + 1);
40 if ($outfile) {
41  $report = $import->getReport();
42  switch ($ext) {
43  case "json":
44  if ($outfile === "-.json") {
45  $outfile = "php://stdout";
46  }
47  file_put_contents($outfile, json_encode($report, JSON_PRETTY_PRINT));
48  break;
49 
50  case "csv":
52  break;
53 
54  default:
56  break;
57  }
58 }
59 
61 {
62  if ($outfile === "-.csv") {
63  $outfile = "php://stdout";
64  }
65  $csvFile = fopen($outfile, "w+");
66  fputcsv($csvFile, array(
67  "login",
68  "action",
69  "error",
70  "message",
71  "node"
72  ));
73  foreach ($report as $row) {
74  fputcsv($csvFile, array(
75  $row["login"],
76  $row["action"],
77  $row["error"],
78  $row["message"],
79  $row["node"]
80  ));
81  }
82  fclose($csvFile);
83 }
84 
86 {
87  $out = '';
88  $format = "| %20s | %20s | %30s | %20s |\n";
89  $out = sprintf($format, "login", "action", "error", "message");
90  $spaces = sprintf("%80s", " ");
91  $out.= "---------------------------------------------------------------------------------------------------\n";
92  foreach ($report as $row) {
93  if ($row["error"]) {
94  $out.= chr(0x1b) . "[1;31m";
95  }
96 
97  $out.= sprintf($format, $row["login"], $row["action"], $row["error"], str_replace("\n", ", ", $row["message"]));
98  if ($row["error"]) {
99  $out.= chr(0x1b) . "[0;39m";
100  }
101  }
102  if ($outfile) {
103  if ($outfile === "-") {
104  $outfile = "php://stdout";
105  }
106  }
107  file_put_contents($outfile, $out);
108 }
if($outfile) printCsv($report, $outfile)
exitError($error= '')
$filename
const GET_USAGE
$outfile
$usage
$import
printText($report, $outfile)
Verify arguments for wsh programs.
← centre documentaire © anakeen