2

Here is what I'm trying to do :

  1. Write a php script that import external data into drupal.
  2. Run that script every night with a cron that executes it using php, not wget.

In other words, I need my import script to run from the command line.

First step is done and working great when run as a module from Drupal. So I took all required bits of it, created a new file, and bootstraped drupal. After fixing a few warnings and errors (path issues mainly), the file is executed but actually no job is done.

So I start adding echo commands to see where everything goes to hell. And it appears that it's right after

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

What I mean is that no error, warning or whatever is raised, but it's like the script exit somewhere inside that call.

I tried to lower the boostrap level to DRUPAL_BOOTSTRAP_LANGUAGE, but then I get the following error :

Fatal error: Call to undefined function field_info_field() in /www/includes/entity.inc on line 768

I'm a bit lost on what happens and would welcome any suggestion.

Also, here is the beginning of the script that bootstrap Drupal :

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
    chdir('/www/');

    $cmd = 'index.php';
    $_SERVER['HTTP_HOST']       = 'default';
    $_SERVER['PHP_SELF']        = '/index.php';
    $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
    $_SERVER['SERVER_SOFTWARE'] = NULL;
    $_SERVER['REQUEST_METHOD']  = 'GET';
    $_SERVER['QUERY_STRING']    = '';
    $_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
    $_SERVER['HTTP_USER_AGENT'] = 'console';    

    define('DRUPAL_ROOT', '/www/');
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);

    // nothing gets executed past this point
    print("0\n");   
  • For anyone who would run into the same issue, I ended up doing what Damien suggested (using drush). Unfortunatly, our drupal setup seemed a bit unstable and drush would not run. So I installed a blank drupal to run drush from, and changed the running DB with db_set_active – Sebastien F. Mar 27 '13 at 14:18
6

Don't even try this. Use drush, it is meant for this type of things precisely.

The drush php-script command will allow you to run any PHP script in the context of Drupal, but I would recommend writing those as a proper command inside a custom module.

  • Actually the cron is doing far more things than just calling the php script. It's connecting to a remote FTP, download the sql dump, import it to a local database, backup the drupal database, do the import into drupal, and then cleanup on succes or restore + log + mail on error. Tbh, if we good have inserted the data directly in the drupal db, without leveraging drupal, I'd be happy. But we need the drupal API. What I also want to avoid is curling a drupal module to make the cron work. Why can't we use the bootstrap for this ? it seems to be designed for that kind of thing. – Sebastien F. Mar 23 '13 at 8:00
  • @SebastienF. I think you missed a a key point here. If you call 'drush php-script myimporter.php', then it will run just like it was called from Drupal. drush takes care of all of the quirks of doing a bootstrap from the command line. – mpdonadio Mar 23 '13 at 13:18
  • Oh ok, didn't understood it like that. Will try asap and update accordingly. – Sebastien F. Mar 23 '13 at 17:33
  • Installed drush, made a simple test (node_load(21)), getting the "Fatal error: Call to undefined function node_load()", tried everything I found so far (putting the script in default site directory, calling the script from the root drupal directory, etc). – Sebastien F. Mar 24 '13 at 9:55
1

I agree with Damien. Trying to invoke the bootstrap is not the way to go.

If the data you are importing is in csv or xml format, I would suggest you use the Feeds module. It is tailor made for this and you can schedule it as a daily task.

  • The data are in an external database, there's quite a lot of them to process, and for everything described in the comment I added on Damien's answer, I'd rather avoid doing this from drupal but rather doing it from the command line. – Sebastien F. Mar 23 '13 at 8:03
  • There is the Feeds database module which is an add-on fetcher. Once the local db has been loaded from the dump, it might provide a simpler way of mapping the content creation. I like command line too, but sometimes it is really more efficient to let drupal handle the mappings to the various tables. You can still use drush to invoke the process. – Triskelion Mar 23 '13 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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