Changeset 1558 for trunk/gregarius/dist

Show
Ignore:
Timestamp:
09/04/06 19:30:31 (2 years ago)
Author:
mbonetti
Message:

Almost there

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/gregarius/dist/convert.php

    r1557 r1558  
    44    $EXCLUDE = array('intl', 'extlib'); 
    55 
     6    Convert('.'); 
     7 
     8    function __callback__($matches) { 
     9        if (defined($matches[1])) { 
     10            return '__(\''.constant($matches[1]) .'\')'; 
     11        } else { 
     12            echo "??? " . print_r($matches,true) . "\n"; 
     13        } 
     14    } 
     15     
     16     
    617    function Convert($dir) 
    718    { 
     
    1122            return; 
    1223        } 
     24        $files=array_keys(preg_find('/\.php$/',$dir,PREG_FIND_RECURSIVE | PREG_FIND_FULLPATH)); 
     25        var_dump($files); 
     26        foreach($files as  $file) { 
    1327 
    14         $baseDir = opendir($dir); 
    15         while($file = readdir($baseDir)) { 
    16             if('.' == $file || 
    17                  '..' == $file) { 
    18                 continue; 
     28            echo "Now handling $file ... "; 
     29     
     30            $oldfile=$file; 
     31            $newfile=$file.'.new'; 
     32            $old = fopen($oldfile, 'r'); 
     33            $new = fopen($newfile, 'w'); 
     34            if (! ($old && $new)){ 
     35                die("Failed opening $oldfile and $newfile\n"); 
     36            } 
     37            while (!feof($old)) { 
     38                fwrite($new, preg_replace_callback('#(LBL_[A-Z0-9_]+)#','__callback__', fgets($old))); 
    1939            } 
    2040 
    21             if(is_dir($file)) { 
    22                 Convert($file); 
    23             } else { 
    24                 $old = fopen($file, 'r'); 
    25                 $new = fopen($file . '.new', 'w'); 
     41            flush($new); 
     42            fclose($old); 
     43            fclose($new); 
    2644 
    27                 while(($line = fread($old)) != feof($old)) { 
    28                     preg_match('/\.*.LBL_*.\.', $line, $matches); 
     45            //rename($old, $old . '.old'); 
     46            //rename($new, $old); 
    2947 
    30                     if(is_array($matches)) { 
    31                         foreach($matches as $match) { 
    32                             preg_replace($match, '__("' . eval('echo ' . $match . ';') . '")', $line); 
    33                         } 
    34                     } 
    35  
    36                     fwrite($new, $line); 
    37                 } 
    38  
    39                 flush($new); 
    40                 fclose($old); 
    41                 fclose($new); 
    42  
    43                 rename($old, $old . '.old'); 
    44                 rename($new, $old); 
    45  
    46                 echo "Completed .. " . $old . "\n"; 
    47             } 
     48            echo "\tdone!\n"; 
    4849        } 
    4950    } 
    5051 
    51     Convert('.'); 
     52     
     53    /* 
     54     * Find files in a directory matching a pattern 
     55     * 
     56     * 
     57     * Paul Gregg <pgregg@pgregg.com> 
     58     * 20 March 2004,  Updated 20 April 2004 
     59     * 
     60     * Open Source Code:   If you use this code on your site for public 
     61     * access (i.e. on the Internet) then you must attribute the author and 
     62     * source web site: http://www.pgregg.com/projects/php/code/preg_find.phps 
     63     * Working example: http://www.pgregg.com/projects/php/code/preg_find_ex.phps 
     64     * 
     65     */ 
     66     
     67    define('PREG_FIND_RECURSIVE', 1); 
     68    define('PREG_FIND_DIRMATCH', 2); 
     69    define('PREG_FIND_FULLPATH', 4); 
     70    define('PREG_FIND_NEGATE', 8); 
     71    define('PREG_FIND_DIRONLY', 16); 
     72    define('PREG_FIND_RETURNASSOC', 32); 
     73     
     74    // PREG_FIND_RECURSIVE   - go into subdirectorys looking for more files 
     75    // PREG_FIND_DIRMATCH    - return directorys that match the pattern also 
     76    // PREG_FIND_DIRONLY     - return only directorys that match the pattern (no files) 
     77    // PREG_FIND_FULLPATH    - search for the pattern in the full path (dir+file) 
     78    // PREG_FIND_NEGATE      - return files that don't match the pattern 
     79    // PREG_FIND_RETURNASSOC - Instead of just returning a plain array of matches, 
     80    //                         return an associative array with file stats 
     81    // to use more than one simply seperate them with a | character 
     82     
     83     
     84     
     85    // Search for files matching $pattern in $start_dir. 
     86    // if args contains PREG_FIND_RECURSIVE then do a recursive search 
     87    // return value is an associative array, the key of which is the path/file 
     88    // and the value is the stat of the file. 
     89    Function preg_find($pattern, $start_dir='.', $args=NULL) { 
     90     
     91     
     92      $files_matched = array(); 
     93     
     94      $fh = opendir($start_dir); 
     95     
     96      while (($file = readdir($fh)) !== false) { 
     97        if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue; 
     98        $filepath = $start_dir . '/' . $file; 
     99        if (preg_match($pattern, 
     100                       ($args & PREG_FIND_FULLPATH) ? $filepath : $file)) { 
     101          $doadd =    is_file($filepath) 
     102                   || (is_dir($filepath) && ($args & PREG_FIND_DIRMATCH)) 
     103                   || (is_dir($filepath) && ($args & PREG_FIND_DIRONLY)); 
     104          if ($args & PREG_FIND_DIRONLY && $doadd && !is_dir($filepath)) $doadd = false; 
     105          if ($args & PREG_FIND_NEGATE) $doadd = !$doadd; 
     106          if ($doadd) { 
     107            if ($args & PREG_FIND_RETURNASSOC) { // return more than just the filenames 
     108              $fileres = array(); 
     109              if (function_exists('stat')) { 
     110                $fileres['stat'] = stat($filepath); 
     111                $fileres['du'] = $fileres['stat']['blocks'] * 512; 
     112              } 
     113              if (function_exists('fileowner')) $fileres['uid'] = fileowner($filepath); 
     114              if (function_exists('filegroup')) $fileres['gid'] = filegroup($filepath); 
     115              if (function_exists('filetype')) $fileres['filetype'] = filetype($filepath); 
     116              if (function_exists('mime_content_type')) $fileres['mimetype'] = mime_content_type($filepath); 
     117              if (function_exists('dirname')) $fileres['dirname'] = dirname($filepath); 
     118              if (function_exists('basename')) $fileres['basename'] = basename($filepath); 
     119              if (isset($fileres['uid']) && function_exists('posix_getpwuid ')) $fileres['owner'] = posix_getpwuid ($fileres['uid']); 
     120              $files_matched[$filepath] = $fileres; 
     121            } else 
     122              array_push($files_matched, $filepath); 
     123          } 
     124        } 
     125        if ( is_dir($filepath) && ($args & PREG_FIND_RECURSIVE) ) { 
     126          $files_matched = array_merge($files_matched, 
     127                                       preg_find($pattern, $filepath, $args)); 
     128        } 
     129      } 
     130     
     131      closedir($fh);  
     132      return $files_matched; 
     133     
     134    } 
    52135?>