Web Services Database Migration
Digest
Cascade Server's Web Services interface exposes to SOAP clients an interface for adding and editing assets. This allows repetitive or difficult tasks to be accomplished easily with any SOAP-compatible programming language. As a demonstration of these features, this entry will further explain the task of importing many database entries (from an old CMS) into Cascade Server with PHP and SOAP calls. Web Services makes this process much quicker than a manual import.
Technical
Preparation for Web Services Database Migration
To access Cascade Web Services with PHP, users will obviously need a working PHP installation as well as an installation of Cascade Server. To use the MySQL, SOAP, and Tidy interfaces in PHP ensure that the following lines are in the php.ini file:
On Unix-like systems ensure that the following lines are in your php.conf file:
The Database
The data that the import application will import is stored in database from a homegrown CMS application. In this case, the database is structured with two tables, one containing web pages and another containing quotes. Each entry in the web_pages table consists of a block of HTML content, a path, and some miscellaneous metadata. The script will import the old pages into an existing template in the system, while preserving the metadata and path. This will require a folder structure to hold the pages. The script will take care of this step as well. The quotes table contains unformatted quotes with an entry for the type of quote, the source of the quote and the quote body itself. The script will pull this data from the database and create a page using a data definition for quotes.
TABLE 1: the structure of the web_pages table
|
Id |
title |
path |
keywords |
content |
metaDesc |
|
1 |
Company Overview |
/company/index.html |
Our Company, Service providers, global business, e-economy solutions |
<p>Within five years we have seen <i>global... |
A quick overview of our company |
|
2 |
Mission Statement |
/company/statement.html |
Our Company, Service providers, global business, mission |
<p>When our company started... |
The statement of our mission |
|
3 |
ProductOne Overview |
/products/product1.html |
Our Company, Service providers, global business, ProductOne |
<p>Advancements in research through... |
Overview of the features of ProductOne |
TABLE 2: the structure of the quotes table
|
id |
type |
source |
body |
|
1 |
Training |
Francois de La Rochefoucauld |
Good advice is something a man gives when... |
|
2 |
Global |
Jim Cantalupo |
And ours is a business that requires discipline and focus. |
Implementation of Web Services Database Migration Script
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.

