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

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

Migration from version 0.4.0 to 0.5.1 implemented

  • Property svn:executable set to *
File size: 9.4 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['installed']=AMD_VERSION2;
50      $this->config['newInstall']='y';
51      $this->saveConfig();
52
53      $tables_def=array(
54"CREATE TABLE `".$this->tables['used_tags']."` (
55  `numId` int(10) unsigned NOT NULL auto_increment,
56  `tagId` varchar(80) NOT NULL default '',
57  `translatable` char(1) NOT NULL default 'n',
58  `name` varchar(200) NOT NULL default '',
59  `numOfImg` int(10) unsigned NOT NULL default '0',
60  `translatedName` varchar(200) NOT NULL default '',
61  PRIMARY KEY  (`numId`),
62  KEY `by_tag` (`tagId`)
63);",
64"CREATE TABLE `".$this->tables['images_tags']."` (
65  `imageId` mediumint(8) unsigned NOT NULL default '0',
66  `numId` int(10) unsigned NOT NULL default '0',
67  `value` text,
68  `numValue` decimal(10,8) default NULL,
69  PRIMARY KEY  USING BTREE (`imageId`,`numId`),
70  KEY `byNumId` (`numId`,`value`(35)),
71  KEY `byNumId2` (`numId`,`numValue`)
72);",
73"CREATE TABLE `".$this->tables['images']."` (
74  `imageId` MEDIUMINT(8) UNSIGNED NOT NULL,
75  `analyzed` CHAR(1)  NOT NULL DEFAULT 'n',
76  `nbTags` int(10) unsigned NOT NULL default '0',
77  PRIMARY KEY (`imageId`)
78);",
79"CREATE TABLE `".$this->tables['selected_tags']."` (
80  `tagId` VARCHAR(80)  NOT NULL,
81  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
82  `groupId` INTEGER  NOT NULL DEFAULT -1,
83  PRIMARY KEY (`tagId`)
84);",
85"CREATE TABLE `".$this->tables['groups_names']."` (
86  `groupId` INTEGER  NOT NULL,
87  `lang` CHAR(5)  NOT NULL,
88  `name` VARCHAR(80)  NOT NULL,
89  PRIMARY KEY (`groupId`, `lang`)
90);",
91"CREATE TABLE `".$this->tables['groups']."` (
92  `groupId` INTEGER  NOT NULL AUTO_INCREMENT,
93  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
94  PRIMARY KEY (`groupId`)
95);",
96"CREATE TABLE `".$this->tables['user_tags_label']."` (
97  `numId` INTEGER UNSIGNED NOT NULL,
98  `lang` CHAR(5)  NOT NULL,
99  `label` VARCHAR(200)  NOT NULL,
100  PRIMARY KEY (`numId`, `lang`)
101);",
102"CREATE TABLE `".$this->tables['user_tags_def']."` (
103  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
104  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
105  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
106  `order` int(10) unsigned NOT NULL,
107  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
108  `value` varchar(200) NOT NULL,
109  `conditionType` char(2) NOT NULL default 'E',
110  `conditionValue` varchar(200) NOT NULL,
111  PRIMARY KEY  (`numId`,`defId`),
112  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
113  KEY `byTagOrder` (`numId`,`order`)
114);",
115      );
116
117
118      $tables_def = create_table_add_character_set($tables_def);
119      $result=$this->tablef->create($tables_def);
120      unset($tables_def);
121
122
123      $tablesInsert=array(
124"INSERT INTO `".$this->tables['groups']."` VALUES(1, 0)",
125"INSERT INTO `".$this->tables['groups_names']."` VALUES(1, '".$user['language']."', '".$lang['g003_default_group_name']."')",
126"INSERT INTO `".$this->tables['selected_tags']."` VALUES
127    ('magic.Camera.Make', 0, 1),
128    ('magic.Camera.Model', 1, 1),
129    ('magic.ShotInfo.Lens', 2, 1),
130    ('magic.ShotInfo.Aperture', 3, 1),
131    ('magic.ShotInfo.Exposure', 4, 1),
132    ('magic.ShotInfo.ISO', 5, 1),
133    ('magic.ShotInfo.FocalLength', 6, 1),
134    ('magic.ShotInfo.FocalLengthIn35mm', 7, 1),
135    ('magic.ShotInfo.Flash.Fired', 8, 1)"
136      );
137      foreach($tablesInsert as $sql)
138      {
139        pwg_query($sql);
140      }
141
142      GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED);
143      return($result);
144    }
145
146
147    /*
148        function for uninstall process
149    */
150    public function uninstall()
151    {
152      $this->deleteConfig();
153      $this->tablef->drop();
154      GPCCore::unregister($this->getPluginName());
155    }
156
157    public function activate()
158    {
159      global $template, $user;
160
161      $this->initConfig();
162      $this->loadConfig();
163      if(method_exists($this, 'loadConfigFromFile'))
164      {
165        $this->loadConfigFromFile(dirname($this->getFileLocation()).'/activatePlugin.conf.php');
166      }
167
168      /*
169       * if there is no version information available, assume the previous
170       *  installed release of the plugin is 0.4.0
171       */
172      if(!isset($this->config['installed'])) $this->config['installed']='00.04.00';
173
174      switch($this->config['installed'])
175      {
176        case '00.04.00':
177          $this->config['newInstall']='n';
178          $this->updateFrom_000400();
179          break;
180        default:
181          /*
182           * default is applied for fresh install, and consist to fill the
183           * database with default values
184           */
185          $this->initializeDatabase();
186          break;
187      }
188
189      $this->config['amd_FillDataBaseExcludeTags']=array();
190      $this->config['installed']=AMD_VERSION2; //update the installed release number
191      $this->saveConfig();
192
193      GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED);
194      GPCRequestBuilder::register($this->getPluginName(), dirname($this->getFileLocation()).'/amd_rb_callback.class.inc.php');
195    }
196
197
198    public function deactivate()
199    {
200      GPCRequestBuilder::unregister($this->getPluginName());
201    }
202
203    /**
204     * update the database from the release 0.4.0
205     */
206    private function updateFrom_000400()
207    {
208      /*
209       * create new tables & alter existing tables
210       */
211      $tablesCreate=array(
212"CREATE TABLE `".$this->tables['user_tags_label']."` (
213  `numId` INTEGER UNSIGNED NOT NULL,
214  `lang` CHAR(5)  NOT NULL,
215  `label` VARCHAR(200)  NOT NULL,
216  PRIMARY KEY (`numId`, `lang`)
217);",
218"CREATE TABLE `".$this->tables['user_tags_def']."` (
219  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
220  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
221  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
222  `order` int(10) unsigned NOT NULL,
223  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
224  `value` varchar(200) NOT NULL,
225  `conditionType` char(2) NOT NULL default 'E',
226  `conditionValue` varchar(200) NOT NULL,
227  PRIMARY KEY  (`numId`,`defId`),
228  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
229  KEY `byTagOrder` (`numId`,`order`)
230);",
231      );
232      $tablesUpdate=array(
233        $this->tables['images_tags'] => array(
234          'byNumId' => "ADD INDEX `byNumId`(`numId`, `value`(35))",
235        )
236      );
237
238      $tablesDef = create_table_add_character_set($tablesCreate);
239
240      $tablef=new GPCTables(array($this->tables['user_tags_label'], $this->tables['user_tags_def']));
241
242      if(count($tablesCreate)>0) $tablef->create($tablesCreate);
243      if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate);
244
245      unset($tablesCreate);
246      unset($tablesUpdate);
247    }
248
249
250
251
252    /**
253     * fill the database with some default value
254     */
255    private function initializeDatabase()
256    {
257      global $user;
258
259      L10n::setLanguage('en_UK');
260
261      pwg_query("DELETE FROM ".$this->tables['used_tags']);
262      pwg_query("DELETE FROM ".$this->tables['images_tags']);
263      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
264      pwg_query("INSERT INTO ".$this->tables['images']."
265                  SELECT id, 'n', 0
266                    FROM ".IMAGES_TABLE."
267                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
268      /*
269       * fill the 'used_tags' table with default values
270       */
271      foreach(AMD_JpegMetaData::getTagList(Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED, 'xmp' => true, 'maker' => true, 'iptc' => true)) as $key => $val)
272      {
273        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."');";
274        pwg_query($sql);
275      }
276
277      /*
278       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
279       */
280      if(count($this->config['amd_FillDataBaseExcludeTags']))
281      {
282        $sql="";
283        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
284        {
285          if($sql!="") $sql.=" OR ";
286          $sql.=" tagId LIKE '$tag' ";
287        }
288        $sql="DELETE FROM ".$this->tables['used_tags']."
289              WHERE ".$sql;
290        pwg_query($sql);
291      }
292    }
293
294  } //class
295
296?>
Note: See TracBrowser for help on using the repository browser.