source: extensions/AMetaData/amd_install.class.inc.php @ 7519

Last change on this file since 7519 was 7519, checked in by grum, 13 years ago

Exploit the JpegMetadata class previous evolution
feature:1975, feature:1976, feature:1978

  • Property svn:executable set to *
File size: 12.8 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: Advanced MetaData
5 * -----------------------------------------------------------------------------
6 * Author     : Grum
7 *   email    : grum@piwigo.org
8 *   website  : http://photos.grum.fr
9 *   PWG user : http://forum.piwigo.org/profile.php?id=3706
10 *
11 *   << May the Little SpaceFrog be with you ! >>
12 *
13 * -----------------------------------------------------------------------------
14 *
15 * See main.inc.php for release information
16 *
17 * AMD_install : classe to manage plugin install
18 * ---------------------------------------------------------------------------
19 */
20
21  include_once('amd_root.class.inc.php');
22
23  class AMD_install extends AMD_root
24  {
25    private $tablef;
26
27    public function __construct($prefixeTable, $filelocation)
28    {
29      parent::__construct($prefixeTable, $filelocation);
30      $this->tablef= new GPCTables($this->tables);
31    }
32
33    public function __destruct()
34    {
35      unset($this->tablef);
36      parent::__destruct();
37    }
38
39    /*
40     * function for installation process
41     * return true if install process is ok, otherwise false
42     */
43    public function install()
44    {
45      global $user, $lang;
46
47      $this->initConfig();
48      $this->loadConfig();
49      $this->config['amd_FillDataBaseIgnoreSchemas']=> array('exif', 'iptc', 'xmp', 'com');
50      $this->config['installed']=AMD_VERSION2;
51      $this->config['newInstall']='y';
52      $this->saveConfig();
53
54      $tables_def=array(
55"CREATE TABLE `".$this->tables['used_tags']."` (
56  `numId` int(10) unsigned NOT NULL auto_increment,
57  `tagId` varchar(80) NOT NULL default '',
58  `translatable` char(1) NOT NULL default 'n',
59  `name` varchar(200) NOT NULL default '',
60  `numOfImg` int(10) unsigned NOT NULL default '0',
61  `translatedName` varchar(200) NOT NULL default '',
62  `newFromLastUpdate` char(1) NOT NULL default 'n',
63  PRIMARY KEY  (`numId`),
64  KEY `by_tag` (`tagId`)
65);",
66"CREATE TABLE `".$this->tables['images_tags']."` (
67  `imageId` mediumint(8) unsigned NOT NULL default '0',
68  `numId` int(10) unsigned NOT NULL default '0',
69  `value` text,
70  `numValue` decimal(10,8) default NULL,
71  PRIMARY KEY  USING BTREE (`imageId`,`numId`),
72  KEY `byNumId` (`numId`,`value`(35)),
73  KEY `byNumId2` (`numId`,`numValue`)
74);",
75"CREATE TABLE `".$this->tables['images']."` (
76  `imageId` MEDIUMINT(8) UNSIGNED NOT NULL,
77  `analyzed` CHAR(1)  NOT NULL DEFAULT 'n',
78  `nbTags` int(10) unsigned NOT NULL default '0',
79  PRIMARY KEY (`imageId`)
80);",
81"CREATE TABLE `".$this->tables['selected_tags']."` (
82  `tagId` VARCHAR(80)  NOT NULL,
83  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
84  `groupId` INTEGER  NOT NULL DEFAULT -1,
85  PRIMARY KEY (`tagId`)
86);",
87"CREATE TABLE `".$this->tables['groups_names']."` (
88  `groupId` INTEGER  NOT NULL,
89  `lang` CHAR(5)  NOT NULL,
90  `name` VARCHAR(80)  NOT NULL,
91  PRIMARY KEY (`groupId`, `lang`)
92);",
93"CREATE TABLE `".$this->tables['groups']."` (
94  `groupId` INTEGER  NOT NULL AUTO_INCREMENT,
95  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
96  PRIMARY KEY (`groupId`)
97);",
98"CREATE TABLE `".$this->tables['user_tags_label']."` (
99  `numId` INTEGER UNSIGNED NOT NULL,
100  `lang` CHAR(5)  NOT NULL,
101  `label` VARCHAR(200)  NOT NULL,
102  PRIMARY KEY (`numId`, `lang`)
103);",
104"CREATE TABLE `".$this->tables['user_tags_def']."` (
105  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
106  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
107  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
108  `order` int(10) unsigned NOT NULL,
109  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
110  `value` varchar(200) NOT NULL,
111  `conditionType` char(2) NOT NULL default 'E',
112  `conditionValue` varchar(200) NOT NULL,
113  PRIMARY KEY  (`numId`,`defId`),
114  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
115  KEY `byTagOrder` (`numId`,`order`)
116);",
117      );
118
119
120      $tables_def = create_table_add_character_set($tables_def);
121      $result=$this->tablef->create($tables_def);
122      unset($tables_def);
123
124
125      $tablesInsert=array(
126"INSERT INTO `".$this->tables['groups']."` VALUES(1, 0)",
127$this->buildDefaultGroup(),
128"INSERT INTO `".$this->tables['selected_tags']."` VALUES
129    ('magic.Camera.Make', 0, 1),
130    ('magic.Camera.Model', 1, 1),
131    ('magic.ShotInfo.Lens', 2, 1),
132    ('magic.ShotInfo.Aperture', 3, 1),
133    ('magic.ShotInfo.Exposure', 4, 1),
134    ('magic.ShotInfo.ISO', 5, 1),
135    ('magic.ShotInfo.FocalLength', 6, 1),
136    ('magic.ShotInfo.FocalLengthIn35mm', 7, 1),
137    ('magic.ShotInfo.Flash.Fired', 8, 1)"
138      );
139      foreach($tablesInsert as $sql)
140      {
141        pwg_query($sql);
142      }
143
144      GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED);
145      return($result);
146    }
147
148
149    /*
150        function for uninstall process
151    */
152    public function uninstall()
153    {
154      $this->deleteConfig();
155      $this->tablef->drop();
156      GPCCore::unregister($this->getPluginName());
157    }
158
159    public function activate()
160    {
161      global $template, $user;
162
163      $this->initConfig();
164      $this->loadConfig();
165      if(method_exists($this, 'loadConfigFromFile'))
166      {
167        $this->loadConfigFromFile(dirname($this->getFileLocation()).'/activatePlugin.conf.php');
168      }
169
170      /*
171       * if there is no version information available, assume the previous
172       *  installed release of the plugin is 0.4.0
173       */
174      if(!isset($this->config['installed'])) $this->config['installed']='00.04.00';
175
176      switch($this->config['installed'])
177      {
178        case '00.04.00':
179          $this->config['newInstall']='n';
180          $this->updateFrom_000400();
181        case '00.05.01':
182        case '00.05.02':
183          $this->config['newInstall']='n';
184          $this->updateFrom_000502();
185        default:
186          /*
187           * default is applied for fresh install, and consist to fill the
188           * database with default values
189           */
190          $this->fillDatabase();
191          break;
192      }
193
194      $this->config['amd_FillDataBaseExcludeTags']=array();
195      $this->config['installed']=AMD_VERSION2; //update the installed release number
196      $this->saveConfig();
197
198      GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED);
199      GPCRequestBuilder::register($this->getPluginName(), dirname($this->getFileLocation()).'/amd_rb_callback.class.inc.php');
200    }
201
202
203    public function deactivate()
204    {
205      GPCRequestBuilder::unregister($this->getPluginName());
206    }
207
208    /**
209     * update the database from the release 0.4.0
210     */
211    private function updateFrom_000400()
212    {
213      /*
214       * create new tables & alter existing tables
215       */
216      $tablesCreate=array(
217"CREATE TABLE `".$this->tables['user_tags_label']."` (
218  `numId` INTEGER UNSIGNED NOT NULL,
219  `lang` CHAR(5)  NOT NULL,
220  `label` VARCHAR(200)  NOT NULL,
221  PRIMARY KEY (`numId`, `lang`)
222);",
223"CREATE TABLE `".$this->tables['user_tags_def']."` (
224  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
225  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
226  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
227  `order` int(10) unsigned NOT NULL,
228  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
229  `value` varchar(200) NOT NULL,
230  `conditionType` char(2) NOT NULL default 'E',
231  `conditionValue` varchar(200) NOT NULL,
232  PRIMARY KEY  (`numId`,`defId`),
233  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
234  KEY `byTagOrder` (`numId`,`order`)
235);",
236      );
237      $tablesUpdate=array(
238        $this->tables['images_tags'] => array(
239          'byNumId' => "ADD INDEX `byNumId`(`numId`, `value`(35))",
240        )
241      );
242
243      $tablesDef = create_table_add_character_set($tablesCreate);
244
245      $tablef=new GPCTables(array($this->tables['user_tags_label'], $this->tables['user_tags_def']));
246
247      if(count($tablesCreate)>0) $tablef->create($tablesCreate);
248      if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate);
249
250      unset($tablesCreate);
251      unset($tablesUpdate);
252    }
253
254    /**
255     * update the database from the release 0.5.2
256     */
257    private function updateFrom_000502()
258    {
259      /*
260       * alter existing tables
261       */
262      $tablesUpdate=array(
263        $this->tables['used_tags'] => array(
264          'newFromLastUpdate' => "ADD COLUMN `newFromLastUpdate` CHAR(1)  NOT NULL DEFAULT 'n' AFTER `translatedName`",
265        )
266      );
267
268      $tablef=new GPCTables(array($this->tables['used_tags']));
269
270      if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate);
271
272      unset($tablesUpdate);
273    }
274
275
276    /**
277     * fill the database with some default value
278     */
279    private function fillDatabase()
280    {
281      if($this->config['newInstall']=='y')
282      {
283        $this->initializeDatabaseContent();
284      }
285      else
286      {
287        $this->updateDatabaseContent();
288      }
289    }
290
291    /**
292     * reset and initialize the database content (for a fresh install)
293     */
294    private function initializeDatabaseContent()
295    {
296      global $user;
297
298      L10n::setLanguage('en_UK');
299
300      pwg_query("DELETE FROM ".$this->tables['used_tags']);
301      pwg_query("DELETE FROM ".$this->tables['images_tags']);
302      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
303      pwg_query("INSERT INTO ".$this->tables['images']."
304                  SELECT id, 'n', 0
305                    FROM ".IMAGES_TABLE."
306                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
307      /*
308       * fill the 'used_tags' table with default values
309       */
310      foreach(AMD_JpegMetaData::getTagList(
311                Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED,
312                      'xmp' => true,
313                      'maker' => true,
314                      'iptc' => true,
315                      'com' => true)
316              ) as $key => $val
317             )
318      {
319        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."', 'n');";
320        pwg_query($sql);
321      }
322
323      /*
324       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
325       */
326      if(count($this->config['amd_FillDataBaseExcludeTags']))
327      {
328        $sql="";
329        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
330        {
331          if($sql!="") $sql.=" OR ";
332          $sql.=" tagId LIKE '$tag' ";
333        }
334        $sql="DELETE FROM ".$this->tables['used_tags']."
335              WHERE ".$sql;
336        pwg_query($sql);
337      }
338    }
339
340    /**
341     * update the database content (for an update)
342     */
343    private function updateDatabaseContent()
344    {
345      global $user;
346
347      L10n::setLanguage('en_UK');
348
349      pwg_query("INSERT INTO ".$this->tables['images']."
350                  SELECT id, 'n', 0
351                    FROM ".IMAGES_TABLE."
352                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
353
354      $tagList=array();
355      $sql="SELECT tagId FROM ".$this->tables['used_tags'];
356      $result=pwg_query($sql);
357      if($result)
358      {
359        while($row=pwg_db_fetch_row($result))
360        {
361          $tagList[$row[0]]='';
362        }
363      }
364
365      /*
366       * fill the 'used_tags' table with default values
367       */
368      foreach(AMD_JpegMetaData::getTagList(
369                Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED,
370                      'xmp' => true,
371                      'maker' => true,
372                      'iptc' => true,
373                      'com' => true)
374              ) as $key => $val
375             )
376      {
377        if(!array_key_exists($key, $tagList))
378        {
379          $sql="INSERT IGNORE INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."', 'y');";
380          pwg_query($sql);
381        }
382      }
383
384      /*
385       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
386       */
387      if(count($this->config['amd_FillDataBaseExcludeTags']))
388      {
389        $sql="";
390        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
391        {
392          if($sql!="") $sql.=" OR ";
393          $sql.=" tagId LIKE '$tag' ";
394        }
395        $sql="DELETE FROM ".$this->tables['used_tags']."
396              WHERE ".$sql;
397        pwg_query($sql);
398      }
399    }
400
401
402    private function buildDefaultGroup()
403    {
404      $sql=array();
405      $languages=get_languages();
406      foreach($languages as $key => $val)
407      {
408        load_language('plugin.lang', AMD_PATH, array('language' => $key, 'no_fallback'=>true));
409        $sql[]="(1, '".$key."', '".l10n('g003_default_group_name')."')";
410      }
411
412      //reload default user language
413      load_language('plugin.lang', AMD_PATH);
414      return("INSERT INTO `".$this->tables['groups_names']."` VALUES ".implode(',', $sql));
415    }
416
417  } //class
418
419?>
Note: See TracBrowser for help on using the repository browser.