Problems with static variables

jennycita - November 15, 2007 - 04:37
Project:Page Array
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

I ran into some issues with the menu active trail and blocks...

From menu.inc:

<?php
function _menu_get_active_trail() {
  static
$trail;
...
?>

...and block.module:

<?php
function block_list($region) {
  global
$user, $theme_key;

  static
$blocks = array();
...
?>

...as you can see, both of these items are static.

If pagearray_page() is called very early (say, as part of a menu callback), you get lovely correct information (corresponding to the $path argument) in the array passed back... but then the active trail and blocks of your original page are skewed.

If pagearray_page() is called later on (say, from within the content of a node), the active trail and blocks of your original page remain intact, but this information is replicated inside the array passed back, rather than being changed to reflect the path passed in.

Unfortunately, I don't have a good solution for this. I found myself in the first situation and have attached a patch that will help anyone else having the same problem... but it will render active trail and block information in the returned array permanently incorrect.

AttachmentSize
patch.txt905 bytes

#1

nedjo - November 15, 2007 - 21:19

Thanks for the note. I wasn't aware of these issues. There are many static variables used in core without available reset options. I'm also not sure what we should be doing here. Should we make the behaviour depend on an argument fed into pagearray_page()?

#2

jennycita - November 16, 2007 - 19:41

Yeah... I had a nagging feeling that there were probably other static variables whose side effects I hadn't yet run into.

I don't think there's a way to get around the potential side effects by using an argument -- you're asking module developers to have a really good understanding of Drupal core and when in the call stack problems with static variables might occur.

I think a separate http request is in order... to give the static variables a chance to reset. How to accomplish that is a bit beyond the scope of my knowledge... I think xmlrpc provides that functionality?

#3

nedjo - November 16, 2007 - 20:36

I think a separate http request is in order.

That's a creative line of thinking and one we should explore. Let's consider XMLRPC. Something like:

<?php
/**
* Implementation of hook_xmlrpc().
*/
function pagearray_xmlrpc() {
 
$xmlrpc = array();
 
$xmlrpc[] = array(
   
'pagearray.page',
   
'pagearray_page',
    array(
'array', 'string'),
   
t('Handling page request')
  );
  return
$xmlrpc;
}

/**
* Fetch a page via XMLRPC. We use a separate page request to avoid issues
* with static variables set on initial page request.
*/
function pagearray_get($path) {
  global
$base_url;
  return
xmlrpc($base_url .'/xmlrpc.php', 'pagearray.page', $path);
}
?>

Then a request would look like:

<?php
$result
= pagearray_get('my/path');
?>

What about session/authentication implications though? I'm fuzzy on how this works. Is the session retained, or is the connection anonymous?

 
 

Drupal is a registered trademark of Dries Buytaert.