a social spider

I was reading about the BloGee project and some other stuff and then I thought “how much trouble would it be writing a WordPress plugin to do some basic ‘social spidering'”. BloGee is about a micro-content format and that’s a bit out of my scope.

I want a simple ‘social’ spider for WordPress so I am going to take some functions of simpleTags and add them to the WordPress xml-rpc server methods, to get some basic functionality I can call upon through the xml-rpc endpoint.

I don’t know if I mentioned that, adding methods to WordPress XML-RPC differs from the straight forward use of Incutio because WordPress uses its own filter/hook system, the actual IXR_Server instance is made and managed by WordPress itself.

I hence don’t use the

class PeekAtYouServer extends IXR_Server {

style class instancing, in stead I make a class where I hook the function I would normally hand to the IXR_Server as callback into wordpress by adding method/callback to the ‘xmlrpc_methods’ filter array.

	class PeekAtYouServer {

		function PeekAtYouServer() {
		//  add callbacks as methods to the array (filter) xmlrpc_methods 
			add_filter('xmlrpc_methods', array(&$this, 'filterXmlrpcMethods'));
		}

		/**
		 * Here I connect the methodName pay.PeekAtYou to a custom function
		 *
		 * This is the array that is added (as pair) to the xmlrpc_methods filter
		 **/

		function filterXmlrpcMethods(&$methods) {
			$methods['pay.PeekAtYou'] = array(&$this, 'onXmlRpcpayPeekAtYou');
			return $methods;
		}

		/**
		 * the custom function used as callback for pay.PeekAtYou
		 **/

		function onXmlRpcpayPeekAtYou($args) {
				global $wpdb; 
//grab posts
				$sql = "SELECT ID, post_title FROM " . $wpdb->posts . " WHERE post_status = 'publish'";
				$posts = $wpdb->get_results($sql);
//cycle through all posts and grab the IDs

				$result = array();
				if (!empty($posts)) {
					foreach ($posts as $post) $this->postids[] = $post->ID;
//I got all the ids in an array, 
//now I grab the tags (query is from the simpleTags plugin)
					$this->getTagsFromCurrentPosts();
				} else {
					return new IXR_Error(404, 'no posts for the selected criterium.');
				}
				return $this->tags_currentposts;
		}

	/**
	 * Get tags from current post views
	 * (SimpleTags plugin)
	 * @return boolean
	 */
	function getTagsFromCurrentPosts() {
		if ( is_array($this->postids) && count($this->postids) > 0 ) {

			// Generate SQL from post id
			$postlist = implode( "', '", $this->postids);

			global $wpdb;
			$results = $wpdb->get_results("
				SELECT t.name AS name, t.term_id AS term_id, tt.count AS count
				FROM {$wpdb->term_relationships} AS tr
				INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
				INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
				WHERE tt.taxonomy = 'post_tag'
				AND ( tr.object_id IN ('{$postlist}') )
				GROUP BY t.term_id
				ORDER BY tt.count DESC");

			$this->tags_currentposts = $results;
			unset($results, $key);
		}
		return true;
	}
	}

I saved the file as PeekAtYouServer.class.php.

Now I need a simple file for WordPress to ‘spot the plugin’, so I can activate it and make the class instance, that adds the custom method and callback function.

	/*
	Plugin Name: PeekAtYou XMLRPC Server
	Plugin URI: https://www.juust.org/
	Description: Adds Social Spidering to your blog
	Author: juust
	Author URI: https://www.juust.org/
	License: GPL
	Version: 1.1
	*/

	require_once 'PeekAtYouServer.class.php';
	$PeekAtYouServer = new PeekAtYouServer();

I save that as PeekAtYouServer.php and upload the lot to a directory /wp-plugins/pay-xmlrpc-server.

In the Plugin screen (wp 2.5) I can activate the plugin, and then make a call to the xmlrpc-endpoint of the blog using pay.PeekAtYou as methodName.

	include('wp-includes/class-IXR.php');
        $client = new IXR_Client('https://www.juust.org/xmlrpc.php');
	$client->query('pay.PeekAtYou');
	$response = &$client->getResponse();
	print_r( $response);

That returns all tags the blog uses.

Next week : adding some basic social blog-spider functions.

Posted in wordpress, xml-rpc and tagged , .

One Comment

  1. Pingback: fjbnheipsssf

Leave a Reply

Your email address will not be published. Required fields are marked *