[4905] | 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 | |
---|
[6891] | 21 | include_once('amd_root.class.inc.php'); |
---|
[4905] | 22 | |
---|
| 23 | class AMD_install extends AMD_root |
---|
| 24 | { |
---|
| 25 | private $tablef; |
---|
| 26 | |
---|
[5183] | 27 | public function __construct($prefixeTable, $filelocation) |
---|
[4905] | 28 | { |
---|
| 29 | parent::__construct($prefixeTable, $filelocation); |
---|
[5935] | 30 | $this->tablef= new GPCTables($this->tables); |
---|
[4905] | 31 | } |
---|
| 32 | |
---|
[5183] | 33 | public function __destruct() |
---|
| 34 | { |
---|
| 35 | unset($this->tablef); |
---|
| 36 | parent::__destruct(); |
---|
| 37 | } |
---|
| 38 | |
---|
[4905] | 39 | /* |
---|
| 40 | * function for installation process |
---|
| 41 | * return true if install process is ok, otherwise false |
---|
| 42 | */ |
---|
| 43 | public function install() |
---|
| 44 | { |
---|
[5040] | 45 | global $user, $lang; |
---|
| 46 | |
---|
[5935] | 47 | $this->initConfig(); |
---|
| 48 | $this->loadConfig(); |
---|
[7553] | 49 | $this->config['amd_FillDataBaseIgnoreSchemas']=array('exif', 'iptc', 'xmp', 'com'); |
---|
[5935] | 50 | $this->config['installed']=AMD_VERSION2; |
---|
[6919] | 51 | $this->config['newInstall']='y'; |
---|
[5935] | 52 | $this->saveConfig(); |
---|
| 53 | |
---|
[4905] | 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', |
---|
[5226] | 61 | `translatedName` varchar(200) NOT NULL default '', |
---|
[7519] | 62 | `newFromLastUpdate` char(1) NOT NULL default 'n', |
---|
[4905] | 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', |
---|
[6891] | 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`) |
---|
[4905] | 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 | );", |
---|
[4906] | 81 | "CREATE TABLE `".$this->tables['selected_tags']."` ( |
---|
[4905] | 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`) |
---|
[6722] | 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']."` ( |
---|
[6730] | 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', |
---|
[6722] | 107 | `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent', |
---|
[6730] | 108 | `order` int(10) unsigned NOT NULL, |
---|
[6722] | 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, |
---|
[6730] | 113 | PRIMARY KEY (`numId`,`defId`), |
---|
| 114 | KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`), |
---|
| 115 | KEY `byTagOrder` (`numId`,`order`) |
---|
[6722] | 116 | );", |
---|
| 117 | ); |
---|
| 118 | |
---|
| 119 | |
---|
[4905] | 120 | $tables_def = create_table_add_character_set($tables_def); |
---|
[5935] | 121 | $result=$this->tablef->create($tables_def); |
---|
[5040] | 122 | unset($tables_def); |
---|
[4905] | 123 | |
---|
[6919] | 124 | |
---|
| 125 | $tablesInsert=array( |
---|
[5040] | 126 | "INSERT INTO `".$this->tables['groups']."` VALUES(1, 0)", |
---|
[7399] | 127 | $this->buildDefaultGroup(), |
---|
[5040] | 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 | ); |
---|
[6919] | 139 | foreach($tablesInsert as $sql) |
---|
[5040] | 140 | { |
---|
| 141 | pwg_query($sql); |
---|
| 142 | } |
---|
| 143 | |
---|
[6977] | 144 | GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED); |
---|
[4905] | 145 | return($result); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | /* |
---|
| 150 | function for uninstall process |
---|
| 151 | */ |
---|
| 152 | public function uninstall() |
---|
| 153 | { |
---|
[5935] | 154 | $this->deleteConfig(); |
---|
| 155 | $this->tablef->drop(); |
---|
[6977] | 156 | GPCCore::unregister($this->getPluginName()); |
---|
[4905] | 157 | } |
---|
| 158 | |
---|
| 159 | public function activate() |
---|
| 160 | { |
---|
[5191] | 161 | global $template, $user; |
---|
[6722] | 162 | |
---|
| 163 | $this->initConfig(); |
---|
| 164 | $this->loadConfig(); |
---|
[6977] | 165 | if(method_exists($this, 'loadConfigFromFile')) |
---|
| 166 | { |
---|
| 167 | $this->loadConfigFromFile(dirname($this->getFileLocation()).'/activatePlugin.conf.php'); |
---|
| 168 | } |
---|
[6722] | 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': |
---|
[6977] | 179 | $this->config['newInstall']='n'; |
---|
[6722] | 180 | $this->updateFrom_000400(); |
---|
[7519] | 181 | case '00.05.01': |
---|
| 182 | case '00.05.02': |
---|
| 183 | $this->config['newInstall']='n'; |
---|
| 184 | $this->updateFrom_000502(); |
---|
[7563] | 185 | case '00.05.03': |
---|
| 186 | $this->updateFrom_000503(); |
---|
[6722] | 187 | default: |
---|
| 188 | /* |
---|
| 189 | * default is applied for fresh install, and consist to fill the |
---|
| 190 | * database with default values |
---|
| 191 | */ |
---|
[7519] | 192 | $this->fillDatabase(); |
---|
[6722] | 193 | break; |
---|
| 194 | } |
---|
| 195 | |
---|
[6729] | 196 | $this->config['amd_FillDataBaseExcludeTags']=array(); |
---|
[6722] | 197 | $this->config['installed']=AMD_VERSION2; //update the installed release number |
---|
| 198 | $this->saveConfig(); |
---|
[6977] | 199 | |
---|
| 200 | GPCCore::register($this->getPluginName(), AMD_VERSION, AMD_GPC_NEEDED); |
---|
[7563] | 201 | GPCRequestBuilder::register('AMetaData', dirname($this->getFileLocation()).'/amd_rb_callback.class.inc.php'); |
---|
[6722] | 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | public function deactivate() |
---|
| 206 | { |
---|
[7563] | 207 | GPCRequestBuilder::unregister('AMetaData'); |
---|
[6722] | 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * update the database from the release 0.4.0 |
---|
| 212 | */ |
---|
| 213 | private function updateFrom_000400() |
---|
| 214 | { |
---|
| 215 | /* |
---|
[6891] | 216 | * create new tables & alter existing tables |
---|
[6722] | 217 | */ |
---|
[6977] | 218 | $tablesCreate=array( |
---|
[6722] | 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']."` ( |
---|
[6730] | 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', |
---|
[6722] | 228 | `parentId` int(10) unsigned NOT NULL default '0' COMMENT 'Id of the parent', |
---|
[6730] | 229 | `order` int(10) unsigned NOT NULL, |
---|
[6722] | 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, |
---|
[6730] | 234 | PRIMARY KEY (`numId`,`defId`), |
---|
| 235 | KEY `byTagParentId` USING BTREE (`numId`,`parentId`,`order`), |
---|
| 236 | KEY `byTagOrder` (`numId`,`order`) |
---|
[6891] | 237 | );", |
---|
[6722] | 238 | ); |
---|
[6977] | 239 | $tablesUpdate=array( |
---|
| 240 | $this->tables['images_tags'] => array( |
---|
| 241 | 'byNumId' => "ADD INDEX `byNumId`(`numId`, `value`(35))", |
---|
| 242 | ) |
---|
| 243 | ); |
---|
[6722] | 244 | |
---|
[6977] | 245 | $tablesDef = create_table_add_character_set($tablesCreate); |
---|
[6722] | 246 | |
---|
[6977] | 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); |
---|
[6722] | 254 | } |
---|
| 255 | |
---|
[7519] | 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 | ); |
---|
[6722] | 269 | |
---|
[7519] | 270 | $tablef=new GPCTables(array($this->tables['used_tags'])); |
---|
[6722] | 271 | |
---|
[7519] | 272 | if(count($tablesUpdate)>0) $tablef->updateTablesFields($tablesUpdate); |
---|
[6722] | 273 | |
---|
[7519] | 274 | unset($tablesUpdate); |
---|
| 275 | } |
---|
| 276 | |
---|
[7563] | 277 | /** |
---|
| 278 | * update the database from the release 0.5.3 |
---|
| 279 | */ |
---|
| 280 | private function updateFrom_000503() |
---|
| 281 | { |
---|
| 282 | GPCRequestBuilder::unregister('Advanced MetaData'); |
---|
| 283 | } |
---|
[7519] | 284 | |
---|
[7563] | 285 | |
---|
[6722] | 286 | /** |
---|
| 287 | * fill the database with some default value |
---|
| 288 | */ |
---|
[7519] | 289 | private function fillDatabase() |
---|
[6722] | 290 | { |
---|
[7519] | 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 | { |
---|
[6729] | 306 | global $user; |
---|
| 307 | |
---|
[5226] | 308 | L10n::setLanguage('en_UK'); |
---|
[4905] | 309 | |
---|
[4931] | 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;"); |
---|
[5183] | 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'].")"); |
---|
[4931] | 317 | /* |
---|
| 318 | * fill the 'used_tags' table with default values |
---|
| 319 | */ |
---|
[7519] | 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 | ) |
---|
[4931] | 328 | { |
---|
[7519] | 329 | $sql="INSERT INTO ".$this->tables['used_tags']." VALUES('', '".$key."', '".(($val['translatable'])?'y':'n')."', '".$val['name']."', 0, '".addslashes(L10n::get($val['name']))."', 'n');"; |
---|
[4931] | 330 | pwg_query($sql); |
---|
| 331 | } |
---|
| 332 | |
---|
[6729] | 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 | } |
---|
[4905] | 348 | } |
---|
| 349 | |
---|
[7519] | 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 | |
---|
[7399] | 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 | |
---|
[4905] | 427 | } //class |
---|
| 428 | |
---|
| 429 | ?> |
---|