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

Last change on this file since 6891 was 6891, checked in by grum, 10 years ago

Implement metadata search, release 0.5.1
bug:1846, bug:1691

  • Property svn:executable set to *
File size: 10.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->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,
69  `numValue` decimal(10,8) default NULL,
70  PRIMARY KEY  USING BTREE (`imageId`,`numId`),
71  KEY `byNumId` (`numId`,`value`(35)),
72  KEY `byNumId2` (`numId`,`numValue`)
73);",
74"CREATE TABLE `".$this->tables['images']."` (
75  `imageId` MEDIUMINT(8) UNSIGNED NOT NULL,
76  `analyzed` CHAR(1)  NOT NULL DEFAULT 'n',
77  `nbTags` int(10) unsigned NOT NULL default '0',
78  PRIMARY KEY (`imageId`)
79);",
80"CREATE TABLE `".$this->tables['selected_tags']."` (
81  `tagId` VARCHAR(80)  NOT NULL,
82  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
83  `groupId` INTEGER  NOT NULL DEFAULT -1,
84  PRIMARY KEY (`tagId`)
85);",
86"CREATE TABLE `".$this->tables['groups_names']."` (
87  `groupId` INTEGER  NOT NULL,
88  `lang` CHAR(5)  NOT NULL,
89  `name` VARCHAR(80)  NOT NULL,
90  PRIMARY KEY (`groupId`, `lang`)
91);",
92"CREATE TABLE `".$this->tables['groups']."` (
93  `groupId` INTEGER  NOT NULL AUTO_INCREMENT,
94  `order` INTEGER UNSIGNED NOT NULL DEFAULT 0,
95  PRIMARY KEY (`groupId`)
96);",
97"CREATE TABLE `".$this->tables['user_tags_label']."` (
98  `numId` INTEGER UNSIGNED NOT NULL,
99  `lang` CHAR(5)  NOT NULL,
100  `label` VARCHAR(200)  NOT NULL,
101  PRIMARY KEY (`numId`, `lang`)
102);",
103"CREATE TABLE `".$this->tables['user_tags_def']."` (
104  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
105  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
106  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
107  `order` int(10) unsigned NOT NULL,
108  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
109  `value` varchar(200) NOT NULL,
110  `conditionType` char(2) NOT NULL default 'E',
111  `conditionValue` varchar(200) NOT NULL,
112  PRIMARY KEY  (`numId`,`defId`),
113  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
114  KEY `byTagOrder` (`numId`,`order`)
115);",
116      );
117
118
119      $tables_def = create_table_add_character_set($tables_def);
120      $result=$this->tablef->create($tables_def);
121      unset($tables_def);
122
123      $tables_insert=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($tables_insert as $sql)
138      {
139        pwg_query($sql);
140      }
141
142      return($result);
143    }
144
145
146    /*
147        function for uninstall process
148    */
149    public function uninstall()
150    {
151      $this->deleteConfig();
152      $this->tablef->drop();
153    }
154
155    public function activate()
156    {
157      global $template, $user;
158
159      $this->initConfig();
160      $this->loadConfig();
161      $this->loadConfigFromFile(dirname($this->getFileLocation()).'/activatePlugin.conf.php');
162
163      /*
164       * if there is no version information available, assume the previous
165       *  installed release of the plugin is 0.4.0
166       */
167      if(!isset($this->config['installed'])) $this->config['installed']='00.04.00';
168
169      switch($this->config['installed'])
170      {
171        case '00.04.00':
172          $this->updateFrom_000400();
173          break;
174        default:
175          /*
176           * default is applied for fresh install, and consist to fill the
177           * database with default values
178           */
179          $this->initializeDatabase();
180          break;
181      }
182
183      $this->config['amd_FillDataBaseExcludeTags']=array();
184      $this->config['installed']=AMD_VERSION2; //update the installed release number
185      $this->saveConfig();
186    }
187
188
189    public function deactivate()
190    {
191    }
192
193    /**
194     * update the database from the release 0.4.0
195     */
196    private function updateFrom_000400()
197    {
198      /*
199       * create new tables & alter existing tables
200       */
201      $tableDef=array(
202"CREATE TABLE `".$this->tables['user_tags_label']."` (
203  `numId` INTEGER UNSIGNED NOT NULL,
204  `lang` CHAR(5)  NOT NULL,
205  `label` VARCHAR(200)  NOT NULL,
206  PRIMARY KEY (`numId`, `lang`)
207);",
208"CREATE TABLE `".$this->tables['user_tags_def']."` (
209  `numId` int(10) unsigned NOT NULL COMMENT 'Id of the tag',
210  `defId` int(10) unsigned NOT NULL default '0' COMMENT 'also used for ordering',
211  `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent',
212  `order` int(10) unsigned NOT NULL,
213  `type` char(1) NOT NULL default 'T' COMMENT 'T = static text ; M = metadata value ; C = condition',
214  `value` varchar(200) NOT NULL,
215  `conditionType` char(2) NOT NULL default 'E',
216  `conditionValue` varchar(200) NOT NULL,
217  PRIMARY KEY  (`numId`,`defId`),
218  KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`),
219  KEY `byTagOrder` (`numId`,`order`)
220);",
221"ALTER TABLE `".$this->tables['images_tags']."`
222  ADD COLUMN `numValue` DECIMAL(10,8)  DEFAULT NULL AFTER `value`,
223  ADD INDEX `byNumId`(`numId`, `value`(35)),
224  ADD INDEX `byNumId2`(`numId`, `numValue`)
225;"
226      );
227      $tablesDef = create_table_add_character_set($tablesDef);
228      $result=$this->tablef->create($tablesDef);
229      unset($tablesDef);
230
231      /*
232       * update old tables
233       */
234
235      // no tables to update
236    }
237
238
239
240
241    /**
242     * fill the database with some default value
243     */
244    private function initializeDatabase()
245    {
246      global $user;
247
248      L10n::setLanguage('en_UK');
249
250      pwg_query("DELETE FROM ".$this->tables['used_tags']);
251      pwg_query("DELETE FROM ".$this->tables['images_tags']);
252      pwg_query("UPDATE ".$this->tables['images']." SET analyzed='n', nbTags=0;");
253      pwg_query("INSERT INTO ".$this->tables['images']."
254                  SELECT id, 'n', 0
255                    FROM ".IMAGES_TABLE."
256                    WHERE id NOT IN (SELECT imageId FROM ".$this->tables['images'].")");
257      /*
258       * fill the 'used_tags' table with default values
259       */
260      foreach(AMD_JpegMetaData::getTagList(Array('filter' => AMD_JpegMetaData::TAGFILTER_IMPLEMENTED, 'xmp' => true, 'maker' => true, 'iptc' => true)) as $key => $val)
261      {
262        $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."');";
263        pwg_query($sql);
264      }
265
266      /*
267       * exclude unauthorized tag with the 'amd_FillDataBaseExcludeTags' option
268       */
269      if(count($this->config['amd_FillDataBaseExcludeTags']))
270      {
271        $sql="";
272        foreach($this->config['amd_FillDataBaseExcludeTags'] as $key => $tag)
273        {
274          if($sql!="") $sql.=" OR ";
275          $sql.=" tagId LIKE '$tag' ";
276        }
277        $sql="DELETE FROM ".$this->tables['used_tags']."
278              WHERE ".$sql;
279        pwg_query($sql);
280      }
281
282      $listToAnalyze=Array(Array(), Array());
283      /*
284       * select 25 pictures into the caddie
285       */
286      $sql="SELECT ti.id, ti.path
287            FROM ".CADDIE_TABLE." tc
288              LEFT JOIN ".IMAGES_TABLE." ti ON ti.id = tc.element_id
289            WHERE tc.user_id = ".$user['id']."
290              AND ti.id IS NOT NULL
291            ORDER BY RAND() LIMIT 25;";
292      $result=pwg_query($sql);
293      if($result)
294      {
295        while($row=pwg_db_fetch_assoc($result))
296        {
297          $listToAnalyze[0][]=$row;
298          $listToAnalyze[1][]=$row['id'];
299        }
300      }
301      /*
302       * if caddie is empty, of is have less than 25 pictures, select other
303       * pictures from the gallery
304       */
305      if(count($listToAnalyze[0])<25)
306      {
307        if(count($listToAnalyze[0])>0)
308        {
309          $excludeList="WHERE ti.id NOT IN(".implode(",", $listToAnalyze[1]).") ";
310        }
311        else
312        {
313          $excludeList="";
314        }
315        $sql="SELECT ti.id, ti.path, ti.has_high
316              FROM ".IMAGES_TABLE." ti ".$excludeList."
317              ORDER BY RAND() LIMIT ".(25-count($listToAnalyze[0])).";";
318        $result=pwg_query($sql);
319        if($result)
320        {
321          while($row=pwg_db_fetch_assoc($result))
322          {
323            $listToAnalyze[0][]=$row;
324          }
325        }
326      }
327
328      /*
329       * analyze the 25 selected pictures
330       */
331      if(count($listToAnalyze[0])>0)
332      {
333        // $path = path of piwigo's on the server filesystem
334        $path=dirname(dirname(dirname(__FILE__)));
335
336        foreach($listToAnalyze[0] as $val)
337        {
338          if($val['has_high']===true and $this->config['amd_UseMetaFromHD']=='y')
339          {
340            $this->analyzeImageFile($path."/".dirname($val['path'])."/pwg_high/".basename($val['path']), $val['id']);
341          }
342          else
343          {
344            $this->analyzeImageFile($path."/".$val['path'], $val['id']);
345          }
346
347        }
348
349        $this->makeStatsConsolidation();
350      }
351    }
352
353  } //class
354
355?>
Note: See TracBrowser for help on using the repository browser.