What about adding a script variable for the document the script is written into?

When investigating why my custom PDF template stopped loading its attachments when exporting a page, after an upgrade (Custom PDF template broken after upgrade to 14.10.18), I learnt that doc recerences the document being processed, that is, if page-A is the content and page-B is the PDF template, doc written in B references A.

Now, consider that it was my first pdf template and at the same time the first velocity script I’ve ever writter.
And that the explanations and examples mflorea gave me were totally clear.

Though this meant that:

  • in my custom template I couln’t get a reference to the document itself;
  • I had to pick the right complete path (an extra .WebHome is needed beside the path in the browser);
  • moving the template under another path would break its usage in an Export.

Would it make sense to add another variable to reference the document the script belongs to, beside the document being processed?

I think it makes sense, but the issue is finding the right variable name that doesn’t break existing scripts that might already use a variable with the same name (possibly with a different meaning).

I guess that’s close to impossible, if you’re referring to existing user written scripts, of course.
But, what if the new variable name is already being used? Would it just mask the existing (XWiki defined) one? Or could $doc = 1 have side effects on page processing?

Sorted that out, the only thing I see you could do is to have some helper tool that would scan user scripts and somehow highlight where there could be issues.

Moreover, the only way to be sure about that would be to define some keyword / convention, such as: variables starting with an _ (underscore), with a $xw prefix, reserve a specific namespace or package, and so forth.

Or, maybe, take the chance now and start defining such convention? E.g. $xw_edoc (document embedding the current script).

Should I open an issue on Jira for this, or are you handling it differently?

Hi, I thought

(1) a script which is stored in an object of type XWiki.WikiMacroClass has the object xwiki.context where


delivers the full name the document containing the macro

(and sometimes usefull: xwiki.context.macro.context.getId() the name of the document using the macro)

(2) if the document containing the code will be included with the include macro, the parameter context exists with the possible values “new” or “current”. When using the non default value “new” the variable $doc.fullName will contain the full name of the document containing the code.

May be this helps

Bump :upside_down_face: