RpSequel : rfc sql crud with wordpress

I set out to use MsAccess with xhr/ajax to maintain tables I added to my wordpress database, from my desktop. (Because I suck at html forms backends and consider them a waste of time.)

I used a similar technique ten years ago, setting up msaccess as reporting tool for SAP R/3 with RFC dll’s and ActiveX. That remained stable for eight years without maintenance. Hey, I might get lucky with WordPress xml-rpc and xhr/ajax.

I called the example RpSequel.

adding sql rpc functions

To sync the data sets, I will duplicate a list with sequel operations from my desktop database as rfc call to my blog’s xmlrpc-endpoint. To handle that list, I plug a sql crud method into the xml-rpc method array in WordPress :

  1. add_filter( 'xmlrpc_methods', 'rpsequel_methods' );
  2.  
  3. function rpsequel_methods( $methods ) {
  4.     $methods['rpsequel.rpsequelInsert'] = 'rpsequelInsert';
  5.     return $methods;
  6. }
  7.  
  8. function rpsequelInsert($args) {
  9. }

The basic INSERT method itself can be simple:

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //pick the first
  14.         $struct = $structs[0];
  15.  
  16. //compose the mysql insert statement    
  17.         foreach($struct as $key => $value){
  18.             $SqlFields .= " `".$key . "`,";
  19.             $SqlValues .= " '".$value . "',";
  20.         }
  21.  
  22.         $SqlFields=substr($SqlFields, 0, strlen($SqlFields)-1);
  23.         $SqlValues=substr($SqlValues, 0, strlen($SqlValues)-1);
  24.    
  25.         $SqlStatement .= "INSERT INTO ".$wpdb->prefix.$tablename. " (". $SqlFields .  ") VALUES (".$SqlValues . ")";
  26.        
  27. //execute the query        
  28.         $wpdb->query($wpdb->prepare($SqlStatement));
  29.  
  30. //return the record id
  31.        return $wpdb->insert_id;
  32. }

That in itself is not very exciting, it stuffs records in the host’s database. Which is fine, however, I want the blog to respond to individual operations.

I can write rpc-functions for every single function I want the blog to perform, that means truckloads of rpc-functions, on both client and server end. I am incredibly lazy, so I ain’t gonna go there.

adding hooks to sql rpc functions

Lucky for me, WordPress has hooks, hooks are cool.

Adding action hooks (before_insert and after_insert) to the crud method makes it more powerful. Two hooks are enough to separate the business logic of the desktop database from the blogs reporting logic.

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //here is the first action hook,
  14. //it hands the tablename and the records over to any
  15. //function that 'listens' on the action hook
  16.  
  17. do_action('rpsequel_before_insert',  $tablename, $structs);
  18.  
  19. //the rest of the rather dull method
  20.  
  21.         $struct = $structs[0];
  22. //….
  23. //execute the query        
  24.         $wpdb->query($wpdb->prepare($SqlStatement));
  25.  
  26. //here is the second action hook,
  27. //it hands the table name with the new record id
  28. //to any function that 'listens' on the action hook
  29.  
  30. do_action('rpsequel_after_insert',  $tablename, $wpdb->insert_id);
  31.  
  32. //return the record id
  33.        return $wpdb->insert_id;
  34. }

Now it is more exciting.

  • I can send a list of records and a rfc insert-method to the blogs xmlrpc endpoint
  • the rfc crud-plugin can process the records one by one
  • Before and after each insert operation, the method triggers an action.
  • Before inserting, it exposes the record data.
  • After inserting, it exposes the record id.

And the last two, was exactly what I wanted.

adding functionality with plugins

Now I can add tiny plugins, that ‘listen’ on the action hooks in the rfc-methods. If there is an INSERT into the database, my plugins read which table it affects. They can perform actions, either before the insert, with the new record data, or after the insert, with the new record id.

  1. add_action ( 'rpsequel_before_insert', 'before_insert_logic', 10, 2);
  2. add_action ( 'rpsequel_after_insert', 'after_insert_logic', 10, 2);
  3.  
  4. function before_insert_logic($rpc_tablename, $rpc_array) {
  5.    if($rpc_tablename=="ships") {
  6. //do some stuff before inserting incoming records
  7.    }
  8. }
  9.  
  10. function after_insert_logic($rpc_tablename, $rpc_insert_id) {
  11.    if($rpc_tablename=="ships") {
  12. //do some stuff after inserting incoming records
  13.    }
  14. }

That’s basically all it takes. As technique, it has it’s limitations, but it can come in handy sometimes.

integrating ms office and wordpress with vba and xml-rpc

Okay.

Yesterday I made some basic stuff to grab my WordPress blog data with visual basic for applicationsn cos I don’t feel like programming php admin pages for tables.

So let’s make a basic xml-rpc crud plugin and put my agenda on vba remote control.

The basic xml-rpc plugin is simple, plug extra methods into the xml-rpc method array, and write a function per crud-method.

  1.  
  2. add_filter( 'xmlrpc_methods', 'add_agenda_xmlrpc_methods' );
  3.  
  4. function add_agenda_xmlrpc_methods( $methods ) {
  5.     $methods['agenda.addAgendaItem'] = 'addAgendaItem';
  6.     $methods['agenda.updateAgendaItem'] = 'updateAgendaItem';
  7.     $methods['agenda.deleteAgendaItem'] = 'deleteAgendaItem';
  8.     $methods['agenda.reportAgendaItem'] = 'reportAgendaItem';
  9.     return $methods;
  10. }
  11.  
  12. function addAgendaItem($args) { }
  13.  
  14. function updateAgendaItem($args) { }
  15.  
  16. function deleteAgendaItem($args) { }
  17.  
  18. function reportAgendaItem($args) { }
  19.  
  20. //basic login helper function
  21. function CheckLogin($user, $pwd) {}

There now, if I call on the xmlrpc.php file, the extra methods are added to the callback array and I can use the table CRUD functions from my vba desktop.

  1. function addAgendaItem($args) {
  2.  
  3.         $blog_id = (int) $args[0];
  4.         $username = $args[1];
  5.         $password = $args[2];
  6.         $AgendaItem     = $args[3];
  7.  
  8. //remember : add a login check
  9. //(for the example it is irrelevant)
  10.  
  11.         global $wpdb;
  12.         $sql = "INSERT INTO ".$wpdb->prefix."Agenda (
  13.                `userid`, `tags`, `description`, `firstdate`, `enddate`, `link`, `price`, `location`
  14.                ) VALUES (
  15.                '".$AgendaItem[0]['userid']."',
  16.                '".$AgendaItem[0]['tags']."',
  17.                '".$AgendaItem[0]['description']."',
  18.                '".$AgendaItem[0]['firstdate']."',
  19.                '".$AgendaItem[0]['enddate']."',
  20.                '".$AgendaItem[0]['link']."',
  21.                '".$AgendaItem[0]['price']."',
  22.                '".$AgendaItem[0]['location']."'                
  23.                )";
  24.         $wpdb->query($wpdb->prepare($sql));
  25.  
  26.         return $wpdb->insert_id;
  27. }

note : the agendaitem is a struct in an array (see below), I use [0] to get the first struct (which is the actual array with field-value pairs, my record with agenda info).

Activate the plugin, and write a simple test

  1.  
  2. 'Type to hold an agenda info record
  3. Type AgendaItem
  4.     userid As String
  5.     tags As String
  6.     Description As String
  7.     firstdate As String
  8.     enddate As String
  9.     link As String
  10.     price As String
  11.     location As String
  12. End Type
  13.  
  14.  
  15. Function AddAgendaItem()
  16.  
  17. txtURL = "http://wwwblog.com/xmlrpc.php"
  18. txtUserName = "MyUsername"
  19. txtPassword = "MyPassword"
  20.  
  21.   Dim objSvrHTTP As ServerXMLHTTP
  22.   Dim strT As String
  23.   Set objSvrHTTP = New ServerXMLHTTP
  24.  
  25.   objSvrHTTP.Open "POST", txtURL, False, CStr(txtUserName), _
  26.    CStr(txtPassword)
  27.  
  28.   objSvrHTTP.setRequestHeader "Accept", "application/xml"
  29.   objSvrHTTP.setRequestHeader "Content-Type", "application/xml"
  30.  
  31.     strT = ""
  32.     strT = strT & "<methodcall>"
  33.     strT = strT & "<methodname>agenda.addAgendaItem</methodname>"
  34.    
  35.     strT = strT & "<params>"
  36.     strT = strT & "<param><value><string>" & txtBlogId & "</string></value></param>"
  37.     strT = strT & "<param><value><string>" & txtUserName & "</string></value></param>"
  38.     strT = strT & "<param><value><string>" & txtPassword & "</string></value></param>"
  39.  
  40. 'now we go make the struct, I use a Type (stdobject), normally
  41. 'you'd use a recordset
  42.  
  43. Dim a As AgendaItem
  44.  
  45. With a
  46.     .userid = 1
  47.     .Description = "rpc testing"
  48.     .firstdate = "2009/10/14"
  49.     .enddate = "2009/10/14"
  50.     .link = "http://www.juust.org/"
  51.     .price = "rpc testing"
  52.     .location = "limmen"
  53.     .tags = "php, xml-rpc"
  54. End With
  55.  
  56.     strT = strT & "<param><value><array>"
  57.     strT = strT & "<data>"
  58.    
  59.     strT = strT & "<value><struct>"
  60.  
  61.     strT = strT & "<member><name>userid</name><value><string>" & a.userid & "</string></value></member>"
  62.     strT = strT & "<member><name>tags</name><value><string>" & a.tags & "</string></value></member>"
  63.     strT = strT & "<member><name>description</name><value><string>" & a.Description & "</string></value></member>"
  64.     strT = strT & "<member><name>firstdate</name><value><string>" & a.firstdate & "</string></value></member>"
  65.     strT = strT & "<member><name>enddate</name><value><string>" & a.enddate & "</string></value></member>"
  66.     strT = strT & "<member><name>link</name><value><string>" & a.link & "</string></value></member>"
  67.     strT = strT & "<member><name>price</name><value><string>" & a.price & "</string></value></member>"
  68.     strT = strT & "<member><name>location</name><value><string>" & a.location & "</string></value></member>"
  69.  
  70. 'close the struct    
  71.     strT = strT & "</struct></value>"
  72.     strT = strT & "</data>"
  73.  
  74. 'close the struct array
  75.     strT = strT & "</array></value></param>"
  76.  
  77. 'end parameters
  78.     strT = strT & "</params>"
  79.  
  80. 'end method
  81.     strT = strT & "</methodcall>"
  82.  
  83. 'send the lot to the blog  
  84.   objSvrHTTP.send strT
  85.  
  86. 'print the response to debug
  87.   Debug.Print  objSvrHTTP.responseText
  88.  
  89. End function

Et voila :

agenda rpc

That’s yer basic Office-Wordpress XML-RPC integration.

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

  1. 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.

  1.  class PeekAtYouServer {
  2.  
  3.   function PeekAtYouServer() {
  4.   //  add callbacks as methods to the array (filter) xmlrpc_methods
  5.    add_filter('xmlrpc_methods', array(&$this, 'filterXmlrpcMethods'));
  6.   }
  7.  
  8.   /**
  9.    * Here I connect the methodName pay.PeekAtYou to a custom function
  10.    *
  11.    * This is the array that is added (as pair) to the xmlrpc_methods filter
  12.    **/
  13.  
  14.   function filterXmlrpcMethods(&$methods) {
  15.    $methods['pay.PeekAtYou'] = array(&$this, 'onXmlRpcpayPeekAtYou');
  16.    return $methods;
  17.   }
  18.  
  19.   /**
  20.    * the custom function used as callback for pay.PeekAtYou
  21.    **/
  22.  
  23.   function onXmlRpcpayPeekAtYou($args) {
  24.     global $wpdb;
  25. //grab posts
  26.     $sql = "SELECT ID, post_title FROM " . $wpdb->posts . " WHERE post_status = 'publish'";
  27.     $posts = $wpdb->get_results($sql);
  28. //cycle through all posts and grab the IDs
  29.  
  30.     $result = array();
  31.     if (!empty($posts)) {
  32.      foreach ($posts as $post) $this->postids[] = $post->ID;
  33. //I got all the ids in an array,
  34. //now I grab the tags (query is from the simpleTags plugin)
  35.      $this->getTagsFromCurrentPosts();
  36.     } else {
  37.      return new IXR_Error(404, 'no posts for the selected criterium.');
  38.     }
  39.     return $this->tags_currentposts;
  40.   }
  41.  
  42.  /**
  43.   * Get tags from current post views
  44.   * (SimpleTags plugin)
  45.   * @return boolean
  46.   */
  47.  function getTagsFromCurrentPosts() {
  48.   if ( is_array($this->postids) && count($this->postids) > 0 ) {
  49.  
  50.    // Generate SQL from post id
  51.    $postlist = implode( "', '", $this->postids);
  52.  
  53.    global $wpdb;
  54.    $results = $wpdb->get_results("
  55.    SELECT t.name AS name, t.term_id AS term_id, tt.count AS count
  56.    FROM {$wpdb->term_relationships} AS tr
  57.    INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
  58.    INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
  59.    WHERE tt.taxonomy = 'post_tag'
  60.    AND ( tr.object_id IN ('{$postlist}') )
  61.    GROUP BY t.term_id
  62.    ORDER BY tt.count DESC");
  63.  
  64.    $this->tags_currentposts = $results;
  65.    unset($results, $key);
  66.   }
  67.   return true;
  68.  }
  69.  }

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.

  1.  /*
  2.  Plugin Name: PeekAtYou XMLRPC Server
  3.  Plugin URI: http://www.juust.org/
  4.  Description: Adds Social Spidering to your blog
  5.  Author: juust
  6.  Author URI: http://www.juust.org/
  7.  License: GPL
  8.  Version: 1.1
  9.  */
  10.  
  11.  require_once 'PeekAtYouServer.class.php';
  12.  $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.

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

That returns all tags the blog uses.

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