DATABASE PUBLISHING

Introduction

The WebSite Director (WSD) Database Publishing feature allows WSD users to update information in any database that can be accessed by WSD. This allows site owners to add workflow to database updating for dynamic content and other types of information stored in SQL databases.

Note:The WSD Database Publishing feature does not support the retrieval of dynamic content directly from the SQL database. A directory structure on the web site (usually hidden from public view) mirrors the content stored in the database. WSD uses Page Layout Templates and data records to update database content.

This tutorial will guide you through the following steps required for database publishing using WSD:

      1. Site Setup for Database Publishing
      2. Converting Existing Database Content for Updating by WSD
      3. Creating Page Layout Templates to Update a Database
      4. Using Page Layout Templates to Update a Database
      5. Publishing into a Database

If your site has already been set up to use WSD for database publishing, you may go directly to "Creating Page Layout Templates to Update a Database" or "Using Page Layout Templates to Update a Database" below.

1.Site Setup for Database Publishing

To implement this feature, the site owner must maintain a "flat-file data record" for EACH database record created/managed by WSD. To do this, you must first create a directory to be used for storing database information in the file system (for example, /database_info). If you have multiple database record types (for example, Employee records and Product records), create a child directory for each record type (for example, /database_info/employees and /database_info/products).

For additional security, you can password-protect the /database_info subdirectory, using the same usernames and passwords assigned to WSD users, by placing a copy of the ".htaccess" file located in the WSD install directory into the /database_info directory. If your web server does not support .htaccess files, you can set up another web server authentication mechanism for the /database_info directory.

You should clearly define and document the naming convention you use for the "flat-file data record" content associated with your database records. For example, if you are updating an employee database, your flat file records should clearly indicate exactly WHICH employee’s information is contained in the file. A sample directory listing for employee records might look like this:

If your database is empty and all content will be updated using WSD, go directly to "Creating Page Layout Templates to Update a Database" below.

2.Converting Existing Database Content for Updating

If you have existing content in your database, you should use the following to manage that content using WSD:

  1. Backup your existing database.
  2. Create the Page Layout Templates that will be used to update the database (see "Creating Page Layout Templates to Update a Database" below). These templates will also be used to export existing data from the database during this conversion process.
  3. If any of the template field names in the Page Layout Templates do not match the corresponding database record field names, you will need to add a 'dbfield="<database record field name>"' attribute to each template field definition that does not have matching field/record names.
  4. Test the templates created in Steps 1 and 2 to verify that they have the correct field definitions to update the database table properly.
  5. For each table in the database that will be updated by WSD, perform Steps A through C below.
  6. Verify that the database content is correct.
  7. Begin using WSD’s database updating functionality and update your database content using Page Layout Templates in WSD.
     

Step A.
Make sure that the content directory for the appropriate table has been created (as defined in "Site Setup for Database Publishing", above).

Step B.
From the command line in the WSD installation directory, execute the following command:

     
wsdutils exportdb <template> <table> <directory>

where:

  • <template> is the filename (not path) of the WSD page layout template containing the database connection commands and the template fields that match the database table you are exporting.
  • <table> is the name of the table in the database specified in <template> that you want to export records from.
  • <directory> is the name of the directory relative to the Document Root that the content files will be generated in (verified in Step 1 above).

This WSD utility function will create one file in the directory specified by <directory> for each record in the table specified by <table>.  The field data in each database record will be written to that file within template tags defined in the template specified by <template>.  The filename of each generated file will be built from the database key value(s) and will end in an extension of ".html".

Step C. 
Republish each of the files created in Step 2 through WSD to assign the appropriate Page Layout Template and any other desired properties.  This can be done through a Windows command script or Unix shell script.

If you are using WSD on a Windows server, create a command script similar to the following:

set REMOTE_USER=wsduser
set REQUEST_METHOD=GET
set NOTCGI=YES
set QUERY_STRING="Command=MODIFY
       &Filename=\database_info\employees\%1
       &ServerFilename=\InetPub\wwwroot\database_info\employees\%1
       &Stage=99
       &layout=employeedb.template"
\InetPub\Scripts\wsd\wsdsubmit


In this example, "wsduser" is a WSD username, the web server's Document Root is "\InetPub\wwwroot", WSD is installed in the "\InetPub\Scripts\wsd" directory, the database content directory is "\database_info\employees", and the Page Layout Template associated with the database table is "employeedb.template". The "QUERY_STRING" value following the equal sign must be all one line. Assuming that the above command script is called "domodify.cmd" and is located in the WSD installation directory (\InetPub\Scripts\wsd), you would then use the following command from a Windows Command Prompt to run the script for each file in the database content directory:

     for %F in (*.html) do \InetPub\Scripts\wsd\domodify %F

On a Unix server, the following Bourne shell script would perform the same operation:

#!/bin/sh
#
#     Submit and Publish all HTML documents in the current directory
#
export REMOTE_USER=wsduser
export REQUEST_METHOD=GET
export NOTCGI=YES
for DOCUMENT in *.html do
   export QUERY_STRING="Command=MODIFY&Filename=/database_info/employees/
     $DOCUMENT&ServerFilename=/apache/htdocs/database_info/employees/
     $DOCUMENT&Stage=99&layout=employeedb.template"
   /apache/cgi-bin/wsd/wsdsubmit
done


Or if using the C shell:

#!/bin/csh
#
#     Submit and Publish all HTML documents in the current directory
#
setenv REMOTE_USER wsduser
setenv REQUEST_METHOD GET
setenv NOTCGI YES
foreach DOCUMENT (*.html)
  setenv QUERY_STRING "Command=MODIFY&Filename=/database_info/employees/
     $DOCUMENT&ServerFilename=/apache/htdocs/database_info/employees/
     $DOCUMENT&Stage=99
    &layout=employeedb.template"
  /apache/cgi-bin/wsd/wsdsubmit
end

3.Creating Page Layout Templates to Update a Database

Database Publishing using WSD is enabled through the use of Page Layout Templates containing database update commands. These templates are used to maintain database content corresponding to records stored in the file system. The database commands in a Page Layout Template define the database and database tables into which WSD will publish data when a template is applied.

The Page Layout Templates you use to publish database content must contain SQL queries. These queries, used to Insert, Update, and Delete database records, are based on the WSD request type (ADD, MODIFY, DELETE, etc.) that you will use to publish your database content from the file system version of each database record. If you are not familiar with your file system structure of database information, carefully read "Converting Existing Database Content for Updating by WSD" above!

In addition to specifying database information, Page Layout Templates used for database updates also define the data entry fields used to enter the database field data on the Create/Edit Online screen. Instructions for defining these fields are contained in the Page Layout Template tutorials and the Page Layout Templates User Guide (PDF version) accessed from the Tutorial Index on any WSD screen.

The database definition used in the following examples is that defined for the WSD demo site at http://demoweb.cyberteams.com, where CyberTeams provides a PHP-based dynamic content engine for simple experimentation. If you do not have access to this demo area and wish to experiment using the "sample.db_template" installed with WSD, please contact your CyberTeams' representative for a username and password.

The "sample.db_template" installed with WSD assumes the following database definition:

To specify Database Publishing for a specific Page Layout Template, the template must include two sets of template commands, normally defined at the top of each Page Layout Template used for database updating.

The first set of commands specifies the information WSD needs to connect to the database server to publish the data into your database

    1. DatabaseName <text-value> - Data Source (ODBC) or Database Name
    2. DatabaseUser <database login username> - Database User Name
    3. DatabasePassword <text-value> - Database User Password
    4. DatabaseHost <text-value> - Database Server Hostname (MySQL or MiniSQL only)
    5. DatabasePort <value> - Database Server Port or Config File (MySQL or MiniSQL only)

Note: Fields 4) and 5) apply to MySQL and MiniSQL databases only, and are not required when using ODBC

    1. DatabaseName wsddb
    2. DatabaseUser wsduser
    3. DatabasePassword wsdpassword
    4. DatabaseHost wsdserver

The second set of commands specifies the SQL queries used by WSD to update the database:

  1. QueryInsert <SQL INSERT query>
    - WSD will use this query when publishing content ADD and COPY requests

  2. QueryUpdate <SQLUPDATE query>
    - WSD will use this query when publishing content MODIFY and MOVE requests

  3. QueryRename <SQL UPDATE query>
    - WSD will use this query when publishing content RENAME requests

  4. QueryDelete <SQL DELETE query>
    - WSD will use this query when publishing content DELETE requests

Syntax Note: The System Administration Tutorial links you to the tutorial on "Page Layout Templates" (listed under Template Processing). Please read this tutorial carefully. Also, review the "Page Layout Templates" User Guide, available in PDF format.

    1. QueryInsert INSERT into articles values('$NewsReleaseDate$','$Icon$','$Headline$','$Story$','<$prop:authorname$>',' <$prop:authoremail$>')
    2. QueryRename UPDATE articles set releasedate =’$NewsReleaseDate$’ where releasedate=‘<$prop:dbkey$>-<$prop:NewsReleaseDate$>’ (See Note 1 below.)
    3. QueryUpdateUPDATEarticlesseticon='$Icon$',headline='$Headline$',story='$Story$',authorname='<$prop:authorname$>',
      authoremail='<$prop:authoremail$>' where releasedate='$NewsReleaseDate$'
    4. QueryDelete DELETE from articles where releasedate='$NewsReleaseDate$'

Note 1: WSD will automatically create temporary properties containing existing values for all "dbkey" fieldname variables when creating RENAME requests. These values can be used to refer to the original field values during the update process.

Note 2: Each SQL Query must be on a single line.

Multiple queries of each type (Insert, Update, or Delete) can be specified when multiple tables need to be updated from the same Page Layout Template. The query definitions can contain three kinds of replacement variables:

  1. WSD properties - Specified using the normal template tag format for WSD properties ("<$prop:property$>"). For each occurrence of one of these variables, WSD retrieves the specified property from the meta data record associated with the WSD request.
  2. WSD template field value - For each occurrence, WSD retrieves the value the user enters into the specified template field when the Page Layout Template is used to create, modify, or delete database information. This type of template field is referenced in the format "<$field:field-name$>". The template field name is case-sensitive and may not contain spaces. Any type of WSD template field can be used in database queries.
  3. WSD template field reference - Specified using the normal template tag format for WSD references ("<$reference $prop:reference-prop$:$prop:source-prop$>" or "<$reference {/path/to/file.html}:$field:source-field$>"). For each occurrence of a reference tag, WSD retrieves the specified property for template field value from the document specified directly or indirectly in the first part of the reference tag.

Multiple databases can be updated from a single template by specifying multiple sets of database commands and SQL queries. All database and SQL query commands are processed sequentially in the template, so any later database commands will override previous commands (and the previous database will be closed and the new one opened). If the values of one or more database commands remain the same for multiple databases, they do not need to be repeated. For example:

After issuing the SQL query commands to the database, WSD checks the results of the query to determine how many records were updated in the database. Unless told otherwise, WSD assumes that a single record will be updated for each SQL query command. If no records are updated (if the "where" clause in an UPDATE or DELETE query did not match any existing records, for example) or more than one record is updated, WSD will display an error to the user and the publishing operation will be cancelled.

If you know that a query will update more than one record, you can change the expected results by using the "QueryResults" command, in the format "QueryResults <number>". For example, if you know that an SQL query will update two records, put the command "QueryResults 2" before that query. The results of all queries following a "QueryResults" command will be checked for the specified number of updated records, until another "QueryResults" command changes that number. If a query will result in a variable number of updated records, you can turn off the expected results checking using "QueryResults *".

In addition to placing Database and Query commands directly in the Page Layout Template, you can also include such commands from a file or dynamic script or program using the "QueryHref" command, in the format "QueryHref <URL>", where <URL> is a local or remote URL. The QueryHref URL is in the same format as the URL used in the standard <$href ...> template tag and can contain either a hard-coded URL or a URL computed from the values of properties or template fields in the document being published.

The output from the page or script specified by the URL is processed for Database and Query commands and other template tags, just as if it were specified directly in the template itself. Some examples:

You can also use <$if ...> and <$setproperty ...> template tags with the Database and Query commands to selectively control which Database and Query commands are processed based on the value of properties and fields in the document. See the Page Layout Template tutorial for details on how to use these template tags.

4.Using Page Layout Templates to Update a Database

WSD users submit the same types of requests (Add, Modify, etc.) to update database content that they do for static file content. The difference is that the database content in the file system is associated with a Page Layout Template containing database update commands, as described above.

To ADD a new database record, a WSD user submits an Add/Upload request to create a new document in the special database portion of the web site file system. The referenced document can be a web page, text document, or XML file.

The selected Page Layout Template contains references to the database and all SQL queries used to update that database. It is the WSD user’s responsibility to ensure that the database record key is consistent with the record indicated by the file name, based on the naming conventions defined for your company!

While database requests are within the WSD workflow (submit, edit, review, and approve), each request functions just like a normal WSD static content management request. All editing is done in the standard WSD Edit Online screen, with the database fields represented as form fields. After all approvals are completed, the request is published, triggering WSD's database update functionality.

The SQL query associated with the request type is submitted to the database in addition to publishing the regular content into the file system. See "Creating Page Layout Templates to Update a Database" above for additional information regarding these SQL queries and how/when they are used.

Database updates and data content deletions (WSD Modify, Delete, etc. commands) work in a similar fashion. WSD users browse the special database portion of the file system (web site) searching for the document corresponding to a specific database record they want to update or delete. When a user finds the appropriate record, he/she submits a standard WSD MODIFY, COPY, MOVE, RENAME, or DELETE request for that document.

When WSD publishes the document, it submits all appropriate SQL queries to update or delete the referenced data record(s) in the SQL database and manipulate the static pages in the file system. The following table describes the actions WSD will perform in manipulating the static and database content by WSD request-type.

WSD Request-Type

Static File Updating

Database SQL query

Add/Upload <new>

Creates a <new> record in the file system

Inserts a record with a <new> record key along with its associated data

Modify <existing>

Updates an <existing> record in the file system

Updates existing record data for <existing> key value

Copy to <new> location

Creates a new record at a <new> location in the file system

Inserts a new record using a <new> record key with existing data from the old record

Move from <old> to <new> location using <existing> filename

Moves an existing record to a new location in the file system without changing its filename

Updates existing record data for an <existing> key value

Rename <old> to <new>

Renames an existing record in the file system

Replaces existing <old> record key with a <new> key value

Delete <existing>

Deletes an <existing> record from the file system

Deletes an <existing> record from the database

5.Publishing into a Database

All database updates are attempted/completed before the document is published to the special database portion of the web site. If an error occurs during the database update, the database error is displayed to the user and publishing is terminated. When errors occur, the request remains in the approval stage from which the publishing approval request was issued. After the database error has been resolved, the WSD user should again attempt to approve the previously failing request for publishing.

Copyright 2000-2005 CyberTeams, Inc., http://www.cyberteams.com All rights reserved.
CyberTeams and WebSite Director are registered trademarks of CyberTeams, Inc. All other marks are the property of their respective owners.