Monday, May 18, 2009

Dealing with Zotero collections

Zotero offers some nice API functions for collection management.

  • Zotero.Collections() return an array to all the collections in the current library. I am not sure how this will handle shared collections but it is worth investigation.
  • Zotero.Collection is an object that will handle everything related to a Zotero collection. 
To add a new collection, one can:
  • Add the row directly to the DB. A dirty solution that used to work for version 1.* until 1.5 beta. However, it stopped working with version 2.* (the beta version at least) because the key field in collections table can not be empty anymore.
  • Use Zotero.Collections.Add(__collection_name__) and reference the new object 
// Create a Zotero collection object
newCollection = new Zotero.Collection();

To add items to a collection, the direct DB query is still working but I would not advise you to use it as it will break the stability of the DB sometimes. So the better approch is to use Collection.addItem() or Collection.addItems() methods.

Saturday, May 16, 2009

Javascript Date Format

Just a small note, Javascript has two functions for the Date.Year. I needed to use the Full year function but unintentionally I have used getYear() function.

The correct method is getFullYear()

Zotero Database Abstraction

It should be noted that the database abstraction layer for Zotero is still not complete. The authors noted that
            // Until the native dataset methods work (or at least exist),
            // we build a multi-dimensional associative array manually

It took me a while to understand how I can get query results from Zotero.DB. It was mentioned in one of the functions comments but was not clarified in a document file. The query returns varies depending on the query type as per the following comments on the Zotero.DB.query() function
  * Run an SQL query
 *  Optional _params_ is an array of bind parameters in the form
 *        [1,"hello",3] or [{'int':2},{'string':'foobar'}]
 *     Returns:
 *       - Associative array (similar to mysql_fetch_assoc) for SELECT's
 *     - lastInsertId for INSERT's
 *     - TRUE for other successful queries
 *     - FALSE on error
You should notice in the first comment that the SELECT Query would return an associative array that is built by Zotero coders and not a native multidimentional javascript array.

The easiest way to parse such an array is to use a For Each loop or a while loop.
            for (duplicateItems in potentialDuplicateItems)
                // Get each row now
                var oneRow = potentialDuplicateItems[duplicateItems];
                idsArray[duplicateItems] = oneRow['ID'];

Make sure you use the correct index to read from each row or you might get a lot of undefined errors. Errors are not bad but when you are developing for a firefox extention and you have to restart your browser for each test or correction, it might become a hassle.

Zotero Extension Development

I beleive that Zotero is the future of Reference Management. This tool is free, nice, simple, and powerful.
I am developing some simple extenstions for it and I wanted to share this experience with other readers who might be interested in that.

The documentation on extension development for Zotero is still not adequare and I would love to help anyone who is looking for help.