Open Monograph Press  3.3.0
xmlToPo.php
1 <?php
2 
14 require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');
15 
16 class xmlToPo extends CommandLineTool {
18  protected $source;
19 
21  protected $target;
22 
26  function __construct($argv = array()) {
27  parent::__construct($argv);
28 
29  array_shift($argv); // Shift the tool name off the top
30 
31  $this->source = array_shift($argv);
32  $this->target = array_shift($argv);
33 
34  // The source file/directory must be specified and exist.
35  if (empty($this->source) || !file_exists($this->source)) {
36  $this->usage();
37  exit(2);
38  }
39 
40  // The target file must be specified, unless we're converting an entire directory.
41  if (empty($this->target)) {
42  if (is_file($this->source)) {
43  $this->usage();
44  exit(3);
45  } else {
46  // If we're converting a directory and no target was specified, we use the source dir.
47  $this->target = $this->source;
48  }
49  }
50 
51  // If the target file/directory exists, it must be like the source file/directory.
52  if (file_exists($this->target) && (is_dir($this->source) != is_dir($this->target))) {
53  $this->usage();
54  exit(4);
55  }
56  }
57 
61  function usage() {
62  echo "Script to convert XML locale file to PO format\n\n"
63  . "Usage: {$this->scriptName} input-locale-file.xml output-file.po\n\n"
64  . "or, to convert all locale files in a specified directory:\n\n"
65  . "{$this->scriptName} input-path [output-path]\n\n"
66  . "When specifying a path, the output path is optional. If it is not specified, the input path will be used.\n";
67  }
68 
74  static function parseLocaleFile($filename) {
75  $localeData = null;
76  $xmlDao = new XMLDAO();
77  $data = $xmlDao->parseStruct($filename, array('message'));
78 
79  // Build array with ($key => $string)
80  if (isset($data['message'])) {
81  foreach ($data['message'] as $messageData) {
82  $localeData[$messageData['attributes']['key']] = $messageData['value'];
83  }
84  }
85 
86  return $localeData;
87  }
88 
94  static function convertFile($source, $target) {
95  $localeData = array();
96 
97  $sourceData = self::parseLocaleFile($source);
98  if (!$sourceData) throw new Exception('Unable to load source file ' . $source);
99 
100  $translations = new \Gettext\Translations();
101  foreach ($sourceData as $key => $sourceTranslation) {
102  $translation = new \Gettext\Translation('', $key);
103  $translation->setTranslation("$sourceTranslation");
104  $translations->append($translation);
105  }
106 
107  return $translations->toPoFile($target);
108  }
109 
113  function execute() {
114  if (is_dir($this->source)) {
115  // The caller specified a directory of files.
116  import('lib.pkp.classes.file.FileManager');
117  $fileManager = new FileManager();
118 
119  // Look recursively for files to convert.
120  $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->source));
121  foreach ($iterator as $file) {
122  if ($file->isDir()) continue;
123  $pathname = $file->getPathname();
124  if (strrchr($pathname, '.') != '.xml') continue;
125 
126  // If we can't glean any data from the file, skip it.
127  if (!self::parseLocaleFile($pathname)) continue;
128 
129  // This seems to be a locale file. Try to convert it.
130  if (substr($pathname, 0, strlen($this->source)) !== $this->source) continue;
131  $targetPath = $this->target . dirname(substr($pathname, strlen($this->source)));
132  $targetFile = $targetPath . '/' . basename($pathname, '.xml') . '.po';
133 
134  // Ensure the target directory exists.
135  $fileManager->mkdirtree($targetPath);
136 
137  echo "$pathname => $targetFile\n";
138  self::convertFile($pathname, $targetFile);
139  }
140  } else {
141  // Convert just a single file, as specified.
142  self::convertFile($this->source, $this->target);
143  }
144  }
145 }
146 
147 $tool = new xmlToPo(isset($argv) ? $argv : array());
148 $tool->execute();
149 
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
xmlToPo\$source
$source
Definition: xmlToPo.php:21
xmlToPo\parseLocaleFile
static parseLocaleFile($filename)
Definition: xmlToPo.php:80
CommandLineTool
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
xmlToPo\$target
$target
Definition: xmlToPo.php:27
$tool
$tool
Definition: mergeCoverageReportTool.php:120
xmlToPo
Definition: xmlToPo.php:16
xmlToPo\convertFile
static convertFile($source, $target)
Definition: xmlToPo.php:100
xmlToPo\__construct
__construct($argv=array())
Definition: xmlToPo.php:32
xmlToPo\usage
usage()
Definition: xmlToPo.php:67
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
xmlToPo\execute
execute()
Definition: xmlToPo.php:119
CommandLineTool\$argv
$argv
Definition: CliTool.inc.php:53