Forum

Posted by
ITANI Eiichiro  -  November 2011
I'm having problems related to <use> tag and page scope.

1. page scope variables get reset after <use> (hit on Roxen4.5)

My site has pages like below:

  <use file="/lib/basic_lib" />  <!-- contains local tag defines -->
  <tag_page_info_setup />  <!-- This tag sets some variables to page scope -->
  <use file="/lib/extend_lib" />  <!-- contains another local tag defs. -->

<tag_page_info_setup /> sets up some variables in page scope,like
page.cur_month, page.site_https_top_url, and so on.
But after second <use>, variables set in above tag cleared. Page scope seems containinig only variables Roxen initially sets.

2. Sometimes page scope variable become wrong (hit on Roxen 5.1)

I have pages like below.

  /index.html
  /subdir/index.html

Both page use common file with use tag like below:

  <use file="/lib/basic_lib" />

After restarting roxen, I accessed /index.html.  Then go to /subdir/index.html,  sometimes page.self become "/index.html".  This modification occurs after <use file /> evaluation.

  &page.self;
  <use file="/lib/basic_lib" />
  &page.self;

will produce these output:

  /subdir/index.html
  /index.html

These two results are not what I expected.  <use file> can be used as if "import" or "include", which does not affect current page/var/form scopes.

Is use file remembers initialy loaded environment and recover them?
And another question, setting user defined variables to page scope is  not recommended?


 
Posted by
ITANI Eiichiro  -  November 2011
I'm sorry, problem 2 written above is caused by my modification to rxmltags.pike.  I don't remember why and for what and from where I applied this modification, anyway, it's not upstream roxen problem.

So please forget about 2nd problem.
 
Posted by
ITANI Eiichiro  -  November 2011
About first problem, I believe I've found when it occurs.

In rxmltags.pike, class TagUse::do_enter has lines below (line 4475 at v 1.648):

      foreach (newdefs; string defname; mixed def) {
ctx->misc[defname] = def;
if (has_prefix (defname, "tag\0")) ctx->add_runtime_tag (def[3]);
      }

newdefs contains members  "scope_page" and "scope_rxml".

When value of misc->scope_page or misc->scope_roxen is empty mapping,  it seems that scope rebuild from what initially set instead of what currently has.

If mapping newdefs->scope_page or scope_roxen has something, that scope seems to be extended by what mapping has with what current scope holds.  So it would be nicer to modify rxmltags.pike as:

      foreach (newdefs; string defname; mixed def) {

if (has_prefix(defname,"scope_") && sizeof(def) == 0) {
  continue;
ctx->misc[defname] = def;
if (has_prefix (defname, "tag\0")) ctx->add_runtime_tag (def[3]);
      }

 
Posted by
Martin Stjernholm  -  November 2011
Had a look at this now.

I'm not quite sure what you mean, but yes, proper treatment of those special mappings are lacking there (they're not the actual scopes, they just keep track of user overrides in them). Could you please try if this patch makes it behave better?

--- modules/tags/rxmltags.pike 30 Sep 2011 09:57:53 -0000 1.648
+++ modules/tags/rxmltags.pike 26 Nov 2011 23:04:52 -0000
@@ -4473,8 +4473,20 @@
        mapping(string:mixed)|RXML.Scope formvars,
        mapping(string:mixed)|RXML.Scope varvars] = res;
       foreach (newdefs; string defname; mixed def) {
- ctx->misc[defname] = def;
- if (has_prefix (defname, "tag\0")) ctx->add_runtime_tag (def[3]);
+ if (defname == "scope_roxen" || defname == "scope_page") {
+   // The user override mappings for the "roxen" and "page"
+   // scopes. Merge with existing mappings.
+   mapping(string:mixed) tgt_map = ctx->misc[defname];
+   if (!tgt_map)
+     ctx->misc[defname] = def + ([]);
+   else
+     foreach (def; string var; mixed val)
+       tgt_map[var] = val;
+ }
+ else {
+   ctx->misc[defname] = def;
+   if (has_prefix (defname, "tag\0")) ctx->add_runtime_tag (def[3]);
+ }
       }
       ctx->extend_scope ("form", formvars);
       ctx->extend_scope ("var", varvars);

The forum code messes it up a bit, but patch -l should still be able to apply it, I hope.
 
Posted by
ITANI Eiichiro  -  November 2011
Thank you for your reply.

I have patched rxmltags.pike(I sent my modification to bugzilla, #6130), and tested your patch too. And both work good (at least for my expectation).  So if this merged into upstream, I'll appreciate.

By the way, I have three question about your patch, just for my pike study.

1.
  ctx->misc[defname] = def + ([]);

My private modification is below, is this not enough?

 ctx->misc[defname] = def;

Mmm, that intention is  to create new mapping instance by adding ([]), am I right?

2. Are second level foreach loop required?  I guess

  ctx->misc[defname] += def;

will do same thing.

3.  Is this works better

  if (defname == "scope_roxen" || defname == "scope_page" )

than writing like below?

  if (has_prefix(defname,"scope_"))



Anyway, thank you for having checked this post. :)
 
 
 
Posted by
Henrik Grubbstrom  -  November 2011
I'm not mast, but...

> 1.  ctx->misc[defname] = def + ([]);
>
> My private modification is below, is this not enough?
>
>  ctx->misc[defname] = def;
>
> Mmm, that intention is  to create new mapping instance by adding ([]), am I right?

Correct. The usual reason is to protect the original mapping from modification.

> 2. Are second level foreach loop required?  I guess
>
>   ctx->misc[defname] += def;
>
> will do same thing.

As far as I can see, yes, that should do the same thing.

> 3.  Is this works better
>
>   if (defname == "scope_roxen" || defname == "scope_page" )
>
> than writing like below?
>
>   if (has_prefix(defname,"scope_"))

It looks like it's mainly a case of paranoia, in case there's some other scope_* that shouldn't be copied.
 
Posted by
Martin Stjernholm  -  November 2011
1. Right.

2. The second loop is required to modify the target mapping instead of replacing it.

3. Partly because I believe the equality checks are faster, and partly (more importantly) to signify that this code concerns those two special cases only. There is no generic scope_* thing in ctx->misc; the actual scopes are stored in another place.
 
Posted by
Martin Stjernholm  -  November 2011
I've now also committed my suggested patch (and closed [bug 6130] as well).

Thank you.
 
Posted by
ITANI Eiichiro  -  December 2011
Thank you for your description, mast, grubba.

I applied your patch, and it now works good.  Thanks again!
 
1
Search this thread: