source: extensions/BatchDownloader/maintain.inc.php @ 27153

Last change on this file since 27153 was 26645, checked in by mistic100, 10 years ago

cant' define class properties as computed value (here, time() unexpected)

File size: 5.1 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4class BatchDownloader_maintain extends PluginMaintain
5{
6  private $installed = false;
7
8  function install($plugin_version, &$errors=array())
9  {
10    global $conf, $prefixeTable;
11
12    // configuration
13    if (empty($conf['batch_download']))
14    {
15      $batch_download_default_config = array(
16        'groups'          => array(),
17        'level'           => 0,
18        'what'            => array('categories','specials','collections'),
19        'photo_size'      => 'original',
20        'multisize'       => true,
21        'archive_prefix'  => 'piwigo',
22        'archive_timeout' => 48, /* hours */
23        'max_elements'    => 500,
24        'max_size'        => 100, /* MB */
25        'last_clean'      => time(),
26        'one_archive'     => false,
27        'force_pclzip'    => false,
28        'direct'          => false,
29        );
30
31      $conf['batch_download'] = serialize($batch_download_default_config);
32      $conf['batch_download_comment'] = null;
33
34      conf_update_param('batch_download', $conf['batch_download']);
35      conf_update_param('batch_download_comment', $conf['batch_download_comment']);
36    }
37    else
38    {
39      $new_conf = is_string($conf['batch_download']) ? unserialize($conf['batch_download']) : $conf['batch_download'];
40
41      if (!isset($new_conf['what']))
42      {
43        $new_conf['what'] = array('categories','specials','collections');
44      }
45      if (!isset($new_conf['one_archive']))
46      {
47        $new_conf['one_archive'] = false;
48        $new_conf['force_pclzip'] = isset($conf['batch_download_force_pclzip']) && $conf['batch_download_force_pclzip'];
49        $new_conf['direct'] = isset($conf['batch_download_direct']) && $conf['batch_download_direct'];
50      }
51      if (!isset($new_conf['multisize']))
52      {
53        $new_conf['multisize'] = true;
54      }
55
56      $conf['batch_download'] = serialize($new_conf);
57      conf_update_param('batch_download', $conf['batch_download']);
58    }
59
60    // archives directory
61    if (!file_exists(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/'))
62    {
63      mkgetdir(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/', MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR);
64    }
65
66    // create tables
67    $query = '
68CREATE TABLE IF NOT EXISTS `' . $prefixeTable . 'download_sets` (
69  `id` mediumint(8) NOT NULL AUTO_INCREMENT,
70  `user_id` smallint(5) NOT NULL,
71  `date_creation` datetime NOT NULL,
72  `type` varchar(16) NOT NULL,
73  `type_id` varchar(64) NOT NULL,
74  `size` varchar(16) NOT NULL DEFAULT "original",
75  `nb_zip` smallint(3) NOT NULL DEFAULT 0,
76  `last_zip` smallint(3) NOT NULL DEFAULT 0,
77  `nb_images` mediumint(8) NOT NULL DEFAULT 0,
78  `total_size` int(10) NOT NULL DEFAULT 0,
79  `status` enum("new","ready","download","done") NOT NULL DEFAULT "new",
80  PRIMARY KEY (`id`)
81) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
82;';
83    pwg_query($query);
84
85    $query = '
86CREATE TABLE IF NOT EXISTS `' . $prefixeTable . 'download_sets_images` (
87  `set_id` mediumint(8) NOT NULL,
88  `image_id` mediumint(8) NOT NULL,
89  `zip` smallint(5) NOT NULL DEFAULT 0,
90  UNIQUE KEY `UNIQUE` (`set_id`,`image_id`)
91) ENGINE=MyISAM DEFAULT CHARSET=utf8
92;';
93    pwg_query($query);
94
95    $query = '
96CREATE TABLE IF NOT EXISTS `' . $prefixeTable . 'image_sizes` (
97  `image_id` mediumint(8) NOT NULL,
98  `type` varchar(16) NOT NULL,
99  `width` smallint(9) NOT NULL,
100  `height` smallint(9) NOT NULL,
101  `filesize` mediumint(9) NOT NULL,
102  `filemtime` int(16) NOT NULL,
103  PRIMARY KEY (`image_id`)
104) ENGINE=MyISAM DEFAULT CHARSET=utf8
105;';
106    pwg_query($query);
107
108    // add a "size" column to download_sets
109    $result = pwg_query('SHOW COLUMNS FROM `' . $prefixeTable . 'download_sets` LIKE "size";');
110    if (!pwg_db_num_rows($result))
111    {
112      pwg_query('ALTER TABLE `' . $prefixeTable . 'download_sets` ADD `size` varchar(16) NOT NULL DEFAULT "original";');
113    }
114
115    // add "ready" status
116    pwg_query('ALTER TABLE `' . $prefixeTable . 'download_sets` CHANGE `status` `status` enum("new","ready","download","done") NOT NULL DEFAULT "new";');
117   
118    $this->installed = true;
119  }
120
121  function activate($plugin_version, &$errors=array())
122  {
123    if (!$this->installed)
124    {
125      $this->install($plugin_version, $errors);
126    }
127  }
128
129  function deactivate()
130  {
131  }
132
133  function uninstall()
134  {
135    global $prefixeTable, $conf;
136
137    conf_delete_param('batch_download');
138
139    pwg_query('DROP TABLE IF EXISTS `' . $prefixeTable . 'download_sets`;');
140    pwg_query('DROP TABLE IF EXISTS `' . $prefixeTable . 'download_sets_images`;');
141
142    self::rrmdir(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/');
143  }
144
145  static function rrmdir($dir)
146  {
147    if (!is_dir($dir))
148    {
149      return false;
150    }
151    $dir = rtrim($dir, '/');
152    $objects = scandir($dir);
153    $return = true;
154
155    foreach ($objects as $object)
156    {
157      if ($object !== '.' && $object !== '..')
158      {
159        $path = $dir.'/'.$object;
160        if (filetype($path) == 'dir')
161        {
162          $return = $return && self::rrmdir($path);
163        }
164        else
165        {
166          $return = $return && @unlink($path);
167        }
168      }
169    }
170
171    return $return && @rmdir($dir);
172  }
173}
Note: See TracBrowser for help on using the repository browser.