[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 #!/usr/bin/php -qC 2 <?php 3 /**************************************************************************\ 4 * eGroupWare - Tool to modernize the eGW code automaticaly * 5 * http://www.eGroupWare.org * 6 * Written and (c) by Ralf Becker <RalfBecker@outdoor-training.de> * 7 * ------------------------------------------------------- * 8 * This program is free software; you can redistribute it and/or modify it * 9 * under the terms of the GNU General Public License as published by the * 10 * Free Software Foundation; either version 2 of the License, or (at your * 11 * option) any later version. * 12 \**************************************************************************/ 13 14 /* $Id: modernize.php 20295 2006-02-15 12:31:25Z $ */ 15 16 error_reporting(E_ALL & ~ E_NOTICE); 17 18 // defaults 19 $no_phpgw = $do_replace = false; 20 $remove_space_indention = 2; // replace 2 space with a tab 21 22 while ($argc > 2) 23 { 24 switch($argv[1]) 25 { 26 case '--no-phpgw': 27 $no_phpgw = true; 28 break; 29 30 case '--remove-space-indention': 31 $remove_space_indention = (int) $argv[2]; 32 array_shift($argv); 33 --$argc; 34 break; 35 36 case '--replace': 37 $do_replace = true; 38 break; 39 40 default: 41 $argc = 0; // invalid parameter ==> break the loop and give usage message 42 break; 43 } 44 array_shift($argv); 45 --$argc; 46 } 47 48 if ($argc != 2 || !file_exists($file = $argv[1])) 49 { 50 if ($argc == 2) echo "File '$file' not found !!!\n\n"; 51 echo "Usage: modernize.php [--no-phpgw] [--remove-space-indention N] [--replace] <filename>\n"; 52 echo "--no-phpgw dont change phpgw to egw, necessary for some API files\n"; 53 echo "--remove-space-indention N substitute every N space at the beginning of a line with a tab (default 2)\n"; 54 echo "--replace replaces the given file (plus creating a backup) instead of acting as filter\n\n"; 55 exit; 56 } 57 58 if ($do_replace) 59 { 60 if (!is_writable($file) && !is_writable(dirname($file))) 61 { 62 echo "'$file' is NOT writeable !!!\n"; 63 exit; 64 } 65 $do_replace = $file; 66 $file .= '.bak'; 67 if (is_writable(dirname($file))) 68 { 69 rename($do_replace,$file); 70 } 71 else // only file is writable not the directory, so we cant create a backup 72 { 73 $file = $do_replace; 74 } 75 ob_start(); 76 } 77 78 79 // some code modernizations 80 $modernize = array( 81 // saves an unnecessary copy 82 '= CreateObject' => '=& CreateObject', 83 '= new' => '=& new', 84 // php5 cloning of the DB object 85 '= $GLOBALS[\'phpgw\']->db;' => '= clone($GLOBALS[\'egw\']->db);', 86 '= $this->db;' => '= clone($this->db);', 87 // remove windows lineends (CR) 88 "\r" => '', 89 ); 90 91 foreach(array('GET','POST','SERVER','SESSION') as $name) 92 { 93 $modernize['$HTTP_'.$name.'_VARS'] = '$_'.$name; 94 $modernize['$GLOBALS[\'HTTP_'.$name.'_VARS\']'] = '$_'.$name; 95 $modernize['$GLOBALS["HTTP_'.$name.'_VARS"]'] = '$_'.$name; 96 } 97 98 if (!$no_phpgw) 99 { 100 $modernize += array( 101 // phpGW --> eGW 102 // done now separate as it is case sensitve 'PHPGW_' => 'EGW_', 103 'global $phpgw_info;' => '', 104 'global $phpgw;' => '', 105 '$GLOBALS[\'phpgw_info\']' => '$GLOBALS[\'egw_info\']', 106 '$GLOBALS["phpgw_info"]' => '$GLOBALS[\'egw_info\']', 107 '$phpgw_info[' => '$GLOBALS[\'egw_info\'][', 108 '$GLOBALS[\'phpgw\']' => '$GLOBALS[\'egw\']', 109 '$GLOBALS["phpgw"]' => '$GLOBALS[\'egw\']', 110 '$phpgw->' => '$GLOBALS[\'egw\']->', 111 'common->phpgw_header' => 'common->egw_header', 112 'common->phpgw_footer' => 'common->egw_footer', 113 'common->phpgw_exit' => 'common->egw_exit', 114 'common->phpgw_final' => 'common->egw_final', 115 ); 116 } 117 118 119 $modernize_from = array_keys($modernize); 120 $modernize_to = array_values($modernize); 121 122 $in_doc_block = false; 123 foreach(file($file) as $n => $line) 124 { 125 $func = function_exists('str_ireplace') ? 'str_ireplace' : 'str_replace'; 126 $line = str_replace('PHPGW_','EGW_',$func($modernize_from,$modernize_to,$line)); 127 128 if ($remove_space_indention) 129 { 130 while (preg_match("/^(\t*)".str_repeat(' ',$remove_space_indention).'/',$line)) 131 { 132 $line = preg_replace("/^(\t*)".str_repeat(' ',$remove_space_indention).'/',"\\1\t",$line); 133 } 134 } 135 136 if (!$in_doc_block) 137 { 138 $parts = explode('/*!',$line); 139 if (count($parts) <= 1) 140 { 141 echo $line; 142 continue; 143 } 144 $in_doc_block = true; 145 146 list($indent,$rest) = $parts; 147 echo $indent."/**\n"; 148 if (strlen($rest) <= 2) 149 { 150 continue; 151 } 152 $line = $indent.$rest; 153 154 if (($one_line_block = strstr($line,'*/') !== false)) $line = str_replace('*/','',$line); 155 } 156 // now we are inside a comment-block 157 158 if (preg_match('/[ \t]*\*\//',$line)) // exiting the comment-block 159 { 160 $in_doc_block = false; 161 echo str_replace('*/',' */',$line); 162 continue; 163 } 164 if (preg_match('/^(.*)@([a-zA-Z]+) (.*)$/',$line,$parts)) 165 { 166 list(,$indent,$cmd,$value) = $parts; 167 switch ($cmd) 168 { 169 // to ignore 170 case 'syntax': 171 case 'function': 172 case 'class': 173 break; 174 175 case 'abstract': 176 echo $indent.' * '.$value."\n".$indent." *\n"; 177 break; 178 179 case 'discussion': 180 case 'example': 181 default: 182 echo $indent.' * '.$value."\n"; 183 break; 184 185 case 'result': 186 $cmd = 'return'; 187 // fall through 188 case 'param': 189 case 'return': 190 case 'var': 191 case 'author': 192 case 'copyright': 193 case 'licence': 194 case 'package': 195 case 'access': 196 echo $indent.' * @'.$cmd.' '.$value."\n"; 197 break; 198 } 199 } 200 else 201 { 202 echo str_replace($indent,$indent.' * ',$line); 203 } 204 if ($one_line_block) 205 { 206 echo $indent." */\n"; 207 $one_line_block = $in_doc_block = false; 208 } 209 } 210 211 if ($do_replace && ($f = fopen($do_replace,'wb'))) 212 { 213 fwrite($f,ob_get_contents()); 214 fclose($f); 215 } 216 else 217 { 218 ob_flush(); 219 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |