Outdated Article Warning on Page

Dear Community,

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?

Thank you very much :smiley:

Best regards,
ZD14

Which error ?

You might want to print the class of $actual ($actual.class), I’m sure you will understand better why $actual - $end does not work :slight_smile:

Thank you! You’re quick! :slight_smile:

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)

Is that important?

I don’t get your second sentence.

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 :slight_smile: ).

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.

omg! That was easy. Thank you very much :smiley:

I didn’t know $ indicates a variable.

Ok. I fixed the first problem.

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. :confused:

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 ).

Hi,

I’m back. Struggled to get the same formats.

Using this code now:

{{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.

regards,
ZD14

You can’t do that. Velocity does know what - means when dealing with objects, it’s only able to work with numbers.

Why don’t you simply compare the milliseconds ? All that was missing was to add .time.

now i got it!

thank you very very much :smiley: i really appreciate your effort to help a newbie!

You’re welcome :slight_smile: