source: extensions/menalto2piwigo/admin.php @ 28825

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

import data from Menalto Gallery

File size: 10.9 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  foreach ($piwigo_paths as $dir => $piwigo_id)
144  {
145    $path = explode('/', $dir);
146    $basename = $path[count($path)-1];
147    $level = count($path);
148
149    $parentId = $ids[$level-1];
150
151    // get id and title/summary/description of tail element in path
152    $query = "
153SELECT
154    f.".$pc."id,
155    i.".$pc."title,
156    i.".$pc."summary,
157    i.".$pc."description,
158    i.".$pc."canContainChildren,
159    a.".$pc."orderWeight,
160    a.".$pc."viewCount,
161    FROM_UNIXTIME(e.".$pc."creationTimestamp)
162  FROM ".$pt."Item i
163    JOIN ".$pt."FileSystemEntity f ON i.".$pc."id = f.".$pc."id
164    JOIN ".$pt."ChildEntity c ON f.".$pc."id = c.".$pc."id
165    JOIN ".$pt."ItemAttributesMap a ON i.".$pc."id = a.".$pc."itemId
166    JOIN ".$pt."Entity e ON e.".$pc."id = i.".$pc."id
167  WHERE c.".$pc."parentId = ".$parentId."
168    AND f.".$pc."pathComponent='".$basename."'
169;";
170    // echo '<pre>'.$query."</pre>";
171    $row = pwg_db_fetch_row(pwg_query($query));
172   
173    // print "$row[4] - $parentId -> $row[0] : $row[1] $row[2] $row[3]\n";
174    $title = m2p_remove_bbcode($row[1]);
175    $summary = m2p_remove_bbcode($row[2]);
176    $description = m2p_remove_bbcode($row[3]);
177    $weight = $row[5];
178    $views = $row[6];
179    $date_available = $row[7];
180    $ids[$level] = $row[0];
181    $pid[$row[0]] = $dir;
182
183    if ($row[4] == 0)
184    {
185      // Menalto says it's an image
186
187      if (strpos($piwigo_id, 'i') === false)
188      {
189        echo 'Error, '.$piwig_id.' is not an image and Menalto says it is an image';
190      }
191     
192      $comment = "";
193      if ( $summary != "" and $description != "" )
194      {
195        $comment = "<b>$summary</b> - $description";
196      }
197      else
198      {
199        if ($summary != "")
200        {
201          $comment = $summary;
202        }
203        else
204        {
205          $comment = $description;
206        }
207      }
208
209      $image_id = substr($piwigo_id, 1);
210     
211      $image_updates[] = array(
212        'id' => $image_id,
213        'name' => pwg_db_real_escape_string($title),
214        'comment' => pwg_db_real_escape_string($comment),
215        'date_available' => $date_available,
216        );
217     
218      // build a map from gallery2 ids to piwigo image ids
219      $iid[$row[0]] = $image_id;
220    }
221    else
222    {
223      // album (folder)
224      if (strpos($piwigo_id, 'c') === false)
225      {
226        echo 'Error, '.$piwig_id.' is not an album and Menalto says it is an album';
227      }
228     
229      $comment = "";
230      if ( $summary != "" and $description != "" )
231      {
232        $comment = "$summary <!--complete--> $description";
233      }
234      else
235      {
236        if ($summary != "")
237        {
238          $comment = $summary;
239        }
240        else
241        {
242          $comment = "<!--complete-->$description";
243        }
244      }
245
246      // get piwigo category id
247      $cat_id = substr($piwigo_id, 1);
248      $uct[$level] = $cat_id;
249     
250      $cat_updates[] = array(
251        'id' => $cat_id,
252        'name' => pwg_db_real_escape_string($title),
253        'comment' => pwg_db_real_escape_string($comment),
254        'rank' => $weight,
255        );
256
257      // get highlight picture
258      $query = "
259SELECT d2.".$pc."derivativeSourceId
260  FROM ".$pt."ChildEntity c
261    JOIN ".$pt."Derivative d1 ON c.".$pc."id = d1.".$pc."id
262    JOIN ".$pt."Derivative d2 ON d1.".$pc."derivativeSourceId=d2.".$pc."id
263  WHERE c.".$pc."parentId = ".$ids[$level];
264      $subresult = pwg_query($query);
265      $subrow = pwg_db_fetch_row($subresult);
266      $hid[$cat_id] = $subrow[0];
267    }
268  }
269
270  // apply highlites as representative images
271  foreach ($hid as $cat_id => $menalto_id)
272  {
273    if (!empty($menalto_id))
274    {
275      $album_thumbs[] = array(
276        'id' => $cat_id,
277        'representative_picture_id' => $iid[$menalto_id],
278        );
279    }
280  }
281
282  // copy comments
283  $query = "
284SELECT
285    c.".$pc."parentId AS id,
286    t.".$pc."subject AS subject,
287    t.".$pc."comment AS comment,
288    t.".$pc."author AS author,
289    FROM_UNIXTIME(t.".$pc."date) AS date
290  FROM ".$pt."ChildEntity c
291    JOIN ".$pt."Comment t ON t.".$pc."id = c.".$pc."id
292  WHERE t.".$pc."publishStatus=0
293";
294  $result = pwg_query($query);
295  while ($row = pwg_db_fetch_assoc($result))
296  {
297    if (isset($iid[ $row['id'] ]))
298    {
299      $comment = $row['comment'];
300      if (!empty($row['subject']))
301      {
302        $comment = '[b]'.$row['subject'].'[/b] '.$comment;
303      }
304
305      $comment_inserts[] = array(
306        'image_id' => $iid[ $row['id'] ],
307        'date' => $row['date'],
308        'author' => pwg_db_real_escape_string($row['author']),
309        'content' => pwg_db_real_escape_string($comment),
310        'validated' => 'true',
311        );
312    }
313  }
314
315  m2p_db_disconnect();
316
317  // echo '<pre>'; print_r($image_updates); echo '</pre>';
318  // echo '<pre>'; print_r($cat_updates); echo '</pre>';
319  // echo '<pre>'; print_r($hid); echo '</pre>';
320  // echo '<pre>'; print_r($iid); echo '</pre>';
321  // echo '<pre>'; print_r($album_thumbs); echo '</pre>';
322  // echo '<pre>'; print_r($comment_inserts); echo '</pre>';
323 
324  mass_updates(
325    IMAGES_TABLE,
326    array(
327      'primary' => array('id'),
328      'update'  => array('name', 'comment', 'date_available'),
329      ),
330    $image_updates
331    );
332
333  mass_updates(
334    CATEGORIES_TABLE,
335    array(
336      'primary' => array('id'),
337      'update'  => array('name', 'comment', 'rank'),
338      ),
339    $cat_updates
340    );
341
342  mass_updates(
343    CATEGORIES_TABLE,
344    array(
345      'primary' => array('id'),
346      'update'  => array('representative_picture_id'),
347      ),
348    $album_thumbs
349    );
350
351  mass_inserts(
352    COMMENTS_TABLE,
353    array_keys($comment_inserts[0]),
354    $comment_inserts
355    );
356 
357  array_push($page['infos'], l10n('Information data registered in database'));
358}
359
360
361// +-----------------------------------------------------------------------+
362// | Template & Form                                                       |
363// +-----------------------------------------------------------------------+
364
365$template->set_filenames(
366  array(
367    'plugin_admin_content' => dirname(__FILE__).'/admin.tpl'
368    )
369  );
370
371$template->assign('action_url', $admin_base_url);
372$template->assign($conf['menalto2piwigo']);
373
374// +-----------------------------------------------------------------------+
375// | Sending html                                                          |
376// +-----------------------------------------------------------------------+
377
378$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
379?>
Note: See TracBrowser for help on using the repository browser.