source: extensions/menalto2piwigo/admin.php @ 29382

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

avoit empty album name

File size: 17.5 KB
RevLine 
[28825]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
[29070]137  // Gallery2 or Gallery3?
138  $menalto_tables = m2p_get_tables($pt);
[28825]139
[29070]140  if (in_array($pt.'FileSystemEntity', $menalto_tables))
[28825]141  {
[29070]142    // Gallery version 2
[28825]143
[29070]144    // Gallery2 parent Ids (root is always 7!)
145    $ids = array(7,0,0,0,0,0);
146    // Piwigo uppercats
147    $uct = array('NULL',0,0,0,0,0);
148    $ranks = array();
[28825]149
[29070]150    // the following algorithm is a conversion into PHP of the Perl script
151    // convertcomments.pl by dschwen, see https://github.com/dschwen/g2piwigo
152    //
153    // this plugin just makes things "simpler" for users but the hard part
154    // comes from dschwen, he deserves all credits!
155 
156    foreach ($piwigo_paths as $dir => $piwigo_id)
157    {
158      $path = explode('/', $dir);
159      $basename = $path[count($path)-1];
160      $level = count($path);
161
162      $parentId = $ids[$level-1];
163
164      // get id and title/summary/description of tail element in path
165      $query = "
[28825]166SELECT
167    f.".$pc."id,
168    i.".$pc."title,
169    i.".$pc."summary,
170    i.".$pc."description,
171    i.".$pc."canContainChildren,
172    a.".$pc."orderWeight,
173    a.".$pc."viewCount,
174    FROM_UNIXTIME(e.".$pc."creationTimestamp)
175  FROM ".$pt."Item i
176    JOIN ".$pt."FileSystemEntity f ON i.".$pc."id = f.".$pc."id
177    JOIN ".$pt."ChildEntity c ON f.".$pc."id = c.".$pc."id
178    JOIN ".$pt."ItemAttributesMap a ON i.".$pc."id = a.".$pc."itemId
179    JOIN ".$pt."Entity e ON e.".$pc."id = i.".$pc."id
180  WHERE c.".$pc."parentId = ".$parentId."
181    AND f.".$pc."pathComponent='".$basename."'
182;";
[29070]183      // echo '<pre>'.$query."</pre>";
184      $row = pwg_db_fetch_row(pwg_query($query));
[28825]185   
[29382]186      // print "$row[4] - $parentId -> $row[0] : $row[1] $row[2] $row[3]<br>";
[29070]187      $title = m2p_remove_bbcode($row[1]);
[29382]188      if (empty($title))
189      {
190        $title = $basename;
191      }
192     
[29070]193      $summary = m2p_remove_bbcode($row[2]);
194      $description = m2p_remove_bbcode($row[3]);
195      $weight = $row[5];
196      $views = $row[6];
197      $date_available = $row[7];
198      $ids[$level] = $row[0];
199      $pid[$row[0]] = $dir;
[28825]200
[29070]201      if ($row[4] == 0)
202      {
203        // Menalto says it's an image
[28825]204
[29070]205        if (strpos($piwigo_id, 'i') === false)
206        {
207          echo 'Error, '.$piwig_id.' is not an image and Menalto says it is an image';
208        }
[28825]209     
[29070]210        $comment = "";
211        if ( $summary != "" and $description != "" )
[28825]212        {
[29070]213          $comment = "<b>$summary</b> - $description";
[28825]214        }
215        else
216        {
[29070]217          if ($summary != "")
218          {
219            $comment = $summary;
220          }
221          else
222          {
223            $comment = $description;
224          }
[28825]225        }
226
[29070]227        $image_id = substr($piwigo_id, 1);
[28825]228     
[29070]229        $image_updates[] = array(
230          'id' => $image_id,
231          'name' => pwg_db_real_escape_string($title),
232          'comment' => pwg_db_real_escape_string($comment),
233          'date_available' => $date_available,
234          );
[28825]235     
[29070]236        // build a map from gallery2 ids to piwigo image ids
237        $iid[$row[0]] = $image_id;
[28825]238      }
239      else
240      {
[29070]241        // album (folder)
242        if (strpos($piwigo_id, 'c') === false)
[28825]243        {
[29070]244          echo 'Error, '.$piwig_id.' is not an album and Menalto says it is an album';
[28825]245        }
[29070]246     
247        $comment = "";
248        if ( $summary != "" and $description != "" )
249        {
250          $comment = "$summary <!--complete--> $description";
251        }
[28825]252        else
253        {
[29070]254          if ($summary != "")
255          {
256            $comment = $summary;
257          }
258          else
259          {
260            $comment = "<!--complete-->$description";
261          }
[28825]262        }
263
[29070]264        // get piwigo category id
265        $cat_id = substr($piwigo_id, 1);
266        $uct[$level] = $cat_id;
[28825]267     
[29070]268        $cat_updates[] = array(
269          'id' => $cat_id,
270          'name' => pwg_db_real_escape_string($title),
271          'comment' => pwg_db_real_escape_string($comment),
272          'rank' => $weight,
273          );
[28825]274
[29070]275        // get highlight picture
276        $query = "
[28825]277SELECT d2.".$pc."derivativeSourceId
278  FROM ".$pt."ChildEntity c
279    JOIN ".$pt."Derivative d1 ON c.".$pc."id = d1.".$pc."id
280    JOIN ".$pt."Derivative d2 ON d1.".$pc."derivativeSourceId=d2.".$pc."id
281  WHERE c.".$pc."parentId = ".$ids[$level];
[29070]282        $subresult = pwg_query($query);
283        $subrow = pwg_db_fetch_row($subresult);
284        $hid[$cat_id] = $subrow[0];
285      }
[28825]286    }
287
[29070]288    // apply highlites as representative images
289    foreach ($hid as $cat_id => $menalto_id)
[28825]290    {
[29070]291      if (!empty($menalto_id))
292      {
293        $album_thumbs[] = array(
294          'id' => $cat_id,
295          'representative_picture_id' => $iid[$menalto_id],
296          );
297      }
[28825]298    }
299
[29070]300    // copy comments
301    $query = "
[28825]302SELECT
303    c.".$pc."parentId AS id,
304    t.".$pc."subject AS subject,
305    t.".$pc."comment AS comment,
306    t.".$pc."author AS author,
307    FROM_UNIXTIME(t.".$pc."date) AS date
308  FROM ".$pt."ChildEntity c
309    JOIN ".$pt."Comment t ON t.".$pc."id = c.".$pc."id
310  WHERE t.".$pc."publishStatus=0
311";
[29070]312    $result = pwg_query($query);
313    while ($row = pwg_db_fetch_assoc($result))
314    {
315      if (isset($iid[ $row['id'] ]))
316      {
317        $comment = $row['comment'];
318        if (!empty($row['subject']))
319        {
320          $comment = '[b]'.$row['subject'].'[/b] '.$comment;
321        }
322
323        $comment_inserts[] = array(
324          'image_id' => $iid[ $row['id'] ],
325          'date' => $row['date'],
326          'author' => pwg_db_real_escape_string($row['author']),
327          'content' => pwg_db_real_escape_string($comment),
328          'validated' => 'true',
329          );
330      }
331    }
332
333    m2p_db_disconnect();
334
335    // echo '<pre>'; print_r($image_updates); echo '</pre>';
336    // echo '<pre>'; print_r($cat_updates); echo '</pre>';
337    // echo '<pre>'; print_r($hid); echo '</pre>';
338    // echo '<pre>'; print_r($iid); echo '</pre>';
339    // echo '<pre>'; print_r($album_thumbs); echo '</pre>';
340    // echo '<pre>'; print_r($comment_inserts); echo '</pre>';
[29382]341
[29070]342    mass_updates(
343      IMAGES_TABLE,
344      array(
345        'primary' => array('id'),
346        'update'  => array('name', 'comment', 'date_available'),
347        ),
348      $image_updates
349      );
350
351    mass_updates(
352      CATEGORIES_TABLE,
353      array(
354        'primary' => array('id'),
355        'update'  => array('name', 'comment', 'rank'),
356        ),
357      $cat_updates
358      );
359
360    mass_updates(
361      CATEGORIES_TABLE,
362      array(
363        'primary' => array('id'),
364        'update'  => array('representative_picture_id'),
365        ),
366      $album_thumbs
367      );
368
369    mass_inserts(
370      COMMENTS_TABLE,
371      array_keys($comment_inserts[0]),
372      $comment_inserts
373      );
[29382]374
[29070]375    array_push($page['infos'], l10n('Information data registered in database'));
376  }
377  elseif (in_array($pt.'items_tags', $menalto_tables))
[28825]378  {
[29070]379    // Gallery version 3
380
381    $query = '
382SELECT
383    id,
384    name,
385    parent_id,
386    relative_path_cache,
387    title,
388    description,
389    type,
390    view_count,
391    created,
392    weight,
393    album_cover_item_id
394  FROM '.$pt.'items
395;';
396    $result = pwg_query($query);
397    while ($row = pwg_db_fetch_assoc($result))
[28825]398    {
[29070]399      if (isset($piwigo_paths[ $row['relative_path_cache'] ]))
[28825]400      {
[29070]401        $piwigo_id = $piwigo_paths[ $row['relative_path_cache'] ];
[28825]402      }
[29070]403      else
404      {
405        continue;
406      }
407     
408      if ('photo' == $row['type'])
409      {
410        $image_id = substr($piwigo_id, 1);
411     
412        $image_updates[] = array(
413          'id' => $image_id,
414          'name' => pwg_db_real_escape_string($row['title']),
415          'comment' => pwg_db_real_escape_string($row['description']),
416          'date_available' => date('Y-m-d H:i:s', $row['created']),
417          'hit' => $row['view_count'],
418          );
[28825]419
[29070]420        // build a map from menalto ids to piwigo image ids
421        $iid[ $row['id'] ] = $image_id;
422      }
423      elseif ('album' == $row['type'])
424      {
425        $cat_id = substr($piwigo_id, 1);
426       
427        $cat_updates[] = array(
428          'id' => $cat_id,
429          'name' => pwg_db_real_escape_string($row['title']),
430          'comment' => pwg_db_real_escape_string($row['description']),
431          'rank' => $row['weight'],
432          );
433       
434        $cover_id[$cat_id] = $row['album_cover_item_id'];
435      }
436    }
437
438    // album cover id
439    foreach ($cover_id as $cat_id => $menalto_id)
440    {
441      if (!empty($menalto_id) and isset($iid[$menalto_id]))
442      {
443        $album_thumbs[] = array(
444          'id' => $cat_id,
445          'representative_picture_id' => $iid[$menalto_id],
446          );
447      }
448    }
449
450    // user comments;
451    $query = '
452SELECT
453    author_id,
454    server_remote_addr,
455    created,
456    name,
457    full_name,
458    email,
459    url,
460    guest_email,
461    guest_name,
462    guest_url,
463    item_id,
464    state,
465    text
466  FROM '.$pt.'comments
467    JOIN '.$pt.'users AS u ON author_id = u.id
468;';
469    $result = pwg_query($query);
470    while ($row = pwg_db_fetch_assoc($result))
471    {
472      if (isset($iid[ $row['item_id'] ]))
473      {
474        if (!empty($row['guest_name']))
475        {
476          $name = $row['guest_name'];
477          $email = $row['guest_email'];
478          $url = $row['guest_url'];
479        }
480        else
481        {
482          $name = $row['full_name'].' ('.$row['name'].')';
483          $email = $row['email'];
484          $url = $row['url'];
485        }
486
487        if (2 == $row['author_id']) // default admin on G3
488        {
489          $author_id = 1; // default admin on Piwigo
490        }
491        else
492        {
493          $author_id = 2; // guest on Piwigo
494        }
495
496        $validated = 'true';
497        if ('unpublished' == $row['state'])
498        {
499          $validated = 'false';
500        }
501
502        $anonymous_id = implode('.', array_slice(explode('.', $row['server_remote_addr']), 0, 3));
503       
504        $comment_inserts[] = array(
505          'image_id' => $iid[ $row['item_id'] ],
506          'date' => date('Y-m-d H:i:s', $row['created']),
507          'author' => pwg_db_real_escape_string($name),
508          'author_id' => $author_id,
509          'anonymous_id' => $anonymous_id,
510          'email' => pwg_db_real_escape_string($email),
511          'website_url' => pwg_db_real_escape_string($url),
512          'content' => pwg_db_real_escape_string($row['text']),
513          'validated' => $validated,
514          );
515      }
516    }
517
518    // tags
519    $query = '
520SELECT
521    id,
522    name
523   FROM '.$pt.'tags
524;';
525    $result = pwg_query($query);
526    while ($row = pwg_db_fetch_assoc($result))
527    {
528      $tag_inserts[] = array(
529        'name' => pwg_db_real_escape_string($row['name']),
530        'url_name' => str2url($row['name']),
[28825]531        );
[29070]532
533      $menalto_tag_ids[ $row['name'] ] = $row['id'];
[28825]534    }
535
[29070]536    $query = '
537SELECT
538    item_id,
539    tag_id
540  FROM '.$pt.'items_tags
541;';
542    $result = pwg_query($query);
543    while ($row = pwg_db_fetch_assoc($result))
544    {
545      $items_tags[] = $row;
546    }
[28825]547
[29070]548    m2p_db_disconnect();
[28825]549
[29070]550    mass_updates(
551      IMAGES_TABLE,
552      array(
553        'primary' => array('id'),
554        'update'  => array('name', 'comment', 'date_available', 'hit'),
555        ),
556      $image_updates
557      );
558   
559    mass_updates(
560      CATEGORIES_TABLE,
561      array(
562        'primary' => array('id'),
563        'update'  => array('name', 'comment', 'rank'),
564        ),
565      $cat_updates
566      );
567   
568    mass_updates(
569      CATEGORIES_TABLE,
570      array(
571        'primary' => array('id'),
572        'update'  => array('representative_picture_id'),
573        ),
574      $album_thumbs
575      );
576   
577    mass_inserts(
578      COMMENTS_TABLE,
579      array_keys($comment_inserts[0]),
580      $comment_inserts
581      );
[28825]582
[29070]583    mass_inserts(
584      TAGS_TABLE,
585      array_keys($tag_inserts[0]),
586      $tag_inserts
587      );
[28825]588
[29070]589    // we need to retrieve the mapping of piwigo tag name => piwigo tag id,
590    // for image_tag associations
591    $query = '
592SELECT
593    id,
594    name
595  FROM '.TAGS_TABLE.'
596;';
597    $result = pwg_query($query);
598    while ($row = pwg_db_fetch_assoc($result))
599    {
600      if (isset($menalto_tag_ids[ $row['name'] ]))
601      {
602        $tag_id_convert[ $menalto_tag_ids[ $row['name'] ] ] = $row['id'];
603      }
604    }
605
606    foreach ($items_tags as $item_tag)
607    {
608      if (isset($iid[ $item_tag['item_id'] ]) and isset($tag_id_convert[ $item_tag['tag_id'] ]))
609      {
610        $image_tag_inserts[] = array(
611          'image_id' => $iid[ $item_tag['item_id'] ],
612          'tag_id' => $tag_id_convert[ $item_tag['tag_id'] ],
613          );
614      }
615    }
616
617    mass_inserts(
618      IMAGE_TAG_TABLE,
619      array_keys($image_tag_inserts[0]),
620      $image_tag_inserts
621      );
622
623    array_push($page['infos'], l10n('Information data registered in database'));
624  }
625  else
626  {
627    m2p_db_disconnect();
628    array_push($page['errors'], l10n('No Menalto tables found!'));
629  }
[28825]630}
631
632
633// +-----------------------------------------------------------------------+
634// | Template & Form                                                       |
635// +-----------------------------------------------------------------------+
636
637$template->set_filenames(
638  array(
639    'plugin_admin_content' => dirname(__FILE__).'/admin.tpl'
640    )
641  );
642
643$template->assign('action_url', $admin_base_url);
644$template->assign($conf['menalto2piwigo']);
645
646// +-----------------------------------------------------------------------+
647// | Sending html                                                          |
648// +-----------------------------------------------------------------------+
649
650$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
651?>
Note: See TracBrowser for help on using the repository browser.