Simple script to access DB

Hello,
I'd like to build a small script to update my DB using omeka core functions. For example I'd like to be able to use get_db().

Which includes should I use ? I've tried "require_once("bootstrap.php")" but it does'nt seem to be enough as I get an error : "Database not available!"

I don't want to build a plugin or anything else, just a small script that I should call from the command line ("php myscript.php").

Hope someone has the solution !

nobody has an idea ? :(

I've managed to do what I wanted. If somebody needs the same thing, here's the script I've written :
https://github.com/symac/OpenLayersZoom/blob/5f4ac40814fa321b4535565cbba933289cb63a0c/bulk_build_tiles.php

Thank you very much for showing your solution.
Your original question had me stumped. It also got me curious so your answer is great to see.

How did you discover the sequence of statements in lines 26 through 38 that creates the needed context?

On line 26, $autoloader is set but not used. Is there a line missing that uses it?

Hi Bob,
in fact it was easier than I was thinking, the lines you refer to are available in the main index.php file of omeka. You just have to change :
$application->initialize()->run();
to
$application->initialize();

to prevent zend from starting to display the page.

The autoloader must be something I've tried (spend some hours trying to discover the solution, stupid I am !).

Hope it helps.

Thank you for your explanation. I see that the main Omeka index.php is similar. You must have worked hard to discover that calling run() was not needed.

I just tried out a minimal version to run a batch script based on the code at github, and was not successful.

I took out the references to the extra zoom functions, and made only a simple database call, to get this script.
Note that it is changed so that it is located in the top level Omeka install folder, where bootstrap.php exists.

<?php
/**
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
* @author Sylvain Machefert - Bordeaux 3
*/

require_once dirname(__FILE__).'/bootstrap.php';
$application = new Omeka_Application(APPLICATION_ENV);

$application->getBootstrap()->setOptions(array(
   'resources' => array(
     'theme' => array(
     'basePath' => THEME_DIR,
     'webBasePath' => WEB_THEME
     )
   )
 ));
$application->initialize();

$db = get_db();
$sql = " SELECT count(*) FROM {$db->Item} items ";
$result = $db->fetchAll($sql);
print "Items count: $result \n";
exit;

?>

The php code is in file test.php and is run with command line:

php test.php

It gets a warning message on stdout before showing the html reporting a fatal error:

Warning: ini_set(): Cannot change zlib.output_compression - headers already sent in D:\xampp\xampp\htdocs\omeka\bootstrap.php on line 127

It gets a fatal error message and stack trace in the error.log file:

2013-10-10T13:22:13-07:00 ERR (3): exception 'Zend_Session_Exception' with message 'Session must be started before any output has been sent to the browser; output started in D:\xampp\xampp\htdocs\omeka\test.php/2' in D:\xampp\xampp\htdocs\omeka\application\libraries\Zend\Session.php:451
Stack trace:
#0 D:\xampp\xampp\htdocs\omeka\application\libraries\Zend\Session\Namespace.php(143): Zend_Session::start(true)
:
#18 D:\xampp\xampp\htdocs\omeka\application\libraries\Omeka\Application.php(51): Zend_Application->bootstrap()
#19 D:\xampp\xampp\htdocs\omeka\test.php(21): Omeka_Application->initialize()
#20 {main}

In case it is the warning message that is causing the fatal error, another version of the test is run.
bootstrap.php is changed to comment out the lines causing the warning message:

// Set the zlib config values if the extension has been loaded.
if (extension_loaded('zlib')) {
    // ini_set('zlib.output_compression', true);
    // ini_set('zlib.output_compression_level', '5');
}

Running this version of the test.php also fails.
The warning message is not output but the same fatal error and stack trace is in the error.log file.

Sylvain, what did you do to avoid the warning message from line 127 in bootstrap.php?

And what did you do to avoid the fatal error in Session.php line 451?

Thanks for any pointers.

Having the ability to run an Omeka batch script is a powerful tool to have available. That is clear when seeing your code to create zoom tiles in a batch process. I want to learn how to run batch files that use Omeka's helper functions.