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

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

bug:1686, feature:1718, feature:1719, feature:1688, feature:1692

  • Picture analysis finish with an Error 500 or with a problem of memory limit
  • Coding a DateTime class
  • Make JpegMetadata class tests images lighter
  • Improve performance when the database is filled
  • Add possibility for user to build their own tags
  • ajax management entirely rewritted
  • Property svn:executable set to *
File size: 9.5 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->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  PRIMARY KEY  (`numId`),
63  KEY `by_tag` (`tagId`)
64);",
65"CREATE TABLE `".$this->tables['images_tags']."` (
66  `imageId` mediumint(8) unsigned NOT NULL default '0',
67  `numId` int(10) unsigned NOT NULL default '0',
68  `value` text default NULL,
69  PRIMARY KEY  USING BTREE (`imageId`,`numId`)
70);",
71"CREATE TABLE `".$this->tables['images']."` (
72  `imageId` MEDIUMINT(8) UNSIGNED NOT NULL,
73  `analyzed` CHAR(1)  NOT NULL DEFAULT 'n',
74  `nbTags` int(10) unsigned NOT NULL default '0',
75  PRIMARY KEY (`imageId`)
76);",
77"CREATE TABLE `".$this->tables['selected_tags']."` (
78  `tagId` VARCHAR(80)  NOT NULL,
79  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
80  `groupId` INTEGER  NOT NULL DEFAULT -1,
81  PRIMARY KEY (`tagId`)
82);",
83"CREATE TABLE `".$this->tables['groups_names']."` (
84  `groupId` INTEGER  NOT NULL,
85  `lang` CHAR(5)  NOT NULL,
86  `name` VARCHAR(80)  NOT NULL,
87  PRIMARY KEY (`groupId`, `lang`)
88);",
89"CREATE TABLE `".$this->tables['groups']."` (
90  `groupId` INTEGER  NOT NULL AUTO_INCREMENT,
91  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
92  PRIMARY KEY (`groupId`)
93);",
94"CREATE TABLE `".$this->tables['user_tags_label']."` (
95  `numId` INTEGER UNSIGNED NOT NULL,
96  `lang` CHAR(5)  NOT NULL,
97  `label` VARCHAR(200)  NOT NULL,
98  PRIMARY KEY (`numId`, `lang`)
99);",
100"CREATE TABLE `".$this->tables['user_tags_def']."` (
101  `defId` int(10) unsigned NOT NULL auto_increment,
102  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
103  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
104  `order` int(10) unsigned NOT NULL COMMENT 'Order, relative to the parent',
105  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
106  `value` varchar(200) NOT NULL,
107  `conditionType` char(2) NOT NULL default 'E',
108  `conditionValue` varchar(200) NOT NULL,
109  PRIMARY KEY  (`defId`),
110  KEY `byTagNumId` (`numId`,`parentId`,`order`),
111  KEY `byTagParentId` (`parentId`,`order`)
112);",
113      );
114
115
116      $tables_def = create_table_add_character_set($tables_def);
117      $result=$this->tablef->create($tables_def);
118      unset($tables_def);
119
120      $tables_insert=array(
121"INSERT INTO `".$this->tables['groups']."` VALUES(1, 0)",
122"INSERT INTO `".$this->tables['groups_names']."` VALUES(1, '".$user['language']."', '".$lang['g003_default_group_name']."')",
123"INSERT INTO `".$this->tables['selected_tags']."` VALUES
124    ('magic.Camera.Make', 0, 1),
125    ('magic.Camera.Model', 1, 1),
126    ('magic.ShotInfo.Lens', 2, 1),
127    ('magic.ShotInfo.Aperture', 3, 1),
128    ('magic.ShotInfo.Exposure', 4, 1),
129    ('magic.ShotInfo.ISO', 5, 1),
130    ('magic.ShotInfo.FocalLength', 6, 1),
131    ('magic.ShotInfo.FocalLengthIn35mm', 7, 1),
132    ('magic.ShotInfo.Flash.Fired', 8, 1)"
133      );
134      foreach($tables_insert as $sql)
135      {
136        pwg_query($sql);
137      }
138
139      return($result);
140    }
141
142
143    /*
144        function for uninstall process
145    */
146    public function uninstall()
147    {
148      $this->deleteConfig();
149      $this->tablef->drop();
150    }
151
152    public function activate()
153    {
154      global $template, $user;
155
156      $this->initConfig();
157      $this->loadConfig();
158
159      /*
160       * if there is no version information available, assume the previous
161       *  installed release of the plugin is 0.4.0
162       */
163      if(!isset($this->config['installed'])) $this->config['installed']='00.04.00';
164
165      switch($this->config['installed'])
166      {
167        case '00.04.00':
168          $this->updateFrom_000400();
169          break;
170        default:
171          /*
172           * default is applied for fresh install, and consist to fill the
173           * database with default values
174           */
175          $this->initializeDatabase();
176          break;
177      }
178
179      $this->config['installed']=AMD_VERSION2; //update the installed release number
180      $this->saveConfig();
181    }
182
183
184    public function deactivate()
185    {
186    }
187
188    /**
189     * update the database from the release 0.4.0
190     */
191    private function updateFrom_000400()
192    {
193      /*
194       * create new tables
195       */
196      $tableDef=array(
197"CREATE TABLE `".$this->tables['user_tags_label']."` (
198  `numId` INTEGER UNSIGNED NOT NULL,
199  `lang` CHAR(5)  NOT NULL,
200  `label` VARCHAR(200)  NOT NULL,
201  PRIMARY KEY (`numId`, `lang`)
202);",
203"CREATE TABLE `".$this->tables['user_tags_def']."` (
204  `defId` int(10) unsigned NOT NULL auto_increment,
205  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
206  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
207  `order` int(10) unsigned NOT NULL COMMENT 'Order, relative to the parent',
208  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
209  `value` varchar(200) NOT NULL,
210  `conditionType` char(2) NOT NULL default 'E',
211  `conditionValue` varchar(200) NOT NULL,
212  PRIMARY KEY  (`defId`),
213  KEY `byTagNumId` (`numId`,`parentId`,`order`),
214  KEY `byTagParentId` (`parentId`,`order`)
215);"
216      );
217      $tablesDef = create_table_add_character_set($tablesDef);
218      $result=$this->tablef->create($tablesDef);
219      unset($tablesDef);
220
221      /*
222       * update old tables
223       */
224
225      // no tables to update
226    }
227
228
229
230
231    /**
232     * fill the database with some default value
233     */
234    private function initializeDatabase()
235    {
236      L10n::setLanguage('en_UK');
237
238      pwg_query("DELETE FROM ".$this->tables['used_tags']);
239      pwg_query("DELETE FROM ".$this->tables['images_tags']);
240      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
241      pwg_query("INSERT INTO ".$this->tables['images']."
242                  SELECT id, 'n', 0
243                    FROM ".IMAGES_TABLE."
244                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
245      /*
246       * fill the 'used_tags' table with default values
247       */
248      foreach(AMD_JpegMetaData::getTagList(Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED, 'xmp' => true, 'maker' => true, 'iptc' => true)) as $key => $val)
249      {
250        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."');";
251        pwg_query($sql);
252      }
253
254      $listToAnalyze=Array(Array(), Array());
255      /*
256       * select 25 pictures into the caddie
257       */
258      $sql="SELECT ti.id, ti.path
259            FROM ".CADDIE_TABLE." tc
260              LEFT JOIN ".IMAGES_TABLE." ti ON ti.id = tc.element_id
261            WHERE tc.user_id = ".$user['id']."
262              AND ti.id IS NOT NULL
263            ORDER BY RAND() LIMIT 25;";
264      $result=pwg_query($sql);
265      if($result)
266      {
267        while($row=pwg_db_fetch_assoc($result))
268        {
269          $listToAnalyze[0][]=$row;
270          $listToAnalyze[1][]=$row['id'];
271        }
272      }
273      /*
274       * if caddie is empty, of is have less than 25 pictures, select other
275       * pictures from the gallery
276       */
277      if(count($listToAnalyze[0])<25)
278      {
279        if(count($listToAnalyze[0])>0)
280        {
281          $excludeList="WHERE ti.id NOT IN(".implode(",", $listToAnalyze[1]).") ";
282        }
283        else
284        {
285          $excludeList="";
286        }
287        $sql="SELECT ti.id, ti.path
288              FROM ".IMAGES_TABLE." ti ".$excludeList."
289              ORDER BY RAND() LIMIT ".(25-count($listToAnalyze[0])).";";
290        $result=pwg_query($sql);
291        if($result)
292        {
293          while($row=pwg_db_fetch_assoc($result))
294          {
295            $listToAnalyze[0][]=$row;
296          }
297        }
298      }
299
300      /*
301       * analyze the 25 selected pictures
302       */
303      if(count($listToAnalyze[0])>0)
304      {
305        // $path = path of piwigo's on the server filesystem
306        $path=dirname(dirname(dirname(__FILE__)));
307
308        foreach($listToAnalyze[0] as $val)
309        {
310          $this->analyzeImageFile($path."/".$val['path'], $val['id']);
311        }
312
313        $this->makeStatsConsolidation();
314      }
315    }
316
317  } //class
318
319?>
Note: See TracBrowser for help on using the repository browser.