[xwiki-devs] best practice to programme with XWiki objects?
hello fellow developers, at Curriki, we are starting to process XWiki objects in the background using subclasses of AbstractXWikiRunnable. Currently I do the following: // setup XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); Context context = new Context(xcontext); XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getXWiki(), xcontext); // fetch background info Document d = xwiki.getDocument(DOCNAME_x); Object obj = d.getObject("DOCNAME_class"); // modify something // save d.save("comment"); However, I wonder if this is the best practice: - is it expensive to construct a context and xwiki supposing this setup code is run multiple times? - is it preferrable to work with the com.xpn.xwiki.* classes? - is DocReference behaving much differently than fullName if we have a single wiki? Thanks in advance. Paul
Hi Paul, On 09/12/2012 11:47 PM, Paul Libbrecht wrote:
hello fellow developers,
at Curriki, we are starting to process XWiki objects in the background using subclasses of AbstractXWikiRunnable.
Currently I do the following:
// setup XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); Context context = new Context(xcontext); XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getXWiki(), xcontext);
// fetch background info Document d = xwiki.getDocument(DOCNAME_x);
Object obj = d.getObject("DOCNAME_class"); // modify something
// save d.save("comment");
However, I wonder if this is the best practice:
- is it expensive to construct a context and xwiki supposing this setup code is run multiple times?
As far as I know there is not much overhead to construct the api wrapper.
- is it preferrable to work with the com.xpn.xwiki.* classes?
Now I tend to prefer working as much as possible with api.* classes rather than internal classes, for no specific reason other than the fact they are proper APIs vs. de facto APIs and that I'm sure I have the same behavior as when writing scripts.
- is DocReference behaving much differently than fullName if we have a single wiki?
Yes in the sense it handles escaping properly. It matters if you don't have control over document names, it matters less if you do have this control (for example you generate the name automatically or clean user input with defined rules). I try to use the reference (a.k.a new model) API as often as possible, but I admit sometimes it feels just too much when you know you're not having escaping issues or multiwiki issues. Those times I do things like xwiki.getDocument("XWiki.XWikiPreferences") and just live with it happily :) Of course I'm speaking about my experience of using XWiki for my own needs as an API consumer, not about the development of the XWiki platform. That was my 2 cents, hope it helped having perspective on what others do, Jerome
Thanks in advance.
Paul _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Peace, —Jerome
On Thu, Sep 13, 2012 at 12:14 AM, Jerome Velociter <[email protected]> wrote:
Hi Paul,
On 09/12/2012 11:47 PM, Paul Libbrecht wrote:
hello fellow developers,
at Curriki, we are starting to process XWiki objects in the background using subclasses of AbstractXWikiRunnable.
Currently I do the following:
// setup XWikiContext xcontext = (XWikiContext) Utils.getComponent(Execution.class).getContext() .getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); Context context = new Context(xcontext); XWiki xwiki = new com.xpn.xwiki.api.XWiki(context.getXWiki(), xcontext);
// fetch background info Document d = xwiki.getDocument(DOCNAME_x);
Object obj = d.getObject("DOCNAME_class"); // modify something
// save d.save("comment");
However, I wonder if this is the best practice:
There is no official best practice actually so it's going to be my personal point of view.
- is it expensive to construct a context and xwiki supposing this setup code is run multiple times?
As far as I know there is not much overhead to construct the api wrapper.
Not by creating the API wrapper itself but keep in mind that every time you call some of the script API methods (like save and getDocument in your example) you will have a right test behind the scene for many of them.
- is it preferrable to work with the com.xpn.xwiki.* classes?
IMO it mostly depends if you want right test or not. For example in your example your save will not work if whatever user you have in your context does not have edit right on that document (same thing with getDocument and view right). Sometimes that's exactly what you want (we use the public API a lot in the REST resources for example) and sometime it's a daemon thread which doesn't care about rights. If you are in a Java code where you know you can do everything and don't care about the rights you should use the com.xpn.xwiki.* classes. Those class have the same level of retro compatibility than the api.* classes so that should not be an issue.
Now I tend to prefer working as much as possible with api.* classes rather than internal classes, for no specific reason other than the fact they are proper APIs vs. de facto APIs and that I'm sure I have the same behavior as when writing scripts.
- is DocReference behaving much differently than fullName if we have a single wiki?
Yes in the sense it handles escaping properly. It matters if you don't have control over document names, it matters less if you do have this control (for example you generate the name automatically or clean user input with defined rules). I try to use the reference (a.k.a new model) API as often as possible, but I admit sometimes it feels just too much when you know you're not having escaping issues or multiwiki issues. Those times I do things like xwiki.getDocument("XWiki.XWikiPreferences") and just live with it happily :) Of course I'm speaking about my experience of using XWiki for my own needs as an API consumer, not about the development of the XWiki platform.
Not much to add here but I think I'm using document reference way more than Jerome even wen I "control the name" ;) When you do xwiki.getDocument("XWiki.XWikiPreferences") a resolver is called to parse ir and resolve the current wiki to create the DocumentReference so it's better to create a DocumentReference directly when you know all the three parts.
That was my 2 cents, hope it helped having perspective on what others do, Jerome
Thanks in advance.
Paul _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Peace, —Jerome
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Thomas Mortagne
Thanks for sharing Thomas and Jérôme,
However, I wonder if this is the best practice: There is no official best practice actually so it's going to be my personal point of view.
That's already good (and good to know).
- is it expensive to construct a context and xwiki supposing this setup code is run multiple times? As far as I know there is not much overhead to construct the api wrapper.
Not by creating the API wrapper itself but keep in mind that every time you call some of the script API methods (like save and getDocument in your example) you will have a right test behind the scene for many of them.
This is a very important point. Is there no way to still have the API luxury and disable rights' check? If no, then indeed, it would be useful to migrate to the base classes but they seem to be harder to work with.
If you are in a Java code where you know you can do everything and don't care about the rights you should use the com.xpn.xwiki.* classes. Those class have the same level of retro compatibility than the api.* classes so that should not be an issue.
I could attempt this but I had a much harder time to do that.
- is DocReference behaving much differently than fullName if we have a single wiki?
Yes in the sense it handles escaping properly. It matters if you don't have control over document names, it matters less if you do have this control (for example you generate the name automatically or clean user input with defined rules). I try to use the reference (a.k.a new model) API as often as possible, but I admit sometimes it feels just too much when you know you're not having escaping issues or multiwiki issues. Those times I do things like xwiki.getDocument("XWiki.XWikiPreferences") and just live with it happily :) Of course I'm speaking about my experience of using XWiki for my own needs as an API consumer, not about the development of the XWiki platform.
Not much to add here but I think I'm using document reference way more than Jerome even wen I "control the name" ;)
When you do xwiki.getDocument("XWiki.XWikiPreferences") a resolver is called to parse ir and resolve the current wiki to create the DocumentReference so it's better to create a DocumentReference directly when you know all the three parts.
So here's a simple question: how do you simply create a document reference with a String? I think the area of working with the base class is where I miss a tutorial. paul
On Thu, Sep 13, 2012 at 11:59 AM, Paul Libbrecht <[email protected]> wrote:
Thanks for sharing Thomas and Jérôme,
However, I wonder if this is the best practice: There is no official best practice actually so it's going to be my personal point of view.
That's already good (and good to know).
- is it expensive to construct a context and xwiki supposing this setup code is run multiple times? As far as I know there is not much overhead to construct the api wrapper.
Not by creating the API wrapper itself but keep in mind that every time you call some of the script API methods (like save and getDocument in your example) you will have a right test behind the scene for many of them.
This is a very important point. Is there no way to still have the API luxury and disable rights' check? If no, then indeed, it would be useful to migrate to the base classes but they seem to be harder to work with.
Well I don't find the java API much more complex than the script API, its pretty much the same things. The main differences are: * you have to pass the context in most of the methods * you save a document by doing xwiki.save(document) instead of document.save() If you look at the api.* classes implementations you can see that most of the methods just call the same methods and add context as parameter (and sometimes not even).
If you are in a Java code where you know you can do everything and don't care about the rights you should use the com.xpn.xwiki.* classes. Those class have the same level of retro compatibility than the api.* classes so that should not be an issue.
I could attempt this but I had a much harder time to do that.
- is DocReference behaving much differently than fullName if we have a single wiki?
Yes in the sense it handles escaping properly. It matters if you don't have control over document names, it matters less if you do have this control (for example you generate the name automatically or clean user input with defined rules). I try to use the reference (a.k.a new model) API as often as possible, but I admit sometimes it feels just too much when you know you're not having escaping issues or multiwiki issues. Those times I do things like xwiki.getDocument("XWiki.XWikiPreferences") and just live with it happily :) Of course I'm speaking about my experience of using XWiki for my own needs as an API consumer, not about the development of the XWiki platform.
Not much to add here but I think I'm using document reference way more than Jerome even wen I "control the name" ;)
When you do xwiki.getDocument("XWiki.XWikiPreferences") a resolver is called to parse ir and resolve the current wiki to create the DocumentReference so it's better to create a DocumentReference directly when you know all the three parts.
So here's a simple question: how do you simply create a document reference with a String? I think the area of working with the base class is where I miss a tutorial.
See http://extensions.xwiki.org/xwiki/bin/view/Extension/Model+Module for details and examples about references in general. Now it depends on what is your input exactly, if what you get from the user is the actual full name of the document as String then you will not add much value when converting it on you side before calling getDocument obviously.
paul _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Thomas Mortagne
participants (3)
-
Jerome Velociter -
Paul Libbrecht -
Thomas Mortagne