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

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

Update for Piwigo 2.6
TODO: button on Admin Tools bar

File size: 7.2 KB
Line 
1<?php
2defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
3
4class gvideo_maintain extends PluginMaintain
5{
6  private $installed = false;
7 
8  private $default_conf = array(
9    'autoplay' => 0,
10    'width' => 640,
11    'height' => 360,
12    'sync_description' => 1,
13    'sync_tags' => 1,
14    'vimeo' => array(
15      'title' => 1,
16      'portrait' => 1,
17      'byline' => 1,
18      'color' => '00adef',
19      ),
20    'dailymotion' => array(
21      'logo' => 1,
22      'title' => 1,
23      'color' => 'F7FFFD',
24      ),
25    'youtube' => array(),
26    'wat' => array(),
27    'wideo' => array(),
28    );
29   
30  private $table;
31 
32  function __construct($plugin_id)
33  {
34    global $prefixeTable;
35   
36    parent::__construct($plugin_id);
37   
38    $this->table = $prefixeTable . 'image_video';
39  }
40
41  function install($plugin_version, &$errors=array())
42  {
43    global $conf;
44
45    // add config parameter
46    if (empty($conf['gvideo']))
47    {
48      $conf['gvideo'] = serialize($this->default_conf);
49      conf_update_param('gvideo', $conf['gvideo']);
50    }
51    else
52    {
53      $old_conf = is_string($conf['gvideo']) ? unserialize($conf['gvideo']) : $conf['gvideo'];
54
55      if (!isset($old_conf['sync_description']))
56      {
57        $old_conf['sync_description'] = 1;
58        $old_conf['sync_tags'] = 1;
59       
60        $conf['gvideo'] = serialize($old_conf);
61        conf_update_param('gvideo', $conf['gvideo']);
62      }
63    }
64
65    // create table
66  $query = '
67CREATE TABLE IF NOT EXISTS `' . $this->table . '` (
68  `picture_id` mediumint(8) NOT NULL,
69  `url` varchar(255) DEFAULT NULL,
70  `type` varchar(64) NOT NULL,
71  `video_id` varchar(128) NOT NULL,
72  `width` smallint(9) DEFAULT NULL,
73  `height` smallint(9) DEFAULT NULL,
74  `autoplay` tinyint(1) DEFAULT NULL
75) ENGINE=MyISAM DEFAULT CHARSET=utf8
76;';
77    pwg_query($query);
78   
79    // update video_id lenght
80    $query = 'ALTER TABLE `' . $this->table . '` CHANGE `video_id` `video_id` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;';
81    pwg_query($query);
82   
83    // new collumn in images table
84    $result = pwg_query('SHOW COLUMNS FROM `' . IMAGES_TABLE . '` LIKE "is_gvideo";');
85    if (!pwg_db_num_rows($result))
86    {
87      pwg_query('ALTER TABLE `' . IMAGES_TABLE . '` ADD `is_gvideo` TINYINT(1) NOT NULL DEFAULT 0;');
88     
89      $query = '
90UPDATE `' . IMAGES_TABLE . '`
91  SET is_gvideo = 1
92  WHERE id IN(
93    SELECT picture_id FROM `' . $this->table . '`
94    )
95;';
96      pwg_query($query);
97    }
98   
99    // remove old configuration
100    if (isset($conf['PY_GVideo']))
101    {
102      conf_delete_param('PY_GVideo');
103    }
104   
105    // updade video files
106    $this->update_24();
107
108    $this->installed = true;
109  }
110
111  function activate($plugin_version, &$errors=array())
112  {
113    if (!$this->installed)
114    {
115      $this->install($plugin_version, $errors);
116    }
117  }
118
119  function deactivate()
120  {
121  }
122
123  function uninstall()
124  {
125    conf_delete_param('gvideo');
126
127    pwg_query('DROP TABLE `' . $this->table . '`;');
128
129    pwg_query('ALTER TABLE `'. IMAGES_TABLE .'` DROP `is_gvideo`;');
130  }
131 
132  /**
133   * update from 2.3 to 2.4
134   */
135  function update_24()
136  {
137    global $conf;
138   
139    // search existing videos
140  $query = '
141SELECT *
142  FROM '.IMAGES_TABLE.'
143  WHERE
144    file LIKE "%.gvideo"
145    OR file LIKE "%.dm"
146    OR file LIKE "%.ytube"
147    OR file LIKE "%.wideo"
148    OR file LIKE "%.vimeo"
149    OR file LIKE "%.wat"
150;';
151    $result = pwg_query($query);
152   
153    if (!pwg_db_num_rows($result))
154    {
155      return;
156    }
157   
158    if (!isset($conf['prefix_thumbnail']))
159    {
160      $conf['prefix_thumbnail'] = 'TN-';
161    }
162
163    if (!isset($conf['dir_thumbnail']))
164    {
165      $conf['dir_thumbnail'] = 'thumbnail';
166    }
167   
168    set_time_limit(600);
169    include_once(gvideo_path . 'include/functions.inc.php');
170    include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
171   
172    $videos_inserts = array();
173    $images_updates = array();
174    $images_delete = array();
175   
176    while ($img = pwg_db_fetch_assoc($result))
177    {
178      $file_content = file_get_contents($img['path']);
179      list($file['id'], $file['height'], $file['width'], ) = explode('/', $file_content);
180      $file['type'] = get_extension($img['path']);
181     
182      switch ($file['type'])
183      {
184        case 'vimeo':
185          $video = array(
186            'type' => 'vimeo',
187            'url' => 'http://vimeo.com/'.$file['id'],
188            );
189          break;
190        case 'dm':
191          $video = array(
192            'type' => 'dailymotion',
193            'url' => 'http://dailymotion.com/video/'.$file['id'],
194            );
195          break;
196        case 'ytube':
197          $video = array(
198            'type' => 'youtube',
199            'url' => 'http://youtube.com/watch?v='.$file['id'],
200            );
201          break;
202        case 'wideo':
203          $video = array(
204            'type' => 'wideo',
205            'url' => 'http://wideo.fr/video/'.$file['id'].'.html',
206            );
207          break;
208        case 'wat':
209          $video = array(
210            'type' => 'wat',
211            'url' => null,
212            );
213          break;
214        case 'gvideo': // closed
215        default:
216          $images_delete[] = $img['id'];
217          continue;
218      }
219     
220      $real_path = str_replace($img['file'], null, str_replace('././', './', $img['path']));
221     
222      // get existing thumbnail
223      $thumb = $real_path.$conf['dir_thumbnail'].'/'.$conf['prefix_thumbnail'].get_filename_wo_extension($img['file']).'.*';
224      $thumb = glob($thumb);
225      if (!empty($thumb))
226      {
227        $thumb_name = $video['type'].'-'.$file['id'].'-'.uniqid().'.'.get_extension($thumb[0]);
228        $thumb_source = $conf['data_location'].$thumb_name;
229        copy($thumb[0], $thumb_source);
230      }
231      else
232      {
233        $thumb_name = $video['type'].'-'.$file['id'].'-'.uniqid().'.jpg';
234        $thumb_source = $conf['data_location'].$thumb_name;
235        copy(gvideo_path.'mimetypes/'.$video['type'].'.jpg', $thumb_source);
236        add_film_frame($thumb_source);
237      }
238     
239      // update element
240      $image_id = add_uploaded_file($thumb_source, $thumb_name, null, null, $img['id']);
241     
242      // update path and rename the file
243      $img['new_path'] = $real_path.$thumb_name;
244      rename($img['path'], $img['new_path']); // why ? what's the purpose of this line ?
245     
246      $images_updates[] = array(
247        'id' => $img['id'],
248        'path' => $img['new_path'],
249        'is_gvideo' => 1,
250        );
251     
252      if (empty($file['width'])) $file['width'] = '';
253      if (empty($file['height'])) $file['height'] = '';
254     
255      // register video   
256      $videos_inserts[] = array(
257        'picture_id' => $image_id,
258        'url' => $video['url'],
259        'type' => $video['type'],
260        'video_id' => $file['id'],
261        'width' => $file['width'],
262        'height' => $file['height'],
263        'autoplay' => '',
264        );
265       
266      unset($thumb_source, $thumb_name, $file, $video, $url);
267    }
268   
269    // delete obsolete elements
270    delete_elements($images_delete);
271   
272    // registers videos
273    mass_inserts(
274      gvideo_table,
275      array('picture_id', 'url', 'type', 'video_id', 'width', 'height', 'autoplay'),
276      $videos_inserts
277      );
278     
279    // update images
280    mass_updates(
281      IMAGES_TABLE,
282      array('primary'=>array('id'), 'update'=>array('path', 'is_gvideo')),
283      $images_updates
284      );
285  }
286}
Note: See TracBrowser for help on using the repository browser.