Issue 85 January 21 2010

Automating PHP to revTalk Conversion
FMPro Migrator Platinum Edition offers new features and possibilities for conversion

By David Simpson

Introduction

This article discusses a new PHP to revTalk conversion feature included in FmPro Migrator Platinum Edition. This new feature is based upon the idea:

Wouldn't it be great if existing PHP code could be converted for use in a Rev stack or On-Rev web page?

According to the TIOBE Community Index, PHP is the 3rd most popular programming language in use today. revTalk is also listed, and appears within the Top 100 list (listed as Revolution). There are millions of web hosts supporting PHP and uncounted numbers of apps written in PHP. Performing a conversion to revTalk provides some distinct advantages in terms of reduced code size, ease of development, and wide range of deployment options. Unlike revTalk, PHP scripts are not typically deployed as either desktop apps or event-driven rich internet apps. It is possible to package a PHP script for use as desktop app, but the process is not cross-platform compatible and doesn't offer a rich graphical interface for the user or the developer. In situations where PHP is used within a rich internet app, it usually performs back-end processing behind an AJAX front end providing the graphical interface. So that means that web developers have to write, test and debug code in at least two different languages in order to create the entire app.

The Rev development platform provides for a very seamless development and deployment process across multiple desktop platforms, and rich internet app web deployment via the revWeb plug-in. revTalk code can also be deployed within web pages between <?rev ?> HTML tags using the On-Rev web hosting service.

A Few Ideas...

Create A Richer GUI Interface for Converted PHP Apps - Due to the availability of a wide variety of free PHP scripts, there are business opportunities for creating both revLet and desktop apps based upon the original PHP code. Rev can be used to develop apps having unique functionality and branding . Paid services or ad supported services could be created having unique functionality and a rich graphical interface in order to improve the customer experience. Making changes to the positions or sizes of objects within a Rev stack is very easy within the Rev IDE. This contrasts which the challenges of dealing with nested HTML tables or absolute positioning CSS style sheet code to achieve this task on a web page. Once changes are made to a stack, it is compiled into a revLet and re-uploaded to the website. As long as the size and name of the stack file has not changed, none of the surrounding HTML code even needs to be modified.

A File Upload and Digital Prints Ordering App - I utilize a couple of internet based digital printing services to create digital posters for my trade show booths. Each of these services provides a file manager and order submission feature on their website I can see a number of advantages to using either a revLet or a desktop app as a replacement for this functionality. Normally, you have to wait some period of time for roundtrip server processing just in order to move from one section of the app to another. But a Rev developed app would be able to provide instantaneous responses to simple tasks like clicking another tab in the app. This is because Rev loads the entire app in memory at once, so that moving to another card happens immediately and doesn't require a trip back to the server.

Convert PHP-based Frameworks (like CakePHP) - Though currently beyond the capabilities of the automated conversion process, the conversion of sophisticated open source MVC frameworks like CakePHP or the Zend Framework could potentially be attempted in the future. This was one of my motivations for creating this PHP conversion feature. I thought that having an automated PHP conversion app could help me convert more of the functionality from FileMaker and Access databases into Rev applications. I will discuss this topic in more detail later in the article.

Features

The PHP to revTalk conversion feature traverses all subdirectories of a selected top-level source directory and converts all of the .php or HTML files which it finds. The converted revTalk code is either saved as an .irev file in the destination directory, or as a card script assigned to a card within a new Rev stack file. Some of the automated conversions to the code include:

  • PHP Variable Conversion - The $ character is removed from PHP variables and the variable name is checked to determine if it conflicts with an existing Rev keyword or function name. Conflicting variable names are changed by adding an underscore character to the end of the name.
  • <?php tags are converted to <?rev tags - The <?rev tags will be usable within .irev files but will be removed if the code is being saved into a Rev stack. Code outside of <?php tags is assumed to be HTML code and it is either passed thru without changes within .irev files or it is commented out within Rev stack code.
  • Trailing Semicolon And Curly Bracket Removal - Each complete PHP instruction is terminated with a semicolon which is removed during the conversion. Code blocks including conditionals and Try/Catch statements are bracketed with curly braces, which either get removed or replaced with the appropriate revTalk instruction.
  • Commonly Used Keyword Conversion - Keywords such as IF, ELSE, TRY/CATCH, REQUIRE, SPRINT, WHILE are converted into the equivalent revTalk instruction.
  • Commonly Used Function Conversion - Some built-in PHP functions including trim(), strlen(), printf(), strtolower() and strtoupper() have direct equivalents in revTalk so these are converted accordingly.
  • PHP Operator Conversion - PHP operators including = =, = = =, ++, - -, &&, and || have direct equivalent operators or replacement keywords which are used in revTalk.
  • Custom Function Conversion - revTalk functions don't use parenthesis surrounding the parameter list and don't need to have opening and closing curly brackets so these are removed. The closing curly bracket is replaced with the end keyword followed by the name of the function.
  • Multi-line HERE Document Code - PHP scripts often include blocks of text/HTML containing variables which are evaluated and placed into a variable. The HERE document spans multiple lines and is converted into a single line revTalk put statement.
  • Code Indenting - Code indenting and comments are retained from the original code in order to improve the readability and maintainability of the converted code. However you can easily reformat the code by clicking the TAB key within the Rev IDE editor.

About the PHP to revTalk revLet

I could have listed samples of Before/After code examples within this article to show off the PHP to revTalk conversion capability. But I decided that one of the best ways to show off this functionality would be to create a revLet and let you test out the features for yourself. You should see the revLet loaded below if you have the revWeb plug-in installed. Click the Convert button to convert the pre-loaded sample PHP code or click the clipboard icon to paste in your own sample code from the clipboard.

 

This PHP to revTalk demo revLet not only lets you test the PHP conversion process, but also showcases a sample of the functionality which can be easily implemented within the revLet technology. First of all, this revLet includes all of the core PHP to revTalk conversion handlers implemented within the FmPro Migrator Platinum Edition desktop application. It was a simple matter to copy the code from the FmPro Migrator stack into this revLet stack file. I added appropriate fields within the revLet and modified the mouseUp button handler code. Since this is a demo revLet, it doesn't have to traverse the source directories looking for files and it doesn't have to include any licence key validation code. Clicking the Convert button performs the conversion and writes resulting revTalk code into the revTalk Code field where you can scroll thru the results. In order to maintain the demo limitation functionality of the revLet, I have set the Lock Text property for the revTalk Code field. You can scroll thru the converted code and even select it with the mouse, but the Edit menu is grayed out in the browser. You can think of it as a "Look but don't touch" type of feature, because of course I have to provide an incentive for customers to purchase or upgrade a license for my software.

With relatively little additional development effort, I have been able to create a feature rich Web 2.0 web application, based upon my desktop application code. You can paste in your own PHP code into the revLet displayed above and instantly see the results. And did I mention that Rev is fast? Generally most pasted code gets processed in milliseconds. Also, notice that fields are scrollable vertically and horizontally, and summary results are displayed in a normally invisible label at the lower part of the revLet window, just as you might implement within a desktop app. Images, fields, buttons and text labels are precisely positioned within the revLet, without requiring nested HTML tables or complex browser-specific CSS style sheet code.

How the Conversion Process Works

There is more than one way to write a code conversion utility. One very sophisticated conversion method requires reading and parsing the code for a source language and building up metadata about the code. This method requires having enough information about the source language to understand the meaning of any individual line of code, while also taking into consideration the code which surrounds it, and any modules or functions which get called by the module. Some systems may then convert the metadata of the source code into an intermediate programming language. Once the translation has been made into the intermediate programming language, all they have to do is write code generators which convert the intermediate programming language into the requested target language. These are the types of converters which have been developed over many years by a significant sized team of developers. These systems are not typically sold to end users and are generally provided only as a service with pricing based upon number of lines of code converted. The advantages of this type of approach are substantial and because they may be able to convert as much as 99% of the code. This type of approach also provides the flexibility of converting from any supported source language to any supported target language. This Any-to-Any conversion flexibility is practical because they only need to write a given code parser or code generator one time. The code generator always takes the intermediate language code and uses it to generate the target output code.

But that is not the type of tool which I have developed. The approach I have used to write the PHP to revTalk converter is a line by line conversion approach. This technique stores relatively little information about the source code as it is being converted. The most critical "state" information used during the conversion process includes: WithinIF, WithinWHILE, WithinTRY, WithinCATCH, WithinFUNCTION <Function Name> and WithinQuotes.

The conversion method is a single pass process and converts each line of code as soon as it is read. For each line of source code, the line is examined as a series of words. This is straightforward to write in revTalk as:

<<code>>

put word 1 of pInstruction into tWord1
put word 2 of pInstruction into tWord2

<<end code>>

This code is is followed by a large SWTICH statement having individual CASE blocks for each of the supported keywords and operators. Using a SWTICH statement improves code execution efficiency because once a particular CASE block is executed, no more evaluations need to be done, and the code returns to the calling handler. By keeping track of the state of the processing (i.e. WithinIF, WithinCATCH, WithinFUNCTION) it is possible to figure out whether a closing curly brace should be replaced with "end if", "end try" or "end <function name>".

Following the processing of each line, there is code at the file processing level which performs global replacement tasks upon commonly used operators and built-in PHP functions.

Limits of the Conversion Process

There are some notable limitations to the conversion process. First, each line of PHP source code should consist of only one complete instruction of well formed PHP code. By "well formed" code, I mean code which contains at least one space between keywords, variables and operators. The conversion process is not sophisticated enough to be able to separate multiple instructions from each other if they appear on the same line.

PHP code does not require spaces between variables and operators as the PHP interpreter can still parse the source. But the code conversion process is simply looking at a line of code as a group of words, so if there isn't at least one space between keywords, operators and variables, the code will appear to be one single word instead of multiple words.

A significant challenge occurs when dealing with OOP (object oriented programming) code. I haven't been able to figure out a straightforward way to make sense of OOP code within the context of revTalk. I have considered whether the behavior feature might be a suitable replacement for OOP constructs within the PHP code. But behaviors just don't have the same functionality as PHP OOP code. This limitation of course has important ramifications which prevents the automated conversion of large frameworks like CakePHP. It is rather impressive to see over 500 files and almost 200,000 lines of code in the CakePHP project getting converted in less than 30 seconds. Until you realize that most of the PHP source code consists of OOP code!

CakePHP Conversion Results

PHP built-in functions and open source PEAR packages present an additional conversion challenge. Some commonly used functions are already converted. But there are almost 6000 functions listed on the php.net website, so I have not completed an exhaustive (and exhausting!) examination of all of them. Many functions such as the database connectivity functions use different parameters or parameters passed in a different order compared to the equivalent revTalk functions. These types of functions are left out of the conversion process on purpose in order to require manual intervention. For more info on tasks which need to be performed manually, please see the PHP to revTalk conversion PDF manual on the FmPro Migrator support web page.

Some of these conversion limitations are due to the overall conversion method which has been chosen or the lack of OOP functionality. Others are due to the limited amount of time which has been available to develop the conversion process so far. I have decided to spend a reasonable amount of time on this initial release and to continue making additional improvements based upon customer requests from revenue generating consulting projects. This way the technology is immediately available to benefit the Rev community while it continues to be enhanced.

If you aren't sure how a particular piece of code will be converted, feel free to come back to this web page and test it within the PHP to revTalk revLet. If you want to performa a more detailed examination of the code, download the demo version of FmPro Migrator and test your code. FmPro Migrator will process 5 PHP scripts of unlimited length in demo mode.

Conclusion

FmPro Migrator's PHP to revTalk Conversion feature can save many hours of manual work by automating boring, repetitive code conversion tasks. This time savings can leave more time for the Rev developer to spend on more interesting tasks which only a human developer can perform. Additionally, for PHP developers who are new to developing in revTalk this feature can serve as an educational tool to speed the the learning process.

Special Offer

FmPro Migrator Platinum Edition is available for $599 from the runrev online store, or $399 as an FmPro Migrator Developer Edition upgrade or annual renewal. As a limited time introductory offer [until the end of the month], .com Solutions Inc. is offering FmPro Migrator Platinum Edition for a 50% discount (new purchase for $299 or upgrade for $199). Use Coupon Code FMPLATINUM for this special offer. FmPro Migrator Platinum Edition includes license keys for the following features:

  • PHPToRevTalk License Key (Qty = Unlimited)
  • BasicToRevTalk License Key (Qty = Unlimited)
  • AccessToFmPro License Key (Qty = 250)
  • FmProToAccess License Key (Qty = 250)

About the Author

David Simpson is the President of .com Solutions Inc. and a Revolution developer of database applications since 2001.

Main Menu

What's New

Get revMobile Pre-Release