source: extensions/AMetaData/JpegMetaData/test/makePo.php @ 5222

Last change on this file since 5222 was 5222, checked in by grum, 14 years ago

JpegMetaData class is updated

  • english Tag.po file is (almost) ready to be translated in other lang
  • fixes some bugs on readers & tag definitions
  • Property svn:executable set to *
File size: 15.7 KB
Line 
1<?php
2/*
3 * --:: JPEG MetaDatas ::-------------------------------------------------------
4 *
5 *  Author    : Grum
6 *   email    : grum at piwigo.org
7 *   website  : http://photos.grum.fr
8 *
9 *   << May the Little SpaceFrog be with you ! >>
10 *
11 *
12 * +-----------------------------------------------------------------------+
13 * | JpegMetaData - a PHP based Jpeg Metadata manager                      |
14 * +-----------------------------------------------------------------------+
15 * | Copyright(C) 2010  Grum - http://www.grum.fr                          |
16 * +-----------------------------------------------------------------------+
17 * | This program is free software; you can redistribute it and/or modify  |
18 * | it under the terms of the GNU General Public License as published by  |
19 * | the Free Software Foundation                                          |
20 * |                                                                       |
21 * | This program is distributed in the hope that it will be useful, but   |
22 * | WITHOUT ANY WARRANTY; without even the implied warranty of            |
23 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
24 * | General Public License for more details.                              |
25 * |                                                                       |
26 * | You should have received a copy of the GNU General Public License     |
27 * | along with this program; if not, write to the Free Software           |
28 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
29 * | USA.                                                                  |
30 * +-----------------------------------------------------------------------+
31 *
32 *
33 * -----------------------------------------------------------------------------
34 *
35 * This file is used to prepare the .po file
36 *
37 * Method : the script parse all JpegMetaData/TagDefinition and select all the
38 *          implemented tags
39 *
40 * Options :
41 *  * file type : can make a .po file for translatable metadata values or can
42 *                make a .po file for metadata description
43 *  * compare   : can compare the generated .po file with an existing .po file
44 *                only all new/deleted keys are listed
45 *  * save      : can save the result in a .po file
46 *
47 * -----------------------------------------------------------------------------
48 *
49 *
50 * -----------------------------------------------------------------------------
51 */
52
53 ini_set('error_reporting', E_ALL | E_STRICT);
54 ini_set('display_errors', true);
55 date_default_timezone_set('UTC');
56
57  require_once("./../JpegMetaData.class.php");
58  require_once(JPEG_METADATA_DIR."Readers/JpegReader.class.php");
59  require_once(JPEG_METADATA_DIR."Common/XmlData.class.php");
60  require_once(JPEG_METADATA_DIR."Common/L10n.class.php");
61
62  require_once(JPEG_METADATA_DIR."TagDefinitions/IfdTags.class.php");
63  require_once(JPEG_METADATA_DIR."TagDefinitions/PentaxTags.class.php");
64  require_once(JPEG_METADATA_DIR."TagDefinitions/NikonTags.class.php");
65  require_once(JPEG_METADATA_DIR."TagDefinitions/CanonTags.class.php");
66  require_once(JPEG_METADATA_DIR."TagDefinitions/GpsTags.class.php");
67  require_once(JPEG_METADATA_DIR."TagDefinitions/XmpTags.class.php");
68
69
70  $hardCoded=Array(
71    "Yes",
72    "No",
73    "Contrast",
74    "Sharpness",
75    "Saturation",
76    "No extended bracketing",
77    "Custom",
78    "not yet implemented",
79  );
80
81  function cmp($a, $b)
82  {
83    if(strtolower($a['value']) == strtolower($b['value']))
84    {
85      if($a['value']==$b['value'])
86      {
87        return(0);
88      }
89      return(($a['value'] < $b['value'])?-1:1);
90    }
91    return((strtolower($a['value']) < strtolower($b['value']))?-1:1);
92  }
93
94  function loadPoFile($fileName)
95  {
96    $returnedKeys=Array();
97    if(file_exists($fileName))
98    {
99      $fHandler=fopen($fileName, "r");
100      if($fHandler)
101      {
102        $fileContent=fread($fHandler, filesize($fileName));
103        fclose($fHandler);
104
105        $pattern='/\s*msgid\s*"(.*)"\s*\n/im';
106        $result=Array();
107
108        preg_match_all($pattern, $fileContent, $result);
109
110        $returnedKeys=array_flip($result[1]);
111      }
112    }
113    return($returnedKeys);
114  }
115
116  function makePoValues($fileSaveName, $fileCompareName)
117  {
118    global $hardCoded;
119
120    $tmpTagName=Array();
121    $tmpValues=Array();
122
123    $tagList=Array(
124      new IfdTags(),
125      new XmpTags(),
126      new IptcTags(),
127      new GpsTags(),
128      new PentaxTags(),
129      new CanonTags(),
130      new NikonTags(),
131      new MagicTags(),
132    );
133
134    foreach($tagList as $key => $tag)
135    {
136
137      foreach($tag->getTags() as $key => $val)
138      {
139        if(array_key_exists('tagName', $val))
140          $name=$val['tagName'];
141        else
142          $name="";
143
144        if(is_string($key))
145          $tKey=$key;
146        else
147          $tKey=sprintf("0x%04x", $key);
148
149        if($name!="")
150          $tKey.=" ($name)";
151
152        if($name!="")
153        {
154          $tmpTagName[]=Array('group' => "[Metadata name] ".$tag->getLabel()." / ".$tKey, 'value' => $name);
155        }
156        else
157        {
158          $tmpTagName[]=Array('group' => "[Metadata name] ".$tag->getLabel()." / ".$tKey, 'value' => $key);
159        }
160
161
162        if(array_key_exists('tagValues', $val) and $val['translatable'] and $val['implemented'])
163        {
164          foreach($val['tagValues'] as $key2 => $val2)
165          {
166            if(is_array($val2))
167            {
168              foreach($val2 as $val3)
169              {
170                $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val3);
171              }
172            }
173            else
174            {
175              $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val2);
176            }
177          }
178        }
179
180        if(array_key_exists('tagValues.special', $val) and $val['translatable'])
181        {
182          foreach($val['tagValues.special'] as $key2 => $val2)
183          {
184            if(is_array($val2))
185            {
186              foreach($val2 as $val3)
187              {
188                $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val3);
189              }
190            }
191            else
192            {
193              $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val2);
194            }
195          }
196        }
197
198        if(array_key_exists('tagValues.specialNames', $val) and $val['translatable'])
199        {
200          foreach($val['tagValues.specialNames'] as $key2 => $val2)
201          {
202            if(is_array($val2))
203            {
204              foreach($val2 as $val3)
205              {
206                $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val3);
207              }
208            }
209            else
210            {
211              $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val2);
212            }
213          }
214        }
215
216        if(array_key_exists('tagValues.specialValues', $val) and $val['translatable'])
217        {
218          foreach($val['tagValues.specialValues'] as $key2 => $val2)
219          {
220            if(is_array($val2))
221            {
222              foreach($val2 as $val3)
223              {
224                $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val3);
225              }
226            }
227            else
228            {
229              $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val2);
230            }
231          }
232        }
233
234        if(array_key_exists('tagValues.computed', $val) and $val['translatable'])
235        {
236          foreach($val['tagValues.computed'] as $key2 => $val2)
237          {
238            if(is_array($val2))
239            {
240              foreach($val2 as $val3)
241              {
242                $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val3);
243              }
244            }
245            else
246            {
247              $tmpValues[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $val2);
248            }
249          }
250        }
251
252      }
253
254    }
255
256    $tmp=array_merge($tmpTagName, $tmpValues);
257    foreach($hardCoded as $val)
258    {
259      $tmp[]=Array(
260        'group' => "Hardcoded value",
261        'value' => $val,
262      );
263    }
264    usort($tmp, "cmp");
265
266
267    $tmp2=array(
268      array(
269        'value' => $tmp[0]['value'],
270        'group' => Array($tmp[0]['group']),
271      )
272    );
273    $tmp2k=0;
274
275    $nbCapitalization = 0;
276
277    for($i=1;$i<count($tmp);$i++)
278    {
279      if($tmp[$i]['value']==$tmp2[$tmp2k]['value'])
280      {
281        $tmp2[$tmp2k]['group'][]=$tmp[$i]['group'];
282      }
283      else
284      {
285        $tmp2k++;
286        $tmp2[$tmp2k]=array(
287          'group' => Array( $tmp[$i]['group'] ),
288          'value' => $tmp[$i]['value']
289        );
290
291        if($tmp2k>0)
292          if(strtolower($tmp2[$tmp2k]['value'])==strtolower($tmp2[$tmp2k-1]['value']))
293          {
294            $tmp2[$tmp2k]['group'] = Array("**** PREVIOUS IS THE SAME, TAKE A LOOK ABOUT CAPITALIZATION ****", $tmp[$i]['group'] );
295            $nbCapitalization++;
296          }
297      }
298    }
299
300    manageResult($tmp2, $fileSaveName, $fileCompareName, "");
301  }
302
303
304  function makePoDesc($fileSaveName, $fileCompareName)
305  {
306    $tmpTagName=Array();
307
308    $tagList=Array(
309      new IfdTags(),
310      new XmpTags(),
311      new IptcTags(),
312      new GpsTags(),
313      new PentaxTags(),
314      new CanonTags(),
315      new NikonTags(),
316      new MagicTags(),
317    );
318
319    $poKeys=loadPoFile($fileCompareName);
320
321    foreach($tagList as $key => $tag)
322    {
323      foreach($tag->getTags() as $key => $val)
324      {
325        if(array_key_exists('tagName', $val))
326          $name=$val['tagName'];
327        else
328          $name="";
329
330        if(is_string($key))
331          $tKey=$key;
332        else
333          $tKey=sprintf("0x%04x", $key);
334
335        if($name!="")
336          $tKey.=" ($name)";
337
338        if($name!="")
339        {
340          $tmpTagName[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $name);
341        }
342        else
343        {
344          $tmpTagName[]=Array('group' => $tag->getLabel()." / ".$tKey, 'value' => $key);
345        }
346      }
347    }
348
349    usort($tmpTagName, "cmp");
350
351    $tmp2=array(
352      array(
353        'value' => $tmpTagName[0]['value'],
354        'group' => Array($tmpTagName[0]['group']),
355      )
356    );
357    $tmp2k=0;
358
359    $nbCapitalization = 0;
360
361    for($i=1;$i<count($tmpTagName);$i++)
362    {
363      if($tmpTagName[$i]['value']==$tmp2[$tmp2k]['value'])
364      {
365        $tmp2[$tmp2k]['group'][]=$tmpTagName[$i]['group'];
366      }
367      else
368      {
369        $tmp2k++;
370        $tmp2[$tmp2k]=array(
371          'group' => Array( $tmpTagName[$i]['group'] ),
372          'value' => $tmpTagName[$i]['value']
373        );
374
375        if($tmp2k>0)
376          if(strtolower($tmp2[$tmp2k]['value'])==strtolower($tmp2[$tmp2k-1]['value']))
377          {
378            $tmp2[$tmp2k]['group'] = Array("**** PREVIOUS IS THE SAME, TAKE A LOOK ABOUT CAPITALIZATION ****", $tmpTagName[$i]['group'] );
379            $nbCapitalization++;
380          }
381      }
382    }
383
384    manageResult($tmp2, $fileSaveName, $fileCompareName, "There is no description available for this metadata");
385
386  }
387
388  function isAllowed($value)
389  {
390    $returned=true;
391
392    if(preg_match('/^[\d]{1,5}x[\d]{1,5}$/i', $value) or
393       preg_match('/^canon\s(ef|mp|ts)/i', $value) or
394       preg_match('/^sigma$/i', $value) or
395       preg_match('/^sigma\s(af|\d+|apo|dl|ex|uc|df)/i', $value) or
396       preg_match('/^smc pentax/i', $value) or
397       preg_match('/^pentax-f/i', $value) or
398       preg_match('/^takumar/i', $value) or
399       preg_match('/^smc pentax/i', $value) or
400       preg_match('/^samsung\sd/i', $value) or
401       preg_match('/^samsung\/schneider/i', $value) or
402       preg_match('/^schneider\sd/i', $value) or
403       preg_match('/^tamron\s(\d+|af|sp|xr)/i', $value) or
404       preg_match('/^tokina\s/i', $value) or
405       preg_match('/^cosina\s/i', $value) or
406       preg_match('/^carl zeiss\s/i', $value) or
407       preg_match('/^voigtlander ultron/i', $value) or
408       preg_match('/^\{/i', $value) or
409       preg_match('/^(4x|2x)$/i', $value) or
410       preg_match('/^(x:|xmlns:)/i', $value) or
411       ($value == "?")
412     )
413    {
414      //echo "$value<br>";
415      $returned=false;
416    }
417    //echo "$value : ".(($returned)?"Y":"N")."<br>";
418    return($returned);
419  }
420
421
422  function manageResult($data, $fileSaveName, $fileCompareName, $default="")
423  {
424    $poKeys=loadPoFile($fileCompareName);
425
426    $result="msgid \"\"
427msgstr \"\"
428\"Project-Id-Version: TagNames\\n\"
429\"POT-Creation-Date: \\n\"
430\"PO-Revision-Date: ".date('Y-m-d')."\\n\"
431\"Last-Translator: grum <grum@piwigo.org>\\n\"
432\"Language-Team: grum <grum@piwigo.org>\\n\"
433\"MIME-Version: 1.0\\n\"
434\"Content-Type: text/plain; charset=utf-8\\n\"
435\"Content-Transfer-Encoding: 8bit\\n\"
436\"X-Poedit-Language: en\\n\"
437\"X-Poedit-Country: UK\\n\"
438\"X-Poedit-SourceCharset: utf-8\\n\"
439
440";
441
442    $nb=0;
443    foreach($data as $key => $val)
444    {
445      if(($fileCompareName=="" or
446          ($fileCompareName!="" and !array_key_exists($val['value'], $poKeys))) and
447          isAllowed($val['value']))
448      {
449        foreach($val['group'] as $group)
450        {
451          $result.="#. ".$group."\n";
452        }
453        if($default!="")
454        {
455          $result.="#, fuzzy\n";
456        }
457        $result.="msgid \"".$val['value']."\"\n";
458        $result.="msgstr \"".$default."\"\n\n";
459        $nb++;
460      }
461    }
462
463    echo "Number of keys : $nb<br><hr>";
464    echo nl2br($result);
465
466    if($fileSaveName!="")
467    {
468      $fHandler=fopen($fileSaveName, "w");
469      if($fHandler)
470      {
471        fwrite($fHandler, $result);
472        fclose($fHandler);
473      }
474    }
475  }
476
477  function makeForm()
478  {
479    if(isset($_REQUEST['fFileType']))
480    {
481      $fileType=$_REQUEST['fFileType'];
482    }
483    else
484    {
485      $fileType='values';
486    }
487
488    if(isset($_REQUEST['fFileSaveName']))
489    {
490      $fileSaveName=$_REQUEST['fFileSaveName'];
491    }
492    else
493    {
494      $fileSaveName='';
495      $_REQUEST['fFileSaveName']='';
496    }
497
498    if(isset($_REQUEST['fFileSave']))
499    {
500      $fileSave=$_REQUEST['fFileSave'];
501    }
502    else
503    {
504      $fileSave='';
505      $_REQUEST['fFileSave']='';
506    }
507
508    if(isset($_REQUEST['fFileCompareName']))
509    {
510      $fileCompareName=$_REQUEST['fFileCompareName'];
511    }
512    else
513    {
514      $fileCompareName='';
515      $_REQUEST['fFileCompareName']='';
516    }
517
518    if(isset($_REQUEST['fFileCompare']))
519    {
520      $fileCompare=$_REQUEST['fFileCompare'];
521    }
522    else
523    {
524      $fileCompare='';
525      $_REQUEST['fFileCompare']='';
526    }
527
528
529    $checked['values']=($fileType=='values')?"checked":"";
530    $checked['desc']=($fileType=='desc')?"checked":"";
531
532    if($fileSave=='on')
533    {
534      $fileSave="checked";
535    }
536
537    if($fileCompare=='on')
538    {
539      $fileCompare="checked";
540    }
541
542
543    echo "
544    <form>
545      <fieldset>
546        Generate <i>.po</i> file :<br>
547        <label><input type='radio' name='fFileType' value='values' ".$checked['values'].">metadata values</label><br>
548        <label><input type='radio' name='fFileType' value='desc' ".$checked['desc'].">metadata description</label><br>
549
550        <br><br>
551
552        <label><input type='checkbox' name='fFileCompare' ".$fileCompare.">Compare with an existing file</label><br>
553        <label>File name :
554          <input type='text' name='fFileCompareName' value='".$fileCompareName."'>
555        </label>
556
557
558        <br><br>
559
560        <label><input type='checkbox' name='fFileSave' ".$fileSave.">Save result in a file</label><br>
561        <label>File name :
562          <input type='text' name='fFileSaveName' value='".$fileSaveName."'>
563        </label>
564
565        <br><br><input type='submit'>
566      </fieldset>
567    </form><br>";
568
569  }
570
571  makeForm();
572
573  if(isset($_REQUEST['fFileType']))
574  {
575    switch($_REQUEST['fFileType'])
576    {
577      case "values":
578        makePoValues(($_REQUEST['fFileSave']=='on')?$_REQUEST['fFileSaveName']:"", ($_REQUEST['fFileCompare']=='on')?$_REQUEST['fFileCompareName']:"");
579        break;
580      case "desc":
581        makePoDesc(($_REQUEST['fFileSave']=='on')?$_REQUEST['fFileSaveName']:"", ($_REQUEST['fFileCompare']=='on')?$_REQUEST['fFileCompareName']:"");
582        break;
583    }
584  }
585
586?>
Note: See TracBrowser for help on using the repository browser.