I want to display a warning box on each article page if the article’s last changes were saved more than a year ago.
I tried the following code, but it doesn’t work. Note: I’m not used to code in XWiki.
{{velocity}}
#set ($actual = &doc.date)
#set ($end = $xwiki.jodatime.dateTime.minusDays(365).millis)
#if ($actual - $end > 0)
{{warning}}
THIS ARTICLE IS OUTDATED!
Please check the information and update the article if necessary. If the information is still valid, click on edit and save the article again.
{{/warning}}
#end
{{/velocity}}
“&doc.date” gives the error. How can I get the article’s date?
Caused by: org.apache.velocity.runtime.parser.ParseException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 1, column 18. Encountered: "d" (100), after : "&"
That’s the error. Additionally, there’s plenty of other Information given.
Like:
org.xwiki.rendering.macro.MacroExecutionException: Failed to evaluate Velocity Macro for content [#set ($actual = &doc.date) #set ($end = $xwiki.jodatime.dateTime.minusDays(365).millis)
#if ($actual - $end > 0)
{{warning}}
THIS ARTICLE IS OUTDATED!
Please check the information and update the article if necessary. If the information is still valid, click on edit and save the article again.
{{/warning}}
#end]
at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:139)
at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:52)
at org.xwiki.rendering.macro.script.AbstractScriptMacro.evaluateBlock(AbstractScriptMacro.java:286)
at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:182)
at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:58)
at org.xwiki.rendering.internal.transformation.macro.MacroTransformation.transform(MacroTransformation.java:272)
Right so you actually have two error in that script. The error you get is because you used & instead of $ which is the Velocity syntax for variable (which is what you are using everywhere else in that script ).
Once you fixed your first error you will see that your #if does not work great either. Put the following code before it
$actual.class
and you will see that $actual is not what you think it is.
And your hint tells me that I want to compare jodatime with Java.util.date. That doesn’t work.
I googled a bit. I found some pages dealing with this topic. But as I am not familiar with velocity, I struggle to understand what they’re saying and I don’t want to do things, which might crash an important class.
Actually $end is not a jodatime object, it’s the number of milliseconds since January 1, 1970, 00:00:00. So if you want to compare it with $actual you first need to get the same kind of data from $actual, see Java Date class documentation on Date (Java Platform SE 8 ).
{{velocity}}
#set ($actual = $xwiki.jodatime.getDateTime($doc.date.time))
#set ($end = $xwiki.jodatime.getDateTime($xwiki.jodatime.dateTime.minusDays(365).millis))
$actual.class
$actual
$end.class
$end
#if ($end - $actual>0)
{{warning}}
THIS ARTICLE IS OUTDATED!
Please check the information and update the article if necessary. If the information is still valid, click on edit and save the article again.
{{/warning}}
#else
{{info}}This article is up-to-date.{{/info}}
#end
{{/velocity}}
Basically the format and the dates look good, but the IF doesn’t work. How can I compare the dates. Sorry for asking stupid questions! I found “before” and “after” but I don’t know how to use it.