availability test

I was writing an availability test with starttime-endtime, on a database with ICS schedule data from Google Calendar (userid, eventid, starttime, endtime), and i came up with this one :

  • get all user id’s
  • a get id’s for test.start between table.start and table.end
  • b get id’s for test.end between table.start and table.end
  • c get id’s for test.start < table.start and test.end > table.end
  • merge the three overlapping sets (a b c)
  • available = the difference between all id’s and the merged set

It works but it takes four queries on the entire table.

Isn’t there a more intelligent way of doing that ?

  1. function maat_agenda_check_available($start, $end) {
  2.  
  3. global $wpdb;
  4. //get all id's
  5. $sql = "SELECT userid FROM ".$wpdb->prefix."maat_agenda GROUP BY userid";
  6. $all = $wpdb->get_results($sql, ARRAY_A);
  7.  
  8. //get ids with overlaps
  9. $a = "SELECT userid FROM ".$wpdb->prefix."maat_agenda WHERE endtime > ".$start." AND starttime < ".$start." GROUP BY userid";
  10. $b = "SELECT userid FROM ".$wpdb->prefix."maat_agenda WHERE endtime > ".$end." AND starttime < ".$end." GROUP BY userid";
  11. $c = "SELECT userid FROM ".$wpdb->prefix."maat_agenda WHERE endtime < ".$end." AND starttime > ".$start." GROUP BY userid";
  12.  
  13.  
  14. $rsta = $wpdb->get_results($a, ARRAY_A);
  15. $rstb = $wpdb->get_results($b, ARRAY_A);
  16. $rstc = $wpdb->get_results($c, ARRAY_A);
  17.  
  18. //merge arrays
  19. if(!is_array($rsta)) $rsta = array();
  20. if(!is_array($rstb)) $rstb = array();
  21. if(!is_array($rstc)) $rstc = array();
  22. $rst = array_merge($rsta, $rstb, $rstc);
  23.  
  24. //get uniques
  25. array_unique($rst);
  26.  
  27. return array_diff($all, $rst);
  28.  
  29. }
Posted in mysql, wordpress | 4 Comments

Wazzup

I haven’t touched a website since october.

Posted in juust | 2 Comments

p2p with wordpress xml-rpc

Simple fun with WordPress xml-rpc. I want a way to return a welcome message to interested blog owners.

To get that done, I make a new plugin that adds a new method to the xml-rpc server.

The method ‘p2p.welcome’ is mapped to the plugin function ‘p2p_welcome’, which returns the “about yourself” description of the admin profile.

  1. /*
  2. Plugin Name: P2P rpc
  3. Description: RPC Peer to Peer stuff
  4. Version: 1.0.0  
  5. Author: Juust
  6. Author URI: http://www.juust.org/
  7. */
  8.  
  9. /* xmlrpc_methods hooks into IXR_Server */    
  10. add_filter( 'xmlrpc_methods', 'p2p_xmlrpc_methods' );
  11.  
  12. /* pass the $methods array and add a new one */    
  13. function p2p_xmlrpc_methods( $methods ) {
  14.      $methods['p2p.whoami'] = 'p2p_whoami';
  15.      return $methods;
  16. }
  17.  
  18. /* the function that returns the profile description of user 1 (admin) */    
  19. function p2p_whoami() {
  20.  return get_the_author_meta( 'user_description', 1 );
  21. }

I plug that into the wordpress blog.

Then I can make a small test program :

  • include class-IXR.php which has the IXR_Client class
  • extend IXR_Client as P2PClient
  • add a function P2PClient->whoareyou()
  • …that calls the new method p2p.whoami on the blog
  • echo the returned data : the admin profile description
  1. include_once('/absolute/path/to/class-IXR.php');
  2.  
  3. Class P2PClient extends IXR_Client {
  4.  
  5. var $MyPeerConnection;
  6. var $response;
  7. var $url;
  8.  
  9. function __construct($url="") {
  10.                  $this->url = $url;
  11.     if(!$this->connect($url)) return false;
  12.      }
  13.      
  14. //see if there is an xml rpc endpoint
  15. function connect($url)
  16.      {
  17.        if(!$this->MyPeerConnection = new IXR_Client($this->url)) return false;
  18.      }
  19.  
  20.  //query 'p2p.whoami', store the response
  21. function whoareyou() {
  22.   $this->MyPeerConnection->query('p2p.whoami');
  23.   $this->response = $this->MyPeerConnection->getResponse();
  24.      }
  25. }
  26.  
  27. //init the client, query, echo response
  28. $p2p = new P2PClient('http://loaneys.com/xmlrpc.php');
  29. $p2p->whoareyou();
  30. echo "resp ". $p2p->response;

That returns :

Hi I am juust, admin of loaneys.com. I worked as business economist at project control and proces automation in engineering and utilities. These days, I do some scripting as a hobby and stick to painting as job.

That is a basic way to exchange blog data by extending the xml-rpc endpoint.

Posted in wordpress, xml-rpc | Leave a comment