Implementation

Implementation ( download code)

The script will be segregated into two parts. The first part will handle importing the web pages, while the second will handle importing quotes. To import the web pages from the MySQL database, the script will first fetch the data with PHP's MySQL API. Next it will tidy it up with PHP's Tidy interface. Tidy will ensure that our data is well-formed XHTML encoded in UTF-8, which is suitable for importing into Cascade. Finally, the PHP SOAP API will take the data as an associative array and make the appropriate Web Services calls. The procedure for importing quotes will be similar.

Importing the Web Pages

Initially, the import_web_pages() function will attempt a connection with the MySQL server. Luckily, PHP makes MySQL interaction very simple with an intuitive API. The script also configures Tidy to output the type of XHTML we need.

Next, the script will initialize some arrays to hold pages and paths. The paths will be an associative array that acts like a hash table to keep track of each unique path. Finally, we will send the query to the MySQL server and store the result set in $result.

The script will now iterate over every result returned from the table. Notice that it replaces the old HTML in the content field with tidied XHTML. We ensure that the content will be valid and well-formed XHTML after the wrapping it in Cascade's system-xml tag.

Here we split the old CMS-style path field into a Cascade path and a system name for our new page. We know that path elements are separated by '/' so we look for the last one in the path. The string after that is the system name (once we remove the extension) and everything before it is the Cascade parent directory.

Finally, we store the path and the page in our previously-initialized arrays and end the while loop.

The rest of import_web_pages() simply closes the MySQL connection, sorts the path array, creates a SOAP client and calls functions to add each folder ( add_folder()) and each web page ( add_page()). These functions will be described below.

Adding a Single Folder

The add_folder() function adds a single folder to Cascade. It takes in the path of the folder to add and removes the last path element as the folder name. Then it constructs an associative array that the SOAP API will turn into a SOAP message. When using SOAP be warned that even though your message might conform to the WSDL that you use, this does not mean that your message was a success. Be sure to check the response that the server returns with __getLastReponse() as shown below.

In PHP the associative array that the SOAP functions accept is structured in a 'tag name' => "tag value" fashion. To pass in multiple tags of a particular tag name, simply associate the 'tag name' key with array of values. This is demonstrated in the add_page() function. Tags may be nested by associating particular tags with other associative arrays.

Adding a Single Web Page

The add_page() function is very similar to the add_folder() function, although the request structure is more complex and we must ensure that the metadata we pass to Cascade is valid UTF-8 text.

Importing Quotes

Importing the quotes is very similar to importing the web pages with one major difference: instead of simply passing XHTML content to Cascade, we want to place values in a data definition. Before running this script, we created a data definition in Cascade named Quote which contained fields for the source, type, and body of each quote. Then we created a configuration set which would format this data definition. Here we see the data definition we used for each quote.

Next, you can see the code for importing the quotes. Notice the structure of the parameter array this time. Structured data definition nodes are packaged in an array inside of the structuredDataNodes tag. Each element is an entry of the structuredDataNode array. To pass more than one node, simply add another level of arrays inside of structuredDataNode.

Summary

The Web Service interface to Cascade Server eases automation of asset creation. PHP's built-in SOAP and MySQL support makes it useful for migrating to Cascade Server from another product. Content passed to Cascade may have to be converted to XHTML with PHP Tidy and to UTF-8 with the PHP function, utf8_encode(). The PHP SOAP API requires parameters be formed using associative arrays, whose keys describe the tag names. If the call succeeds the server's response can be accessed through the __getLastResponse method of the SoapClient object. Migration becomes far less tedious and time consuming with the use of Web Services.