Gallery2:How can I call a View or Controller from the Command Line - Gallery Codex
Personal tools

Gallery2:How can I call a View or Controller from the Command Line

From Gallery Codex

How Can I Call A View Or Controller From The Command Line?

Also see: How To Write Scripts Interacting With Gallery

Requirements:

  • Wget: wget is a unix/linux command line user agent to fetch websites from the command line. Wget for Windows is also available.

This example script first logs into G2, then it runs the build all thumbnails and resizes maintenance task.

To run another controller or request a view, just browse in G2 to the page and copy the URL of the page (views) or copy the URL of the hand-craft the controller URL with all necessary arguments (take a look at the HTML source of a page to find the <form> element and the forum input fields. then copy all input field name=value pairs into your hand-crafted URL.

wget --cookies=on  --save-cookies mycookies "http://example.com/gallery2/main.php?g2_controller=core.UserLogin&g2_form[formName]=UserLogin&g2_form[username]=admin&g2_form[password]=mysecret&g2_form[action][login]=Login"

wget --cookies=on --load-cookies mycookies "http://example.com/gallery2/main.php?g2_controller=core.AdminMaintenance&g2_form%5Baction%5D%5BrunTask%5D=1&g2_taskId=BuildDerivativesTask"

Replace mysecret with your password. And maybe you also have to replace admin with your admin's username. And replace example.com/gallery2/ with your actual G2 address.

You could execute the above calls in a a bash script

#!/bin/bash
your commands here...

And you could execute the bash script periodically as a cron script.

Caveat

Since G2.2, the above instructions don't work anymore without some additions. As a security measure, Gallery not only requires you to be logged in, you also need to include a valid authToken (authentication token) in all controller requests.

There are workarounds:

Create A Wrapper Script

You could write a buildDerivatives.php script:

<?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);

/* Grab the authToken */
global $gallery;
$session =& $gallery->getSession();
$authToken = $session->getAuthToken();
GalleryUtilities::putRequestVariable('authToken', $authToken);

/* Here we could either call any API mehtods or declare the controller request params */
GalleryUtilities::putRequestVariable('controller', 'core.AdminMaintenance');
GalleryUtilities::putRequestVariable('form[action][runTask]', 1);
GalleryUtilities::putRequestVariable('taskId', 'BuildDerivativesTask');

/* Execute the above defined controller request */
$data = GalleryEmbed::handleRequest();

/* $data['bodyHtml'] contains the resulting HTML */

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

And you could call that script from the command line (e.g. with cron) or you could browse to it with http://example.com/gallery2/buildDerivatives.php.

Security Warning: You shouldn't put such scripts into web-accessible folders (like your gallery2/ folder) because this script is sidestepping the auth-token security mechanism which is there to protect you from malicious sites (Cross Site Request Forgery Protection).

Include The Auth Token After Grabbing It

  • After doing the first request to login, do another request for a view that includes a form and parse / scan the HTML for the g2_authToken string.
  • Then append the auth token to your controller requests (&g2_authToken=...).

You can use grep or script languages to extract the authToken from the returned HTML.