Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-05-08 15:55:50

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

On section/article-list page: How to list keywords of all articles

How can I list the keywords of all the articles in a section, without repeating? Using this, I can list them all, but it renders duplicates for each article that shares individual keywords:

<txp:article
	break=","
	limit="99999"
	sort="keywords"><txp:keywords/></txp:article>

Any ideas?

Last edited by johnstephens (2010-05-08 15:56:04)

Offline

#2 2010-05-08 17:29:14

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

Tru_tags? The article-tag can not execute the queries that the thing requires. But if you still want to do it with it, you need some tiny little PHP (descriped below). Of course you could likely do it with smd_query.

<txp:variable name="keywords">
	<txp:article break="," limit="99999" sort="ID asc">
		<txp:keywords/>
	</txp:article>
</txp:varibable>
<txp:php>
	$keywords = explode(',',variable(array('name' => 'keywords')));
	$keywords = array_unique($keywords);
	sort($keywords);
	echo implode(',',$keywords);
</txp:php>

Not tested, most likely won’t work. Better query is probably faster.

Offline

#3 2010-05-08 17:49:52

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: On section/article-list page: How to list keywords of all articles

A job for rah_metas maybe? ;)

Particularly useful if used in tandem with the future feature of supporting id attribute.
That way, you could fetch the section metadescription from an specific article on that section, and all the keywords from all the articles on that section.

Oh, what a nice caturday to write some code… don’t you think, Gocom?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#4 2010-05-08 18:04:38

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

If it comes to metas, then yes, you could use rah_metas. If it’s not for metas then no. Either way, I would not suggest rah_metas. My code above is faster and better than the validation code that rah_metas uses.

maniqui wrote:

Oh, what a nice caturday to write some code… don’t you think, Gocom?

Heh, actually, I’m thinking of splitting current rah_metas it into couple functions; like unique list. That is all what actually rah_metas does.

The massive amount of code and amount of attributes turns me off and makes the plugin really hard to understand. Yes, it means that the (new?) plugin wouldn’t be a single tag and wouldn’t be a thing that can be called “metas”.

But at the end everybody wants customized output and the plugin really can not do that, because it doesn’t allow custom input. That is what you want too… with the ID request, right? If we make the features into their own tags, the input is fully customizable by the user.

<txp:rah_unique>
	<txp:article>
		<txp:if_custom_field name="meta_keywords">
			<txp:custom_field name="meta_keywords" />
		<txp:else />
			<txp:keywords />
		</txp:if_custom_field>
	</txp:article>
</txp:rah_unique>

That allows any sort of conditional thing output and can be used outside metas. But yes, it’s turn off for those that want single tag. That is why I’m not going to deprecate rah_metas – atleast. For now that all is not that up in my fix list.

Last edited by Gocom (2010-05-08 18:25:04)

Offline

#5 2010-05-08 19:08:33

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: On section/article-list page: How to list keywords of all articles

johnstephens wrote:

Using this, I can list them all, but it renders duplicates for each article that shares individual keywords:

Wouldn’t if_different work here?

Edit: on second thought, I don’t think so.

Last edited by els (2010-05-08 19:09:41)

Offline

#6 2010-05-08 19:30:03

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

Els wrote:

Wouldn’t if_different work here?

Remember that the words are not even in alphabetical order and plus the keyword tag outputs all keywords at once :-) With a huge list like that, it’s really not all that effecient even if we seperate the keywords; The list to check is as long as there are keywords in total.

Offline

#7 2010-05-08 21:18:08

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: On section/article-list page: How to list keywords of all articles

Gocom wrote:

<txp:variable name="keywords">
	<txp:article break="," limit="99999" sort="ID asc">
		<txp:keywords/>
	</txp:article>
</txp:varibable>
<txp:php>
	$keywords = explode(',',variable(array('name' => 'keywords')));
	$keywords = array_unique($keywords);
	sort($keywords);
	echo implode(',',$keywords);
</txp:php>

Prefect! (That is, almost perfect!) There are only two idiosyncrasies with the output:

  1. Several of the keywords were entered in both ‘lower case’ and in ‘Mixed Case’, so this code renders duplicates (although not nearly so many as the way I tried to do it). Is there a way to make it case-insensitive when creating the array of unique keywords?
  2. The list begins with a comma.

Do you know if there is a treatment for these two issues? If not, I think I can live with it for the present. Thank you for your awesome support!

Of course you could likely do it with smd_query.

I’m a SQL newbie, but I use that plugin elsewhere on the site this code is for. If anyone knows a ninja query that will output a comma-separated list of the keywords without duplicates, I’m there. Thank you again!

Offline

#8 2010-05-08 21:54:45

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

johnstephens wrote:

Several of the keywords were entered in both ‘lower case’ and in ‘Mixed Case’, so this code renders duplicates (although not nearly so many as the way I tried to do it). Is there a way to make it case-insensitive when creating the array of unique keywords?

Probably best fix for that is to change the keywords to lowercase in the database.

The list begins with a comma.

Probably you have empty keyword there somewhere (no keywords set for an article) :-)

The newest rah_repeat release includes the code that I mentioned above. So, if I were crazy, I might suggest something along line below:

<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value='<txp:rah_function call="strtolower"><txp:article limit="9999" break="," sort="ID asc"><txp:keywords /></txp:article></txp:rah_function>'>
	<txp:rah_repeat_value />
</txp:rah_repeat>

I have no idea if that works at all. It’s also probably really slow and it requires rah_function and rah_repeat. Because it uses offset="1" it expects that the list starts with empty item and that some article has no keywords.

This soooooo looks like an advertisement. Mmm, I need a catchy theme song.

Last edited by Gocom (2010-05-08 22:00:57)

Offline

#9 2010-05-09 03:01:44

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: On section/article-list page: How to list keywords of all articles

Gocom wrote:

<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value='<txp:rah_function call="strtolower"><txp:article limit="9999" break="," sort="ID asc"><txp:keywords /></txp:article></txp:rah_function>'>
	<txp:rah_repeat_value />
</txp:rah_repeat>

It has been too long since I told you how awesome you are, sir. rah_repeat looks like exactly what I need. I’m still having a couple of odd problems:

#1: I have this right above another article list that includes pagination generated by soo_page_numbers. When I have the above code on the page, the pagination below disappears, still rendering the rest of the page. I looked at the tag trace, but instead of a tag trace I see this:

<!— Runtime: 1.5790 —>
<!— Query time: 0.446909 —>
<!— Queries: 32 —>
<!— Memory: 7665Kb, <txp:keywords/> —>
<br />
<b>Fatal error</b>: Allowed memory size of 8388608 bytes exhausted (tried to allocate 209595 bytes) in <b>/Users/me/Sites/example.dev/textpattern/publish.php</b> on line <b>527</b><br />

#2: If I add any custom field parameters to the article tag enclosed by the the rah_function, to narrow down the keywords list to articles that have a certain custom field value, the whole page dies, without rendering a single line of code or anything. The same custom field attribute works fine using an identical article tag outside the rah_repeat/rah_function context.

Thanks again for your guidance and wisdom! For your theme song, I propose Freezpop’s Here Comes a Special Boy — it’s impossible not to be happy when you hear it.

Offline

#10 2010-05-09 03:42:45

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

johnstephens wrote:

I looked at the tag trace, but instead of a tag trace I see this:

It all seems to use too much resource and there seem to be some limitations what can be put inside tag’s values. If it causes too many parallel parser calls, or something, the whole page dies.

#2: If I add any custom field parameters to the article tag enclosed by the the rah_function, to narrow down the keywords list to articles that have a certain custom field value, the whole page dies, without rendering a single line of code or anything. The same custom field attribute works fine using an identical article tag outside the rah_repeat/rah_function context.

What happens if you do:

<txp:variable name="keywords">
	<txp:article break="," limit="99999" sort="ID asc">
		<txp:if_custom_field name="somestuff">
			<txp:keywords/>
		</txp:if_custom_field>
	</txp:article>
</txp:varibable>
<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value='<txp:variable name="keywords" />'>
	<txp:rah_repeat_value />
</txp:rah_repeat>

It will still cause undefined amount of parser calls and huge loop that isn’t that fast or pretty. In some situations TXP’s syntax is a troll face.

The answer reply is now sponsored by…

Freezpop’s Here Comes a Special Boy

:-)

Last edited by Gocom (2010-05-09 03:44:17)

Offline

#11 2010-05-09 04:06:12

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: On section/article-list page: How to list keywords of all articles

Gocom wrote:

What happens if you do:

<txp:variable name="keywords">
	<txp:article break="," limit="99999" sort="ID asc">
		<txp:if_custom_field name="somestuff">
			<txp:keywords/>
		</txp:if_custom_field>
	</txp:article>
</txp:varibable>
<txp:rah_repeat offset="1" break="," duplicates="1" sort="regular asc" value='<txp:variable name="keywords" />'>
	<txp:rah_repeat_value />
</txp:rah_repeat>

This:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 680 bytes) in /Users/me/Sites/example.dev/textpattern/lib/txplib_misc.php on line 1106

Gocom wrote:

It will still cause undefined amount of parser calls and huge loop that isn’t that fast or pretty. In some situations TXP’s syntax is a troll face.

I guess that’s what’s going on. I forgot to say: The disappearance of my soo_page_numbers article-list pagination happened also when I used the raw PHP you posted in your first response.

So, do you think there is a reasonable way to do this that won’t break Textpattern? Or is it an inherent limit?

The page has an very long article list that I’m able to filter by piping URL-variables into a keywords attribute. What I want to accomplish is to display a list of keywords that a user can click on to filter the list.

The answer reply is now sponsored by…

“Freezpop’s Here Comes a Special Boy”

:-)

:-)

Offline

#12 2010-05-09 04:47:19

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: On section/article-list page: How to list keywords of all articles

johnstephens wrote:

Or is it an inherent limit?

Your server probably has some kind of memory limit. If it is shared host, then probably it’s set to really low. TXP’s parser has some limitations, altho, also advantages (tag syntax)… my plugins might be dangerous too, they mostly use really crappy code.

Let me say, that I don’t personally use TXP tags for long article lists for various reasons (like for example this page’s runtime improved 200% without article_custom, and today there is only 562 articles in db). Same goes for super uber nesting. I try to avoid it.

johnstephens wrote:

The page has an very long article list that I’m able to filter by piping URL-variables into a keywords attribute. What I want to accomplish is to display a list of keywords that a user can click on to filter the list.

Can you post the tag trace from the page? Before you add the keywords code.

Last edited by Gocom (2010-05-09 05:01:33)

Offline

Board footer

Powered by FluxBB