Tag Archives: NextGen Gallery

Convert myGallery to NextGen Gallery

For a lot of web logs, photo galleries are used. The web logs in this server used to run myGallery. However, this WordPress plugin wasn’t updated for a long time. (Recently there was an update for WordPress 3. I haven’t tested this one yet). So I needed a new gallery plugin. The NextGen Gallery plugin has all the requirements that I need. And best of all, it’s reasonably compatible with myGallery. So I decided to write a script that can convert all myGallery galleries to NextGen galleries. It will also update all the pages that display the gallery. Last, it will convert the thumbnail directory, so NextGen Gallery can use them. Of course, you can also let NextGen Gallery generate new thumbnails. But since my server isn’t that fast, using existing thumbnails is a good idea.

If you are running myGallery, and want to upgrade to NextGen Gallery and perform the conversion, this are the steps to perform. First, download NextGen Gallery. Disable myGallery, and enable NextGen Gallery. Next, copy and paste the script below in a PHP file. Modify the configuration and run the script. This will give a series of SQL statements. You can run these on the MySQL database. For example, you can use phpMyAdmin to run the queries easily.


// CONFIG

// the prefix for your WordPress tables (default: wp_)
$WP_PREFIX = 'wp_';

// the directory on the server where your photos are stored
$GALLERYBASEPATH = 'wp-content/myfotos/daniel';

// username for the database
$USERNAME = 'username';

// password for the database
$PASSWORD = 'password';

// name of the database containing WordPress
$DATABASE = 'wordpress';

// host for the database (99% chance it will be localhost)
$HOST = 'localhost';

// END OF CONFIG

if (substr($GALLERYBASEPATH,-1)!='/' && $GALLERYBASEPATH!='')
{
$GALLERYBASEPATH = $GALLERYBASEPATH . '/';
}

$link = mysql_connect($HOST,$USERNAME,$PASSWORD) or die('Cannot connect to database '.$HOST);
$x = mysql_select_db($DATABASE,$link) or die('Cannot select database '.$DATABASE);

// convert the gallery
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'mygallery') or die('Cannot execure queries. Is the prefix correct?');
while ($row = mysql_fetch_assoc($result))
{
$row['longname'] = addslashes($row['longname']);
$row['galdescrip'] = addslashes($row['galdescrip']);
echo 'INSERT INTO '.$WP_PREFIX.'ngg_gallery (gid,name,path,title,galdesc,pageid,previewpic,author) VALUES ('.$row['id'].',\''.$row['name'].'\',\''.$GALLERYBASEPATH.$row['name'].'\',\''.$row['longname'].'\',\''.$row['galdescrip'].'\',\''.$row['pageid'].'\',\''.$row['previewpic'].'\',0);' . '
';
}

// convert the images
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'mypictures INNER JOIN '.$WP_PREFIX.'mygprelation ON '.$WP_PREFIX.'mypictures.id='.$WP_PREFIX.'mygprelation.pid INNER JOIN '.$WP_PREFIX.'mygallery ON '.$WP_PREFIX.'mygallery.id='.$WP_PREFIX.'mygprelation.gid');

while ($row = mysql_fetch_assoc($result))
{
$row['description'] = addslashes($row['description']);
echo 'INSERT INTO '.$WP_PREFIX.'ngg_pictures (pid,galleryid,filename,description) VALUES ('.$row['pid'].','.$row['gid'].',\''.$row['picturepath'].'\',\''.$row['description'].'\');' . '
';
}

// convert pages
$result = mysql_query('SELECT id,pageid FROM '.$WP_PREFIX.'mygallery');
while ($row = mysql_fetch_assoc($result))
{
echo 'UPDATE '.$WP_PREFIX.'posts SET post_content=\'[nggallery id='.$row['id'].']\' WHERE ID='.$row['pageid'] . ';
';
}

// convert page with master gallery
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'posts WHERE post_content=\'[mygallistgal]\'');
while ($row = mysql_fetch_assoc($result))
{
echo 'UPDATE '.$WP_PREFIX.'posts SET post_content=\'[album template=extend]\' WHERE ID='.$row['ID'] . ';
';
}

There are a number of caveats you should be aware of:

  • Gallery pages are updated, so they contain the new gallery tag. If there is any other text on a page, this text is lost
  • The page looks for the master gallery page (the page with links to all galleries. It will only find it if this page does not contain any other text. If the last code block does not give any results, you have to change this page manually

If you visit your gallery pages now, the should work, only the thumbnails are not displayed. To fix this, I’ve made a second script.


error_reporting(E_ALL);
$prefix = '/home/www/wordpress_mu/wp-content/myfotos/daniel';
$dirs = scandir($prefix);

foreach ($dirs as $dirname) {
if ($dirname=='.' || $dirname=='..') continue;
$dir = $prefix.'/'.$dirname;
if (is_dir($dir)) {

if ($dh = opendir($dir)) {
echo $dir . " open
";

if (is_dir($dir.'/tumbs')) {
if ($dh2 = opendir($dir.'/tumbs')) {

while (($file = readdir($dh2)) !== false) {
if (substr($file,0,3)=='tmb') {
$newname = str_replace('tmb','thumbs',$file);
$r = rename($dir.'/tumbs/'.$file,$dir.'/tumbs/'.$newname);
echo "filename: $file to $newname : rename: $r
";
}
}
// rename tumbs dir
$r = rename($dir.'/tumbs',$dir.'/thumbs');
echo $dir . " rename " .$r."
";
}
closedir($dh2);

}
closedir($dh);
}
}
}

Just change the prefix variable, to point to the path where all galleries are located. This script will rename all thumbnail directories immediately.