chapter 8homepage.smc.edu/seno_vicky/cs85/unit8/unit8.pdf · · 2015-11-10... inserts text into...
TRANSCRIPT
Chapter 8XML AND CONTENT MANAGEMENT SYSTEMS
Chapter Objective
Build a basic CMS system using only one PHP program
Examine XML as a data storage scheme
Implement the simpleXML Application Programming Interface (API) for working with XML
Create a more sophisticated CMS using XML
Understanding CMS: Content Management Systems
The nature of the Web has changed◦ Originally a repository of interconnected documents now a series of interconnected applications
◦ Much of the Web content is generated dynamically by server-side scripts, often written in PHP
◦ CMS is a popular solution for creating dynamic Web sites
◦ Many popular CMSs are written in PHP (WordPress, Drupal, Moodle)
Common features of a CMS:◦ User Management - user logs in; multiple levels of access/permissions
◦ Separation of content into semantic blocks - news stories, links, etc.
◦ Isolation of layout from content - for uniform appearance of site and so content developers don't need to know HTML
◦ User-contributed content - access is controlled through grants; content management based on user preferences
Working with WordPress/Drupal WordPress and Drupal are one of the most popular open-source CMSs They both use PHP and MySQL to dynamically generate a Web portal The functionality is extremely capable Can choose themes - change site's overall appearance Can create your own themes, add new modules, easily modify your site
Installing WordPress/DrupalNote: You are NOT required to install WordPress or Drupal for this course. If you choose to install it on your system, you will need a fair amount of LAMP savvy and will be pretty much on your own.
Goto to http://www.wordpress.org for the latest version
Written entirely in PHP and MySQL
Whenever installing software, it is strongly recommended that you read and carefully follow all installation instructions.
From the PHP-Nuke Install.txt file:
In order to setup PHP-Nuke the following prerequisits are necessary:
- A Linux or Windows Box installed and working properly.
- Apache Web Server (http://www.apache.org)
- PHP version 4.2.x or better (mod_php) Apache module (http://www.php.net)
- MySQL database server (http://www.mysql.com)
May I suggest installing the ever-so-easy-to-install XAMPP Server.
Customizing WordPress/DrupalThere are a large number of themes and a huge number of options you can modify the PHP and HTML files
Regardless of the particular CMS, the general concepts are the same. A CMS system is nothingmore than a data structure of some sort controlled by a series of PHP programs. All the content is stored in a database or other format, and the job of the PHP programs is to generate custom pages on the fly from the data sources.
Introducing simpleCMSWordPress/Drupal might be a overkill for many sites, intimidating for new programmers.
The textbook introduce simpleCMS which is a extremely basic Content Management System developed by the author of the textbook.
Viewing Pages from a User's PerspectivesimpleCMS (example CMS developed by Harris) is a lightweight CMS that provides core features but is easier to use and modify
To understand it, you need to rethink what a Web page is three primary segments1. standard banner across the top of the page
2. list of links acts like a menu along the left side
3. main section contains dynamic content (changes frequently)
HTML code for the page comes from three HTML pages, one CSS file, and one PHP script why not just use frames?o can be hard to build a frame-based site that behaves well
Examining the PHP CodeSample Code: simpleCMS.php
Code expects two parameters: menu and content◦ The value of each is a URL. They specify the menu and content HTML files that are
to be displayed.
Viewing Pages from a User’s PerspectiveThis page has a couple of interesting features with three primary segments of the page.
◦ A standard banner goes across the top of the page. This banner remains the same even when other parts of the page change.
◦ A list of links, which acts as a menu, occupies the left side. You can use multiple menus to support a complex web hierarchy.
◦ The page’s main section contains dynamic content. This part of the page will change frequently. When it changes, however, the other parts of the page will not.
◦ The HTML code for the page is combined from three different HTML pages and one CSS style. One (surprisingly simple) PHP script controls all the action.
Viewing the CSSSeparation of content and layout is critical in a CMS Harris uses CSS as primary layout management tool defines style classes for the menu and contento span.menuPanel - on the left, 15% of browser width
o span.item - to the right, 80% of browser width
Examining the PHP Code
The simpleCMS.php program puts these three elements together according to a specific style sheet.All the PHP program does is retrieve values for menu and itemand use them to generate the page on the fly.
Examining the PHP Codeinclude statement
◦ Include: Inserts text into the simpleCMS code at this point in the PHP document
◦ The PHP interpreter goes into HTML mode when it starts processing the inserted/included file
◦ PHP code in the included file must be inside a valid php tags (<?php...?>)
◦ Included file can be a url. For example, include 'http://...
◦ if included file is a url, the allow_url_include setting must be On in php.ini
◦ this setting is Off by default, and it is Off on ciswebs. An Off setting is probably best on ciswebs for security reasons. (Of course, you can turn this setting On in the php.inifile for your own system.)
◦ therefore, the Harris examples that use urls in the include will not work on ciswebs
◦ some of the examples have been modified to include local files (not urls) so as to work on ciswebs
Inspecting the Menu SystemNote that the links are never directly linked to another URL, but are always to the simpleCMS.php script in the query string (after the question mark) there may be menu and/or content parameters the script uses these parameters to determine the menu and content to display this way the CMS can control the layout and keep it consistent, which the user will appreciate
Looking at the HeaderThe simpleCMS program always calls in a page called top.html. This page contains all the XHTML code necessary for starting the standards-compliant page.
Looking at Content BlocksEach of the content blocks is a small snippet of XHTML code without a header.
Since this code is meant to be embedded into another page, it doesn’t need a header or CSS.
Improving the CMS with XMLThe simpleCMS approach which uses GET parameters which allows a limited amount of data and a tedious URL when there are more than a couple of parameters.
Many CMSs like WordPress and Drupal use a relational database to store information about page values, but this can be somewhat involved and intimidating.
XML offers another solution which is in between the aforementioned two approaches.
Introducing XMLeXtensible Markup Language, or XML:a flexible and sensible way to manipulate data.
XML uses many conventions that are familiar to HTML developers, including nested and closing tags and attributes. The most significant difference is the tags themselves.HTML tags are specifically about web page markup, but XML tags can describe anything. As long as you have (or can write) a program to interpret the XML code, you can use HTML-like code to describe the information. XML, like XHTML allows you to encode data in two different ways. In the cat example, I stored a number of small nodes inside the major (cat) node. For the dog, I stored data as a series of attributes, each with a name – value pair.
Working with XMLAdvantages
XML files can be stored and manipulated as string data and ordinary text files. This makes it easy to duplicate data and move it around the Internet.
XML data is somewhat self-documenting. You can look at XML data in a text editor and have a good idea what it means. This would be impossible if the data were stored in a database table or proprietary format.
Most languages have features that allow you to easily extract data from an XML document even if you don’t know exactly how the document is formatted.
Many data packages allow you to export a database as XML data.
Understanding XML RulesXML is very similar to HTML, but it is not quite as forgiving on syntax. Very strict syntax rules:
◦ XML is case sensitive. Most tags use lowercase or camelCase (just like PHP). <pet> and <PET> are two different tags.
◦ All attributes must be encased in quotation marks. In HTML, quotation marks are always optional. In XML, almost all attribute values should be quoted. For example, <dog name = muchacha> is not legal in XML. The appropriate expression is <dog name = “muchacha”>.
◦ All tags require an ending tag. HTML is pretty forgiving about whether you include ending tags. XML is much stricter. Every tag must have an ending tag or indicate with a trailing slash that it doesn’t have an end. In the earlier example, <cat> has an ending </cat> tag. I defined dog to encase all its data in attributes rather than subtags, so it doesn’t have an explicit ending tag. Notice how the dog tag ends with a slash (/>) to indicate it has no end tag.
Examining main.xmlThe second example CMS system in this chapter uses XML files to store page information.
The entire document is stored in a <cpage></cpage> element. cpage represents a CMS page.
Inside the page are five parameters. Each page has a title as well as URLs to a CSS style, top page, menu page, and content page.
The XML succinctly describes all the data necessary to build a page in my CMS.
Simplifying the Menu PagesFile: menuX.html
Each link in the menu calls the XCMS.php script, passing to it a single parameter, the name of the XML file that describes the desired page
(main.xml, classes.xml, etc.)
Introducing XML ParsersA program that reads and interprets XML data is usually called an XML parser.
The textbook uses the simpleXML API (Application Programming Interface)oan XML parser which comes standard with PHP 5oa very easy way to get started with XML programming
Working with Simple XMLEasy to learn. A simple model of the data in an XML documentThe XML document is viewed as a tree. The root node has children which also may have children.
Table 8.1 illustrates the main methods of the simplexml_element object.
Working with the simpleXML API
File: XMLDemo.php
Creating a simpleXML Object
simplexml_load_file() function loads an XML document, creates a simpleXML object, and returns a reference to the object which the script stores in the $xml variable
Viewing the XML Code
asXML() method returns a string representation of the target object
htmlentities() function converts special characters to their HTML entity representation
Working with the simpleXML API cont…Accessing XML Directly: If you know the names of various tags
$xml->title returns the contents of the title element the data type of the return value is object (not string) can cast a value to a given type with the cast operatorfor example, (string)$xml->title casts the value to a string Page 318
Using a foreach Loop on a Nodechildren() method returns an associative array of the object's children where the key is the tagname and the value is the contents of the elementfor example, for the first child of $xml the key is "title" and the value is "Andy's main Page"
Returning to XCMSWith all this XML knowledge, you’re ready to refit the simple CMS introduced earlier in this chapter with an XML structure. The author basically created XCMS to allow more parameters for each page.
The previous simpleCMS example allowed only two parameters - menu and content.
Now the code will allow for more parameters and a more flexible and powerful CMS, the author use an XML document with an element for each parameter.
In the XML document there are elements for <title>, <css>, <top>, <menu>, and <content>
Extracting Data from the XML File
The first step is determining if an XML file has been sent through the $theXML parameter. If not, a default value of main.xml is defined. This, of course, presumes that a copy of main.xml is available and properly formatted. If the program is called with some other XML file as its parameter, that file is interpreted instead.
Then it attempts to open the XML file. If the simplexml_load_filecommand is unsuccessful, it returns the value FALSE. The program reports this failure if it occurs. If it does not fail, the program creates a page based on the parameters indicated in this file. I expect a page with five parameters (top, css, title, menu, and content), but I could easily modify the program to accept as many parameters as you want. I ignored the title parameter in this particular program version because I have the page title already stored in top.html.
SummaryWe have covered popular Content Management Systems (CMS), building a basic CMS, XML as a data storage scheme, the simpleXML Application Programming Interface (API), building a CMS using XML