Changeset 14795

Show
Ignore:
Timestamp:
02/07/08 20:01:22 (10 months ago)
Author:
rambo
Message:

refactoring the normalizer so that we can use same helper with midgard objects

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.core/support/normalize_whitespace.php

    r14772 r14795  
    22<?php 
    33error_reporting(E_ALL); 
    4 if ($argc < 2) 
     4require_once('normalize_whitespace_normalizer.php'); 
     5if ($argc < 4) 
    56{ 
    67    $name = basename($argv[0]); 
    7     echo "\nUsage: {$name} <files_list>\n"; 
     8    echo "\nUsage: {$name} <configfile> <username> <password>\n"; 
    89    echo "  For example:\n"; 
    9     echo "  {$name} `find ~/svn/midcom/ -name '*.php'` \n\n"; 
     10    echo "  {$name} midgard 'sgadmin+sgname' 'adminpasswd' \n\n"; 
    1011    exit(1); 
    1112} 
    12 $files = array_slice($argv, 1); 
    13  
    14 class midcom_support_wsnormalizer 
     13$conffile =& $argv[1]; 
     14$username =& $argv[2]; 
     15$password =& $argv[3]; 
     16if (!mgd_config_init($conffile)) 
    1517{ 
    16     var $tab_size = 4; 
    17     var $_tab_strings = array(); 
    18     var $operations = array 
    19     ( 
    20         'newlines', 
    21         'tabs2spaces', 
    22         'open_pre_strip', 
    23         'close_post_strip', 
    24         'line_end_ws', 
    25     ); 
    26  
    27     /** 
    28      * Calls all the normalization operations defined for given $data 
    29      * 
    30      * @param string $data data to normalize (usually file contents) 
    31      * @return string normalized 
    32      */     
    33     function normalize($data) 
    34     { 
    35         foreach($this->operations as $op) 
    36         { 
    37             if (!method_exists($this, $op)) 
    38             { 
    39                 continue; 
    40             } 
    41             $data = $this->$op($data); 
    42         } 
    43         return $data; 
    44     } 
    45  
    46     /** 
    47      * Normalizes various newline schemes to unix newlines 
    48      * 
    49      * @param string $data data to normalize (usually file contents) 
    50      * @return string normalized 
    51      */     
    52     function newlines($data) 
    53     { 
    54         return preg_replace("/\n\r|\r\n|\r/", "\n", $data); 
    55     } 
    56  
    57     /** 
    58      * Normalizes tabs to given number of spaces 
    59      * 
    60      * @see $this->tab_size 
    61      * @param string $data data to normalize (usually file contents) 
    62      * @return string normalized 
    63      */     
    64     function tabs2spaces($data) 
    65     { 
    66         if (!isset($this->_tab_strings[$this->tab_size])) 
    67         { 
    68             $this->_tab_strings[$this->tab_size] = str_pad('', $this->tab_size, ' '); 
    69         } 
    70         return str_replace("\t", $this->_tab_strings[$this->tab_size], $data); 
    71     } 
    72  
    73     /** 
    74      * Removes whitespace between start of string and first PHP open 
    75      * tag (if said tag is the first nonwhitespace in string) 
    76      * 
    77      * @param string $data data to normalize (usually file contents) 
    78      * @return string normalized 
    79      */     
    80     function open_pre_strip($data) 
    81     { 
    82         return preg_replace('%^\s+(<\?(php)?)%s', '\\1', $data); 
    83     } 
    84  
    85     /** 
    86      * Removes whitespace between last PHP close tag and end of 
    87      * string (if said tag is the last nonwhitespace in the string) 
    88      * 
    89      * @param string $data data to normalize (usually file contents) 
    90      * @return string normalized 
    91      */     
    92     function close_post_strip($data) 
    93     { 
    94         return preg_replace('%(\?>)\s+$%s', '\\1', $data); 
    95     } 
    96  
    97     /** 
    98      * Removes whitespace from ends of lines (if lines are not all whitespace) 
    99      * 
    100      * @param string $data data to normalize (usually file contents) 
    101      * @return string normalized 
    102      */     
    103     function line_end_ws($data) 
    104     { 
    105         return $data; 
    106         /* Not so easy afterall 
    107         $data_arr = explode("\n", $data); 
    108         //$data_arr = preg_replace('%[\t\f ]+$%m', '', $data_arr); 
    109         $data_arr = preg_replace('%(^\s+$)|([^\t\f ])\s+%', '\\1', $data_arr); 
    110         return implode("\n", $data_arr); 
    111         */ 
    112     } 
    113  
     18    echo "\nInitialization failed\n\n"; 
     19    exit(1); 
     20
     21mgd_auth_midgard($username, $password); 
     22if (!$_MIDGARD['user']) 
     23
     24    echo "\nAuthentication failed\n\n"; 
     25    exit(1); 
     26
     27if (!$_MIDGARD['sitegroup'] === 0) 
     28
     29    echo "\nSG0 usage not supported\n\n"; 
     30    exit(1); 
    11431} 
    11532 
    116 if (!function_exists('file_put_contents')) 
    117 
    118     function file_put_contents($file, &$data) 
    119     { 
    120         $fp = fopen($file, 'w'); 
    121         if (!$fp) 
    122         { 
    123             return false; 
    124         } 
    125         $ret = fwrite($fp, $data); 
    126         fclose($fp); 
    127         return $ret; 
    128     } 
    129 
     33 
    13034 
    13135$normalizer = new midcom_support_wsnormalizer(); 
    132 foreach ($files as $file) 
    133 { 
    134     $data = file_get_contents($file); 
    135     $normalized = $normalizer->normalize($data); 
    136     if ($data === $normalized) 
    137     { 
    138         unset($data, $normalized); 
    139         continue; 
    140     } 
    141     file_put_contents($file, $normalized); 
    142     unset($data, $normalized); 
    143 } 
    14436 
    14537?>