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

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

fix bug:1990 - Since release 0.5.3, unable to do search

  • Property svn:executable set to *
File size: 13.0 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        case '00.05.03':
186          $this->updateFrom_000503();
187        default:
188          /*
189           * default is applied for fresh install, and consist to fill the
190           * database with default values
191           */
192          $this->fillDatabase();
193          break;
194      }
195
196      $this->config['amd_FillDataBaseExcludeTags']=array();
197      $this->config['installed']=AMD_VERSION2; //update the installed release number
198      $this->saveConfig();
199
200      GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED);
201      GPCRequestBuilder::register('AMetaData', dirname($this->getFileLocation()).'/amd_rb_callback.class.inc.php');
202    }
203
204
205    public function deactivate()
206    {
207      GPCRequestBuilder::unregister('AMetaData');
208    }
209
210    /**
211     * update the database from the release 0.4.0
212     */
213    private function updateFrom_000400()
214    {
215      /*
216       * create new tables & alter existing tables
217       */
218      $tablesCreate=array(
219"CREATE TABLE `".$this->tables['user_tags_label']."` (
220  `numId` INTEGER UNSIGNED NOT NULL,
221  `lang` CHAR(5)  NOT NULL,
222  `label` VARCHAR(200)  NOT NULL,
223  PRIMARY KEY (`numId`, `lang`)
224);",
225"CREATE TABLE `".$this->tables['user_tags_def']."` (
226  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
227  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
228  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
229  `order` int(10) unsigned NOT NULL,
230  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
231  `value` varchar(200) NOT NULL,
232  `conditionType` char(2) NOT NULL default 'E',
233  `conditionValue` varchar(200) NOT NULL,
234  PRIMARY KEY  (`numId`,`defId`),
235  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
236  KEY `byTagOrder` (`numId`,`order`)
237);",
238      );
239      $tablesUpdate=array(
240        $this->tables['images_tags'] => array(
241          'byNumId' => "ADD INDEX `byNumId`(`numId`, `value`(35))",
242        )
243      );
244
245      $tablesDef = create_table_add_character_set($tablesCreate);
246
247      $tablef=new GPCTables(array($this->tables['user_tags_label'], $this->tables['user_tags_def']));
248
249      if(count($tablesCreate)>0) $tablef->create($tablesCreate);
250      if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate);
251
252      unset($tablesCreate);
253      unset($tablesUpdate);
254    }
255
256    /**
257     * update the database from the release 0.5.2
258     */
259    private function updateFrom_000502()
260    {
261      /*
262       * alter existing tables
263       */
264      $tablesUpdate=array(
265        $this->tables['used_tags'] => array(
266          'newFromLastUpdate' => "ADD COLUMN `newFromLastUpdate` CHAR(1)  NOT NULL DEFAULT 'n' AFTER `translatedName`",
267        )
268      );
269
270      $tablef=new GPCTables(array($this->tables['used_tags']));
271
272      if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate);
273
274      unset($tablesUpdate);
275    }
276
277    /**
278     * update the database from the release 0.5.3
279     */
280    private function updateFrom_000503()
281    {
282      GPCRequestBuilder::unregister('Advanced MetaData');
283    }
284
285
286    /**
287     * fill the database with some default value
288     */
289    private function fillDatabase()
290    {
291      if($this->config['newInstall']=='y')
292      {
293        $this->initializeDatabaseContent();
294      }
295      else
296      {
297        $this->updateDatabaseContent();
298      }
299    }
300
301    /**
302     * reset and initialize the database content (for a fresh install)
303     */
304    private function initializeDatabaseContent()
305    {
306      global $user;
307
308      L10n::setLanguage('en_UK');
309
310      pwg_query("DELETE FROM ".$this->tables['used_tags']);
311      pwg_query("DELETE FROM ".$this->tables['images_tags']);
312      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
313      pwg_query("INSERT INTO ".$this->tables['images']."
314                  SELECT id, 'n', 0
315                    FROM ".IMAGES_TABLE."
316                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
317      /*
318       * fill the 'used_tags' table with default values
319       */
320      foreach(AMD_JpegMetaData::getTagList(
321                Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED,
322                      'xmp' => true,
323                      'maker' => true,
324                      'iptc' => true,
325                      'com' => true)
326              ) as $key => $val
327             )
328      {
329        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."', 'n');";
330        pwg_query($sql);
331      }
332
333      /*
334       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
335       */
336      if(count($this->config['amd_FillDataBaseExcludeTags']))
337      {
338        $sql="";
339        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
340        {
341          if($sql!="") $sql.=" OR ";
342          $sql.=" tagId LIKE '$tag' ";
343        }
344        $sql="DELETE FROM ".$this->tables['used_tags']."
345              WHERE ".$sql;
346        pwg_query($sql);
347      }
348    }
349
350    /**
351     * update the database content (for an update)
352     */
353    private function updateDatabaseContent()
354    {
355      global $user;
356
357      L10n::setLanguage('en_UK');
358
359      pwg_query("INSERT INTO ".$this->tables['images']."
360                  SELECT id, 'n', 0
361                    FROM ".IMAGES_TABLE."
362                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
363
364      $tagList=array();
365      $sql="SELECT tagId FROM ".$this->tables['used_tags'];
366      $result=pwg_query($sql);
367      if($result)
368      {
369        while($row=pwg_db_fetch_row($result))
370        {
371          $tagList[$row[0]]='';
372        }
373      }
374
375      /*
376       * fill the 'used_tags' table with default values
377       */
378      foreach(AMD_JpegMetaData::getTagList(
379                Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED,
380                      'xmp' => true,
381                      'maker' => true,
382                      'iptc' => true,
383                      'com' => true)
384              ) as $key => $val
385             )
386      {
387        if(!array_key_exists($key, $tagList))
388        {
389          $sql="INSERT IGNORE INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."', 'y');";
390          pwg_query($sql);
391        }
392      }
393
394      /*
395       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
396       */
397      if(count($this->config['amd_FillDataBaseExcludeTags']))
398      {
399        $sql="";
400        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
401        {
402          if($sql!="") $sql.=" OR ";
403          $sql.=" tagId LIKE '$tag' ";
404        }
405        $sql="DELETE FROM ".$this->tables['used_tags']."
406              WHERE ".$sql;
407        pwg_query($sql);
408      }
409    }
410
411
412    private function buildDefaultGroup()
413    {
414      $sql=array();
415      $languages=get_languages();
416      foreach($languages as $key => $val)
417      {
418        load_language('plugin.lang', AMD_PATH, array('language' => $key, 'no_fallback'=>true));
419        $sql[]="(1, '".$key."', '".l10n('g003_default_group_name')."')";
420      }
421
422      //reload default user language
423      load_language('plugin.lang', AMD_PATH);
424      return("INSERT INTO `".$this->tables['groups_names']."` VALUES ".implode(',', $sql));
425    }
426
427  } //class
428
429?>
Note: See TracBrowser for help on using the repository browser.