Archive pour la catégorie 'Scripting'

WordPress As Back End For Posh Widgets and OpenSocial Gadgets

Mercredi 26 août 2009

I developed for percolab.com, under the supervision of my friend Yves Otis a framework that can generate interfaces for OpenSocial gadgets (Orkut, iGoogle), widgets (Portaneo Posh) and HTML.

The goal of this framework is to offer to Québec’s immigrants a way to show to others and themselves their abilities. These abilities are regrouped in activities, themselves grouped under broader terms like Practical Life, Learning French, Experiences, About Me, Working Outside Québec and so on.

The idea is for the immigrant to be able to edit a widget in Posh, or a gadget in Orkut or iGoogle that is generated by the framework. The same core templates are used to generate all the interfaces.

Here’s two examples:

gadget.orkut.png
gadget.igoogle.png

You will note that nothing is really styled or designed. This is an early stage of the project where the designers have not yet worked on the templates.

The framework is based on the marvelous CodeIgniter. Fundamentally, here how it works. Sorry, I’m not very good at modelling, but you will get the basic idea.

schema_simple_epimm.png

Ok, it’s much more complicated than that. I’ll post about it later because the framework use MySQL 5.1 XML capabilities as an almost free form database, where the all the values populating the forms are filtered using a mix of SQL/XPath, XSLT and jQuery, bypassing PHP to do this work. Basically, you drop a form in the framework, and it just work, you don’t have to write new PHP code to handle the values, you don’t need to modify the database schema, you don’t need to edit a config file for the database like in Rails.

To get back to my schema, the first request to the framework is done by a GET from Posh/Orkut/iGoogle, and then, CodeIgniter throw back at the host of the widgets/gadgets a chunk of XHTML where all interactions (create, update, delete )are done with Ajax. In a way, the widget/gadget contains a self-contained mini application.

Some activities are basically resources where the user does not need to edit anything. The only thing he/she can do to contribute is to comment about the resource and add some pointer to other resources on the Internet. Yves, being what he is, asked me, why not using WordPress, so, average people who are not programmers will be able to edit the resources and control/approve the comments? Since WordPress offer a nice interface that almost anybody can use, I plugged WordPress for some activities on the framework.

Here’s an example of an activity of the kind « ressource ».

The top of the gadget.

orkutdico1.png

The bottom of the same gadget.

orkutdico2.png

The content of this gadget is coming from WordPress, and the comment fields, while done in the template that generate the gadget are done in the framework, are hook to WordPress.

When for example, Orkut load this particular gadget, the framework do an XML-RPC request to WordPress using the WordPress XML-RPC API with the methods metaWeblog.getPost and get the content of a specific WordPress post related to the resource needed. The framework also does a request to get all the approved comments for this post with the method wp.getComments. Then the framework throws back the content to the Orkut gadget.

When a user press the button to submit a comment, the values of the comment fields are send to the framework via Ajax (gadget.io.makeRequest() on Orkut/iGoogle and jQuery/$.ajax with Posh). Then the framework does an XML-RPC request with the method wp.newComment to the WordPress blog. The comment is inserted in the WordPress blog, and an email is sent to the administrator to approve this comment.

Here’s another schema that shows what is happening. (Gee, I should really learn how to use OmniGraffle.)

schema_simple2_epimm.png

Letting the framework doing the XML-RPC calls to WordPress is the way to bypass the troubles you get with Ajax and the same origin policy. Also, it’s easier to program this way, than trying to do this from the Orkut gadget or Posh widget, because these kind of environments are quite hard to debug.

So, to sum it up, the user of the gadget never goes to the WordPress blog, she/he never knows in fact that we are using a WordPress blog. We post the comment anonymously, and set WordPress to accept XML-RPC requests, and also allow commenter not to be registered in WordPress.

There was a catch that I have too figured out how to post comment under the name of the commenter on WordPress. In fact the solution was provided kindly by Joseph Scott on one of the WordPress forums. The basic idea, is to leave blank the name and password of the owner of the blog (param 2 and 3) with the method wp.newComment and install a plugin that will implement a filter to allow XML-RPC anonymous comment.

Sylvain Carle will be guest speaker at an event on using WordPress beyond the blog. I’ll guess I will now have to attend :) .

Update (2009-08-27): This conf is just too expensive for me (300 $ to 400 $ for 4 hours).

Interview of Evan Prodromou of identi.ca

Mercredi 24 septembre 2008

Fascinating interview from Phil Windley of ITConversations of Evan Prodomou which is the promoter of identi.ca, a twitter compatible platform. Beside identi.ca, what was quite interesting for me is the Laconica microblogging platform (PHP) which identi.ca is based upon. It appears that you can install this platform to start your own microblogging interface under your own domain, which can be a fine proposition under some circumstances. Last but not least, it seems that identi.ca is compatible with Twitterrific, a nice twitter client on OS X.

NYT: 24 hours to convert 11 millions images to PDF

Jeudi 1 novembre 2007

Derek Gottfrid of the New York Times on how they converted 11 millions TIFF images of the NYT archives to PDF using Amazon EC2/S3 services.

I had been using Amazon S3 service for some time and was quite impressed. And in late 2006 I had begun playing with Amazon EC2. So the the basic idea I had was this: upload 4TB of source data into S3, write some code that would run on numerous EC2 instances to read the source data, create PDFs, and store the results back into S3. S3 would then be used to serve the PDFs to the general public. It all sounded pretty simple, and that is how I got the folks in charge to agree to such an idea — not to mention that Amazon S3/EC2 is pretty easy on the wallet.

Derek Gottfrid, Self-service, Prorated Super Computing Fun!, nytimes.com

Open in Path Finder from the terminal

Mercredi 2 mai 2007

On OS X, tuck this function in you .bashrc file to open from Terminal.app a repertory in Path Finder.

function pathfinder {
	open -a "Path Finder" $1
}

Then in terminal, to open the current directory do
% pathfinder .

You can open also a specified directory:
% pathfinder /Users/me/Desktop

Since Path Finder can call appropriate applications or open in itself different type of documents, you can also do the following:
% pathfinder ShellScripting.pdf

Trapping Errors with simplexml for Not Well-Formed XML

Mercredi 7 février 2007

I discovered the hard way that in PHP5 there are no obvious ways to detect if some XML is well-formed, especially if you want to deploy on Unix/Windows platform and don’t want to access the shell directly.

Adding to this problem, I discovered also that the DOM and simplexml extensions can’t use the PHP5 exception handling to trap the errors when the XML is not well-formed. Using simplexml or the DOM extensions against not well-formed XML, the errors generated by these extensions are not trapped and are displayed immediately.

It’s possible to load with the DOM or the Tidy extensions not well-formed XML, and then repair it on the fly. But what if you need to detect not well-formed XML and provide a message stating the error?

Fortunately, after some research, I found that you could use the libxml functions (PHP 5.1 and over) to test XML well formedness and trap XML errors. So, I wiped out this little function called get_xml_object (see here for the inspiration) that allow me to trap errors when simplexml is used to parse XML. The function is quite simple, by default, you provide a path to a XML file. If you want to use a string, just add another argument after the first parameter (it can’t be anything, but here’s I chose « string » for clarity sakes). You can also replace the simplexml extension by the DOM extensions if you prefer this extension to parse XML.

The function get_xml_object will return an array that contains two keys, errors and xml. In this example, $result=get_xml_object($s, "string"), $result is an array. If there are no errors, $result['errors'] will be set to null. If everything is ok, $result['xml'] will contains a simplexml object that you can then manipulate with the simplexml extension.

$s = "tag>hello world</tag>";
// $s = "<tag>hello world</tag>";

function get_xml_object ($xml, $xmlFormat="file") {

  $xml_object = null;
  $result = array ("errors" => null, "xml" => null);

  libxml_use_internal_errors (true);
  $xmlFormat == "file"  ? $xml_object = simplexml_load_file ($xml) 
                        : $xml_object = simplexml_load_string ($xml);

  if (!$xml_object) {
     $errors = libxml_get_errors();
     foreach ($errors as $error) {
         $error_msg = "Error: line: " . $error->line 
                    . ": column: " . $error->column . ": " 
                    . $error->message . "\n";
     }
     libxml_clear_errors();
     $result["errors"] = $error_msg;
  } else {
    $result["xml"] = $xml_object;
  }
  return $result;
}

$result = get_xml_object ($s, "string");

if ($result['errors']) {
  var_dump ($result['errors']);
} else {
  var_dump ($result['xml']);
}

Path Finder: Script to Avoid Warning when Closing a Window with Tabs

Vendredi 5 janvier 2007

With Path Finder 4.6.1, when you close a window with tabs, you get an alert that’s asking if you really want to close all the tabs. This is very annoying. On the Path Finder forum, there is a mention of that, its supposed to be on the todo list for the developpers. Mainwhile, you can use this AppleScript if you want to get rid of the warning. Just put the script in your AppleScript Menu, or better, in your FastScripts menu, set a shortcut (mine is control-w) and there you go. Its not ideal, and I rather attach this script to the File menu of Path Finder, but for that, you need PreFab UI Actions because Path Finder is not « AppleScript attachable ». Well, that’s another reason tempting me to buy this app, and also PreFab UI Browser.

tell application "Path Finder"
  activate
  try
    set allWindows to every finder window
    set mainWindow to item 1 of allWindows
    set go to true
    repeat while go
      try
        set mainWindowName to name of mainWindow as string
        if mainWindowName is equal to "" then return
        tell application "System Events"
          keystroke "w" using command down
        end tell
      on error
        return
      end try
    end repeat
  on error
    return
  end try
end tell

So the only way I have found is to ask each time for the name of the front window, and if the name doesn’t exist, then it means that there are no more windows to process. Since we took a reference to the main window with set mainWindow to item 1 of allWindows, this made sure that we won’t close a Path Finder window that is behind the main Path Finder window.