Using a php code working with 6.x, and with errors in 7.0

Notice: Use of undefined constant DRUPAL_ROOT - assumed 'DRUPAL_ROOT' in /home/empresa/public_html/includes/bootstrap.inc on line 2094

Warning: require_once(DRUPAL_ROOT/includes/errors.inc) [function.require-once]: failed to open stream: No such file or directory in /home/empresa/public_html/includes/bootstrap.inc on line 2094

Fatal error: require_once() [function.require]: Failed opening required 'DRUPAL_ROOT/includes/errors.inc' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/empresa/public_html/includes/bootstrap.inc on line 2094

the code afected is at the start of my php program (a program that writes nodes).

function init_script() {
  global $adminuser;
  global $adminpass;
  include_once('includes/bootstrap.inc');
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  bootstrap_invoke_all('init');
  ini_set('memory_limit', '512M');
if( ! function_exists("node_object_prepare")) {
      include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
   } else {
  require_once 'modules/node/node.pages.inc';
   }
  //Authenticate as user 1
  user_authenticate($adminuser, $adminpass);
}

if i try to include DRUPAL_ROOT and change the code to

function init_script() {
  global $adminuser;
  global $adminpass;
define('DRUPAL_ROOT', getcwd());
//require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  bootstrap_invoke_all('init');
  ini_set('memory_limit', '512M');
if( ! function_exists("node_object_prepare")) {
      include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
   } else {
  require_once DRUPAL_ROOT . '/modules/node/node.pages.inc';
   }
  //Authenticate as user 1
  user_authenticate($adminuser, $adminpass);
}

i get the following error

Fatal error: Cannot redeclare comment_page_title_alter() (previously declared in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc:13) in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc on line 27

This is already posted in http://drupal.org/node/1009966 but under code questions. I think maybe this should be treated as bug or error? i imagine it should be some people using node_save() in their php custom programs that fall in the same error?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Category: bug » support

Sounds like you are on the right track.

Fatal error: Cannot redeclare comment_page_title_alter() (previously declared in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc:13) in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc on line 27

That looks like a bug in that particular module, don't you think?

egarias’s picture

Project: Drupal core » Page Title
Version: 7.0 » 7.x-2.4-beta1
Component: base system » Code
Category: support » bug

You are right Damien so i understand:

  • I change the project to the right one.
  • I will need to add as required:
define('DRUPAL_ROOT', getcwd());
include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');

in my php custom programs.
And I keep waiting for page title team for indications regarding the issue

nicholasThompson’s picture

Status: Active » Closed (fixed)

I've added a fix for this in the latest dev - it has a static variable to protect the includes from running more than once.

egarias’s picture

Ok fixed

Everett Zufelt’s picture

Version: 7.x-2.4-beta1 » 7.x-2.7
Status: Closed (fixed) » Active

I wouldn't usually open an old issue, but I am getting the same error in 7.x-2.7, installed on a fresh build of a site today.

Any thoughts?

nicholasThompson’s picture

Could you please describe your system setup and the steps we need to follow to reproduce?

If it is happening again, it's likely due to this section:
http://drupalcode.org/project/page_title.git/blob/ce49055682b59b77b15fdf...

Everett Zufelt’s picture

download drupal 7.15
install drupal
drush dl page_title
drush en page_title
drush cc all

Should reproduce the error

Everett Zufelt’s picture

To be clear, the error is similiar to:

Fatal error: Cannot redeclare comment_page_title_alter() (previously declared in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc:13)
in /home/empresa/public_html/sites/all/modules/page_title/modules/comment.page_title.inc on line 27

Everett Zufelt’s picture

Closing again, it is clear that this should work, it must be another part of the code that is resetting the static.

Everett Zufelt’s picture

Status: Active » Closed (fixed)
stella’s picture

Status: Closed (fixed) » Needs review
FileSize
626 bytes

Sorry to reopen this but I still encounter this issue when I enable the 'page_title' module as part of the setUp() function in a simpletest.

The error received is the same:

An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /batch?id=98&op=do StatusText: OK ResponseText: Fatal error: Cannot redeclare comment_page_title_alter() (previously declared in page_title/modules/comment.page_title.inc:12) in page_title/modules/comment.page_title.inc on line 30

I've changed it so it calls an "include_once" rather than an "include", in line with Drupal core modules, etc, though I know you're worried about a performance hit, but at least this way it works. Patch attached.

bpresles’s picture

I got this issue when running a SimpleTest test from command line (Drush), that uses an installation profile in which there is page_title module as a dependency.

The include_once proposed patch does solve this issue. But I provide another patch that doesn't use include_once, and instead use a constant defined in each sub modules respecting a unique naming convention (PAGE_TITLE_<MODULE_NAME>_SUB_MODULE (e.g: PAGE_TITLE_COMMENT_SUB_MODULE)), and added a check to avoid to perform the include if the constant for the required module is defined (see attached patch).

I think it's a better solution to solve this issue regarding performances.

Ctrl-E’s picture

+1 for this. include_once solved issue running commands via drush for me, thanks. But now I have a patched module. Any possibility of a permanent fix in future updates?

  • nicholasThompson committed c70e932 on master
    #1024624 - fix to avoid accidentally including files more than once.
    
    

  • nicholasThompson committed c70e932 on 8.x-2.x
    #1024624 - fix to avoid accidentally including files more than once.
    
    
torotil’s picture

This seems to me a lot like premature optimization. include_once / require_once are surely one of the most optimized operations in PHP. Is there any profiling / performance problem that justifies the use of a non-standard / complicated workaround for not using include_once?