Details
-
Bug
-
Resolution: Fixed
-
Critical
-
1.4.1, 1.7 M2
-
None
Description
/** * @return the name of the wiki where this element is stored. If null, the context's wiki is used. */ public String getWiki() { return wiki; }
This excerpt above from Base Element suggest that if the wiki (database) is null, the context's wiki (database) should be used.
public BaseClass getxWikiClass(XWikiContext context) { if ((context == null) || (context.getWiki() == null)) { return null; } String name = getClassName(); String wiki = getWiki(); String database = context.getDatabase(); try { context.setDatabase(wiki); return context.getWiki().getClass(name, context); } catch (Exception e) { e.printStackTrace(); return null; } finally { context.setDatabase(database); } }
The excerpt above from BaseCollection is a typical usage of getWiki(), and it shows that if getWiki() is null, the context database is set to null, which produce unexpected behaviours, especially in a farm.
This may raise when an object is created with BaseClass.newObject(), BaseClass.fromMap(), etc..., it has happened to me on a customized registration form, where a temporary XWikiUsers object is created with BaseClass.fromMap() to be able to validate the form against the regex in the class. My Wiki was in a farm. When I display the translated prettyname, using Document().displayPrettyName(..., obj), it indirectly retrieve the object class using the above functions. Since no one has set the wiki on the object, I got the following behaviour:
- on startup, all custom fields add to XWikiUsers does not shows their prettyname, the class retrieve was not the correct one
- after flushing the cache and immediate reload of the page, it usually gets their prettyname correctly
- after any update of any class in any of the wikis hosted, it return to the broken state
Note that getWiki() is also call by other function from the API, and none seems to be aware of this potential flaw.
My current workaround are :
- to set the wiki of any newly object to the current context.getDatabase() if the wiki was null.
- to set the wiki of an object to the database of their document when the object is added to the document
I suggest the following patch to ensure proper operation:
- ensure proper setting of wiki when an object is created with newObject, fromMap, fromValueMap, ...
- ensure proper setting of wiki in XWikiDocument.addObject/setObject
Hope this helps.