Squiz Labs Blog - The latest news from the R&D arm of Squiz®

Subscribe to our RSS feeds

Updating multiple metadata values in a single JavaScript API call

The MySource Matrix JavaScript API now has a new function, setMetadataAllFields(), which accepts an array of metadata values and sets them on a single asset in one call. This function will be made available in version 3.26.1, due out on the 8th of Feb 2010.

/**
 * Set metadata values of multiple fields for an asset
 *
 * @param string   asset_id     Id of asset to set metadata for
 * @param array    field_info   Field Ids and their values        
* @param function dataCallback Custom callback function
*
* @version $Revision: 0.1
 */
function setMetadataAllFields(asset_id, field_info, dataCallback)
The JavaScript API could always be used to set a single metadata value for an asset, but we found ourselves sending many requests to the API to set up to 15 different metadata values from our custom frontend interfaces. The overhead of creating connections for 15 requests, and the overhead of performing the same action 15 times within MySource Matrix, left us searching for a new solution.

With this new function call, all metadata fields and their corresponding values are provided in one go, so that there is only a single connection no matter how many metadata values you have to set. Internally, MySource Matrix only needs to acquire and release metadata locks once, reducing the number of DB queries being executed. An even bigger saving is made by not having to regenerate the metadata content file each time a metadata value is set, saving DB queries, disk I/O and PHP processing time.

If you've got a few metadata values to set on an asset, all these savings will add up to a significant performance improvement. Even just setting 5 metadata values could take almost 10 seconds (depending on a lot of factors) while this new function call can return in less than 2.

Like the call that is made to update a single metadata field, this function will return an error if it fails to update any of the fields.

Updating your code to use this new function is pretty easy. The following two code blocks show the old and new way of setting multiple metadata values for an asset.

The first shows the existing way you would be setting 3 metadata values for an asset:

<script type="text/javascript">
  window.api_key = 'XXXXXXXXXXXX';
 
  // Asset ID of the asset we are updating
  var assetid = "123";
 
  // Update the metadata of the asset
  // Metadata field 1000 = Title
  //                1001 = Description
  //                1002 = Keywords
  setMetadata(assetid, "1000", "Test page");
  setMetadata(assetid, "1001", "Just a simple test page");
  setMetadata(assetid, "1002", "test, page");
</script>

And this is the new way of setting these values:

<script type="text/javascript">
  window.api_key = 'XXXXXXXXXXXX';

  // Asset ID of the asset we are updating
  var assetid = "123";
  var field_info = new Array(); 
 
  // Update the metadata of the asset
  // Metadata field 1000 = Title
  //                1001 = Description
  //                1002 = Keywords
  var field_info     = new Array(); 
 field_info["1000"] = "Test page";
  field_info["1001"] = "Just a simple test page";
  field_info["1002"] = "test page";
  setMetadataAllFields(assetid, field_info); 
</script>

Rest assured that the setMetadata() function will continue to work as normal, so upgrading will not cause your API calls to break. But you may want to consider rewriting any parts of your code that set multiple metadata values to get a performance improvement.

Squizlabs

Squiz Labs is the research and development arm of Squiz, the company behind the MySource Matrix and MySource Mini open source content management systems.

Our PHP and JavaScript developers are responsible for producing innovative enterprise-quality software while our interface designers and testing team ensure our products both look great and are easy to use.

Take a look at our product line-up:

MySource Matrix

MySource Matrix is a robust, scalable Content Management solution for the enterprise. It competes effectively with and surpasses the offerings of other CMS's such as Vignette and Interwoven.

Read Articles | Visit Website

MySource Mini

MySource Mini is the next generation CMS from Squiz boasting the first true inline editing experience. Simplicity is the key to MySource Mini with enterprise features as easy to use as common editing tools.

Read Articles | Visit Website

PHP_CodeSniffer

PHP_CodeSniffer is a tool that tokenises and sniffs PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent.

Read Articles | Visit Website