184 Chapter 5 Basic Web Application Strategies Another very useful tool is CVSweb, a Perl CGI script that gives you an overview of your CVS repositories. It helps to manage larger projects by displaying each file in a list with its last log message, date of modification, and author (see Figure 5.4).You can request any revision of a file and view nicely colored diffs between arbitrary versions. Figure 5.4 The CVSweb interface. Advanced CVS Of course there s a reason that Per Cederqvist s CVS reference manual is such a long beast.The basic commands are learned quickly even by developers who are new to CVS, but the advanced features of CVS require more attention. Tags and Branches For example, CVS uses the concept of tags and branches. CVS tags are quite easy to understand. In our earlier example, John and Jane had worked eagerly until reaching the final project stage and releasing version 1.0 of their F-API. Now, two days later, an important customer discovers a bug in the API.A quite-easy-to-solve bug,actually but our developers have already moved on with development and can t release the current source code as a stable version. If they wanted to apply the bug fix, they d have to find out which version of each file was in the release, check that revision out from CVS,and create a new release.That may be possible with the three files from our example, but they d be stuck back to their weekend toil if the project involved a considerable number of files.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
CVS: Concurrent Versions System 185 The solution CVS offers is to associate a verbose tag with a specific revision when a milestone has been reached. Before the release, Jane tags the files with the name of the release: jane@dev:/home/jane/f-api > cvs tag rel-1_0 If she needs to retrieve the release version at a later time, she can simply issue a cvs checkout command on this tag: jane@dev:/home/jane/f-api > cvs checkout -r rel-1_0 f-api And voil , there s release 1.0 again. It will be created in the ./f-api directory. Branches are a bit more complex, but you can think of them as stricter tags. Indeed, when you want to have a more rigorous separation of two code bases (which are still related to a single project),you d create two branches.To get back to our example, John and Jane want to separate a development version of their API and a stable version just as in the Linux kernel development. A new branch is created using the cvs tag -b command.To start a development branch with the current code base, Jane executes this command: jane@dev:/home/jane/f-api > cvs tag -b dev . The name of the newly created branch is dev.To create a local copy of this branch, Jane checks it out from CVS as she would normally, with a tagged version: jane@dev:/home/jane/f-api > cvs checkout -r dev f-api The difference between normal tagged files and branched files becomes apparent now: All Jane s further commands will act on this branch and not affect the main, stable branch.All commits,updates,and so on will use the dev branch, enabling her to work on the development branch without altering the main branch. But be careful: Maintaining different branches can be an administration nightmare.We suggest that you use a maximum of three branches per project. Tip: To see to which branch a file belongs, use cvs status.The Sticky Tag output will contain the branch name and revision number. A common task is merging a branch back into the main branch. From time to time, Jane wants to add the new features from the development branch to the main branch.This is done by first checking out the main branch and then applying the diffs from the development branch: jane@dev:/home/jane/ > cvs checkout f-api jane@dev:/home/jane/f-api/ > cvs update -j dev . The -j (join) option in the cvs update command tells CVS to merge the differences from the development tree to the local copy. Tip: The main tree s branch name is always HEAD. Knowing this, Jane could merge the main tree to the local development branch by executing cvs update -j HEAD. After having resolved potential conflicts, Jane can now commit her copy to the main tree, and the backport is complete.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
CVS: Concurrent Versions System 181 Obviously, she s working on a non-current version of the file and the need for a merge becomes apparent.When Jane tries to commit her version,CVS shows a warning: jane@dev:/home/jane/f-api > cvs commit config.inc.php3 cvs server: Up-to-date check failed for `config.inc.php3 cvs [server aborted]: correct above errors first! cvs commit: saving log message in /tmp/cvs07789baa This means that she needs to bring her local version up-to-date before a commit is possible again: jane@dev:/home/jane/f-api > cvs update config.inc.php3 RCS file: /usr/local/cvsroot/config.inc.php3/config.inc.php3,v retrieving revision 1.3 retrieving revision 1.5 Merging differences between 1.3 and 1.5 into config.inc.php3 rcsmerge: warning: conflicts during merge cvs server: conflicts found in config.inc.php3 C config.inc.php3 This is a case where an automatic merge is not possible. Instead, CVS creates a special version of the file in conflict, containing special markers to denote conflicting sections. Within this marker, CVS shows the local version of the section in question and the version from the remote repository: <<<<<<< config.inc.php3 define( FT_ZIP_ARCHIVE , ZIP_ARC ); // GZip Archive (not PkZip compatible!) ======= define( FT_ZIP , ZIP_ARC ); // GZip Archive >>>>>>> 1.3 The first group contains Jane s version, the second the version from the repository. It s now Jane s responsibility to go through the code and correct the conflicts.This may be a simple matter of adding the other developer s changes to the local version, or a complex issue that must be resolved with other project members in a phone call or meeting. Even in our simple example, Jane would probably have to talk with John about the reasons he shortened the constant from FT_ZIP_ARCHIVE to FT_ZIP. As soon as Jane has resolved all conflicts, she can commit the file to the repository and CVS will happily accept it: jane@dev:/home/jane/f-api > cvs commit config.inc.php3 In turn, John can now update his local version with the unified copy to complete this round of editing. Manual merges are rare, and we ve found that you can often avoid them by maintaining proper communication between team members.The few conflicts that cause headaches are nearly all due to poor communication between developers a problem no source-control system can avoid.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
182 Chapter 5 Basic Web Application Strategies Now that Jane has had to deal with conflicts, she s getting cautious and wants to check the status of the file f-api_write.php3. She issues a CVS status command: jane@dev:/home/jane/f-api > cvs status f-api_write.php3 The command shows that her local copy is the most current available.These are the status codes in CVS: Code Description Up-to-date The local copy is identical to the copy in the CVS server. Locally Modified The local copy has been modified and not yet committed. Locally Added The file has just been added in the local directory. Locally Removed The file has just been removed in the local directory. Needs Checkout or Needs Patch The version in the remote repository is newer than the local copy, which needs to be updated. Needs Merge The version in the remote repository is newer than the local copy, which has also been modified.This will result in a merge after the update. File had conflicts on merge There was a manual merge and its conflicts have not yet been resolved. Unknown The file is not under CVS control. Each revision committed by a developer is automatically tagged with a version number.As we ve already said,this allows the retrieval of any version of a file.Using the cvs diff command, a developer can even view differences between arbitrary versions without actually retrieving the file.The default action for cvs diff is to compare the local revision with the remote version: jane@dev:/home/jane/f-api > cvs diff f-api_write.php3 By using the -r option, which allows her to specify the revision with most CVS commands, Jane can compare the local copy with remote revision 1.1 (the original version): jane@dev:/home/jane/f-api > cvs diff -r 1.1 f-api_write.php3 Index: config.inc.php3 =================================================================== RCS file: /usr/local/cvsroot/config.inc.php3/config.inc.php3,v retrieving revision 1.1 retrieving revision 1.3
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
180 Chapter 5 Basic Web Application Strategies Now that each developer has a local copy of the whole project, they both can start editing the files.They have agreed that John will work on f-api_read.php3 and Jane on f-api_write.php3.This is an important concept to note: CVS cannot replace team communication! After John has finished his initial pass of development, he s going to transfer the file back to the central repository. Because no one else has edited the file in the meantime, all it takes for this action is a single CVS command: john@dev:/home/john/f-api > cvs commit f-api_read.php3 CVS will now launch the system s standard text editor on UNIX systems often vi; on Windows, Notepad and ask for a log message that will be associated with this revision.This message can be viewed by the other developers,so it should accurately describe the work that has been performed. Messages like changed file or new feature don t make any sense and should be avoided.Try to write a concise and clear summary of the work that has been performed; keep in mind the guidelines raised in Chapter 1, Development Concepts, for inline source code comments.Some development teams even use the log messages to automatically provide the customer with a detailed account of work that has been performed to date. For example, following is an example of a well-written log message: Fixed bug #42; implemented additional checks on user-data (int, date), see spec p25 Unlike other version-control systems,such as RCS or Microsoft s Visual Source Safe, CVS doesn t lock files as soon as you check them out.This means that multiple developers can edit files simultaneously, and is a great advantage in our eyes. In our scenario, both Jane and John could simultaneously edit the config.inc.php3 file.As soon as John checks in a file that has been modified and checked in by Jane, he ll get a warning and will need to update his local copy before he can commit the file. In simple cases for example, if John has edited a function at the top and Jane has edited a function at the bottom of the file CVS will merge the two versions together automatically and John can commit the combined version right away. In other circumstances, an automatic merge may not be possible. Imagine the following original line in the configuration file: define( FT_ZIP_ARCHIVE , ZIP_ARC ); // GZip Archive John modifies the constant and commits into the repository: define( FT_ZIP , ZIP_ARC ); // GZip Archive Jane clarifies the comment in the discussed line: define( FT_ZIP_ARCHIVE , ZIP_ARC ); // GZip Archive (not PkZip compatible!)
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
Project Layout 177 Looking at the first directory unit, notice that there are separate directories for included files (libraries, configuration files, templates) and password files (for use with .htaccess files). Password files should never be accessible over the Web, as the passwords contained therein are encrypted with weak standard algorithms and thus attackable with a cracking program. Include files may not need to be protected from outside access for security reasons, but it s always a good idea to store them outside the document root. Even if the Web server should fail to parse a document because of a misconfiguration, important libraries that might contain trade secrets, system information, or innovative algorithms won t be visible to Web surfers. Tip: On all projects, we use a file named configuration.inc.php3 that defines configuration data in the scope of the project.This file also defines the base path of the project this makes it easier to include additional files located in subdirectories later in the script. There are three different directory groups: live, dev, and staging. n The subdirectory live contains the production environment (the actual Web site). n dev is used for the development server. n staging is the transition from dev to live and is used for quality assurance and final testing before a rollout. Separation of development and live server becomes indispensable on larger sites you simply can t afford to edit liveWeb applications.A script error would immediately affect hundreds or thousands of users.Think about what could happen if this script error produced data inconsistencies on a production database So the solution is to differentiate between a development server and a production server.In our example,these two servers are on the same physical machine.The development server could be made accessible under dev.phpwebdev.com with appropriate access restrictions; for example, IP-based filtering. On larger and more critical systems, it s usually better to move the development server to a second, identically configured server.This makes it easier to test software updates,operating system reconfigurations, hardware changes, etc. But why the staging server, a third server? Does this sound exaggerated? Not if your team is large enough to contain dedicated staff for quality assurance (QA) or security auditing. For a complex site, you ll often need to spend considerable time on testing before launching a revision. Developers can t be required to wait idling while the QA staff is banging on the development server.Another reason to include a staging server in your setup is that developers feel it s safer this way to introduce more significant changes to the development server they don t need to worry that much about breaking other developers code. If a problem occurs, they can solve it before the project manager commits the development version to the staging server. Of course, if someone breaks the staging server, he ll still need to do the traditional thirty push-ups.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
178 Chapter 5 Basic Web Application Strategies How does a typical development life cycle work using such a setup? 1. The new project is started by creating the directory structure and setting up CVS (more on CVS shortly). 2. Developers check out a copy of the development branch. 3. Developers commit their changes to the development branch. 4. After a significant milestone has been reached and the system is ready for testing, the project manager transfers the development code to the staging server. 5. QA and security perform their tests on the staging server. Issues that are found are reported to the developers. 6. The developers fix bugs and return the project to QA unless no more bugs are found. 7. To launch, the staging server is copied over to the production server. Don t forget the launch party at this point! Note that we talk here only about the code development stage of the project. Of course, the project as a whole still follows the cycles of software engineering: 1. Project initialization. 2. Analysis. 3. Design. 4. Technical specification and database design. 5. Implementation (that s what we re talking about). 6. Quality assurance. 7. Release. CVS: Concurrent Versions System When multiple developers work on one project, the potential for version conflicts arises; this is even more likely when the developers are working not in the same cubicle but distributed across national boundaries.What happens if two developers edit one file at the same time? The changes of one developer will inevitably be overwritten.What happens if a script authored by John doesn t work anymore after having been changed by Jane? John will have a hard time figuring out what Jane had changed. This is where version control systems come into play.This software remembers previous versions of a file, allowing you to revert to an older version quickly (a sort of extended undo).The version software will notify you when other developers have edited files you re working on and allow you to react to conflicts.And finally,it will remember who made certain changes.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
CVS: Concurrent Versions System 179 CVS is a software that does all of the above and is even free (Open Source).The development of PHP itself is managed with CVS, and such successful projects as everyone s favorite Web server (Apache), the Mozilla project, and KDE trust CVS to manage the work of dozens of individual developers. Even the XML source code of this book is maintained with CVS.The authors are in Germany (Till) and Italy (Tobias), the publisher (New Riders) and the editors are located in the U.S., the tech reviewer is in Australia, and we all work on one central repository called phpBook on a CVS server in northern Italy.The repository contains all the text, some PHP utilities to create unified versions of the individual chapter files, some resource files for XMetaL (described shortly), the code examples, and the figures. On our local systems, we use WinCVS or command-line CVS to administer the files and work on the text with XMetaL, SoftQuad s XML editor, in a very comfortable way. Many software projects have a similar scenario:The developers are spread across the globe. CVS is ideal for Internet-based software development: It s a client/server application utilizing the Net as transport layer, and maintains a central repository of the source code.Within the repository,single projects are organized in modules.Each developer checks out a module from this repository and works on the local version this is a great way to reduce the online time, especially when you have to use dial-up Internet access.When the developer is done with his changes, he commits the updated file to the repository.The CVS software handles all the rest and does the following: n Associates a version number to each revision of a file. n Stores a log entry for each revision. n Keeps track of who has checked out a file to work on it. n Warns you when others have edited a file you want to check in. Let s walk through a typical CVS session at the beginning of a project.There are two developers,John and Jane,who are working on the implementation of an API.We assume that their project manager has set up a complete CVS server, created a repository module named f-api (as in fictitious API), and set the CVS environment variables in their shell accounts accordingly. (For more information about the installation of the CVS server or client, please refer to the CVS reference manual.) The first step for both developers is logging into the CVS server and checking out a copy of the module.This is done using the cvs checkout command, which will create a directory named after the module (in our example, ./f-api): john@dev:/mnt/daten/home/john > cvs login (Logging in to john@www.phpwebdev.com) CVS password:
john@dev:/home/john > cvs checkout f-api cvs server: Updating f-api U f-api/config.inc.php3 U f-api/f-api_read.php3 U f-api/f-api_write.php3
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
176 Chapter 5 Basic Web Application Strategies Project management is a critical component in effective management of distributed software development.When a new project is started,people know nothing.They don t know what to do and they don t know when to do it.They need someone who coordinates activities,allocates responsibilities,and monitors progress and results.They need a project manager. Software development project management is so vast a topic that we can t cover it in this book, unfortunately. Many good methods and resources are available, and we encourage you to review them if you have no formal training on the subject.This includes literature from The Mythical Man Month to methods like the Personal Software Process. We ll focus on the stages of a project in which technical development is involved. You have already seen in Chapter 3, Application Design, how to plan and lay out an application programming interface (API). Indeed, this was a mini-project of its own. We made some silent assumptions in that discussion: how to organize the code in directories, who s on the development team, how different versions of the API are maintained. In real (larger) projects,however,you ll have to make these decisions yourself.And since they form the basis of your project, you d better think twice before you commit to something that could prove inadequate later. Directory Structure The most basic but nevertheless often neglected technical decision to make when beginning the source code development of a new project is what directory layout to use. Generally, we use the following structure: /home/www/phpwebdev.com/live/cgi-bin htdocs htpasswd include logs /home/www/phpwebdev.com/staging/cgi-bin htdocs htpasswd include logs /home/www/phpwebdev.com/dev/cgi-bin htdocs htpasswd include logs
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services
174 Chapter 5 Basic Web Application Strategies Tip: Unlike PHP 3.0,PHP 4.0 supports multidimensional arrays in forms.This actually makes it realistic to have all form variables, including select multiple fields, contained within one array. Using HTML Templates You may have noticed that there s no HTML code in the form example. Indeed, we have outsourced the HTML to a separate file called PHP_Normal_Form.inc.html.This is a template file, containing all HTML layout and some template tags such as {HEADER}. Maybe this sounds familiar to you because you ve already used the class FastTemplate. EasyTemplate leaves out the advanced features of FastTemplate all it does is provide a fast interface to replace scalar tags in templates with a string value. Because it restricts itself to this simple operation, it offers a performance boost of over 120% compared to FastTemplate when parsing our form template. The template class we developed has just three functions: assign(), easy_parse(), and easy_print(), described in the following table: Function Description void EasyTemplate The constructor of the class.Takes as (string template_filename) argument the filename of the template you want to use. If an error occurs, the $error variable will contain the error message. bool assign Assigns a value for a template tag. Returns (string tag, string value) true on success and false on error. mixed easy_parse() Parses the template and returns it as a string. If an error occurs, the function returns false and sets the class s $error variable to a meaningful message. mixed easy_print() Parses the template and prints it out. If an error occurs, the function returns false and sets the class s $error variable to a meaningful message. Tip: A constructor in PHP always returns void no matter what you re trying to do with return(). What s the advantage of using templates? The main advantage is separation of layout and code.Any designer can open the template file in his favorite HTML authoring software without having to worry about breaking the script code.And the developer doesn t need to deal with HTML at all. The alternative would be embedding PHP code directly in the HTML. If you were to redesign your application, you d have a hard time changing the HTML code manually.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services