The creation of an automated article listing on the central column of the OpenSourceToday's home page would have begun by running the called php source specified in the action attribute of the input form. The php scripts would have begun by creating the text of the article summary that contained the publication date, the linked title, the author's name and the capsule summary. The entire string described would be enclosed within a div tag pair when written into a new text file. The next task would have been creating a backup copy of the original listing, then appending its content into the new file that contained only the newest article summary. Finally this completed new listing would be copied over the original. That is the process I describe here, in detail.
Once these tasks above has been completed, every call to the home page (of OpenSourceToday) would have shown the new article summary listing. Thus, we have would have completed one more step towards describing how the OpenSourceToday site could have been automated. Note, however, this step too is out of order, it would have been the penultimate if not last, certainly not the second in precedence. Nonetheless, I believe easier to understand is the better route.
To have created an automated, detailed article listing on the OpenSourceToday home page, I would have needed some preconditions to hold. For example, I would have begun with the assumption the content deposited in the html input form [1.] had been verified by the user. For simplicity, I would have assumed too that the data was stored in readily accessible locations. Since this is both a thought experiment and the issues are still unresolved, I will assume the inputs are stored in text files having easily identifiable content and names. Remember that is tentative, I may well decide that I prefer other means. I will produce detailed code, however, to limit the bulk and to focus on the most pressing issues, error catching code should be assumed to be present even though not explicitly shown.
Before looking at the necessary alterations, let's view in turn the targets (left and central columns of the OpenSourceToday home page), the older content and finally the necessary alterations. Here is the graphic reminder of the first in a closeup of portions of these two columns:
Figure 1. Open Source Today's Home Page Left and Central Columns
Use this link to view the entire page, however, the main point of interest is not visible in either. Therefore, before expending too much effort let's examine the skeletal listing of this page's structure as our starting point:
<html>
<head>
... <!-- includes title
selection of meta tags, date, author, language,
robots, stylesheet, etc. -->
</head>
<body>
<div identity (page header)>
...
</div>
<div identity (left col)>would use
(Article Link Listing)
</div> End Left Col.
<div identity (central col)>
(Article Summaries List)
</div> End Central Col.
<div identity (right col)>
...
</div> End Right col)>
... Footer section, that we skip
</body>
</html>
Listing 1. Bare Bones Home Page
HTML Structure
The old version had hard coded entries for the left hand column is shown in Listing 2 of the link. The revised (altered) page would have taken that listing and had it read at run time from a text file:
<?php
/* with error trapping code */
$left_link = $text . "/left_col_links.txt";
readfile($left_link);
?>
If you are interested, the method used to add new content was shown in Listing 4. in the link just above this code sample. However, here the detailed listings are more cumbersome. In reference to the central column content, the partial hard coded page (note the leading h tags and headings prior to the individual article div tags, that I would have left in place) is shown to contains more text than the title:
</div> <!-- Left hand column end -->
<div id="central-col">
<h2 id="central-text">Recent Content Listings</h2>
<h2 id="prod-news"><a href="http://[path]/news.html"> \
Go Directly to</a>: News</h2>
<h3 id="central-grouping">Open Source and the Channel</h3>
<div class="bands">
<p><b>October 30, 2007 - \
<a href="http://[path]/embedded_linux_devices.html"> \
Why Embedded Linux Devices?</a> (Tom Adelstein)</b>. \
Digital convergence is here. We're right in the middle of it; \
we're pushed by the Digital Phone revolution, the move from \
analog music devices to iPOds and MP3 players. So, how will you \
take advantage of the opportunities available as the world turns \
digital?</p>
</div> <!-- End of Internal Band -->
<div class="bands">
<p><b>October 16, 2007 - \
<a href="http://[path]/open_source_domain.html"> \
Open Source: No Longer the Provence of a Few</a> \
(Tom Adelstein)</b>. As recently as five years \
ago, Linux and FreeBSD developers stood alone starring \
at the world of Open Source. In the adoption model, they \
were the innovators. Early adopters stood on the sidelines \
waiting to see if Open Source would work and/or take off. \
Jumping into the fray seemed risky.</p>
</div> <!-- End of Internal Band -->
...
Listing 2. Central Column, Home Page
Detailed Article List
The last heading just above the individual article link and summary was the article category, there were several. If we introduce a text file containing each listing, then the home page might have more than one file with content listings. This simpler code, retains the upper section as hard coded text and adds the same read file function script code I have employed elsewhere:
</div> <!-- Left hand column end -->
<div id="central-col">
<h2 id="central-text">Recent Content Listings</h2>
<h2 id="prod-news"><a href="http://[path]/news.html"> \
Go Directly to</a>: News</h2>
<h3 id="central-grouping">Open Source and the Channel</h3>
<?php
/* test for errors, if none read file */
$floss_link = $text . "/cent_col_floss_links.txt";
readfile($floss_link);
?>
...
Listing 3. Modified Central Column, Home Page
Detailed Article List
Normally read file errors are unlikely, hence, before distress signals are sent as urgent email message [2.] to the webmaster and others, retries are advised. However, there is the possibility that the changes implemented to render dynamic content is the cause; if read failure persists, assume the home page has suffered a fatal error [3.] and notify those able to remedy the problem.
Build Article Data String
In both the introductory article for this series and the very first lines in this article, I outlined the more complex set of steps necessary. My first task would be building the article's, detailed descriptive link. There are many components that have to be gathered a strung together as a unit.
Publication Date
Look at Listing 2., above, where the publication date begins the string, the actual date publication date becomes the value of the date function. That is, the date is just formatted onto easily human readable form:
$publication_date = date(F d, Y); // where this is read out as full month // double digit day (leading zero) // and four digit year
Title
Now let's add the spacing, add the link code around the title and we are one step further in creating the new article listing:
$url_link = "http://example.com/[subdirectory]/" . &filename . "\.html"
$article_det_str = "<b>" . $publication_date . " - " . \
"<a href=" . $url_link . ">" . $title \
. "<\a>";
Author's Name
$article_det_str .= " (" . $1st_author . ").<\b>";
Article Summary
Fairly simple, add a space or two and the string as is:
$article_det_str .= " " . $summary;
and we are nearly ready. At this stage we have the complete content for the newest article listing, however, it is preceded by a code line that needs some discussion.
Build Div Tag Containing Lines
This first line can be copied directly from Listing 2. above:
$div_line = "<div class=\"bands\">";
where the "bands" class determines the appearance of the content when rendered on the web page. It specifies two border lines top and bottom, text size, padding width at the sides and justifies the text within the borders of the central column. All those characteristics are set in the cascading style sheet for that web page. The only remaining line to be defined is enclosing ending div tag line:
$div_end = "</div> <!-- End of Internal Band -->";
Now we need to write this new content to a new file,we would use open with the "w" mode [4.]. I stress this, because as it works in python, if the file exists is opened with its content cleared and ready to take only new entries whereas a file is created if none existed previously. Once this temporarily named file has been opened, the content for the newest article listing created in the preceding lines is written into the file and closed:
<?php
$temp_store = $text . "/article-list\.txt\.tmp-hold";
$filehandle = fopen($temp_store, "w");
// write in the three lines of content [5.]
$line_1 = $div_line . "\n";
fwrite($filehandle, $line_1);
$line_2 = "<p>" . $article_det_str . "<p>\n";
fwrite($filehandle, $line_2);
$line_3 = $div_end . "\n";
fclose($filehandle);
?>
Backup Original File
This creation of a backup file is critical; if it fails all efforts should cease and the process should be completed manually. This is the naked code:
<?php
// copy article listing file
$article_list = $text . "/article-list\.txt";
$art_list_bk = $text . "/article-list\.txt.\backup";
if (copy($article_list, $art_list_bk) === FALSE) {
// Call email object with arguments
// high, stop, copy failure, contact list, etc.
} else { // keep running the code ...
...
Append Original into Temporary File
It was important to have closed the temporary hold file, because the original article listing needs to be appended. That is, we need to reopen it (temporary hold file) in append mode. In the code shown below, the append operation is more complex than the adding a link in the left hand column listing:
<?php
// open temp. hold file in append mode
$temp_store = $text . "/article-list\.txt\.tmp-hold";
$filehandle = fopen($temp_store, "a");
// open article listing in read mode
$article_list = $text . "/article-list\.txt";
$filehandle2 = fopen($article_list, "r");
// read content lines of file above and
// write into holding file.
while (! feof($filehandle2)) {
$read_line = fgets($filehandle2);
fwrite($filehandle, $read_line);
}
// failures to read and write are
// very serious errors (think email)
// and kill the process.
fclose($filehandle2);
// if completed successfully
...
Copy Temporary Over Original File
This is one of the critical steps that completes the process. Immediate recovery and urgent warnings should be sent upon any indication of a failure (the code below is a continuation of the one above):
copy($temp_store, $article_list);
// if true, close $temp_store done and complete
// failure, copy backup into list
// and use urgent email action
copy($art_list_bk, $article_list);
// In either case the automated process ends
?>
If this point could be reached, refreshing the page shown in Figure 1. would have the most recent article listed at that top. Indeed, with a bit of foresight and competent error catch it might be possible to recover from an apparent error and still reach this point. Thus, another mile stone has been reached in this series with the penultimate step in automated loading of articles and creating links that would have worked on the OpenSourceToday site.
One more step in my model goal of automating of portions of the OpenSourceToday site would have been accomplished using this fairly simple php scripting code. Again the step was out of order, because I am moving up the ladder of complexity in solving the problems that would have had to be confronted. Unlike the first (adding a linked title at the end of the left hand column listing), this change would have been very visible. Therefore, all required content would have had to been in place.
The next article in this series will show how a News item could have been loaded onto the separate page. The techniques would have been similar to, if not an exact match to those employed here. It is my intention to avoid duplication by referring the reader to this article and then begin where the code differs. Both updates would have used similar steps loading the newest item on a temporary file, however, the News item due to the content the code is more complex. The next steps in the process are nearly identical. Regarding both processes, we loop through the content line by line until the end of the file is reached. We will not have the simple three lines we had building the new detailed article listing. Therefore, by citing and by the similarity of code, I expect a more compact presentation.
There is a possibility I will discuss a second technique using separate small files, if I can resolve one a key coding issue. This technique could solve some addition problems that are inherent in the News page update. My plan is to propose using a similar method employed when I discussed the security issues in password form [stopping SQL injection attacks]. Simple text files could be the quicker method for updating the News page. If my doubts are resolved, the use of simple text files has many advantages over reuse of the coding (structure) techniques shown in this article.
Corrections, suggested extension or comments write: H. Cohen.
© Herschel Cohen, All Rights Reserved
____________________________________________________________________
1. Shown many times previously, introduction and simple link.
Return
2. In these error trapping in the header or within the
rendering of menus, sending email warnings are a big issue.
Return
3. Multiple reads should not be a problem, hence, the failure
to open means the new file has been corrupted or lost.
Fast action would be required to return the site to a
functional state. Return
4. Search on this page with this string "A list of possible
modes for fopen()...", then look for the "w" under the
mode column. Return
5. To capture errors, run this code in a for type loop,
using if (FALSE === fwrite(...)) as the trip wire to
report failures via email. Warning, worse errors than
this are part of this code. Failure of fclose would be
significant. Send email warnings, with the latter the
being the most serious. Return
____________________________________________________________________