16

This is what I have right now:

<?php
    define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
    include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//  drupal_flush_all_caches();  

    $users = views_get_view_result('userlist', 'uid_list');
    $i = 0;
    foreach ($users as $user) {
        $filename = 'sites/default/files/generated_pdfs/individual_pdf_report_' . $user->uid . '.pdf';
        generate_employee_pdf($user->uid, $filename);
        error_log('Generated ' . $filepath);
        $i++;
    }
    // create object
    $zip = new ZipArchive();
    // open archive
    if ($zip->open("sites/default/files/generated_zips/individual-reports.zip", ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    // initialize an iterator
    // pass it the directory to be processed
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("sites/default/files/generated_pdfs/"));
    // iterate over the directory
    // add each file found to the archive
    foreach ($iterator as $key=>$value) {
        $zip->addFile(realpath($key), $value->getFilename()) or die ("ERROR: Could not add file: $key");
    }
    // close and save archive
    $zip->close();
?>

I'm getting a bunch of errors from the above when run standalone. Works fine in my module. Some of the errors are:

Warning: Invalid argument supplied for foreach() in drupal_depth_first_search() (line 56 of /var/www/clients/client1/web15/web/includes/graph.inc).
Warning: Invalid argument supplied for foreach() in _module_build_dependencies() (line 229 of /var/www/clients/client1/web15/web/includes/module.inc).
Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).
14

By far the easiest way to manage the bootstrap is to use drush php-script, alias scr.

file.php:

<?php
echo l('Link', 'path');

Cli:

drush @alias scr file.php

Output:

<a href="/path">Link</a>

This way you need to deal with nothing with regard to the bootstrap, drush will solve it entirely for you, and gives you the ability to use it easily across sites thanks to aliases. (Might even work on remote sites, though I haven't tried that)

  • If you want to do this on a remote site, the script has to be remote (see drupal.stackexchange.com/questions/98098). If I ever get free time (ha!) will try a patch for this. – mpdonadio Jan 9 '14 at 14:53
  • This really is the simplest way to do it and should get more upvotes. – Bala Clark Apr 23 '14 at 9:43
  • What exactly is @alias supposed to do? I only get a Could not find the alias @alias. Unfortunately the link you provided is broken. Could you please fix it and expand your explanation? – leymannx Nov 17 '15 at 10:21
  • @leymannx Hey, sorry I didn't notice your question before. I've updated the link to the documentation. I've not expanded the alias part however, as drush aliases are really out of scope for this particular question. – Letharion Feb 27 '16 at 19:41
11

your standalone Drupal 7 bootstrap is not quite right.

Try this:

/**
 * Do not let execute this file from http request
 */
if (isset($_SERVER['REMOTE_ADDR'])) {
  print "...";
  exit(1);
}

/**
 * Initialize Drupal
 */
define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once DRUPAL_ROOT . '...'; // include required module files to work with here

drupal_override_server_variables(array('url' => 'http://www.example.com/batch.php')); // trick drupal to do bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

set_time_limit(4600); // adjust for your needs
ini_set('memory_limit', '3524M'); // adjust for your needs

Hope this helps.

  • Hmm. It's failing silently at this line: $users = views_get_view_result('userlist', 'uid_list'); Am I correct that your drupal_override_server_variables call should point at the location of my php file? – Joren Apr 17 '12 at 18:59
  • Yes You right. This bootstrap loads only database layer, but not modules. You must invoke module init hook for views to use it's functionality or include module file responsible for data processing here: require_once DRUPAL_ROOT . '...'; // include required module files to work with here – Zydrunas Tamasauskas Apr 18 '12 at 6:53
  • I've included common.inc, views.module and ctools.module as the errors made it obvious they were needed. After these 3 I stopped getting errors and it just died silently. Not sure where to go from here. – Joren Apr 18 '12 at 18:01
  • Do printouts after each inclusion and see where it stuck. Then you may know what's wrong. As an option, try to do full bootstrap and see what's happens. – Zydrunas Tamasauskas Apr 19 '12 at 11:47
2

this is enough:

define('DRUPAL_ROOT', '/path/to/drupal/root/');
 require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
0

The last error

Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).

is a bug in VBO. views_bulk_operations_load_action_includes() does not use DRUPAL_ROOT when using include_once. I would file a bug and fix it in VBO.

The other error (the 2 are related to each other) might be related to _system_rebuild_module_data() not using DRUPAL_ROOT when accessing modules .info file. I would put break points in system_rebuild_module_data() for the $modules variable and see if you can pin point it. Once again file a bug with drupal core as I don't see this issue in the issue queue.

Good luck!

Not the answer you're looking for? Browse other questions tagged or ask your own question.