Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-06-01 03:00:39

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

[request] Auto-populate Link Descriptions

This plugin would attempt to automatically populate link descriptions using the target link’s descrption meta tag:

Something like this:

For each link an empty description
   Add link ID to list

For each list item ID
   Read URL
   Get description meta tag content from URL (example: http://stackoverflow.com/questions/3711357/get-title-and-meta-tags-of-external-site)
   (Clean) insert description into TXP database

The end…?

Last edited by maruchan (2012-06-01 03:02:44)

Offline

#2 2012-06-01 05:08:23

tom1
Member
Registered: 2009-03-20
Posts: 36

Re: [request] Auto-populate Link Descriptions

I’d like the idea of doing it live when page is loaded, so descriptions would be always up to date. Too bad it might be a performance issue, if you show many links on one pageload.

Offline

#3 2012-06-01 05:17:18

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

Re: [request] Auto-populate Link Descriptions

Not only it causes performance issues, it would abuse the sites you would be linking. Doing constant live request to linked remote sites cause unwanted traffic to those others, which they do not want. The right thing would be to fetch the description once and cache it. Otherwise you are — well, you are an asshole to put it the nice way.

Offline

#4 2012-06-01 06:05:21

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

Re: [request] Auto-populate Link Descriptions

Anyways, as far doing such plugin, technically it shouldn’t require more than hooking to link, link_save and link, link_post callback events. Then checking for POST description’s contents and if it’s empty, fetching the POST url with cURL. If the page is received successfully, the description can be picked from the markup using either DOM extension (xPath or plain), regular expression (not ideal for complex markup) or some other 3rd party node/HTML parser.

Along the lines of this, small totally untested example:

/**
 * Registers the function to Link editor's saving events
 */

register_callback('abc_link_description', 'link', 'link_save', 1);
register_callback('abc_link_description', 'link', 'link_post', 1);

/**
 * Fetches and modifies description
 */

function abc_link_description() {

	extract(psa(array(
		'url',
		'description',
	)));

	/*
	 	Checks that CSRF token is valid, that fetching is needed and that required 
		DOM extension and cURL are available
	*/

	if(ps('_txp_token') != form_token() || $description !== '' || !$url || !class_exists('DOMDocument') || !function_exists('curl_init')) {
		return;
	}

	/*
		Fetches the page contents using cURL
	*/

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
	curl_setopt($ch, CURLOPT_TIMEOUT, 15);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

	$r = curl_exec($ch);
	$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);

	/*
		Checks that page is there and HTTP status says OK.
	*/

	if(!$r || $http != 200) {
		return;
	}

	/*
		Parses description from the document using DOM extension
	*/

	$dom = new DOMDocument();
	$dom->loadHTML($r);
	$meta = $dom->getElementsByTagName('meta');

	foreach($meta as $m) {
		if($m->getAttribute('name') == 'description') {
			$desc = $m->getAttribute('content');
		}
	}

	/*
		Replaces HTTP POST description
	*/

	if(!empty($desc)) {
		$_POST['description'] = $desc;
	}
}

Last edited by Gocom (2012-06-01 06:13:45)

Offline

#5 2012-06-01 06:54:26

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Re: [request] Auto-populate Link Descriptions

Nice one, Jukka. I tried your code, but I couldn’t get it to work. Maybe it’s my plugin packaging. I used ied_plugin_composer. I’m not seeing anything in the error logs.

Offline

#6 2012-06-01 07:33:04

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

Re: [request] Auto-populate Link Descriptions

Well, as my tiny disclaimer reads, it’s just an idea along the lines, an example, not really what I would call tested working code. You ask cake, I offer baking soda and eggs.

Last edited by Gocom (2012-06-01 07:36:15)

Offline

#7 2012-06-01 14:47:38

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Re: [request] Auto-populate Link Descriptions

Understood! I will make it work. Thanks for the ingredients.

Offline

#8 2012-08-20 14:37:38

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

Re: [request] Auto-populate Link Descriptions

On public side, you can retrieve descriptions with etc_query, like this:

<txp:etc_query url="http://forum.textpattern.com/viewtopic.php?id=38115" query="//head/meta[@property='og:description']/@content">
{$htmlspecialchars({?})}
</txp:etc_query>

Currently etc_query does not go on the admin side, but it suffices to change its type.

Edit: added htmlspecialchars, just in case. Alternatively,

<txp:etc_query url="http://forum.textpattern.com/viewtopic.php?id=38115" functions="htmlspecialchars" query="htmlspecialchars(string(//head/meta[@property='og:description']/@content))" />

gives the same result.

Last edited by etc (2012-08-20 14:53:49)

Offline

Board footer

Powered by FluxBB