Gallery2:How To Write Scripts - Gallery Codex
Personal tools

Gallery2:How To Write Scripts

From Gallery Codex

You can extend Gallery's functionality by writing modules and themes, but you can also call Gallery from your own scripts.

Applications (examples):

  • Run tasks periodically based on cron as a scheduler calling your own PHP script to execute some operations in Gallery.
  • Write custom scripts to batch-import thousands of users, comments, etc.
  • ...

Note that we don't want you to reinvent the wheel. If you're looking for a command line client to add items, take a look at List of Other Clients.

Basic Script Structure

The GalleryEmbed API is not only handy to integrate Gallery into other web applications, it's also useful to write command line scripts:

<?php
/* Adjust the path to point to your gallery2 folder */
require_once('../gallery2/embed.php');

/*
 * Initialize G2 (includes all necessary classes,
 * puts G2 into a state ready to process your calls)
 */
$ret = GalleryEmbed::init(array('fullInit' => true));
check($ret);

/* Here go your G2 API calls */


/*
 * At the end, commit the transaction by calling ::done()
 * or all changes made above will be rolled back automatically
 */
$ret = GalleryEmbed::done();
check($ret);

function check($ret) {
    if ($ret) die($ret->getAsHtml());
}
?>

Available API

After the GalleryEmbed::init() call, you can use all of Gallery's APIs (GalleryCoreApi, GalleryModuleApi, ...).

Examples