source: extensions/community/maintain.inc.php @ 9539

Last change on this file since 9539 was 9539, checked in by plg, 13 years ago

convert data (pendings and permissions) from Piwigo core 2.1

convert permissions from Community 2.1

File size: 7.4 KB
Line 
1<?php
2
3if (!defined("COMMUNITY_PATH"))
4{
5  define('COMMUNITY_PATH', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)));
6}
7
8function plugin_install()
9{
10  global $conf, $prefixeTable;
11
12  if ('mysql' == $conf['dblayer'])
13  {
14    $query = '
15CREATE TABLE '.$prefixeTable.'community_permissions (
16  id int(11) NOT NULL AUTO_INCREMENT,
17  type varchar(255) NOT NULL,
18  group_id smallint(5) unsigned DEFAULT NULL,
19  user_id smallint(5) DEFAULT NULL,
20  category_id smallint(5) unsigned DEFAULT NULL,
21  recursive enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
22  create_subcategories enum(\'true\',\'false\') NOT NULL DEFAULT \'false\',
23  moderated enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
24  PRIMARY KEY (id)
25) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
26;';
27    pwg_query($query);
28
29    $query = '
30CREATE TABLE '.$prefixeTable.'community_pendings (
31  image_id mediumint(8) unsigned NOT NULL,
32  state varchar(255) NOT NULL,
33  added_on datetime NOT NULL,
34  validated_by smallint(5) DEFAULT NULL
35) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
36;';
37    pwg_query($query);
38  }
39  elseif ('pgsql' == $conf['dblayer'])
40  {
41    $query = '
42CREATE TABLE "'.$prefixeTable.'community_permissions" (
43  "id" serial NOT NULL,
44  "type" VARCHAR(255) NOT NULL,
45  "group_id" INTEGER,
46  "user_id" INTEGER,
47  "category_id" INTEGER,
48  "recursive" BOOLEAN default true,
49  "create_subcategories" BOOLEAN default false,
50  "moderated" BOOLEAN default true,
51  PRIMARY KEY ("id")
52)
53;';
54    pwg_query($query);
55
56    $query = '
57CREATE TABLE "'.$prefixeTable.'community_pendings" (
58  image_id INTEGER NOT NULL,
59  state VARCHAR(255) NOT NULL,
60  added_on TIMESTAMP NOT NULL,
61  validated_by INTEGER
62)
63;';
64    pwg_query($query);
65  }
66  else
67  {
68    $query = '
69CREATE TABLE "'.$prefixeTable.'community_permissions" (
70  "id" INTEGER NOT NULL,
71  "type" VARCHAR(255) NOT NULL,
72  "group_id" INTEGER,
73  "user_id" INTEGER,
74  "category_id" INTEGER,
75  "recursive" BOOLEAN default true,
76  "create_subcategories" BOOLEAN default false,
77  "moderated" BOOLEAN default true,
78  PRIMARY KEY ("id")
79)
80;';
81    pwg_query($query);
82
83    $query = '
84CREATE TABLE "'.$prefixeTable.'community_pendings" (
85  image_id INTEGER NOT NULL,
86  state VARCHAR(255) NOT NULL,
87  added_on TIMESTAMP NOT NULL,
88  validated_by INTEGER
89)
90;';
91    pwg_query($query);
92  }
93}
94
95function plugin_uninstall()
96{
97  global $prefixeTable;
98 
99  $query = 'DROP TABLE '.$prefixeTable.'community_permissions;';
100  pwg_query($query);
101
102  $query = 'DROP TABLE '.$prefixeTable.'community_pendings;';
103  pwg_query($query);
104}
105
106function plugin_activate()
107{
108  community_get_data_from_core21();
109  community_get_data_from_community21();
110}
111
112function community_get_data_from_core21()
113{
114  global $conf, $prefixeTable;
115 
116  $from_piwigo21_file = $conf['local_data_dir'].'/plugins/core_user_upload_to_community.php';
117  if (is_file($from_piwigo21_file))
118  {
119    include($from_piwigo21_file);
120    $user_upload_conf = unserialize($user_upload_conf);
121   
122    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
123    prepare_upload_configuration();
124
125    $user_upload_conf['upload_user_access'] = 1;
126
127    if (isset($user_upload_conf['uploadable_categories']) and is_array($user_upload_conf['uploadable_categories']))
128    {
129      $type = 'any_registered_user';
130      if (isset($user_upload_conf['upload_user_access']) and 1 == $user_upload_conf['upload_user_access'])
131      {
132        $type = 'any_visitor';
133      }
134
135      $inserts = array();
136   
137      foreach ($user_upload_conf['uploadable_categories'] as $category_id)
138      {
139        array_push(
140          $inserts,
141          array(
142            'type' => $type,
143            'category_id' => $category_id,
144            'recursive' => 'false',
145            'create_subcategories' => 'false',
146            'moderated' => 'true',
147            )
148          );
149      }
150     
151      if (count($inserts) > 0)
152      {
153        mass_inserts(
154          $prefixeTable.'community_permissions',
155          array_keys($inserts[0]),
156          $inserts
157          );
158      }
159    }
160   
161    if (isset($user_upload_conf['waiting_rows']) and is_array($user_upload_conf['waiting_rows']))
162    {
163      $id_of_user = array();
164     
165      $query = '
166SELECT
167    '.$conf['user_fields']['id'].' AS id,
168    '.$conf['user_fields']['username'].' AS username
169  FROM '.USERS_TABLE.'
170;';
171      $result = pwg_query($query);
172      while ($row = pwg_db_fetch_assoc($result))
173      {
174        $id_of_user[ $row['username'] ] = $row['id'];
175      }
176     
177      $inserts = array();
178     
179      foreach ($user_upload_conf['waiting_rows'] as $pending)
180      {
181        $source_path = get_complete_dir($pending['storage_category_id']).$pending['file'];
182       
183        if (is_file($source_path))
184        {
185          $image_id = add_uploaded_file($source_path, $pending['file'], array($pending['storage_category_id']), 16);
186         
187          array_push(
188            $inserts,
189            array(
190              'image_id' => $image_id,
191              'added_on' => date ('Y-m-d H:i:s', $pending['date']),
192              'state' => 'moderation_pending',
193              )
194            );
195
196          $data = array();
197         
198          if (isset($pending['username']) and isset($id_of_user[ $pending['username'] ]))
199          {
200            $data['added_by'] = $id_of_user[ $pending['username'] ];
201          }
202         
203          foreach (array('date_creation', 'author', 'name', 'comment') as $field)
204          {
205            $value = getAttribute($pending['infos'], $field);
206            if (!empty($value))
207            {
208            $data[$field] = pwg_db_real_escape_string($value);
209            }
210          }
211         
212          if (count($data) > 0)
213          {
214            $data['id'] = $image_id;
215           
216            mass_updates(
217              IMAGES_TABLE,
218              array(
219                'primary' => array('id'),
220                'update'  => array_keys($data)
221                ),
222              array($data)
223              );
224          }
225         
226          // deletion
227          unlink($source_path);
228          if (!isset($pending['tn_ext']))
229          {
230            $pending['tn_ext'] = 'jpg';
231          }
232          @unlink(get_thumbnail_path(array('path'=>$source_path, 'tn_ext'=>$pending['tn_ext'])));
233        }
234      }
235     
236      if (count($inserts) > 0)
237      {
238        mass_inserts(
239          $prefixeTable.'community_pendings',
240          array_keys($inserts[0]),
241          $inserts
242          );
243      }
244    }
245    unlink($from_piwigo21_file);
246  }
247}
248
249function community_get_data_from_community21()
250{
251  global $prefixeTable;
252 
253  $old_community_table = $prefixeTable.'community';
254  $query = 'SHOW TABLES;';
255  $result = pwg_query($query);
256  while ($row = pwg_db_fetch_row($result))
257  {
258    if ($old_community_table == $row[0])
259    {
260      $inserts = array();
261     
262      $query = '
263SELECT
264    *
265  FROM '.$old_community_table.'
266;';
267      $result = pwg_query($query);
268      while ($row = pwg_db_fetch_assoc($result))
269      {
270        array_push(
271          $inserts,
272          array(
273            'type' => 'user',
274            'user_id' => $row['user_id'],
275            'category_id' => null,
276            'recursive' => 'true',
277            'create_subcategories' => $row['permission_level'] == 2 ? 'true' : 'false',
278            'moderated' => 'false',
279            )
280          );
281      }
282     
283      if (count($inserts) > 0)
284      {
285        mass_inserts(
286          $prefixeTable.'community_permissions',
287          array_keys($inserts[0]),
288          $inserts
289          );
290      }
291     
292      $query = 'DROP TABLE '.$old_community_table.';';
293      pwg_query($query);
294     
295      break;
296    }
297  }
298}
299?>
Note: See TracBrowser for help on using the repository browser.