Quantcast

Trick to delay evaluation of included QUERY to establish context to including topic??

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
Hi Foswiki friends:

"FOSWIKI PUZZLE OF THE DAY"

This has been bugging me for years.
I need to find a trick to delay evaluation of a form field (or QUERY)
until after it is included. Let me explain.

1. I have a whole ton of topics I call MediaTopics in the Common web.
     A media topic has a form called MediaForm.
     MediaForm has a field MediaLink
     Consider one media topic as an example called M1745.

2. Every media topic has %INCLUDE{"MediaTopicHeader"}% at the top.
     In MediaTopicHeader is this:
     %IF{" '%QUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}%' != '' "
then="Media Link:
%QUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}%<BR>" ELSE='"" }%

3. Thus if we have the text "http://www.microsoft.com" in the field
MediaLink, then

         Media Link: http://www.microsoft.com

     is printed. All is well so far.

4. Frequently, attached to a media topic is a file, say a PDF file.
     If I request that a link is added to the topic, I get a link like this:

    * [[%ATTACHURL%/Sample.pdf][Sample.pdf]]: Sample.pdf

5. I copy the link into the MediaLink field,
     MediaLink: %ATTACHURL%/Sample.pdf

This shows up just fine in the form, as %ATTACHURL% is expanded
appropriately, say for example to:

     http://www.myfoswiki.com/w/pub/Common/M1745/Sample.pdf


6. But now, when the MediaTopicHeader is included, I get this:

     Media Link:
http://www.myfoswiki.com/w/pub/Common/MediaTopicHeader/Sample.pdf

But I want it to be

    Media Link: http://www.myfoswiki.com/w/pub/Common/M1745/Sample.pdf

7. One way to work around this is to require that the link copied into
the field is resolved to the topic, i.e.

instead of         MediaLink: %ATTACHURL%/Sample.pdf
this works         MediaLink: %PUBURL%/Common/M1745/Sample.pdf

But that is asking for too much it turns out, plus I have a lot of
topics that use the shorter %ATTACHURL%/Sample.pdf.


QUESTION:
Is there a way to delay evaluation of the %QUERY macro until it is
included in the INCLUDING topic, so that it sees itself at
%BASEWEB%.%BASETOPIC% instead of at Common/MediaTopicHeader ??

Hopefully, there is a way to solve this problem by modifying this line
in the MediaTopicHeader:
     %IF{" '%QUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}%' != '' "
then="Media Link:
%QUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}%<BR>" ELSE='"" }%

I figure that %QUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}% could
be delayed until after the include is done and this would do the trick.

using $percntQUERY{"'%BASEWEB%.%BASETOPIC%'/fields.MediaLink"}$percnt
does not work.

Any advice??

--Ray









------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Chris Hoefler
So, is the question that you need an %ATTACHURL% that works with INCLUDES?

How about %PUBURL%?
https://foswiki.org/System/VarPUBURL

So, your MediaLink is %PUBURL{topic="%BASEWEB%.%BASETOPIC%"}%/link instead of %ATTACHURL%/link.

The reason why your delayed %QUERY% doesn't work is because delayed macro expansion only works in certain situations. Your use of it like that in an %IF% statement is not supported. And even if it was, it still wouldn't do what you are trying to get it to do. There is no way to include a topic without first expanding its macros**. That's why macros like %BASEWEB% and %BASETOPIC% exist in the first place.

**RenderPlugin changes the rules a bit, but that's quite a bit more complicated than a standard INCLUDE.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
But, here is the thing.
The QUERY uses %BASEWEB% and %BASETOPIC% to access the correct form field.
That form field contains the macro %ATTACHURL%.
What I want is for the ATTACHURL to be evaluated based on the topic where the QUERY finds it.
That does make sense.

The delay approach was just to try to get back to the context of the includING topic so the macro would expand correctly.

Yes, it is possible to hand-edit the link the Media Link field.  I am just trying to avoid that.

The way I see it, the possibilities are:
1. some way to get the ATTACHURL macro when found inside a formfield to expand in the context of the topic that owns the form
2. delay the QUERY until after the include so it then uses the context expected.
3. hand edit the link in the macro to avoid ATTACHURL
4. a new macro that forces evaluation within a given context. For example, I am thinking in the INCLUDED topic, the string could be something like:
        %CONTEXT{%BASEWEB%.%BASETOPIC%, %IF{formfield{MediaLink}!=" ","Media Link: formfield{MediaLink}," "}%
(and I am sure I am not getting the correct syntax of the IF, but you probably get the idea)
Which actually simplifies things because instead of needing to specify the %BASEWEB%.%BASETOPIC% as an operand of QUERY, the context sets us to the other topic and then we naturally find the formfield, which is then expanded with the correct ATTACHURL context.

    %CONTEXT{topic-context-specifier, stuff-to-evaluate}%


On 03/23/2017 7:55 PM, Chris Hoefler wrote:
So, is the question that you need an %ATTACHURL% that works with INCLUDES?

How about %PUBURL%?
https://foswiki.org/System/VarPUBURL

So, your MediaLink is %PUBURL{topic="%BASEWEB%.%BASETOPIC%"}%/link instead of %ATTACHURL%/link.

The reason why your delayed %QUERY% doesn't work is because delayed macro expansion only works in certain situations. Your use of it like that in an %IF% statement is not supported. And even if it was, it still wouldn't do what you are trying to get it to do. There is no way to include a topic without first expanding its macros**. That's why macros like %BASEWEB% and %BASETOPIC% exist in the first place.

**RenderPlugin changes the rules a bit, but that's quite a bit more complicated than a standard INCLUDE.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to

BramVanOosterhout
In reply to this post by Raymond Lutz
Hi Ray,
This is quite a puzzle and Chris gets to the core of it: Macros are resolved
in the context of the INCLUDED topic. It appears that the
QUERY does not expand the macro so you end up with an ATTACHURL in the
MediaTopicHeader. The INCLUDE expands the macro in the context of the INCLUDED
topic: MediaTopicHeader and hence you get
...pub/Common/MediatopicHeader.

I have not tried the full query construct, but since the QUERY returns the
ATTACHURL string, you should be able to change that using FilterPlugin SUBST
to BASEWEB,BASETOPIC.

Like:

%SUBST{ text="string with ATTACHURL and other stuff from your query"
        pattern="ATTACHURL"
        format="BASEWEB%.%BASETOPIC"
}%

And then the INCLUDE should to the right thing.

--
Bram van Oosterhout


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Lynnwood Brown
In reply to this post by Raymond Lutz
Ray - 
If I'm understanding you correctly, your real issue is not how the macros are interpreted as much as the fact that the MediaLink field contains the ATTACHURL macro. I would focus on replacing (or by-passing) that macro in the field. I see two options here:
1. Globally replacing the stored content in the field. You could do this manually, or use GlobalReplacePlugin. If I'm reading your description of what's in that field correctly, I would replace the stored content with just the referenced attachment, minus all macros - and then provide the correct macros in rendering, depending on the context. Honestly, I think this would be your best fix.
2. Rewriting the content of the field on the fly. You could do this with the EXTRACT macro provided by FilterPlugin. It would look something like this: %EXTRACT{text="%QUERY{"'%BASEWEB%/%BASETOPIC%'/MediaLink"}%" pattern=".*ATTACHURL.\/([A-Za-z0-9\.]+)$" format="%PUBURL%/%BASEWEB%/%BASETOPIC%/$1"}% This is just a start - you'd need to fiddle with that regex to get it right. 
Good luck,
Lynnwood

On Mar 24, 2017, at 1:51 AM, Ray Lutz <[hidden email]> wrote:

But, here is the thing.
The QUERY uses %BASEWEB% and %BASETOPIC% to access the correct form field.
That form field contains the macro %ATTACHURL%.
What I want is for the ATTACHURL to be evaluated based on the topic where the QUERY finds it.
That does make sense.

The delay approach was just to try to get back to the context of the includING topic so the macro would expand correctly.

Yes, it is possible to hand-edit the link the Media Link field.  I am just trying to avoid that.

The way I see it, the possibilities are:
1. some way to get the ATTACHURL macro when found inside a formfield to expand in the context of the topic that owns the form
2. delay the QUERY until after the include so it then uses the context expected.
3. hand edit the link in the macro to avoid ATTACHURL
4. a new macro that forces evaluation within a given context. For example, I am thinking in the INCLUDED topic, the string could be something like:
        %CONTEXT{%BASEWEB%.%BASETOPIC%, %IF{formfield{MediaLink}!=" ","Media Link: formfield{MediaLink}," "}%
(and I am sure I am not getting the correct syntax of the IF, but you probably get the idea)
Which actually simplifies things because instead of needing to specify the %BASEWEB%.%BASETOPIC% as an operand of QUERY, the context sets us to the other topic and then we naturally find the formfield, which is then expanded with the correct ATTACHURL context.

    %CONTEXT{topic-context-specifier, stuff-to-evaluate}%


On 03/23/2017 7:55 PM, Chris Hoefler wrote:
So, is the question that you need an %ATTACHURL% that works with INCLUDES?

How about %PUBURL%?
https://foswiki.org/System/VarPUBURL

So, your MediaLink is %PUBURL{topic="%BASEWEB%.%BASETOPIC%"}%/link instead of %ATTACHURL%/link.

The reason why your delayed %QUERY% doesn't work is because delayed macro expansion only works in certain situations. Your use of it like that in an %IF% statement is not supported. And even if it was, it still wouldn't do what you are trying to get it to do. There is no way to include a topic without first expanding its macros**. That's why macros like %BASEWEB% and %BASETOPIC% exist in the first place.

**RenderPlugin changes the rules a bit, but that's quite a bit more complicated than a standard INCLUDE.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Chris Hoefler
In reply to this post by Raymond Lutz
Right, because in the following scheme,
  Topic -> Topic Macros -> Included Topic -> Included Topic Macros

It is rendered in the reverse order,
  Evaluate Included Topic Macros -> Render Included Topic -> Evaluate Topic Macros -> Render Topic

In your specific case,
  1) The %IF% in your Included Topic is first evaluated:
    a) %BASEWEB%.%BASETOPIC% is evaluated to Common.M1745
    b) %QUERY{"'Common.M1745'/fields.MediaLink"}% is evaluated to %ATTACHURL%/Sample.pdf
    c) %ATTACHURL%/Sample.pdf is evaluated to Common/MediaTopicHeader/Sample.pdf
    d) %IF{"Common/MediaTopicHeader/Sample.pdf != ''" then="Media Link: Common/MediaTopicHeader/Sample.pdf" else=""}% is evaluated to Media Link: Common/MediaTopicHeader/Sample.pdf
  2) Next move on to processing the %INCLUDE{"MediaTopicHeader"}% which now looks something like this to the rendering engine: INCLUDE START  Media Link: Common/MediaTopicHeader/Sample.pdf INCLUDE END
  3) Finally, finish rendering Topic, which now looks like this: text prior to %INCLUDE% - Common/MediaTopicHeader/Sample.pdf- text following %INCLUDE%

Bram mentioned an interesting possibility using FilterPlugin. I think to make even that work, though, you would have to stop %ATTACHURL% from being evaluated (with a <nop>, for example). Otherwise, it will get evaluated before FilterPlugin can substitute it.

Alternatively, the way I would do this, is use a VIEW_TEMPLATE. Instead of including MediaTopicHeader, just create a MediaFormViewTemplate. Then you can use the %TMPL% markup language to make it look however you like. Macros will be expanded in the context of the topic. Ex:
  %TMPL:INCLUDE{"view"}%
  %TMPL:DEF{"content"}%
    MediaLink: %QUERY{"MediaLink"}%
    %TEXT%
  %TMPL:END%

If you want the MediaLink to be placed at the top of the page.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY

BramVanOosterhout
Hi Ray, Chris.
Re:
> Bram mentioned an interesting possibility using FilterPlugin. I think to make even that work, though, you would have to stop %ATTACHURL% from being evaluated (with a <nop>, for example). Otherwise, it will get evaluated before FilterPlugin can substitute it.

I thought of that after I had submitted my response. Sleeping on it I believe that if you write the substitution as:

%SUBST{ text="string with %ATTACHURL% and other stuff from your query"
       pattern="%ATTACHURL%"
       format="%PUBURL%/%BASEWEB%.%BASETOPIC%"
}%


Then the evaluated ATTACHURL is substituted by the evaluated pattern. I expect that will work.

Regards...

--
Bram van Oosterhout


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
In reply to this post by Lynnwood Brown
Actually, I already realize one option is to globally replace but I notice it is difficult for users to submit media because there is hand-work involved.
Your option #1 is only best if the content is static. The problem is it is being added to constantly and I'm trying to train people to do it. The ATTACHURL macro is provided by the attach function when you request that a link be included in the body of the topic. If this link DID NOT use the ATTACHURL macro, but instead used %PUBURL%/%BASEWEB%/%BASETOPIC% which is functionally the same thing EXCEPT if someone copies and pastes it into a form field, and then if the field is evaluated in an INCLUDE, that it will evaluate correctly.

--> Q: Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname because this would fix it. People can just copy and paste the link into the form without needing additional instructions on how to modify it. (Of course it would only fix future submissions and means I need to search for ATTACHURL in this field and fix it, but most of them are correct.)

What I need is an ATTACHURL which would function like one we might call BASEATTACHURL, which should resolve correctly even if it is included in an INCLUDE. OR, perhaps more simply, to introduce a wrapper macro %CONTEXT{web="web" topic="topic"}% which would set the desired context, to be used in the INCLUDED topic. If used, it actually simplifies things quite a bit, esp. when referencing many form fields of one topic from some other topic, instead of needing to pass the context into each macro that offers a context change.

The option of #2 is something I might look into. Thanks.

--Ray

On 03/24/2017 6:38 AM, Lynnwood Brown wrote:
Ray - 
If I'm understanding you correctly, your real issue is not how the macros are interpreted as much as the fact that the MediaLink field contains the ATTACHURL macro. I would focus on replacing (or by-passing) that macro in the field. I see two options here:
1. Globally replacing the stored content in the field. You could do this manually, or use GlobalReplacePlugin. If I'm reading your description of what's in that field correctly, I would replace the stored content with just the referenced attachment, minus all macros - and then provide the correct macros in rendering, depending on the context. Honestly, I think this would be your best fix.
2. Rewriting the content of the field on the fly. You could do this with the EXTRACT macro provided by FilterPlugin. It would look something like this: %EXTRACT{text="%QUERY{"'%BASEWEB%/%BASETOPIC%'/MediaLink"}%" pattern=".*ATTACHURL.\/([A-Za-z0-9\.]+)$" format="%PUBURL%/%BASEWEB%/%BASETOPIC%/$1"}% This is just a start - you'd need to fiddle with that regex to get it right. 
Good luck,
Lynnwood

On Mar 24, 2017, at 1:51 AM, Ray Lutz <[hidden email]> wrote:

But, here is the thing.
The QUERY uses %BASEWEB% and %BASETOPIC% to access the correct form field.
That form field contains the macro %ATTACHURL%.
What I want is for the ATTACHURL to be evaluated based on the topic where the QUERY finds it.
That does make sense.

The delay approach was just to try to get back to the context of the includING topic so the macro would expand correctly.

Yes, it is possible to hand-edit the link the Media Link field.  I am just trying to avoid that.

The way I see it, the possibilities are:
1. some way to get the ATTACHURL macro when found inside a formfield to expand in the context of the topic that owns the form
2. delay the QUERY until after the include so it then uses the context expected.
3. hand edit the link in the macro to avoid ATTACHURL
4. a new macro that forces evaluation within a given context. For example, I am thinking in the INCLUDED topic, the string could be something like:
        %CONTEXT{%BASEWEB%.%BASETOPIC%, %IF{formfield{MediaLink}!=" ","Media Link: formfield{MediaLink}," "}%
(and I am sure I am not getting the correct syntax of the IF, but you probably get the idea)
Which actually simplifies things because instead of needing to specify the %BASEWEB%.%BASETOPIC% as an operand of QUERY, the context sets us to the other topic and then we naturally find the formfield, which is then expanded with the correct ATTACHURL context.

    %CONTEXT{topic-context-specifier, stuff-to-evaluate}%


On 03/23/2017 7:55 PM, Chris Hoefler wrote:
So, is the question that you need an %ATTACHURL% that works with INCLUDES?

How about %PUBURL%?
https://foswiki.org/System/VarPUBURL

So, your MediaLink is %PUBURL{topic="%BASEWEB%.%BASETOPIC%"}%/link instead of %ATTACHURL%/link.

The reason why your delayed %QUERY% doesn't work is because delayed macro expansion only works in certain situations. Your use of it like that in an %IF% statement is not supported. And even if it was, it still wouldn't do what you are trying to get it to do. There is no way to include a topic without first expanding its macros**. That's why macros like %BASEWEB% and %BASETOPIC% exist in the first place.

**RenderPlugin changes the rules a bit, but that's quite a bit more complicated than a standard INCLUDE.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Chris Hoefler
I was browsing through the documentation of EasyMacroPlugin,
https://foswiki.org/Extensions/EasyMacroPlugin

thinking that might be a possible solution for you. It seems like it actually could be really nice, in general, for exactly this sort of thing. However, EasyMacroPlugin is really just a wrapper around ParameterizedIncludes, and then I went ah ha! That's what you need. I think you can cleverly solve your problem that way.

Right now you have,
  Topic -> %INCLUDE{"MediaTopicHeader"}% -> %IF{"%QUERY{...}%"}%

So, change it to something like this,
  Topic -> %INCLUDE{"MediaTopicHeader" medialink="%FORMFIELD{"MediaLink" format="Media Link: $value" default=""}%"}% -> %medialink%

Brief explanation: The including topic defines a variable, medialink, using data from the attached DataForm. This variable is then passed on to the included topic, which places it on the page using %medialink%. (Note the use of %FORMFIELD% instead of %QUERY%, which gives you access to the "format" and "default" parameters, and allows you to eliminate the %IF%.) The %FORMFIELD% will be evaluated in the context of the including topic, not the included topic. It does require you to do a global replace on topics already created to change the format of the %INCLUDE%, but since you mentioned above that this is not necessarily a problem, it seems like it would be a good solution for you.

Relatedly, I have in the past used AttachLinkPlugin to generate clickable links to attachments. A simple %ATTACHMENT{"attachment name.here"}% is all you need. No, it is not as easy as just clicking a button for the end user, but it is certainly less clumsy than %ATTACHURL%/attachment name.

> -> Q: Does anyone know if I can alter the functionality of the ATTACH function

By the ATTACH function, I assume you are using NatSkin? I don't recall that being in the default PatternSkin. My initial answer was going to be "yes, but it's complicated", but after reading through the code, it is actually quite simple. Set the preference variable ATTACHEDFILELINKFORMAT somewhere that is appropriate (I'm guessing WebPreferences in your case). The default is "%ATTACHURL%/$filename". In your case, you'll want to use something like,
   * Set ATTACHEDFILELINKFORMAT = %PUBURL{"$filename" topic="%BASEWEB%.%BASETOPIC%"}%

The "$filename" will be replaced with the name of the attachment by the action when it is executed. It's even documented in System/DefaultPreferences, but it's not something I ever thought to look for before. You might need a <nop> in the PUBURL and BASE*** macros to make this work...not sure about that.

Note: While you can do this, I think the ParameterizedInclude is a more elegant solution to your problem.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Lynnwood Brown
In reply to this post by Raymond Lutz
Ray - 
I think you hit upon your solution in the statement "Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname". Yes you can and it's simple.

If you define any Foswiki macro locally, that definition over-rides the default higher-level definition. So, simply re-define the ATTACHURL on the Web level. I tried this out by adding the following macro definition in a WebPreference topic:
   * Set ATTACHURL = %PUBURL%/%BASEWEB%/%BASETOPIC%

Then, following your use case, I defined the following section in a topic:
%STARTSECTION{"testinclude"}%
%QUERY{"'%BASEWEB%.%BASETOPIC%'/Image"}%
%ENDSECTION{"testinclude"}%

Finally, in a topic that had a data form with an "Image" field containing "%ATTACHURL%/image.jpg", I added an INCLUDE that called the above topic and section. Lo & behold, it resolved to the the correct pub url.

Give it a try and see if it works for your use case.

For extra credit, I was curious to see if I could get this kind of macro-manipulation to also work for a direct query to the topic containing the attachment - something like this:
%QUERY{"'Web.TopicWithAttachedImage'/Image"}%
Unfortunately, this didn't work, nor could I figure out how to make it work. I guess one would have to define the ATTACH macro in such a way to be aware of the context in which it's being expanded...and I can't figure how to accomplish that.

Cheers,
Lynnwood


On Mar 28, 2017, at 2:01 PM, Ray Lutz <[hidden email]> wrote:

Actually, I already realize one option is to globally replace but I notice it is difficult for users to submit media because there is hand-work involved. 
Your option #1 is only best if the content is static. The problem is it is being added to constantly and I'm trying to train people to do it. The ATTACHURL macro is provided by the attach function when you request that a link be included in the body of the topic. If this link DID NOT use the ATTACHURL macro, but instead used %PUBURL%/%BASEWEB%/%BASETOPIC% which is functionally the same thing EXCEPT if someone copies and pastes it into a form field, and then if the field is evaluated in an INCLUDE, that it will evaluate correctly.

--> Q: Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname because this would fix it. People can just copy and paste the link into the form without needing additional instructions on how to modify it. (Of course it would only fix future submissions and means I need to search for ATTACHURL in this field and fix it, but most of them are correct.)

What I need is an ATTACHURL which would function like one we might call BASEATTACHURL, which should resolve correctly even if it is included in an INCLUDE. OR, perhaps more simply, to introduce a wrapper macro %CONTEXT{web="web" topic="topic"}% which would set the desired context, to be used in the INCLUDED topic. If used, it actually simplifies things quite a bit, esp. when referencing many form fields of one topic from some other topic, instead of needing to pass the context into each macro that offers a context change.

The option of #2 is something I might look into. Thanks.

--Ray


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Chris Hoefler
For extra credit, I was curious to see if I could get this kind of macro-manipulation to also work for a direct query to the topic containing the attachment - something like this:
%QUERY{"'Web.TopicWithAttachedImage'/Image"}%
Unfortunately, this didn't work, nor could I figure out how to make it work. I guess one would have to define the ATTACH macro in such a way to be aware of the context in which it's being expanded...and I can't figure how to accomplish that.

It doesn't work because %BASEWEB%/%BASETOPIC% evaluates to %WEB%/%TOPIC% in the absence of an include, which is the topic doing the QUERY. If you are doing the QUERY in the topic with the DataForm, it works, but not if you do it in a different topic. That's a harder problem to solve...it may be possible with the EasyMacroPlugin. Or, since FORMFIELD expands formatting tokens, you can take the URL construction out of the formfield value and do it with the "format" parameter instead,
%FORMFIELD{"field" topic="web.topic" format="%PUBURL%/web/topic/$value"}%

Then you can construct the url independently of the field value depending on how and from where you are retrieving the value.

On Wed, Mar 29, 2017 at 9:42 AM, Lynnwood Brown <[hidden email]> wrote:
Ray - 
I think you hit upon your solution in the statement "Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname". Yes you can and it's simple.

If you define any Foswiki macro locally, that definition over-rides the default higher-level definition. So, simply re-define the ATTACHURL on the Web level. I tried this out by adding the following macro definition in a WebPreference topic:
   * Set ATTACHURL = %PUBURL%/%BASEWEB%/%BASETOPIC%

Then, following your use case, I defined the following section in a topic:
%STARTSECTION{"testinclude"}%
%QUERY{"'%BASEWEB%.%BASETOPIC%'/Image"}%
%ENDSECTION{"testinclude"}%

Finally, in a topic that had a data form with an "Image" field containing "%ATTACHURL%/image.jpg", I added an INCLUDE that called the above topic and section. Lo & behold, it resolved to the the correct pub url.

Give it a try and see if it works for your use case.

For extra credit, I was curious to see if I could get this kind of macro-manipulation to also work for a direct query to the topic containing the attachment - something like this:
%QUERY{"'Web.TopicWithAttachedImage'/Image"}%
Unfortunately, this didn't work, nor could I figure out how to make it work. I guess one would have to define the ATTACH macro in such a way to be aware of the context in which it's being expanded...and I can't figure how to accomplish that.

Cheers,
Lynnwood


On Mar 28, 2017, at 2:01 PM, Ray Lutz <[hidden email]> wrote:

Actually, I already realize one option is to globally replace but I notice it is difficult for users to submit media because there is hand-work involved. 
Your option #1 is only best if the content is static. The problem is it is being added to constantly and I'm trying to train people to do it. The ATTACHURL macro is provided by the attach function when you request that a link be included in the body of the topic. If this link DID NOT use the ATTACHURL macro, but instead used %PUBURL%/%BASEWEB%/%BASETOPIC% which is functionally the same thing EXCEPT if someone copies and pastes it into a form field, and then if the field is evaluated in an INCLUDE, that it will evaluate correctly.

--> Q: Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname because this would fix it. People can just copy and paste the link into the form without needing additional instructions on how to modify it. (Of course it would only fix future submissions and means I need to search for ATTACHURL in this field and fix it, but most of them are correct.)

What I need is an ATTACHURL which would function like one we might call BASEATTACHURL, which should resolve correctly even if it is included in an INCLUDE. OR, perhaps more simply, to introduce a wrapper macro %CONTEXT{web="web" topic="topic"}% which would set the desired context, to be used in the INCLUDED topic. If used, it actually simplifies things quite a bit, esp. when referencing many form fields of one topic from some other topic, instead of needing to pass the context into each macro that offers a context change.

The option of #2 is something I might look into. Thanks.

--Ray


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
In reply to this post by Chris Hoefler
Sorry, not a feasible solution because
1. MediaLink is not the only form field accessed, that is just an example.
2. Would need to replace all MediaTopicHeader INCLUDES, and revise them if a new field is added to what is accessed.

But thanks for the idea!
--Ray

On 03/28/2017 8:49 PM, Chris Hoefler wrote:
I was browsing through the documentation of EasyMacroPlugin,
https://foswiki.org/Extensions/EasyMacroPlugin

thinking that might be a possible solution for you. It seems like it actually could be really nice, in general, for exactly this sort of thing. However, EasyMacroPlugin is really just a wrapper around ParameterizedIncludes, and then I went ah ha! That's what you need. I think you can cleverly solve your problem that way.

Right now you have,
  Topic -> %INCLUDE{"MediaTopicHeader"}% -> %IF{"%QUERY{...}%"}%

So, change it to something like this,
  Topic -> %INCLUDE{"MediaTopicHeader" medialink="%FORMFIELD{"MediaLink" format="Media Link: $value" default=""}%"}% -> %medialink%

Brief explanation: The including topic defines a variable, medialink, using data from the attached DataForm. This variable is then passed on to the included topic, which places it on the page using %medialink%. (Note the use of %FORMFIELD% instead of %QUERY%, which gives you access to the "format" and "default" parameters, and allows you to eliminate the %IF%.) The %FORMFIELD% will be evaluated in the context of the including topic, not the included topic. It does require you to do a global replace on topics already created to change the format of the %INCLUDE%, but since you mentioned above that this is not necessarily a problem, it seems like it would be a good solution for you.

Relatedly, I have in the past used AttachLinkPlugin to generate clickable links to attachments. A simple %ATTACHMENT{"attachment name.here"}% is all you need. No, it is not as easy as just clicking a button for the end user, but it is certainly less clumsy than %ATTACHURL%/attachment name.

> -> Q: Does anyone know if I can alter the functionality of the ATTACH function

By the ATTACH function, I assume you are using NatSkin? I don't recall that being in the default PatternSkin. My initial answer was going to be "yes, but it's complicated", but after reading through the code, it is actually quite simple. Set the preference variable ATTACHEDFILELINKFORMAT somewhere that is appropriate (I'm guessing WebPreferences in your case). The default is "%ATTACHURL%/$filename". In your case, you'll want to use something like,
   * Set ATTACHEDFILELINKFORMAT = %PUBURL{"$filename" topic="%BASEWEB%.%BASETOPIC%"}%

The "$filename" will be replaced with the name of the attachment by the action when it is executed. It's even documented in System/DefaultPreferences, but it's not something I ever thought to look for before. You might need a <nop> in the PUBURL and BASE*** macros to make this work...not sure about that.

Note: While you can do this, I think the ParameterizedInclude is a more elegant solution to your problem.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
In reply to this post by Lynnwood Brown
Yes this looks wonderful and I can't believe I did not think of it, so I will implement it immediately!

--Ray Lutz

On 03/29/2017 6:42 AM, Lynnwood Brown wrote:
Ray - 
I think you hit upon your solution in the statement "Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname". Yes you can and it's simple.

If you define any Foswiki macro locally, that definition over-rides the default higher-level definition. So, simply re-define the ATTACHURL on the Web level. I tried this out by adding the following macro definition in a WebPreference topic:
   * Set ATTACHURL = %PUBURL%/%BASEWEB%/%BASETOPIC%

Then, following your use case, I defined the following section in a topic:
%STARTSECTION{"testinclude"}%
%QUERY{"'%BASEWEB%.%BASETOPIC%'/Image"}%
%ENDSECTION{"testinclude"}%

Finally, in a topic that had a data form with an "Image" field containing "%ATTACHURL%/image.jpg", I added an INCLUDE that called the above topic and section. Lo & behold, it resolved to the the correct pub url.

Give it a try and see if it works for your use case.

For extra credit, I was curious to see if I could get this kind of macro-manipulation to also work for a direct query to the topic containing the attachment - something like this:
%QUERY{"'Web.TopicWithAttachedImage'/Image"}%
Unfortunately, this didn't work, nor could I figure out how to make it work. I guess one would have to define the ATTACH macro in such a way to be aware of the context in which it's being expanded...and I can't figure how to accomplish that.

Cheers,
Lynnwood


On Mar 28, 2017, at 2:01 PM, Ray Lutz <[hidden email]> wrote:

Actually, I already realize one option is to globally replace but I notice it is difficult for users to submit media because there is hand-work involved. 
Your option #1 is only best if the content is static. The problem is it is being added to constantly and I'm trying to train people to do it. The ATTACHURL macro is provided by the attach function when you request that a link be included in the body of the topic. If this link DID NOT use the ATTACHURL macro, but instead used %PUBURL%/%BASEWEB%/%BASETOPIC% which is functionally the same thing EXCEPT if someone copies and pastes it into a form field, and then if the field is evaluated in an INCLUDE, that it will evaluate correctly.

--> Q: Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname because this would fix it. People can just copy and paste the link into the form without needing additional instructions on how to modify it. (Of course it would only fix future submissions and means I need to search for ATTACHURL in this field and fix it, but most of them are correct.)

What I need is an ATTACHURL which would function like one we might call BASEATTACHURL, which should resolve correctly even if it is included in an INCLUDE. OR, perhaps more simply, to introduce a wrapper macro %CONTEXT{web="web" topic="topic"}% which would set the desired context, to be used in the INCLUDED topic. If used, it actually simplifies things quite a bit, esp. when referencing many form fields of one topic from some other topic, instead of needing to pass the context into each macro that offers a context change.

The option of #2 is something I might look into. Thanks.

--Ray



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Raymond Lutz
Sorry, no cigar, as it turns out, there were many repercussions when I attempted to change ATTACHURL globally.
Sometimes, a field with ATTACHURL in it may be used from another topic which is not an and INCLUDED topic, and then it works improperly.
One possibility is to define ATTACHURL = %PUBURL%/%BASEWEB%/%BASETOPIC% in the media topic just before the INCLUDE. That works in the context of the included topic but if other topics pull out the formfield, say using a QUERY, then it will not be resolved properly.
In those cases, the formfield must be fully resolved (i.e. PUBURL is okay, but web and topic must be resolved PRIOR TO use by any topic that uses it.
I'm afraid I am back to hand-modifying the link before it is used in a formfield.
--Ray

On 03/29/2017 10:59 AM, Ray Lutz wrote:
Yes this looks wonderful and I can't believe I did not think of it, so I will implement it immediately!

--Ray Lutz

On 03/29/2017 6:42 AM, Lynnwood Brown wrote:
Ray - 
I think you hit upon your solution in the statement "Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname". Yes you can and it's simple.

If you define any Foswiki macro locally, that definition over-rides the default higher-level definition. So, simply re-define the ATTACHURL on the Web level. I tried this out by adding the following macro definition in a WebPreference topic:
   * Set ATTACHURL = %PUBURL%/%BASEWEB%/%BASETOPIC%

Then, following your use case, I defined the following section in a topic:
%STARTSECTION{"testinclude"}%
%QUERY{"'%BASEWEB%.%BASETOPIC%'/Image"}%
%ENDSECTION{"testinclude"}%

Finally, in a topic that had a data form with an "Image" field containing "%ATTACHURL%/image.jpg", I added an INCLUDE that called the above topic and section. Lo & behold, it resolved to the the correct pub url.

Give it a try and see if it works for your use case.

For extra credit, I was curious to see if I could get this kind of macro-manipulation to also work for a direct query to the topic containing the attachment - something like this:
%QUERY{"'Web.TopicWithAttachedImage'/Image"}%
Unfortunately, this didn't work, nor could I figure out how to make it work. I guess one would have to define the ATTACH macro in such a way to be aware of the context in which it's being expanded...and I can't figure how to accomplish that.

Cheers,
Lynnwood


On Mar 28, 2017, at 2:01 PM, Ray Lutz <[hidden email]> wrote:

Actually, I already realize one option is to globally replace but I notice it is difficult for users to submit media because there is hand-work involved. 
Your option #1 is only best if the content is static. The problem is it is being added to constantly and I'm trying to train people to do it. The ATTACHURL macro is provided by the attach function when you request that a link be included in the body of the topic. If this link DID NOT use the ATTACHURL macro, but instead used %PUBURL%/%BASEWEB%/%BASETOPIC% which is functionally the same thing EXCEPT if someone copies and pastes it into a form field, and then if the field is evaluated in an INCLUDE, that it will evaluate correctly.

--> Q: Does anyone know if I can alter the functionality of the ATTACH function so that the string added to the body is %PUBURL%/%BASEWEB%/%BASETOPIC%/attachmentname rather than %ATTACHURL%/attachmentname because this would fix it. People can just copy and paste the link into the form without needing additional instructions on how to modify it. (Of course it would only fix future submissions and means I need to search for ATTACHURL in this field and fix it, but most of them are correct.)

What I need is an ATTACHURL which would function like one we might call BASEATTACHURL, which should resolve correctly even if it is included in an INCLUDE. OR, perhaps more simply, to introduce a wrapper macro %CONTEXT{web="web" topic="topic"}% which would set the desired context, to be used in the INCLUDED topic. If used, it actually simplifies things quite a bit, esp. when referencing many form fields of one topic from some other topic, instead of needing to pass the context into each macro that offers a context change.

The option of #2 is something I might look into. Thanks.

--Ray



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot


_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Trick to delay evaluation of included QUERY to establish context to including topic??

Chris Hoefler
If you have multiple "views" of the same data (read this to mean that the QUERY for a formfield should be presented in different ways based on the topic context), the best approach would be to separate the data from the presentation.

As I suggested earlier, this can be accomplished using the FORMFIELD macro which has a format parameter. Here are three ways to use it that solve three different problems,
   1) %FORMFIELD{"field" topic="%BASEWEB%.%BASETOPIC%" format="$percentPUBURL{\"$value\" topic=\"%BASEWEB%.%BASETOPIC%\"}$percent"}%
     # This is for use in an included topic to query a formfield value from the including topic and format it as a link to an attachment.

   2) %FORMFIELD{"field" topic="web.topic" format="$percentPUBURL{\"$value\" topic=\"web.topic\"}$percent"}%
     # This for a direct query to a formfield value in another topic, where both the web and the topic are known.
     # Note: if you are creating these topics from a TemplateTopic, you can use %STARTSECTION{type="expandvariables"}% to generate the web and topic parameters during topic creation.

   3) %SEARCH{"search query" type="query" nonoise="on" format="$percentPUBURL{\"$formfield(field)\" topic=\"$web.$topic\"}$percent"}%
     # This will create attachment links based on the results of a SEARCH.

This does require you to remove the ATTACHURL from all of your fields, but is ultimately a more flexible and extensible way to present your data. As Lynnwood mentioned, GlobalReplacePlugin may help you with this.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Loading...