Hi,
I am trying to move to Drupal from an old, custom CMS. I have a php script that connects to the mysql database and gets the titles and bodies for all of the pages. What I want to do is iterate through the results and add them as new nodes. Unfortunately I'm getting two different kinds of errors. If I do this:
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
I get this error message:
PHP Notice: Use of undefined constant DRUPAL_ROOT - assumed 'DRUPAL_ROOT' in ./includes/bootstrap.inc on line 2094
PHP Warning: require_once(DRUPAL_ROOT/includes/errors.inc): failed to open stream: No such file or directory in ./includes/bootstrap.inc on line 2094
PHP Fatal error: require_once(): Failed opening required 'DRUPAL_ROOT/includes/errors.inc' (include_path='.:/usr/share/pear:/usr/share/php') in ./includes/bootstrap.inc on line 2094
If I add in the specific path to the install like this:
define("DRUPAL_ROOT", "/var/www/html/mydrupal/");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
I get:
PHP Notice: Undefined index: REMOTE_ADDR in /var/www/html/mydrupal/includes/bootstrap.inc on line 2597
PHP Notice: Trying to get property of non-object in /var/www/html/mydrupal/modules/dblog/dblog.module on line 144
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array
(
[:db_insert_placeholder_0] =>
[:db_insert_placeholder_1] => php
[:db_insert_placeholder_2] => %type: !message in %function (line %line of %file).
[:db_insert_placeholder_3] => a:6:{s:5:"%type";s:6:"Notice";s:8:"!message";s:28:"Undefined index: REMOTE_ADDR";s:9:"%function";s:12:"ip_address()";s:5:"%file";s:40:"/var/www/html/mydrupal/includes/bootstrap.inc";s:5:"%line";i:2597;s:14:"severity_level";i:5;}
[:db_insert_placeholder_4] => 5
[:db_insert_placeholder_5] =>
[:db_insert_placeholder_6] => http:///importme.php?importme.php
[:db_insert_placeholder_7] =>
[:db_insert_placeholder_8] =>
[:db_insert_placeholder_9] => 1293647936
)
in dblog_watchdog() (line 155 of /var/www/html/mydrupal/modules/dblog/dblog.module).
My php script is in /var/www/html/mydrupal. I've tried looking through the api examples for 7, but they all assume you are writing a module to be used within Drupal, not a one time import script. Under 6, the require_once command should work, at least according to the examples.
Any ideas or a better way to do this?
Thanks
Comments
Hi, can you try this: put
Hi,
can you try this: put your scrip in the root you your drupal installation, where also index.php is, and then:
This is also like drupal index.php works, so it should also work in your case.
Best,
Vasi.
Nope, no go. I get the same
Nope, no go. I get the same long error message about REMOTE_ADDR, non-object on bootstrap.inc 2597, etc. Same as the long error in my first post. But here's the interesting thing. If I do:
php index.php
from the command line, I get the same error messages. But I can visit index.php in my browser and everything displays just fine. And if I put some print "hello world"; statements in the test code, when I visit the webpage, "hello world" prints out just fine
So it looks like there's something that's using the HTTP variables that are passed in from the webserver or webbrowser. I know I won't be able to run it down, but for a one time import I can get my code done and then just visit the php page in my browser.
So you get the error only
So you get the error only when you run the script from command line? It yes, to avoid this you can just run the script from the web browser...
Vasi.
Yes, the errors only happen
Yes, the errors only happen from the commandline. I can probably work around that and just get the code and open the php file in the browser but there are problems with that:
1) You don't see the other php error messages that are important for debugging
2) Security. For this purpose it doesn't matter if someone loads the page since there's nothing they can hurt. But other people might find this a huge problem from a security standpoint.
For the second issue, with
For the second issue, with the security, you could just create a menu item and protect it with the drupal access system. And in the callback function, you can enable the error_reporting if you want...
Vasi.
Works
Fantastic! Works for me, the code I have is:
You are a time saver, Vasi
Hi, Vasi,
I am trying to "upgrade" from version 5 to 7. I have a few bootstrapped pages, and your comment was a strong pointer to remedy the issue for now. Thanks.
Original working version 5 was this (it was in one directory in the theme folder for good or bad):
And I just gave an definite orientation:
So that it defines DRUPAL_ROOT. Still it feels somehow wrong to define a constant in such a remote page, but it does the job.
This fixed the problem.
This fixed the problem.
Thanks!
-----------------------------
Subir Ghosh
www.subirghosh.in
same error
Getting the same error on a 6.2 to 7.0 upgrade test.
It's a simple php script that accepts posted vars and creates a new node - using node_submit, so I'm loading the bootstrap.
Subscribing
here is my init_script()
After including:
I have
Egarias
Turning opportunities into business
Drupal 7 - DRUPAL_ROOT - REMOTE_ADDR problem solved
After playing around with some code, I finally came up with a solution for Drupal 7:
Below is a complete working example. Some things to note:
I copied this from Drupal 6 code found at http://workingdirectory.net/posts/2009/attach-file-to-node-drupal-6/ with some modifications as follows:
- I took out any code i didn't need (such as custom fields)
- For Drupal 7 I added: define('DRUPAL_ROOT', dirname(realpath(__FILE__)));
- For Drupal 7 I added: $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
- For Drupal 7 I modified the $node->body element, body is no longer a required field
- For Drupal 7 the built in content types are now "page" and "article", article has replaced "story"
Set the remote addr
One way is to check and set remote address to resolve this issue.
akki
UAParser.php
I'm running the above code from the command line and I get this error:
Any ideas
drupal_override_server_variables, before drupal_bootstrap
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drup...
this function was designed to, as the name suggests, override server environment variables prior to bootstrapping for command-line use.