| 1 |
joko |
1.1 |
<?php |
| 2 |
|
|
|
| 3 |
|
|
/* |
| 4 |
|
|
* This script is for upgrading from 1.3.2 -> 1.4.0. |
| 5 |
|
|
* This script will convert Smarty variable references from the old format to |
| 6 |
|
|
* the new one. For example, what used to look like $section1/foo.bar will now |
| 7 |
|
|
* be $foo[section1].bar. This allows for more readable syntax and also allows |
| 8 |
|
|
* referencing deeply nested structures of arbitrary complexity. |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
/* |
| 12 |
|
|
* Set these to match your template delimeters. |
| 13 |
|
|
*/ |
| 14 |
|
|
$left_delimiter = '{'; |
| 15 |
|
|
$right_delimiter = '}'; |
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
|
|
if ($argc < 2) { |
| 19 |
|
|
die("\nUsage: php -q fix_vars.php <templates>\n\n"); |
| 20 |
|
|
} |
| 21 |
|
|
|
| 22 |
|
|
$ldq = preg_quote($left_delimiter, '!'); |
| 23 |
|
|
$rdq = preg_quote($right_delimiter, '!'); |
| 24 |
|
|
|
| 25 |
|
|
$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; |
| 26 |
|
|
$pwd = $HTTP_ENV_VARS['PWD']; |
| 27 |
|
|
|
| 28 |
|
|
foreach (array_slice($argv, 1) as $template) { |
| 29 |
|
|
$template = $pwd . '/' . $template; |
| 30 |
|
|
if (!is_file($template)) continue; |
| 31 |
|
|
|
| 32 |
|
|
$input = implode('', file($template)); |
| 33 |
|
|
$fp = fopen($template.'.out', 'w'); |
| 34 |
|
|
if (!$fp) { |
| 35 |
|
|
die("\nError: could not open $template.out for writing\n\n"); |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
/* Gather all template tags. */ |
| 39 |
|
|
preg_match_all("!({$ldq}\s*)(.*?)(\s*{$rdq})!s", $input, $match); |
| 40 |
|
|
$template_tags = $match[2]; |
| 41 |
|
|
$template_pre_tags = $match[1]; |
| 42 |
|
|
$template_post_tags = $match[3]; |
| 43 |
|
|
/* Split content by template tags to obtain non-template content. */ |
| 44 |
|
|
$text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $input); |
| 45 |
|
|
|
| 46 |
|
|
$fixed_tags = array(); |
| 47 |
|
|
for ($i = 0; $i < count($template_tags); $i++) { |
| 48 |
|
|
$fixed_tags[] = fix_tag($template_tags[$i]); |
| 49 |
|
|
} |
| 50 |
|
|
|
| 51 |
|
|
$output = ''; |
| 52 |
|
|
/* Interleave the compiled contents and text blocks to get the final result. */ |
| 53 |
|
|
for ($i = 0; $i < count($fixed_tags); $i++) { |
| 54 |
|
|
$output .= $text_blocks[$i].$template_pre_tags[$i].$fixed_tags[$i].$template_post_tags[$i]; |
| 55 |
|
|
} |
| 56 |
|
|
$output .= $text_blocks[$i]; |
| 57 |
|
|
|
| 58 |
|
|
fwrite($fp, $output); |
| 59 |
|
|
fclose($fp); |
| 60 |
|
|
copy($template.'.out', $template); |
| 61 |
|
|
unlink($template.'.out'); |
| 62 |
|
|
|
| 63 |
|
|
print "Fixed $template.\n"; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
function fix_tag($template_tag) |
| 67 |
|
|
{ |
| 68 |
|
|
global $qstr_regexp; |
| 69 |
|
|
|
| 70 |
|
|
if ($template_tag{0} == '*' && $template_tag{strlen($template_tag)-1} == '*') |
| 71 |
|
|
return $template_tag; |
| 72 |
|
|
|
| 73 |
|
|
/* Split tag into two parts: command and the arguments. */ |
| 74 |
|
|
preg_match('/^( |
| 75 |
|
|
(?: ' . $qstr_regexp . ' | (?>[^"\'\s]+))+ |
| 76 |
|
|
) |
| 77 |
|
|
(\s+(.*))? |
| 78 |
|
|
/xs', $template_tag, $match); |
| 79 |
|
|
list(, $tag_command, $tag_args) = $match; |
| 80 |
|
|
if (preg_match('!^\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) |
| 81 |
|
|
$tag_command = fix_var($tag_command); |
| 82 |
|
|
else if (preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) |
| 83 |
|
|
$tag_command = fix_other_var($tag_command); |
| 84 |
|
|
else if (preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) |
| 85 |
|
|
$tag_command = fix_other_var($tag_command); |
| 86 |
|
|
|
| 87 |
|
|
if (function_exists("preg_replace_callback")) { |
| 88 |
|
|
$tag_args = preg_replace_callback('!(?<=[\s(:=])\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_var_match', $tag_args); |
| 89 |
|
|
$tag_args = preg_replace_callback('!(?<=[\s(:=])#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_other_var_match', $tag_args); |
| 90 |
|
|
$tag_args = preg_replace_callback('!(?<=[\s(:=])%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!', 'fix_other_var_match', $tag_args); |
| 91 |
|
|
} else { |
| 92 |
|
|
$tag_args = preg_replace('!(?<=[\s(:=])\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_var_match', $tag_args); |
| 93 |
|
|
$tag_args = preg_replace('!(?<=[\s(:=])#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_other_var_match', $tag_args); |
| 94 |
|
|
$tag_args = preg_replace('!(?<=[\s(:=])%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|}\s]+))*)*!F', 'fix_other_var_match', $tag_args); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
return $tag_command.$tag_args; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
function fix_vars_props($tokens) |
| 101 |
|
|
{ |
| 102 |
|
|
global $qstr_regexp; |
| 103 |
|
|
|
| 104 |
|
|
$var_exprs = preg_grep('!^\$(\w+(\.\w+)?/)*\w+(?>\.\w+)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); |
| 105 |
|
|
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); |
| 106 |
|
|
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens); |
| 107 |
|
|
|
| 108 |
|
|
if (count($var_exprs)) { |
| 109 |
|
|
foreach ($var_exprs as $expr_index => $var_expr) { |
| 110 |
|
|
$tokens[$expr_index] = fix_var($var_expr); |
| 111 |
|
|
} |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
|
|
/* |
| 115 |
|
|
if (count($conf_var_exprs)) { |
| 116 |
|
|
foreach ($conf_var_exprs as $expr_index => $var_expr) { |
| 117 |
|
|
$tokens[$expr_index] = $this->_parse_conf_var($var_expr); |
| 118 |
|
|
} |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
|
|
if (count($sect_prop_exprs)) { |
| 122 |
|
|
foreach ($sect_prop_exprs as $expr_index => $section_prop_expr) { |
| 123 |
|
|
$tokens[$expr_index] = $this->_parse_section_prop($section_prop_expr); |
| 124 |
|
|
} |
| 125 |
|
|
} |
| 126 |
|
|
*/ |
| 127 |
|
|
|
| 128 |
|
|
return $tokens; |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
|
function fix_var_match($match) |
| 132 |
|
|
{ |
| 133 |
|
|
return fix_var($match[0]); |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
function fix_other_var_match($match) |
| 137 |
|
|
{ |
| 138 |
|
|
return fix_other_var($match[0]); |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
function fix_var($var_expr) |
| 142 |
|
|
{ |
| 143 |
|
|
list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2); |
| 144 |
|
|
|
| 145 |
|
|
$sections = explode('/', $var_ref); |
| 146 |
|
|
$props = explode('.', array_pop($sections)); |
| 147 |
|
|
$var_name = array_shift($props); |
| 148 |
|
|
|
| 149 |
|
|
$output = "\$$var_name"; |
| 150 |
|
|
|
| 151 |
|
|
foreach ($sections as $section_ref) { |
| 152 |
|
|
$output .= "[$section_ref]"; |
| 153 |
|
|
} |
| 154 |
|
|
if (count($props)) |
| 155 |
|
|
$output .= ".".implode('.', $props); |
| 156 |
|
|
|
| 157 |
|
|
if ($modifiers) |
| 158 |
|
|
$output .= fix_modifiers($modifiers); |
| 159 |
|
|
|
| 160 |
|
|
return $output; |
| 161 |
|
|
} |
| 162 |
|
|
|
| 163 |
|
|
function fix_other_var($var_expr) |
| 164 |
|
|
{ |
| 165 |
|
|
list($var_ref, $modifiers) = explode('|', $var_expr, 2); |
| 166 |
|
|
|
| 167 |
|
|
$output = $var_ref; |
| 168 |
|
|
if ($modifiers) |
| 169 |
|
|
$output .= fix_modifiers($modifiers); |
| 170 |
|
|
|
| 171 |
|
|
return $output; |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
|
function fix_modifiers($modifier_string) |
| 175 |
|
|
{ |
| 176 |
|
|
global $qstr_regexp; |
| 177 |
|
|
|
| 178 |
|
|
preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); |
| 179 |
|
|
list(, $modifiers, $modifier_arg_strings) = $match; |
| 180 |
|
|
$output = ''; |
| 181 |
|
|
for ($i = 0; $i < count($modifiers); $i++) { |
| 182 |
|
|
$modifier_name = $modifiers[$i]; |
| 183 |
|
|
preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); |
| 184 |
|
|
$modifier_args = fix_vars_props($match[1]); |
| 185 |
|
|
|
| 186 |
|
|
$output .= '|' . $modifier_name; |
| 187 |
|
|
if ($modifier_args) |
| 188 |
|
|
$output .= ':'.implode(':', $modifier_args); |
| 189 |
|
|
} |
| 190 |
|
|
|
| 191 |
|
|
return $output; |
| 192 |
|
|
} |
| 193 |
|
|
|
| 194 |
|
|
?> |