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

Last change on this file since 23467 was 23467, checked in by mistic100, 11 years ago

allow multiple language files in a plugin

File size: 4.1 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*)\)#', $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('#\{(?:["\']{1})(.*?)(?:["\']{1})\|(?:@{0,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    // l10n_dec on one line
121    if (preg_match_all('#l10n_dec\((?:\s*)(?:["\']{1})(.*?)(?:["\']{1})(?:\s*),(?:\s*)(?:["\']{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 two or three lines
130    else if (strpos($line, 'l10n_dec')!==false)
131    {
132      $three_lines = $lines[$i];
133      if (isset($lines[$i+1]))
134      {
135        $three_lines.= ' '.$lines[$i+1];
136        if (isset($lines[$i+2])) $three_lines.= ' '.$lines[$i+2];
137      }
138     
139      if (preg_match_all('#l10n_dec\((?:\s*)(?:["\']{1})(.*?)(?:["\']{1})(?:\s*),(?:\s*)(?:["\']{1})(.*?)(?:["\']{1})#', $three_lines, $matches))
140      {
141        for ($j=0; $j<count($matches[1]); ++$j)
142        {
143          $strings[ stripslashes($matches[1][$j]) ][] = $i+1;
144          $strings[ stripslashes($matches[2][$j]) ][] = $i+1;
145        }
146      }
147    }
148  }
149 
150  return $strings;
151}
152
153/**
154 * get language files loaded in main.inc.php
155 * @param: string $id, plugin id
156 * @return: array of file basenames
157 */
158function get_loaded_in_main($id)
159{
160  $content = file_get_contents(PHPWG_PLUGINS_PATH.$id.'/main.inc.php');
161 
162  $files = array();
163 
164  if (preg_match_all('#load_language\((?:\s*)(?:["\']{1})(.*?)(?:["\']{1})#', $content, $matches))
165  {
166    $files = $matches[1];
167  }
168 
169  return $files;
170}
171
172/**
173 * load a language file
174 */
175function load_language_file($path)
176{
177  @include($path);
178  if (!isset($lang)) return array();
179  return $lang;
180}
181
182?>
Note: See TracBrowser for help on using the repository browser.