source: extensions/plugin_lang_analysis/include/functions.inc.php @ 28862

Last change on this file since 28862 was 28862, checked in by mistic100, 10 years ago

update regexes

File size: 4.6 KB
Line 
1<?php
2defined('PLA_PATH') or die('Hacking attempt!');
3
4/**
5 * list files of a plugin
6 * @param: string $id, plugin id
7 * @return: array of paths relative to plugin root
8 */
9function list_plugin_files($id, $path=null)
10{
11  if (empty($path))
12  {
13    $path = '/';
14  }
15 
16  if ($path == '/language/')
17  {
18    return array();
19  }
20 
21  if (strlen($path)-strrpos($path, '_data/') == 6)
22  {
23    return array();
24  }
25 
26  if (($handle = @opendir(PHPWG_PLUGINS_PATH.$id.$path)) === false)
27  {
28    return array();
29  }
30 
31  $data = array();
32 
33  while ($entry = readdir($handle))
34  {
35    if ($entry=='.' || $entry=='..' || $entry=='.svn' || $entry=='index.php') continue;
36   
37    if (is_dir(PHPWG_PLUGINS_PATH.$id.$path.$entry))
38    {
39      $data = array_merge($data, list_plugin_files($id, $path.$entry.'/'));
40    }
41    else
42    {
43      $ext = strtolower(get_extension($entry));
44      if (in_array($ext, array('php', 'tpl')))
45      {
46        $data[] = $path.$entry;
47      }
48    }
49  }
50 
51  closedir($handle);
52 
53  return $data;
54}
55
56/**
57 * list language files of a plugin
58 * @param: string $id, plugin id
59 * @return: array, keys are basenames, values are paths relative to plugin root
60 */
61function list_plugin_languages_files($id)
62{
63  $path = '/language/en_UK/';
64 
65  if (($handle = @opendir(PHPWG_PLUGINS_PATH.$id.$path)) === false)
66  {
67    return array();
68  }
69 
70  $data = array();
71 
72  while ($entry = readdir($handle))
73  {
74    if ($entry=='.' || $entry=='..' || $entry=='.svn' || $entry=='index.php') continue;
75   
76    if (!is_dir(PHPWG_PLUGINS_PATH.$id.$path.$entry))
77    {
78      $ext = strtolower(get_extension($entry));
79      if ($ext == 'php')
80      {
81        $data[ basename($entry, '.php') ] = $path.$entry;
82      }
83    }
84  }
85 
86  closedir($handle);
87 
88  return $data;
89}
90
91/**
92 * list translated strings of a file
93 * @param: string $path, file $path relative to main plugins folder
94 * @return: array, keys are language strings, values are arrays of lines
95 */
96function analyze_file($path)
97{
98  $lines = file(PHPWG_PLUGINS_PATH.$path, FILE_IGNORE_NEW_LINES);
99 
100  $strings = array();
101 
102  foreach ($lines as $i => $line)
103  {
104    // l10n
105    if (preg_match_all('#l10n\\(\s*["\']{1}(.*?)["\']{1}\s*[,)]{1}#', $line, $matches))
106    {
107      for ($j=0; $j<count($matches[1]); ++$j)
108      {
109        $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
110      }
111    }
112    // translate
113    if (preg_match_all('#\\{\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}\\|@?translate#', $line, $matches))
114    {
115      for ($j=0; $j<count($matches[1]); ++$j)
116      {
117        $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
118      }
119    }
120    // translate_dec
121    if (preg_match_all('#translate_dec:\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}:\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}}#', $line, $matches))
122    {
123      for ($j=0; $j<count($matches[1]); ++$j)
124      {
125        $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
126        $strings[ stripslashes($matches[2][$j]) ][] = $i+1;
127      }
128    }
129    // l10n_dec on one line
130    if (preg_match_all('#l10n_dec\\(\s*["\']{1}(.*?)["\']{1}\s*,\s*["\']{1}(.*?)["\']{1}#', $line, $matches))
131    {
132      for ($j=0; $j<count($matches[1]); ++$j)
133      {
134        $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
135        $strings[ stripslashes($matches[2][$j]) ][] = $i+1;
136      }
137    }
138    // l10n_dec on two or three lines
139    else if (strpos($line, 'l10n_dec')!==false)
140    {
141      $three_lines = $lines[$i];
142      if (isset($lines[$i+1]))
143      {
144        $three_lines.= ' '.$lines[$i+1];
145        if (isset($lines[$i+2])) $three_lines.= ' '.$lines[$i+2];
146      }
147     
148      if (preg_match_all('#l10n_dec\\(\s*["\']{1}(.*?)["\']{1}\s*,\s*["\']{1}(.*?)["\']{1}#', $three_lines, $matches))
149      {
150        for ($j=0; $j<count($matches[1]); ++$j)
151        {
152          $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
153          $strings[ stripslashes($matches[2][$j]) ][] = $i+1;
154        }
155      }
156    }
157    // l10n_args
158    if (preg_match_all('#get_l10n_args\\(\s*["\']{1}(.*?)["\']{1}\s*[,)]{1}#', $line, $matches))
159    {
160      for ($j=0; $j<count($matches[1]); ++$j)
161      {
162        $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
163      }
164    }
165  }
166 
167  return $strings;
168}
169
170/**
171 * get language files loaded in main.inc.php
172 * @param: string $id, plugin id
173 * @return: array of file basenames
174 */
175function get_loaded_in_main($id)
176{
177  $content = file_get_contents(PHPWG_PLUGINS_PATH.$id.'/main.inc.php');
178 
179  $files = array();
180 
181  if (preg_match_all('#load_language\((?:\s*)(?:["\']{1})(.*?)(?:["\']{1})#', $content, $matches))
182  {
183    $files = $matches[1];
184  }
185 
186  return $files;
187}
188
189/**
190 * load a language file
191 */
192function load_language_file($path)
193{
194  @include($path);
195  if (!isset($lang)) return array();
196  return $lang;
197}
198
199?>
Note: See TracBrowser for help on using the repository browser.