Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#181 2013-08-18 17:34:06

juanjo
Member
From: A Coruña, Spain
Registered: 2005-08-01
Posts: 55
Website

Re: etc_query: all things Textpattern

Thanks Oleg, my mistake!

But I still can not get the data from fields like {swell/minBreakingHeight?} e.g., from the specific timestamp. I try this query:

query="//localTimestamp[. ='1376784000']"/*

But it does not work, obviously.

Offline

#182 2013-08-18 17:43:29

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Yes, because swell is a sibling of localTimestamp, not its child. Try

query="//localTimestamp[.='1376784000']/following-sibling::swell/minBreakingHeight"

if you wan just this data, or

query="//localTimestamp[.='1376784000']/.."

to match the parent of the desired timestamp, then {swell/minBreakingHeight?} and other selectors inside etc_query.

Offline

#183 2013-08-18 18:10:03

juanjo
Member
From: A Coruña, Spain
Registered: 2005-08-01
Posts: 55
Website

Re: etc_query: all things Textpattern

Thanks a lot again, Oleg! it works perfectly :)

Offline

#184 2013-08-18 18:30:46

juanjo
Member
From: A Coruña, Spain
Registered: 2005-08-01
Posts: 55
Website

Re: etc_query: all things Textpattern

Oleg, sorry for being such a pain in the ass, but it would be possible to replace the datestamp in the query for the current datestamp using something like this?

<txp:php>
        $now = time();
        $date = date('m/d/Y H', $now) . ':00';
        $currenttime = strtotime($date);       
</txp:php>

then

query="//localTimestamp[. =misterious code $currenttime]/.."

Offline

#185 2013-08-18 21:56:33

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Sure, the usual parsing rules apply, just mind the quotes. You can even do it etc_query-way, calling query="//localTimestamp[.='{?||strftime(%F %H:00).strtotime}']/..". However, there is no guarantee that this timestamp will be present in the feed. Edit: you can use query="//localTimestamp[.>='{?||strftime(%F %H:00).strtotime}']/.." to find the immediate successor of the current timestamp.

Last edited by etc (2013-08-18 22:09:32)

Offline

#186 2013-08-25 08:45:44

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,727
GitHub

Re: etc_query: all things Textpattern

Hi Oleg.
If you have time (and inclination), I’d appreciate a pointer on where I’m going wrong with the following. I want to query and display database contents, specifically the ip field of txp_discuss with a provided discussid. My SQL syntax is correct, I think, but this isn’t outputting anything when used in a comment form:

<txp:etc_query data="SELECT ip FROM txp_discuss WHERE discussid = <txp:comment_id />">{ip?}</txp:etc_query>

Thank you in advance.

Offline

#187 2013-08-25 09:16:44

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Hi Pete, I think that’s just double quotes:

<txp:etc_query data='SELECT ip FROM txp_discuss WHERE discussid = <txp:comment_id />'>{ip?}</txp:etc_query>

or even

<txp:etc_query data='SELECT ip FROM txp_discuss WHERE discussid = <txp:comment_id />' />

should be ok.

Offline

#188 2013-08-25 09:18:15

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,727
GitHub

Re: etc_query: all things Textpattern

etc wrote:

Hi Pete, I think that’s just double quotes:

Guh! The one thing I didn’t try!

Thank you, Oleg.

Offline

#189 2013-09-04 09:58:09

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Version 1.2.7: minor update to enable LDAP queries (data is composed of ;-separated rdn, filter and only fields):

<txp:etc_query markup="ldap" url="ldaps://my.ldap.server:636" data="ou=people,dc=org; uid=ol*; uid,sn" query="count" />

Works for me, but testers are more than welcome.

Edit: also tightened the security a little bit: only privileged users can import data from url or databases.

Last edited by etc (2013-09-04 10:49:30)

Offline

#190 2013-09-12 19:14:57

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Version 1.2.8: few changes inspired by this thread:

  • new save attribute, allowing to save the imported and transformed dom tree, that can be quickly reused later with markup="data";
  • new {!variable} pattern, that will be actualized on each call, and not only once like {?variable} pattern;
  • new {##} pattern for internal nodelist number;
  • more secure: requires php privileges for all advanced operations.

Offline

#191 2013-09-26 15:34:51

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: etc_query: all things Textpattern

Here’s one. I want to reformat a mail chimp archive called on a page with javacript… it looks like this…

document.write("<div class=\"display_archive\"><div class=\"campaign\">09\/19\/2013 - <a href=\"http:\/\/us7.campaign-archive1.com\/?u=999&id=888\" title=\"Title 1\" target=\"_blank\">Title 1<\/a><\/div><div class=\"campaign\">09\/12\/2013 - <a href=\"http:\/\/us7.campaign-archive1.com\/?u=777&id=666\" title=\"Title 2\" target=\"_blank\">Title 2<\/a><\/div><\/div>");

So obviously I point the query field at the url of the script, but in order to process this properly I need to do two things first:

  1. strip escape characters
  2. remove document.write(" and "0;

At that point I’m confident that I can do the reformatting I need.

Offline

#192 2013-09-26 16:33:40

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

Hi Dale,

if you import this script from external url and are quite sure about its structure, try this:

<!-- save the script in "some_script" variable -->
<txp:etc_query url="script_url" markup="raw" name="some_script" />

<!-- now clean up and query -->
<txp:etc_query data="{?some_script||trim.preg_replace(/^document\.write\(|\);$/i,,$)}" argsep="," markup="json" />

If it is inside the page itself, you can go the same “save then transform” way, but the optimal solution depends on how many they are, and what has to be extracted.

Offline

#193 2013-09-26 17:34:22

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: etc_query: all things Textpattern

Awesome! Did the trick, now I need to do the following. gonna give it a shot myself, just posting here for reference.

The dataset

<div class="display_archive">
  <div class="campaign">09/19/2013 - <a href="#" title="Item 1" target="_blank">Item 1</a></div>
  <div class="campaign">09/12/2013 - <a href="#" title="Item 2" target="_blank">Item 2</a></div>
</div>

In theory (etc check my logic)

  1. match div.campaign /display_archive/campaign/
  2. extract the date from instances of the date /display_archive/campaign/text() into a variable
  3. remove the “ – “ in the variable
  4. use <txp:if_different> to insert an h4 for the month name when it is different from the preceding (converting 1-12 to month names)
  5. echo .campaign

Now for the Implementation

Offline

#194 2013-09-26 18:39:04

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: etc_query: all things Textpattern

For some weird good reason, <> get encoded when importing JSON to XML. This seems to work:

<txp:etc_query data="{?some_script||trim.preg_replace(/^document\.write\(|\);$/i,,$).json_decode}"
	argsep="," wraptag="div" class="display_archive" functions="strtotime,date"
	query="//div[@class='campaign']"
>
	<txp:if_different><h4>{date('m', strtotime(translate(text(), ' -', '')))}</h4></txp:if_different>
	{.}
</txp:etc_query>

Edit: added date functions.

Last edited by etc (2013-09-27 10:26:34)

Offline

#195 2013-09-26 21:21:57

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: etc_query: all things Textpattern

This is awesome! Thanks… trying very hard to grok this her’s my line by line.

<txp:etc_query data="{?some_script||trim.preg_replace(/^document\.write\(|\);$/i,,$).json_decode}"

{get data from the variable|no default value|transformation chain}

get the variable and apply a transformation chain to it and use that for data (through php functions?)

  1. trim: trims whitespace from start and end of the following
  2. preg_replace: matches the two parts of the unwanted text searching case-insensitive
  3. json_decode decodes the output into an array (why do I need this?)

argsep="," wraptag="div" class="display_archive" functions="strtotime,date" query="//div[@class='campaign']"

  1. argsep: separates the arguments of which part with a comma?
  2. wraptag: and class are txp behaviors
  3. functions: allow the following php functions to be used inside this tag
  4. query: limit the output of this tag to all divs who’s class is “campaign”

<txp:if_different><h4>{date('F', strtotime(translate(text(), ' -', '')))}</h4></txp:if_different>

  1. if_different: behaves as it should ony showing unique values for the inner text returned from the query (inner text of “div.campaign”)
  2. date: returns the date “Month Name” (F) of the enclosed string (make sure to use the code for date not strftime)
  3. strtotime: transforms the text “mm-dd-yyyy” to a timestamp for the enclosed string
  4. translate: this is an Xpath function to do a quick find and replace for unanted text on text()

{.}

Now just return all of the items that are output by the tag’s query from the dataset, rinse, repeat.

Last edited by mrdale (2013-09-27 15:30:09)

Offline

Board footer

Powered by FluxBB