Saturday, January 17, 2009

Coldfusion 8 introduce great feed reader CFfeed

Description

Reads or creates an RSS or Atom syndication feed. This tag can read RSS versions 0.90, 0.91, 0.92, 0.93, 0.94, 1.0, and 2.0, and Atom 0.3 or 1.0. It can create RSS 2.0 or Atom 1.0 feeds.

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

Attributes

Attribute

Req/Opt

Default

Description

action

Optional

read

The action to take, one of the following values:

  • create: creates an RSS 2.0 or Atom 1.0 feed XML document and saves it in a variable, writes it to a file, or both.
  • read: parses an RSS or Atom feed from a URL or an XML file and saves it in a structure or query. You can also get feed metadata in a separate structure.

columnMap

Optional

Used only for the create action with a query attribute.

A structure that specifies a mapping between the names of the columns in the object specified by the query attribute and the columns of the ColdFusion feed format (see the Query object rules section.

The key for each field must be a column name (see the table in the Query object rules section). The value of the field must be the name of the corresponding column in the query object used as input to the create action.

enclosureDir

Optional

Used only for the read action.

Path to the directory in which to save any enclosures that are available in the feed being read. The path can be absolute or relative to the CFML file.

If the directory does not exist, ColdFusion generates an error. If you omit this attribute, ColdFusion does not save enclosures. To specify the directory that contains the current page, set this attribute to "." (period).

ignoreEnclosureError

Optional

no

If this attribute is yes, ColdFusion attempts to save all enclosures. If it encounters an error downloading one enclosure, it continues downloading other enclosures and writes the error information in the server log.

If this attribute is no, when ColdFusion encounters an error downloading an enclosure, it stops downloading all enclosures and generates an error.

Note: Enclosure errors can occur if the specified enclosure is of a type that the web server does not allow to be downloaded.

name

See Note

A structure that contains complete feed data:

  • The output of a read action.
  • The input definition of the feed to create.

When you specify the name attribute for a create action, you must enclose it in number signs (#).

For more information, see Name and properties structure rules.

outputFile

See Note

Path of the file in which to write the feed as XML text.

The path can be absolute, or relative to the CFML file.

overwrite

Optional

no

Whether to overwrite the XML feed file if it already exists. If you do not set this attribute to yes and the cffeed tag tries to write to a file that exists, ColdFusion generates an error.

overwriteEnclosure

Optional

no

Used only for the read action.

Whether to overwrite files in the enclosure directory if they already exist. If you do not set this attribute to yes and the cffeed tag tries to write to a file that exists, ColdFusion generates an error.

properties

See Note

A structure that contains the feed metadata, the information about the entire feed. Can contain either of the following:

  • The output of a read action.
  • Input to a create action.

The properties and query attributes combined provide complete feed information.

When you specify the properties attribute for a create action, you must enclose it in number signs (#).

For more information, see Name and properties structure rules.

proxyPassword

Optional

Password required by the proxy server.

proxyPort

Optional

80

The port to connect to on the proxy server.

proxyServer

Optional

Host name or IP address of a proxy server to which to send the request.

proxyUser

Optional

User name to provide to the proxy server.

query

See Note

A query object that contains the Atom entries or RSS items in the feed. Can contain either of the following:

  • The output of a read action.
  • Input to a create action.

The properties and query attributes combined provide complete feed information.

When you specify the query attribute for a create action, you must enclose it in number signs (#).

For more information, see Query object rules.

source

Required

Used only for the read action.

The URL of the feed or the path to the XML file that contains the feed contents. A path can be absolute, or relative to the CFML file.

timeout

Optional

Request time-out

The number of seconds to wait for a response from the feed source. A value of 0 specifies that the request does not time out.

By default, ColdFusion uses the request time-out setting of the ColdFusion Administrator Server Settings {Closetag} Settings page.

userAgent

Optional

Cold
Fusion

Text to put in the HTTP User-Agent request header field. Used to identify the request client software.

xmlVar

See Note

A variable in which to save the read or created feed as XML text.

Usage

Setting and getting feed information

The cffeed tag lets you specify and save feed data in many, flexible ways.

When you create a feed

  • You specify the feed data in either of the following ways:
    • By putting all metadata and entry or item data in a single structure specified by the name attribute.
    • By putting the metadata in a structure specified by the properties structure and the entries or items as rows in a query object specified by the query attribute.
  • You save the resulting feed XML in one or both of the following places:
    • A file specified by the OutputFile attribute. The cffeed tag saves the data in UTF-8 encoding.
    • An variable specified by the xmlVar attribute

When you read a feed

You can save the feed data in any combination of the following forms:

  • By saving all entry or item data and metadata in a single structure specified by the name attribute
  • By saving entries or items as rows in a query object specified by the query attribute
  • By saving the metadata in a structure specified by the properties structure
  • By writing the feed XML in a file specified by the by the OutputFile attribute. The cffeed tag saves the data in UTF-8 encoding.
  • By saving the feed XML in a ColdFusion XML variable specified by the xmlVar attribute

When you save feed data, you do not have to save both the metadata and the entry or item data. You can specify only the properties attribute, or only the query attribute.

Name and properties structure rules

The name and properties structures must conform to the following rules. For more information on requirements for specific metadata entries, see Representing feed metadata.:

  • All structure key names must be identical to the corresponding feed element names, with the exception of the version and encoding fields. Also, the key names for Dublin Core and Apple® iTunes extension elements start with DC_ and ITUNES_ respectively.
  • The properties structure fields are identical to the metadata fields in the name structure.
  • When you read a feed, the structure contains only those elements and attribute values that exist in the feed. For requirements for the create action, see Creating feeds.
  • If the feed can have multiple elements of the same type (such as entry, item, or link), the name or property structure has a single entry that contains the data for all of the elements. The structure entry has the following format:
    • The key is the element name (for example, item)
    • The value is an array of structures
    • Each structure in the array represents one element.

ColdFusion uses an array even if there is only a single element. If an Atom feed has only one link element, for example, you must specify that element in a name attribute structure by using the following format:

structureName.link[1]

Query object rules

The query object specified by the query attribute conforms to the following rules:

  • The query object format supports multiple feed formats, and many feeds do not include all optional feed attributes or elements. As a result:
    • When you read a feed, the returned query object contains entries for all standard RSS and Atom fields, even for fields that are not supported by the feed type. Any columns that are not used by the feed format, or are not used in that specific feed, contain empty strings or undefined values.
    • When you read a feed, the query object contains all iTunes extension fields if the feed contains any iTunes extension elements, and the query object contains all Dublin Core extension fields if the feed contains any Dublin Core extension elements. Otherwise, the query results do not contain any of the extension fields.
    • When you create a feed, the query that you define requires only those columns that contain data for your feed; you can omit unused columns.
  • If a feed entry or item has multiple child elements with the same name, the query column represents the element values as a comma-delimited list. RSS 2.0 items can have multiple category elements. Atom 1.0 entries can have multiple category, author, contributor, and link elements. The Dublin Core extensions allow all multiples of all element types.
  • Many entry or item elements that can have multiple instances have multiple attributes, not all of which are required for any particular element instance. If an entry or item has multiple instances of an element, and any of those elements omit attributes, ColdFusion represents the omitted attribute in the lists by a space. In XML, an Atom entry, for example, might contain three author elements, as follows:

Representing feed metadata

When you create a feed, the name and properties structures can represent all standard metadata for RSS 2 or Atom 1 feeds, in the format described in the Name and properties structure rules section. Similarly, when you read a feed, the structures represent all received metadata. The following rules apply to specific feed metadata fields in the name and properties structures:

  • The version field identifies or specifies the feed version in the form format_versionNumber. For the create action, you must specify atom_1.0 or rss_2.0. When you read an RSS 0.91 feed, the version field value is rss_0.91U, not rss_0.91.
  • The feedExtension field identifies whether the feed includes iTunes or Dublin Core extension content. Valid values are itunes and DublinCore. You do not have to specify this field when you create a feed with iTunes extensions; ColdFusion automatically determines that you have specified extension fields. (You cannot create a feed with Dublin Core extensions.)
  • For the read action, an encoding field identifies the XML encoding attribute, such as iso-8859-1. Do not specify an encoding field for a create action. Currently, ColdFusion generates all feeds in UTF-8 format and ignores any encoding value that you specify.
  • For RSS feeds, the skiphours field contains a comma-delimited list of up to 24 numbers in the range 0-23, specifying hours of the day when aggregators should not read the feed. The hour beginning at midnight is hour zero. Your application can use the field to decide when to read the feed.
  • For RSS feeds, the skipdays field contains a comma-delimited list of up to seven day-name values, specifying days of the week when aggregators should not read the feed. The valid names are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. Your application can use the field to decide when to read the feed.

Dublin Core Extensions

Dublin Core extension elements provide additional metadata about the feed or an item. You can use the cffeed tag to read feeds that include elements that conform to the Dublin Core Metadata Element Set specification as metadata (channel elements) or as item elements. For detailed information Dublin Core extension elements, see the Dublin Core Metadata Element Set specification. At the time this topic was written, this specification was available at http://dublincore.org/documents/dces/.

ColdFusion support for Dublin Core extensions has the following limitations:

  • You cannot create feeds containing these elements.
  • You cannot get Dublin Core extension elements that are contained in a top-level (metadata) image element. ColdFusion ignores these elements.
  • ColdFusion supports only the Dublin Core Metadata Element Set. It does not support the additional Dublin Core Metadata Initiative elements and element refinements.

When feed items include the Dublin Core extensions, the query specified by a query attribute includes all of the columns listed in the following table. If the feed does not include any Dublin Core extension elements, the query does not include the columns. With the exception of the DC_SUBJECT_TAXONOMURI and DC_SUBJECT_VALUE columns, each column name (without the DC_ prefix) corresponds directly to a Dublin Core extension element name.

Column

Description

DC_CONTRIBUTOR

The people or organizations responsible for contributing to the resource

DC_COVERAGE

The extent of the content in the resource

DC_CREATOR

The person or organization responsible for creating this resource

DC_DATE

A date or date and time associated with this resource

DC_DESCRIPTION

A summary of the resource contents

DC_FORMAT

The file format, physical medium, or dimensions of the resource

DC_IDENTIFIER

A string that can be used to unambiguously identify the resource

DC_LANGUAGE

The language in which the resource is written

DC_PUBLISHER

The person or organization responsible for making the resource available.

DC_RELATION

The identifier of a related resource, typically.

DC_RIGHT

Information about the property rights for the resource.

DC_SOURCE

A reference to the material from which this resource was derived.

DC_SUBJECT_TAXONOMYURI

The taxonomy URI attribute of the Dublin Core subject element

DC_SUBJECT_VALUE

The value of the Dublin Core subject element; a string that the topic of the resource

DC_TITLE

A name to use for the resource

DC_TYPE

The nature or genre of the resource

When you get data for a feed that includes Dublin Core elements as a structure, the element names are identical to the query column names listed above, with the exception of the representation of the Dublin Core subject element. The structure format represents the subject element as a dc_subject entry, which consists of an array of structures. The structures in the array have keys with the names value, for the element value, and taxononmyURI, for the taxonomyURI attribute.

Apple iTunes Extensions

You can use the cffeed tag to create or read feeds that contain elements defined in the Apple iTunes RSS podcast specification. For detailed information on iTunes extension format, see the Apple iTunes RSS specification. At the time this topic was written, this specification was available at http://www.apple.com/itunes/store/podcaststechspecs.html.

You can create feeds with only a subset of the iTunes RSS extensions. When you read a feed, ColdFusion ignores all iTunes extension elements that are not in the supported subset.

The following table lists the names of structure entries or query column names for the supported elements. (These names consist of the ITUNES_ prefix followed by the iTunes extension element name.) The table also indicates which elements are used in the metadata, which are used in the individual items, and which can be used in both:

Element

Used in

Description

ITUNES_AUTHOR

Both

Artist name

ITUNES_BLOCK

Both

a value of yes requests to prevent the podcast or item (episode) from appearing.

When ColdFusion reads a feed your application should determine this field's value and take any appropriate action.

ITUNES_DURATION

Item

The length of the item in second, or in HH:MM:SS format.

ITUNES_EXPLICIT

Both

A string indicating whether the item or items contain explicit material. Valid values are yes, no, and clean.

ITUNES_KEYWORDS

Both

A comma-delimited list of words or phrases used when searching in the iTunes music store.

ITUNES_SUBTITLE

Both

Short description text, usually only a few words.

ITUNES_SUMMARY

Both

A longer description (up to 4000 characters)/

You can also use the following channel elements in the name or properties structures.

Element

Description

itunes_category

A structure that specifies the iTunes Music Store category. The structure has two fields:

  • category
  • subcategory

Notice that these element names do not have the itunes_ prefix.

itunes_image

The URL of the artwork for the podcast.

itunes_owner

A structure that contains contact information about the owner of the podcast for communication. The structure has two fields:

  • itunes_email
  • itunes_mail

Creating feeds

When you create a feed, you specify the feed contents in a name structure or in the combination of a query object and a properties structure. The cffeed tag generates the feed XML and saves in to the variable specified by the xmlVar attribute, the file specified by the outputFile attribute, or both.

To create an RSS 2.0 feed you must specify the following metadata fields in a name structure or in a properties structure. All other RSS2.0 metadata fields, and all item fields, are optional.

  • title
  • link
  • description
  • version (must be "rss_2.0")

The cffeed tag does not enforce any rules on the Atom feed structure that it creates. You are responsible for ensuring that the feed is valid.

In most cases, a database table uses column names that differ from the column names you must use to create the feed. Therefore, you must use the columnmap attribute to map the input query column names to the required column names. The attribute is a structure whose keys are the column names required by the cffeed tag and whose values are the corresponding input query columns.

Note: You must always capitalize the input query column names irrespective of whether the database column names are capitalized or not.

The following example creates a feed using the cfartgallery data source's orders table. It maps the orders table ORDERDATE column to the query publisheddate column, the ADDRESS column to the content column, and so on. The sample code then displays the generated query XML to show the results.

{Opentag}!--- Get the feed data as a query from the orders table. ---{Closetag}

{Opentag}cfquery name="getOrders" datasource="cfartgallery"{Closetag}

SELECT * FROM orders

{Opentag}/cfquery{Closetag}

{Opentag}!--- Map the orders column names to the feed query column names. ---{Closetag}

{Opentag}cfset columnMapStruct = StructNew(){Closetag}

{Opentag}cfset columnMapStruct.publisheddate = "ORDERDATE"{Closetag}

{Opentag}cfset columnMapStruct.content = "ADDRESS"{Closetag}

{Opentag}cfset columnMapStruct.title = "CUSTOMERFIRSTNAME"{Closetag}

{Opentag}cfset columnMapStruct.rsslink = "ORDERID"{Closetag}

{Opentag}!--- Set the feed metadata. ---{Closetag}

{Opentag}cfset meta.title = "Art Orders"{Closetag}

{Opentag}cfset meta.link = "http://feedlink"{Closetag}

{Opentag}cfset meta.description = "Orders at the art gallery"{Closetag}

{Opentag}cfset meta.version = "rss_2.0"{Closetag}

{Opentag}!--- Create the feed. ---{Closetag}

{Opentag}cffeed action="create"

query="#getOrders#"

properties="#meta#"

columnMap="#columnMapStruct#"

xmlvar="rssXML"{Closetag}

{Opentag}cfdump var="#XMLParse(rssXML)#"{Closetag}

Reading feeds

The cffeed tag does not validate the feeds that it reads. It can read invalid or loosely formatted feeds, but ignores some or all of the invalid content. For example, if you put more than one rights element in the Atom feed (which invalidates the feed), the cffeed tag ignores the elements after the first one, and doesn't generate an error.

Dates and times in feeds that are being read must be in W3C or RFC 822 format. ColdFusion can also read iTunes extension dates in the format normally used by the iTunes music store.

Example

The following example creates an RSS feed. You must enter fields for the feed title, link, and description elements. You must also enter title, link, and description fields for one item. A second item is optional. The application saves the feed in a createRSSOutput.xml file in the feedTest subdirectory of the directory that contains the CFML page.

{Opentag}!--- Generate the feed when the user submits a filled in form. ---{Closetag}

{Opentag}cfif isDefined("Form.Submit"){Closetag}

{Opentag}cfscript{Closetag}

// Create the feed data structure and add the metadata.

myStruct = StructNew();

mystruct.link = form.link;

myStruct.title = form.title;

mystruct.description = form.description;

mystruct.pubDate = Now();

mystruct.version = "rss_2.0";

/* Add the feed items. A more sophisticated application would use dynamic variables

and support varying numbers of items. */

myStruct.item = ArrayNew(1);

myStruct.item[1] = StructNew();

myStruct.item[1].description = StructNew();

myStruct.item[1].description.value = form.item1text;

myStruct.item[1].link = form.item1link;

myStruct.item[1].pubDate = Now();

myStruct.item[1].title = form.item1title;

myStruct.item[2] = StructNew();

myStruct.item[2].description = StructNew();

myStruct.item[2].description.value = form.item2text;

myStruct.item[2].link = form.item2link;

myStruct.item[2].pubDate = Now();

myStruct.item[2].title = form.item2title;

{Opentag}/cfscript{Closetag}

{Opentag}!--- Generate the feed and save it to a file and variable. ---{Closetag}

{Opentag}cffeed action = "create"

name = "#myStruct#"

outputFile = "feedTest/createRSSOutput.xml"

overwrite = "yes"

xmlVar = "myXML"{Closetag}

{Opentag}/cfif{Closetag}

{Opentag}!--- The user input form. ---{Closetag}

{Opentag}cfform format="xml" preservedata="yes" style="width:500" height="700"{Closetag}

{Opentag}cfformitem type = "text"{Closetag} Enter The Feed Metadata{Opentag}/cfformitem{Closetag}

{Opentag}cfinput type = "text" label = "title" name = "title"

style = "width:435" required = "yes"{Closetag} {Opentag}br /{Closetag}

{Opentag}cfinput type = "text" label = "link" name = "link"

style = "width:435" required = "yes" validate = "url"{Closetag} {Opentag}br /{Closetag}

{Opentag}cftextarea name = "description"

style = "width:435; height:70" required = "yes" /{Closetag}

{Opentag}cfformitem type = "text"{Closetag} Enter Item 1{Opentag}/cfformitem{Closetag}

{Opentag}cfinput type="text" label="title" name="item1title"

style="width:435" required="yes"{Closetag} {Opentag}br /{Closetag}

{Opentag}cfinput type="text" label="link" name="item1link"

style="width:435" required="yes" validate="url"{Closetag} {Opentag}br /{Closetag}

{Opentag}cftextarea name = "item1text"

style = "width:435; height:70" required = "yes" /{Closetag} {Opentag}br /{Closetag}

{Opentag}cfformitem type = "text"{Closetag} Enter Item 2{Opentag}/cfformitem{Closetag}

{Opentag}cfinput type = "text" label = "title" name = "item2title" style = "width:435"{Closetag} {Opentag}br /{Closetag}

{Opentag}cfinput type = "text" label = "link" name = "item2link" style = "width:435"

validate = "url"{Closetag} {Opentag}br /{Closetag}

{Opentag}cftextarea name = "item2text" style = "width:435; height:70" /{Closetag} {Opentag}br /{Closetag}

{Opentag}cfinput type = "Submit" name = "submit" value = "Submit" {Closetag}

{Opentag}/cfform{Closetag}

The following application is a simple feed reader that handles RSS and Atom feeds. It displays the feed title; for each item or entry, it shows the title as a link, and shows the published date and the item or entry contents. To use this example to read the feed created by the first application, enter the URL for the file the application created, for example, http://localhost:8500/cffeed/feedTest/createRSSOutput.xml.

{Opentag}!--- Process the feed data if the user submitted the form ---{Closetag}

{Opentag}cfif isDefined("Form.Submit"){Closetag}

{Opentag}cffeed source = "#theURL#"

properties = "myProps"

query = "myQuery"{Closetag}

{Opentag}!--- Display the feed output.

Use conditional logic for to handle different feed formats. ---{Closetag}

{Opentag}cfoutput{Closetag}

{Opentag}h2{Closetag}#myProps.title#{Opentag}/h2{Closetag}

{Opentag}/cfoutput{Closetag}

{Opentag}cfoutput query = "myQuery"{Closetag}

{Opentag}cfif myProps.version IS "atom_1.0"{Closetag}

{Opentag}h3{Closetag}{Opentag}a href = "#linkhref#"{Closetag}#title#{Opentag}/a{Closetag}{Opentag}/h3{Closetag}

{Opentag}p{Closetag}{Opentag}b{Closetag}Published:{Opentag}/b{Closetag} #DateFormat(publisheddate)#{Opentag}/p{Closetag}

{Opentag}cfelse{Closetag}

{Opentag}h3{Closetag}{Opentag}a href = "#rsslink#"{Closetag}#title#{Opentag}/a{Closetag}{Opentag}/h3{Closetag}

{Opentag}p{Closetag}{Opentag}b{Closetag}Published:{Opentag}/b{Closetag} #publisheddate#{Opentag}/p{Closetag}

{Opentag}/cfif{Closetag}

{Opentag}p{Closetag}#content#{Opentag}/p{Closetag}

{Opentag}/cfoutput{Closetag}

{Opentag}/cfif{Closetag}

{Opentag}!--- The form for specifying the feed URL or file ---{Closetag}

{Opentag}cfform name = "SetFeed" preserveData = "yes"{Closetag}

Enter Feed URL:

{Opentag}cfinput type = "text" size = "60" name = "theURL"{Closetag}{Opentag}br{Closetag}{Opentag}br{Closetag}

{Opentag}cfinput type = "Submit" name = "submit" value = "Submit"{Closetag}

{Opentag}/cfform{Closetag}