source: extensions/menalto2piwigo/admin.php @ 28827

Last change on this file since 28827 was 28827, checked in by plg, 10 years ago

credits to dschwen and his Perl script

File size: 11.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
22if( !defined("PHPWG_ROOT_PATH") )
23{
24  die ("Hacking attempt!");
25}
26
27include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
28include_once(M2P_PATH.'include/functions.inc.php');
29
30$admin_base_url = get_root_url().'admin.php?page=plugin-menalto2piwigo';
31load_language('plugin.lang', dirname(__FILE__).'/');
32
33// +-----------------------------------------------------------------------+
34// | Check Access and exit when user status is not ok                      |
35// +-----------------------------------------------------------------------+
36
37check_status(ACCESS_WEBMASTER);
38
39// +-----------------------------------------------------------------------+
40// | load database config for Menalto                                      |
41// +-----------------------------------------------------------------------+
42
43if (isset($conf['menalto2piwigo']))
44{
45  $conf['menalto2piwigo'] = unserialize($conf['menalto2piwigo']);
46}
47else
48{
49  $conf['menalto2piwigo'] = array(
50    'db_host' => $conf['db_host'],
51    'db_user' => $conf['db_user'],
52    'db_password' => $conf['db_password'],
53    'db_name' => '',
54    'prefix_table' => 'g2_',
55    'prefix_column' => 'g_',
56    );
57}
58
59// +-----------------------------------------------------------------------+
60// | import data from Menalto                                              |
61// +-----------------------------------------------------------------------+
62
63if (isset($_POST['submit']))
64{
65  foreach (array_keys($conf['menalto2piwigo']) as $key)
66  {
67    $conf['menalto2piwigo'][$key] = $_POST[$key];
68  }
69 
70  conf_update_param('menalto2piwigo', serialize($conf['menalto2piwigo']));
71 
72  $pt = $conf['menalto2piwigo']['prefix_table'];
73  $pc = $conf['menalto2piwigo']['prefix_column'];
74
75  // build an associative array like
76  //
77  // $piwigo_paths = array(
78  //   [album 1] => c2
79  //   [album 1/20081220_173438-014.jpg] => i1
80  //   [album 1/20081220_173603-017.jpg] => i2
81  //   [album 1/sub-album 1.1] => c3
82  //   [album 1/sub-album 1.1/1.1.1] => c4
83  //   [album 1/sub-album 1_2] => c5
84  //   [album 2] => c1
85  //   [album 2/20091011_164753-127.jpg] => i3
86  //   [album 2/20091102_172719-190.jpg] => i4
87  // );
88  //
89  // cN for categories, iN for images
90  $piwigo_paths = array();
91
92  $query = '
93SELECT
94    id,
95    REPLACE(path, "./galleries/", "") AS filepath
96  FROM '.IMAGES_TABLE.'
97  WHERE path like "./galleries/%"
98;';
99  $result = pwg_query($query);
100  while ($row = pwg_db_fetch_assoc($result))
101  {
102    $piwigo_paths[$row['filepath']] = 'i'.$row['id'];
103  }
104
105  $query = '
106SELECT
107    id,
108    dir,
109    uppercats
110  FROM '.CATEGORIES_TABLE.'
111  WHERE site_id = 1
112;';
113  $albums = query2array($query, 'id');
114
115  foreach ($albums as $id => $album)
116  {
117    $path_tokens = array();
118    foreach (explode(',', $album['uppercats']) as $uppercat_id)
119    {
120      $path_tokens[] = $albums[$uppercat_id]['dir'];
121    }
122   
123    $albums[$id]['path'] = implode('/', $path_tokens);
124  }
125
126  foreach ($albums as $id => $album)
127  {
128    $piwigo_paths[$album['path']] = 'c'.$id;
129  }
130  unset($albums);
131
132  ksort($piwigo_paths);
133  // echo '<pre>'; print_r($piwigo_paths); echo '</pre>';
134
135  m2p_db_connect();
136
137  // Gallery2 parent Ids (root is always 7!)
138  $ids = array(7,0,0,0,0,0);
139  // Piwigo uppercats
140  $uct = array('NULL',0,0,0,0,0);
141  $ranks = array();
142
143  // the following algorithm is a conversion into PHP of the Perl script
144  // convertcomments.pl by dschwen, see https://github.com/dschwen/g2piwigo
145  //
146  // this plugin just makes things "simpler" for users but the hard part
147  // comes from dschwen, he deserves all credits!
148 
149  foreach ($piwigo_paths as $dir => $piwigo_id)
150  {
151    $path = explode('/', $dir);
152    $basename = $path[count($path)-1];
153    $level = count($path);
154
155    $parentId = $ids[$level-1];
156
157    // get id and title/summary/description of tail element in path
158    $query = "
159SELECT
160    f.".$pc."id,
161    i.".$pc."title,
162    i.".$pc."summary,
163    i.".$pc."description,
164    i.".$pc."canContainChildren,
165    a.".$pc."orderWeight,
166    a.".$pc."viewCount,
167    FROM_UNIXTIME(e.".$pc."creationTimestamp)
168  FROM ".$pt."Item i
169    JOIN ".$pt."FileSystemEntity f ON i.".$pc."id = f.".$pc."id
170    JOIN ".$pt."ChildEntity c ON f.".$pc."id = c.".$pc."id
171    JOIN ".$pt."ItemAttributesMap a ON i.".$pc."id = a.".$pc."itemId
172    JOIN ".$pt."Entity e ON e.".$pc."id = i.".$pc."id
173  WHERE c.".$pc."parentId = ".$parentId."
174    AND f.".$pc."pathComponent='".$basename."'
175;";
176    // echo '<pre>'.$query."</pre>";
177    $row = pwg_db_fetch_row(pwg_query($query));
178   
179    // print "$row[4] - $parentId -> $row[0] : $row[1] $row[2] $row[3]\n";
180    $title = m2p_remove_bbcode($row[1]);
181    $summary = m2p_remove_bbcode($row[2]);
182    $description = m2p_remove_bbcode($row[3]);
183    $weight = $row[5];
184    $views = $row[6];
185    $date_available = $row[7];
186    $ids[$level] = $row[0];
187    $pid[$row[0]] = $dir;
188
189    if ($row[4] == 0)
190    {
191      // Menalto says it's an image
192
193      if (strpos($piwigo_id, 'i') === false)
194      {
195        echo 'Error, '.$piwig_id.' is not an image and Menalto says it is an image';
196      }
197     
198      $comment = "";
199      if ( $summary != "" and $description != "" )
200      {
201        $comment = "<b>$summary</b> - $description";
202      }
203      else
204      {
205        if ($summary != "")
206        {
207          $comment = $summary;
208        }
209        else
210        {
211          $comment = $description;
212        }
213      }
214
215      $image_id = substr($piwigo_id, 1);
216     
217      $image_updates[] = array(
218        'id' => $image_id,
219        'name' => pwg_db_real_escape_string($title),
220        'comment' => pwg_db_real_escape_string($comment),
221        'date_available' => $date_available,
222        );
223     
224      // build a map from gallery2 ids to piwigo image ids
225      $iid[$row[0]] = $image_id;
226    }
227    else
228    {
229      // album (folder)
230      if (strpos($piwigo_id, 'c') === false)
231      {
232        echo 'Error, '.$piwig_id.' is not an album and Menalto says it is an album';
233      }
234     
235      $comment = "";
236      if ( $summary != "" and $description != "" )
237      {
238        $comment = "$summary <!--complete--> $description";
239      }
240      else
241      {
242        if ($summary != "")
243        {
244          $comment = $summary;
245        }
246        else
247        {
248          $comment = "<!--complete-->$description";
249        }
250      }
251
252      // get piwigo category id
253      $cat_id = substr($piwigo_id, 1);
254      $uct[$level] = $cat_id;
255     
256      $cat_updates[] = array(
257        'id' => $cat_id,
258        'name' => pwg_db_real_escape_string($title),
259        'comment' => pwg_db_real_escape_string($comment),
260        'rank' => $weight,
261        );
262
263      // get highlight picture
264      $query = "
265SELECT d2.".$pc."derivativeSourceId
266  FROM ".$pt."ChildEntity c
267    JOIN ".$pt."Derivative d1 ON c.".$pc."id = d1.".$pc."id
268    JOIN ".$pt."Derivative d2 ON d1.".$pc."derivativeSourceId=d2.".$pc."id
269  WHERE c.".$pc."parentId = ".$ids[$level];
270      $subresult = pwg_query($query);
271      $subrow = pwg_db_fetch_row($subresult);
272      $hid[$cat_id] = $subrow[0];
273    }
274  }
275
276  // apply highlites as representative images
277  foreach ($hid as $cat_id => $menalto_id)
278  {
279    if (!empty($menalto_id))
280    {
281      $album_thumbs[] = array(
282        'id' => $cat_id,
283        'representative_picture_id' => $iid[$menalto_id],
284        );
285    }
286  }
287
288  // copy comments
289  $query = "
290SELECT
291    c.".$pc."parentId AS id,
292    t.".$pc."subject AS subject,
293    t.".$pc."comment AS comment,
294    t.".$pc."author AS author,
295    FROM_UNIXTIME(t.".$pc."date) AS date
296  FROM ".$pt."ChildEntity c
297    JOIN ".$pt."Comment t ON t.".$pc."id = c.".$pc."id
298  WHERE t.".$pc."publishStatus=0
299";
300  $result = pwg_query($query);
301  while ($row = pwg_db_fetch_assoc($result))
302  {
303    if (isset($iid[ $row['id'] ]))
304    {
305      $comment = $row['comment'];
306      if (!empty($row['subject']))
307      {
308        $comment = '[b]'.$row['subject'].'[/b] '.$comment;
309      }
310
311      $comment_inserts[] = array(
312        'image_id' => $iid[ $row['id'] ],
313        'date' => $row['date'],
314        'author' => pwg_db_real_escape_string($row['author']),
315        'content' => pwg_db_real_escape_string($comment),
316        'validated' => 'true',
317        );
318    }
319  }
320
321  m2p_db_disconnect();
322
323  // echo '<pre>'; print_r($image_updates); echo '</pre>';
324  // echo '<pre>'; print_r($cat_updates); echo '</pre>';
325  // echo '<pre>'; print_r($hid); echo '</pre>';
326  // echo '<pre>'; print_r($iid); echo '</pre>';
327  // echo '<pre>'; print_r($album_thumbs); echo '</pre>';
328  // echo '<pre>'; print_r($comment_inserts); echo '</pre>';
329 
330  mass_updates(
331    IMAGES_TABLE,
332    array(
333      'primary' => array('id'),
334      'update'  => array('name', 'comment', 'date_available'),
335      ),
336    $image_updates
337    );
338
339  mass_updates(
340    CATEGORIES_TABLE,
341    array(
342      'primary' => array('id'),
343      'update'  => array('name', 'comment', 'rank'),
344      ),
345    $cat_updates
346    );
347
348  mass_updates(
349    CATEGORIES_TABLE,
350    array(
351      'primary' => array('id'),
352      'update'  => array('representative_picture_id'),
353      ),
354    $album_thumbs
355    );
356
357  mass_inserts(
358    COMMENTS_TABLE,
359    array_keys($comment_inserts[0]),
360    $comment_inserts
361    );
362 
363  array_push($page['infos'], l10n('Information data registered in database'));
364}
365
366
367// +-----------------------------------------------------------------------+
368// | Template & Form                                                       |
369// +-----------------------------------------------------------------------+
370
371$template->set_filenames(
372  array(
373    'plugin_admin_content' => dirname(__FILE__).'/admin.tpl'
374    )
375  );
376
377$template->assign('action_url', $admin_base_url);
378$template->assign($conf['menalto2piwigo']);
379
380// +-----------------------------------------------------------------------+
381// | Sending html                                                          |
382// +-----------------------------------------------------------------------+
383
384$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
385?>
Note: See TracBrowser for help on using the repository browser.