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

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

Implement basic/advanced modes, release 0.5.1
bug:1857

  • Property svn:executable set to *
File size: 8.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  include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/tables.class.inc.php');
23
24
25  class AMD_install extends AMD_root
26  {
27    private $tablef;
28
29    public function __construct($prefixeTable, $filelocation)
30    {
31      parent::__construct($prefixeTable, $filelocation);
32      $this->tablef= new GPCTables($this->tables);
33    }
34
35    public function __destruct()
36    {
37      unset($this->tablef);
38      parent::__destruct();
39    }
40
41    /*
42     * function for installation process
43     * return true if install process is ok, otherwise false
44     */
45    public function install()
46    {
47      global $user, $lang;
48
49      $this->initConfig();
50      $this->loadConfig();
51      $this->config['installed']=AMD_VERSION2;
52      $this->config['newInstall']='y';
53      $this->saveConfig();
54
55      $tables_def=array(
56"CREATE TABLE `".$this->tables['used_tags']."` (
57  `numId` int(10) unsigned NOT NULL auto_increment,
58  `tagId` varchar(80) NOT NULL default '',
59  `translatable` char(1) NOT NULL default 'n',
60  `name` varchar(200) NOT NULL default '',
61  `numOfImg` int(10) unsigned NOT NULL default '0',
62  `translatedName` varchar(200) NOT NULL default '',
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"INSERT INTO `".$this->tables['groups_names']."` VALUES(1, '".$user['language']."', '".$lang['g003_default_group_name']."')",
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
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    }
157
158    public function activate()
159    {
160      global $template, $user;
161
162      $this->initConfig();
163      $this->loadConfig();
164      $this->loadConfigFromFile(dirname($this->getFileLocation()).'/activatePlugin.conf.php');
165
166      /*
167       * if there is no version information available, assume the previous
168       *  installed release of the plugin is 0.4.0
169       */
170      if(!isset($this->config['installed'])) $this->config['installed']='00.04.00';
171
172      switch($this->config['installed'])
173      {
174        case '00.04.00':
175          $this->updateFrom_000400();
176          break;
177        default:
178          /*
179           * default is applied for fresh install, and consist to fill the
180           * database with default values
181           */
182          $this->initializeDatabase();
183          break;
184      }
185
186      $this->config['amd_FillDataBaseExcludeTags']=array();
187      $this->config['installed']=AMD_VERSION2; //update the installed release number
188      $this->saveConfig();
189    }
190
191
192    public function deactivate()
193    {
194    }
195
196    /**
197     * update the database from the release 0.4.0
198     */
199    private function updateFrom_000400()
200    {
201      /*
202       * create new tables & alter existing tables
203       */
204      $tableDef=array(
205"CREATE TABLE `".$this->tables['user_tags_label']."` (
206  `numId` INTEGER UNSIGNED NOT NULL,
207  `lang` CHAR(5)  NOT NULL,
208  `label` VARCHAR(200)  NOT NULL,
209  PRIMARY KEY (`numId`, `lang`)
210);",
211"CREATE TABLE `".$this->tables['user_tags_def']."` (
212  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
213  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
214  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
215  `order` int(10) unsigned NOT NULL,
216  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
217  `value` varchar(200) NOT NULL,
218  `conditionType` char(2) NOT NULL default 'E',
219  `conditionValue` varchar(200) NOT NULL,
220  PRIMARY KEY  (`numId`,`defId`),
221  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
222  KEY `byTagOrder` (`numId`,`order`)
223);",
224"ALTER TABLE `".$this->tables['images_tags']."`
225  ADD COLUMN `numValue` DECIMAL(10,8)  DEFAULT NULL AFTER `value`,
226  ADD INDEX `byNumId`(`numId`, `value`(35)),
227  ADD INDEX `byNumId2`(`numId`, `numValue`)
228;"
229      );
230      $tablesDef = create_table_add_character_set($tablesDef);
231      $result=$this->tablef->create($tablesDef);
232      unset($tablesDef);
233
234      /*
235       * update old tables
236       */
237
238      // no tables to update
239    }
240
241
242
243
244    /**
245     * fill the database with some default value
246     */
247    private function initializeDatabase()
248    {
249      global $user;
250
251      L10n::setLanguage('en_UK');
252
253      pwg_query("DELETE FROM ".$this->tables['used_tags']);
254      pwg_query("DELETE FROM ".$this->tables['images_tags']);
255      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
256      pwg_query("INSERT INTO ".$this->tables['images']."
257                  SELECT id, 'n', 0
258                    FROM ".IMAGES_TABLE."
259                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
260      /*
261       * fill the 'used_tags' table with default values
262       */
263      foreach(AMD_JpegMetaData::getTagList(Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED, 'xmp' => true, 'maker' => true, 'iptc' => true)) as $key => $val)
264      {
265        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."');";
266        pwg_query($sql);
267      }
268
269      /*
270       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
271       */
272      if(count($this->config['amd_FillDataBaseExcludeTags']))
273      {
274        $sql="";
275        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
276        {
277          if($sql!="") $sql.=" OR ";
278          $sql.=" tagId LIKE '$tag' ";
279        }
280        $sql="DELETE FROM ".$this->tables['used_tags']."
281              WHERE ".$sql;
282        pwg_query($sql);
283      }
284    }
285
286  } //class
287
288?>
Note: See TracBrowser for help on using the repository browser.