Revision [8931]

This is an old revision of CompatibilityCode made by JavaWoman on 2005-06-07 19:45:36.

 

Compatibility Code


See also:
Compatibility code is code that serves to "hide" differences in coding to accommodate different versions, such as different PHP versions or different MySQL versions. By providing compatibility code with a public interface we can maintain a clean and simple API while dealing with version differences automatically.
 


Different types of compatibility code


Compatibility code can be classified into three kinds, depending on what kind of differences we are hiding; the aim is always to hide complexity from calling code:

Classification

Missing functions
Hiding decision logic
Configuration

Compatibility code in Wikka 1.1.6.0


Wikka 1.1.6.0 already has some compatibility code in place, but it could do better... See below for some (proposed) new additions.

mysql_real_escape_string()


Classification
Missing function.

Code
The function mysql_real_escape_string() is available in PHP as of version 4.3; Wikka specifies PHP 4.1.0 as minimum PHP version so we need to provide a (near) equivalent for this.

The code is found near the start of wikka.php:
  1. if ( ! function_exists("mysql_real_escape_string") )
  2. {
  3.     function mysql_real_escape_string($string)
  4.     {
  5.         return mysql_escape_string($string);
  6.     }
  7. }


This clearly shows the basic pattern for "missing function" compatibility code:
This ensures that the intended function can be called as normal: if the function does not exist in the current installation, the compatibility code will kick in.

Note that mysql_escape_string() is not exactly equivalent with mysql_real_escape_string(); possibly the code here could be extended a bit to provide better compaitibilty.

Magic quotes


Classification
Configuration

Code
PHP may be configured to use "magic quotes". When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically for GPC (Get/Post/Cookie) operations. To avoid having to check for these backslashes in every string we get via cookies or POST and GET requests the following code turns off magic quotes and removes them where found, so any further code does not have to deal with them. The code is (currently) found near the end of wikka.php:
  1. // workaround for the amazingly annoying magic quotes.
  2. function magicQuotesSuck(&$a)
  3. {
  4.     if (is_array($a))
  5.     {
  6.         foreach ($a as $k => $v)
  7.         {
  8.             if (is_array($v))
  9.                 magicQuotesSuck($a[$k]);
  10.             else
  11.                 $a[$k] = stripslashes($v);
  12.         }
  13.     }
  14. }
  15. {
  16.     magicQuotesSuck($_POST);
  17.     magicQuotesSuck($_GET);
  18.     magicQuotesSuck($_COOKIE);
  19. }


In a future version this would be better placed together with other compatibility code near the start and maybe get a more "polite" name. :)

Proposed new compatibility code


This is compatibility code needed for or used used by new code development presented on this site.

html_entity_decode()


Classification
Missing function.

Code
The function html_entity_decode() is available in PHP as of version 4.3. The AdvancedFormatter "advanced" formmater makes use of it when generating ids for headings. The following code provides a near equivalent. Insert in wikka.php immediately after the mysql_real_escape_string() compatibility code shown above:

  1. if (!function_exists('html_entity_decode'))
  2. {
  3.     // based on http://php.net/html-entity-decode.php
  4.     // third parameter (charset) ignored: only (default) ISO-8859-1 character set supported
  5.     function html_entity_decode($string,$quote_style=ENT_COMPAT)
  6.     {
  7.         $trans_tbl = get_html_translation_table(HTML_ENTITIES,$quote_style);
  8.         $trans_tbl = array_flip($trans_tbl);
  9.         return strtr($string, $trans_tbl);
  10.     }
  11. }


Note that it is not completely equivalent: html_entity_decode() in PHP 4.3+ provides support for a range of character sets (including UTF-8); the character set can be specified in a third parameter. The function get_html_translation_table() we use to provide compatibility only uses the default charset ISO-8859-1; when this code is used in versiosn of PHP lower than 4.3 any third parameter will be ignored.

Note that for the case this was written (generating ids for headings) we actually need ISO-8859-1, so for that application lack of equivalence it is not a problem; used in another context it may be and one should be aware of the limitations.


CategoryDevelopmentCore
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki