[xwiki-users] xwiki Macro using groovy
Hi everyone, Thanks so much for your quick answer on my previous question. I am trying to write a bibliography/cite macro (I am happy to share once it dies something useful). I am writing the macro in a wiki page and I am using groovy as scripting language. I would like to have access to the URL of an attachment of the page the macro is used on. I am doing so by using doc.getAttachmentURL( <name> ); however, the URL uses the page the Macro is saved in, not the page the macro is used on. I think I managed this before using velocity. So my question: what would be the right way of accessing the Document of the page the macro is placed on? Thanks for your help, --Alaina
Some time ago we have implemented some proof of concept in: http://www.ante.lv/xwiki/bin/view/References/ Feel free to investigate it and steal ideas. (I can provide XAR file, if you are interested). Valdis
Hi everyone,
Thanks so much for your quick answer on my previous question.
I am trying to write a bibliography/cite macro (I am happy to share once it dies something useful). I am writing the macro in a wiki page and I am using groovy as scripting language.
I would like to have access to the URL of an attachment of the page the macro is used on. I am doing so by using
doc.getAttachmentURL( <name> );
however, the URL uses the page the Macro is saved in, not the page the macro is used on. I think I managed this before using velocity. So my question: what would be the right way of accessing the Document of the page the macro is placed on?
Thanks for your help, --Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Thu, Mar 18, 2010 at 17:14, Alaina <[email protected]> wrote:
Hi everyone,
Thanks so much for your quick answer on my previous question.
I am trying to write a bibliography/cite macro (I am happy to share once it dies something useful). I am writing the macro in a wiki page and I am using groovy as scripting language.
I would like to have access to the URL of an attachment of the page the macro is used on. I am doing so by using
doc.getAttachmentURL( <name> );
however, the URL uses the page the Macro is saved in, not the page the macro is used on. I think I managed this before using velocity. So my question: what would be the right way of accessing the Document of the page the macro is placed on?
* doc is the current document * context.doc is the wiki macro document
Thanks for your help, --Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
Hi Thomas, Thanks for your comments * doc is the current document * context.doc is the wiki macro document That is indeed the case for velocity. However if I am using groovy I get different results. I have the following XWiki Macro groovy-test in Page GroovyTest {{velocity}} {{/velocity}} {{velocity}} $doc.getAttachmentURL("test.dat") {{/velocity}} {{groovy}} print("{{html}}"); print( doc.getAttachmentURL("test.dat") ); println("{{/html}}"); {{/groovy}} if I put {{groovy-test /}} in the body of the page I get the following output /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat no surprise. However, If i put the same page body int the page GroovyTestExternal the output is /xwiki/bin/download/Macros/GrovvyTestExternal/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat thus the difference is with the macro language. What should I use in order to get the expected output (GroovyTestExternal) in groovy? Thanks so much, --Alaina On Thu, Mar 18, 2010 at 12:14 PM, Alaina <[email protected]> wrote:
Hi everyone,
Thanks so much for your quick answer on my previous question.
I am trying to write a bibliography/cite macro (I am happy to share once it dies something useful). I am writing the macro in a wiki page and I am using groovy as scripting language.
I would like to have access to the URL of an attachment of the page the macro is used on. I am doing so by using
doc.getAttachmentURL( <name> );
however, the URL uses the page the Macro is saved in, not the page the macro is used on. I think I managed this before using velocity. So my question: what would be the right way of accessing the Document of the page the macro is placed on?
Thanks for your help, --Alaina
On Thu, Mar 18, 2010 at 19:38, Alaina <[email protected]> wrote:
Hi Thomas,
Thanks for your comments
* doc is the current document * context.doc is the wiki macro document
Sorry context is deprecated actually and works only with velocity, it's: * doc is the current document * xcontext.doc is the wiki macro document Looks like i'm too old on XWiki :)
That is indeed the case for velocity. However if I am using groovy I get different results.
I have the following XWiki Macro groovy-test in Page GroovyTest
{{velocity}} {{/velocity}} {{velocity}} $doc.getAttachmentURL("test.dat") {{/velocity}} {{groovy}} print("{{html}}"); print( doc.getAttachmentURL("test.dat") ); println("{{/html}}"); {{/groovy}}
if I put {{groovy-test /}} in the body of the page I get the following output
/xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
no surprise. However, If i put the same page body int the page GroovyTestExternal the output is
/xwiki/bin/download/Macros/GrovvyTestExternal/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
thus the difference is with the macro language. What should I use in order to get the expected output (GroovyTestExternal) in groovy?
Thanks so much, --Alaina
On Thu, Mar 18, 2010 at 12:14 PM, Alaina <[email protected]> wrote:
Hi everyone,
Thanks so much for your quick answer on my previous question.
I am trying to write a bibliography/cite macro (I am happy to share once it dies something useful). I am writing the macro in a wiki page and I am using groovy as scripting language.
I would like to have access to the URL of an attachment of the page the macro is used on. I am doing so by using
doc.getAttachmentURL( <name> );
however, the URL uses the page the Macro is saved in, not the page the macro is used on. I think I managed this before using velocity. So my question: what would be the right way of accessing the Document of the page the macro is placed on?
Thanks for your help, --Alaina
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
Hi Thomas, Sorry to keep asking. I was aware xcontext the point is doc and xcontext.doc give the same result. Can you please reread the comment about the test below? I think it is a bug. Thanks for your patience, --Alaina On Thu, Mar 18, 2010 at 2:38 PM, Alaina <[email protected]> wrote:
Hi Thomas,
Thanks for your comments
* doc is the current document * context.doc is the wiki macro document
Sorry context is deprecated actually and works only with velocity, it's: * doc is the current document * xcontext.doc is the wiki macro document Looks like i'm too old on XWiki :)
That is indeed the case for velocity. However if I am using groovy I get different results.
I have the following XWiki Macro groovy-test in Page GroovyTest
{{velocity}} {{/velocity}} {{velocity}} $doc.getAttachmentURL("test.dat") {{/velocity}} {{groovy}} print("{{html}}"); print( doc.getAttachmentURL("test.dat") ); print( xcontext.doc.getAttachmentURL("test.dat",xcontext.getContext()); println("{{/html}}"); {{/groovy}}
if I put {{groovy-test /}} in the body of the page I get the following output
/xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
no surprise. However, If i put the same page body int the page GroovyTestExternal the output is
/xwiki/bin/download/Macros/GroovyTestExternal/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
thus the difference is with the macro language. What should I use in order to get the expected output (GroovyTestExternal) in groovy?
Thanks so much, --Alaina
HI everyone, I still think the behavior of the groovy script variable doc in a xwiki macro is not as intended. But I am using the following workaround. Put the groovy code in a groovy class stored in a different wiki page (Macros.CiteGroovyClass) . There is documentation here ( http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyClassHelloWorldTutor... ) on how to do that. Then the following velocity code in the macro definition will give the correct doc Object to the groovy code. {{velocity}} #set($groovyObject = $xwiki.parseGroovyFromPage("Macros.CiteGroovyClass")) $groovyObject.setObjects($xwiki, $doc, $context) $groovyObject.process() {{/velocity}} Thanks for all your help, --Alaina
That is indeed the case for velocity. However if I am using groovy I get different results.
I have the following XWiki Macro groovy-test in Page GroovyTest
{{velocity}} {{/velocity}} {{velocity}} $doc.getAttachmentURL("test.dat") {{/velocity}} {{groovy}} print("{{html}}"); print( doc.getAttachmentURL("test.dat") ); print( xcontext.doc.getAttachmentURL("test.dat",xcontext.getContext()); println("{{/html}}"); {{/groovy}}
if I put {{groovy-test /}} in the body of the page I get the following output
/xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
no surprise. However, If i put the same page body int the page GroovyTestExternal the output is
/xwiki/bin/download/Macros/GroovyTestExternal/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
thus the difference is with the macro language. What should I use in order to get the expected output (GroovyTestExternal) in groovy?
Thanks so much, --Alaina
Hi Alaina, On Fri, Mar 19, 2010 at 4:46 PM, Alaina <[email protected]> wrote:
HI everyone,
I still think the behavior of the groovy script variable doc in a xwiki macro is not as intended. But I am using the following workaround.
Sorry for not looking into this earlier. I'm a bit stuck on some other issues. Will look into this issue and report a JIRA as soon as I get some free time. Thanks for raising the issue :) - Asiri
Put the groovy code in a groovy class stored in a different wiki page (Macros.CiteGroovyClass) . There is documentation here (
http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyClassHelloWorldTutor... ) on how to do that. Then the following velocity code in the macro definition will give the correct doc Object to the groovy code.
{{velocity}} #set($groovyObject = $xwiki.parseGroovyFromPage("Macros.CiteGroovyClass")) $groovyObject.setObjects($xwiki, $doc, $context) $groovyObject.process() {{/velocity}}
Thanks for all your help, --Alaina
That is indeed the case for velocity. However if I am using groovy I get different results.
I have the following XWiki Macro groovy-test in Page GroovyTest
{{velocity}} {{/velocity}} {{velocity}} $doc.getAttachmentURL("test.dat") {{/velocity}} {{groovy}} print("{{html}}"); print( doc.getAttachmentURL("test.dat") ); print( xcontext.doc.getAttachmentURL("test.dat",xcontext.getContext()); println("{{/html}}"); {{/groovy}}
if I put {{groovy-test /}} in the body of the page I get the following output
/xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
no surprise. However, If i put the same page body int the page GroovyTestExternal the output is
/xwiki/bin/download/Macros/GroovyTestExternal/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat /xwiki/bin/download/Macros/GroovyTest/test.dat
thus the difference is with the macro language. What should I use in order to get the expected output (GroovyTestExternal) in groovy?
Thanks so much, --Alaina
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
Hi Again, On Fri, Mar 19, 2010 at 5:16 PM, Asiri Rathnayake < [email protected]> wrote:
Hi Alaina,
On Fri, Mar 19, 2010 at 4:46 PM, Alaina <[email protected]>wrote:
HI everyone,
I still think the behavior of the groovy script variable doc in a xwiki macro is not as intended. But I am using the following workaround.
Sorry for not looking into this earlier. I'm a bit stuck on some other issues.
Will look into this issue and report a JIRA as soon as I get some free time.
Thanks for raising the issue :)
- Asiri
Put the groovy code in a groovy class stored in a different wiki page (Macros.CiteGroovyClass) . There is documentation here (
http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyClassHelloWorldTutor... ) on how to do that. Then the following velocity code in the macro definition will give the correct doc Object to the groovy code.
{{velocity}} #set($groovyObject = $xwiki.parseGroovyFromPage("Macros.CiteGroovyClass")) $groovyObject.setObjects($xwiki, $doc, $context) $groovyObject.process() {{/velocity}}
Can you try something like: {{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}} (in macro code) I'm just guessing, need to investigate later. - Asiri
On Fri, Mar 19, 2010 at 7:52 AM, Asiri Rathnayake <[email protected]> wrote:
Can you try something like:
{{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}}
(in macro code)
I'm just guessing, need to investigate later.
- Asiri
Hi Asiri, Thanks, I tried your suggestion, it works as intended. --Alaina
hmm i think i know what happen, there is a major difference between velocity and other script languages binding: the VelocityContext is filled at request startup and ScriptContext is filled the first time it's used, so the first time a groovy macro is used for example. So in groovy macro "doc" binding is provided by looking at the context each time a groovy macro is executed. That is to make sure the binding is properly synchronized with the context. Only real solution i can see is to fix http://jira.xwiki.org/jira/browse/XWIKI-4262 (and have the proper current document in the context instead of the current hack) and provide a $context.macro.document (this should be done anyway even before XWIKI-4262 is fixed since it's part of the macro informations). Asiri in the meantime i think you should set "sdoc" in context instead of "doc". I far as i can see in the code hasProgrammingRights do the following: * try sdoc content author * if no sdoc try doc content author * if no doc try current user On Fri, Mar 19, 2010 at 16:20, Alaina <[email protected]> wrote:
On Fri, Mar 19, 2010 at 7:52 AM, Asiri Rathnayake <[email protected]> wrote:
Can you try something like:
{{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}}
(in macro code)
I'm just guessing, need to investigate later.
- Asiri
Hi Asiri,
Thanks, I tried your suggestion, it works as intended.
--Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
Hi thomas,
provide a $context.macro.document (this should be done anyway even before XWIKI-4262 is fixed since it's part of the macro informations).
+1
Asiri in the meantime i think you should set "sdoc" in context instead of "doc". I far as i can see in the code hasProgrammingRights do the following: * try sdoc content author * if no sdoc try doc content author * if no doc try current user
I discussed setting "sdoc" previously with sergiu and on the dev chat, however it was not approved :( Thanks. - Asiri
On Fri, Mar 19, 2010 at 16:20, Alaina <[email protected]> wrote:
On Fri, Mar 19, 2010 at 7:52 AM, Asiri Rathnayake <[email protected]> wrote:
Can you try something like:
{{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}}
(in macro code)
I'm just guessing, need to investigate later.
- Asiri
Hi Asiri,
Thanks, I tried your suggestion, it works as intended.
--Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
On Sat, Mar 20, 2010 at 16:46, Asiri Rathnayake <[email protected]> wrote:
Hi thomas,
provide a $context.macro.document (this should be done anyway even before XWIKI-4262 is fixed since it's part of the macro informations).
+1
Asiri in the meantime i think you should set "sdoc" in context instead of "doc". I far as i can see in the code hasProgrammingRights do the following: * try sdoc content author * if no sdoc try doc content author * if no doc try current user
I discussed setting "sdoc" previously with sergiu and on the dev chat, however it was not approved :(
Using "sdoc" is not the solution and i would be -1 for it from scratch but it's way better than using "doc" until we find a better way.
Thanks.
- Asiri
On Fri, Mar 19, 2010 at 16:20, Alaina <[email protected]> wrote:
On Fri, Mar 19, 2010 at 7:52 AM, Asiri Rathnayake <[email protected]> wrote:
Can you try something like:
{{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}}
(in macro code)
I'm just guessing, need to investigate later.
- Asiri
Hi Asiri,
Thanks, I tried your suggestion, it works as intended.
--Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
On Sat, Mar 20, 2010 at 17:27, Thomas Mortagne <[email protected]> wrote:
On Sat, Mar 20, 2010 at 16:46, Asiri Rathnayake <[email protected]> wrote:
Hi thomas,
provide a $context.macro.document (this should be done anyway even before XWIKI-4262 is fixed since it's part of the macro informations).
+1
http://jira.xwiki.org/jira/browse/XWIKI-5031
Asiri in the meantime i think you should set "sdoc" in context instead of "doc". I far as i can see in the code hasProgrammingRights do the following: * try sdoc content author * if no sdoc try doc content author * if no doc try current user
I discussed setting "sdoc" previously with sergiu and on the dev chat, however it was not approved :(
Using "sdoc" is not the solution and i would be -1 for it from scratch but it's way better than using "doc" until we find a better way.
Thanks.
- Asiri
On Fri, Mar 19, 2010 at 16:20, Alaina <[email protected]> wrote:
On Fri, Mar 19, 2010 at 7:52 AM, Asiri Rathnayake <[email protected]> wrote:
Can you try something like:
{{groovy}} def currentDoc = xcontext.vcontext.doc; {{/groovy}}
(in macro code)
I'm just guessing, need to investigate later.
- Asiri
Hi Asiri,
Thanks, I tried your suggestion, it works as intended.
--Alaina _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
-- Thomas Mortagne
participants (4)
-
Alaina -
Asiri Rathnayake -
Thomas Mortagne -
Valdis Vītoliņš