1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * This class is used to expose maintenance methods to the plugins manager |
---|
6 | * It must extends PluginMaintain and be named "PLUGINID_maintain" |
---|
7 | * where PLUGINID is the directory name of your plugin |
---|
8 | */ |
---|
9 | class skeleton_maintain extends PluginMaintain |
---|
10 | { |
---|
11 | /* |
---|
12 | * My pattern uses a single installation method, which handles both installation |
---|
13 | * and activation, where Piwigo always calls 'activate' just after 'install' |
---|
14 | * As a result I use a marker in order to not execute the installation method twice |
---|
15 | * |
---|
16 | * The installation function is called by main.inc.php and maintain.inc.php |
---|
17 | * in order to install and/or update the plugin. |
---|
18 | * |
---|
19 | * That's why all operations must be conditionned : |
---|
20 | * - use "if empty" for configuration vars |
---|
21 | * - use "IF NOT EXISTS" for table creation |
---|
22 | */ |
---|
23 | private $installed = false; |
---|
24 | |
---|
25 | /** |
---|
26 | * plugin installation |
---|
27 | * |
---|
28 | * perform here all needed step for the plugin installation |
---|
29 | * such as create default config, add database tables, |
---|
30 | * add fields to existing tables, create local folders... |
---|
31 | */ |
---|
32 | function install($plugin_version, &$errors=array()) |
---|
33 | { |
---|
34 | global $conf, $prefixeTable; |
---|
35 | |
---|
36 | // add config parameter |
---|
37 | if (empty($conf['skeleton'])) |
---|
38 | { |
---|
39 | $conf['skeleton'] = serialize(array( |
---|
40 | 'option1' => 10, |
---|
41 | 'option2' => true, |
---|
42 | 'option3' => 'two', |
---|
43 | )); |
---|
44 | |
---|
45 | conf_update_param('skeleton', $conf['skeleton']); |
---|
46 | } |
---|
47 | else |
---|
48 | { |
---|
49 | // if you need to test the "old" configuration you must check if not yet unserialized |
---|
50 | $old_conf = is_string($conf['skeleton']) ? unserialize($conf['skeleton']) : $conf['skeleton']; |
---|
51 | |
---|
52 | if (empty($old_conf['option3'])) |
---|
53 | { // use case: this parameter was added in a new version |
---|
54 | $old_conf['option3'] = 'two'; |
---|
55 | } |
---|
56 | |
---|
57 | $conf['skeleton'] = serialize($old_conf); |
---|
58 | conf_update_param('skeleton', $conf['skeleton']); |
---|
59 | } |
---|
60 | |
---|
61 | // add a new table |
---|
62 | pwg_query(' |
---|
63 | CREATE TABLE IF NOT EXISTS `'. $prefixeTable .'skeleton` ( |
---|
64 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
---|
65 | `field1` mediumint(8) DEFAULT NULL, |
---|
66 | `field2` varchar(64) NOT NULL, |
---|
67 | PRIMARY KEY (`id`) |
---|
68 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
69 | ;'); |
---|
70 | |
---|
71 | // add a new column to existing table |
---|
72 | $result = pwg_query('SHOW COLUMNS FROM `'.IMAGES_TABLE.'` LIKE "skeleton";'); |
---|
73 | if (!pwg_db_num_rows($result)) |
---|
74 | { |
---|
75 | pwg_query('ALTER TABLE `' . IMAGES_TABLE . '` ADD `skeleton` TINYINT(1) NOT NULL DEFAULT 0;'); |
---|
76 | } |
---|
77 | |
---|
78 | // create a local directory |
---|
79 | if (!file_exists(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/')) |
---|
80 | { |
---|
81 | mkdir(PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/', 0755); |
---|
82 | } |
---|
83 | |
---|
84 | $this->installed = true; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * plugin activation |
---|
89 | * |
---|
90 | * this function is triggered after installation, by manual activation |
---|
91 | * or after a plugin update |
---|
92 | * for this last case you must manage updates tasks of your plugin in this function |
---|
93 | */ |
---|
94 | function activate($plugin_version, &$errors=array()) |
---|
95 | { |
---|
96 | if (!$this->installed) |
---|
97 | { |
---|
98 | $this->install($plugin_version, $errors); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * plugin deactivation |
---|
104 | * |
---|
105 | * triggered before uninstallation or by manual deactivation |
---|
106 | */ |
---|
107 | function deactivate() |
---|
108 | { |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * plugin uninstallation |
---|
113 | * |
---|
114 | * perform here all cleaning tasks when the plugin is removed |
---|
115 | * you should revert all changes made by 'install' |
---|
116 | */ |
---|
117 | function uninstall() |
---|
118 | { |
---|
119 | global $prefixeTable; |
---|
120 | |
---|
121 | // delete configuration |
---|
122 | conf_delete_param('skeleton'); |
---|
123 | |
---|
124 | // delete table |
---|
125 | pwg_query('DROP TABLE `'. $prefixeTable .'skeleton`;'); |
---|
126 | |
---|
127 | // delete field |
---|
128 | pwg_query('ALTER TABLE `'. IMAGES_TABLE .'` DROP `skeleton`;'); |
---|
129 | |
---|
130 | // delete local folder |
---|
131 | // use a recursive function if you plan to have nested directories |
---|
132 | $dir = PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'skeleton/'; |
---|
133 | foreach (scandir($dir) as $file) |
---|
134 | { |
---|
135 | if ($file == '.' or $file == '..') continue; |
---|
136 | unlink($dir.$file); |
---|
137 | } |
---|
138 | rmdir($dir); |
---|
139 | } |
---|
140 | } |
---|