Ignore:
Timestamp:
Jun 30, 2014, 9:02:54 PM (10 years ago)
Author:
mistic100
Message:

improve display of files list

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/plugin_lang_analysis/include/functions.inc.php

    r28862 r28876  
    33
    44/**
    5  * list files of a plugin
    6  * @param: string $id, plugin id
    7  * @return: array of paths relative to plugin root
     5 * List files of a plugin
     6 * @param string $id, plugin id
     7 * @return nested array of paths relative to plugin root
     8 *    Keys are numeric for files or directory name
     9 *    Values are file name or array of more entries
    810 */
    911function list_plugin_files($id, $path=null)
     
    1618  if ($path == '/language/')
    1719  {
    18     return array();
     20    return null;
    1921  }
    2022 
    2123  if (strlen($path)-strrpos($path, '_data/') == 6)
    2224  {
    23     return array();
     25    return null;
    2426  }
    2527 
    2628  if (($handle = @opendir(PHPWG_PLUGINS_PATH.$id.$path)) === false)
    2729  {
    28     return array();
     30    return null;
    2931  }
    3032 
     
    3739    if (is_dir(PHPWG_PLUGINS_PATH.$id.$path.$entry))
    3840    {
    39       $data = array_merge($data, list_plugin_files($id, $path.$entry.'/'));
     41      $data[$entry.'/'] = list_plugin_files($id, $path.$entry.'/');
    4042    }
    4143    else
     
    4446      if (in_array($ext, array('php', 'tpl')))
    4547      {
    46         $data[] = $path.$entry;
     48        $data[] = $entry;
    4749      }
    4850    }
     
    5153  closedir($handle);
    5254 
    53   return $data;
     55  uksort($data, 'custom_folder_sort');
     56 
     57  return array_filter($data);
     58}
     59
     60/**
     61 * Merges the result of *list_plugin_files* and data from cache
     62 * Needs the result of *list_plugin_languages_files* and *get_loaded_in_main* in global scope
     63 *
     64 * @param array &$files
     65 * @param array $saved_files
     66 * @return nested array of files with metadata
     67 *    Keys are numeric for files or directory name
     68 *    Values are file metadata (filename, is_admin, ignore, lang_files) or array of more entries
     69 */
     70function populate_plugin_files(&$files, $saved_files, $root='/', $is_admin=false)
     71{
     72  global $language_files, $default_lang_files;
     73 
     74  foreach ($files as $id => &$file)
     75  {
     76    if (is_array($file))
     77    {
     78      populate_plugin_files($file,
     79        isset($saved_files[$id]) ? $saved_files[$id] : array(),
     80        $root.$id,
     81        strpos($id, 'admin') !== false || $is_admin
     82        );
     83    }
     84    else if (isset($saved_files[ $file ]))
     85    {
     86      $id = $file;
     87      $file = $saved_files[ $id ];
     88      $file['filename'] = $id;
     89      $file['lang_files'] = array_intersect($file['lang_files'], array_keys($language_files));
     90    }
     91    else
     92    {
     93      $id = $file;
     94      $file = array(
     95        'filename' => $id,
     96        'is_admin' => strpos($id, 'admin') !== false || $is_admin,
     97        'ignore' => false,
     98        'lang_files' => $default_lang_files,
     99        );
     100    }
     101  }
     102  unset($file);
     103}
     104
     105/**
     106 * Sanitize the result of config form for direct usage and cache
     107 * @param array &$files
     108 * @return nested array of files with metadata
     109 *    Keys are file name or directory name
     110 *    Values are file metadata (is_admin, ignore, lang_files) or array of more entries
     111 */
     112function clean_files_from_config(&$files)
     113{
     114  foreach ($files as $id => &$file)
     115  {
     116    if (!isset($file['ignore']))
     117    {
     118      clean_files_from_config($file);
     119    }
     120    // security against max_input_vars overflow
     121    else if (isset($file['is_admin']) && isset($file['ignore']) && isset($file['lang_files']))
     122    {
     123      $file['is_admin'] = get_boolean($file['is_admin']);
     124      $file['ignore'] = get_boolean($file['ignore']);
     125      $file['lang_files'] = array_keys(array_filter($file['lang_files'], 'get_boolean'));
     126    }
     127  }
     128  unset($file);
     129}
     130
     131/**
     132 * Custom sort callback for files and directories
     133 * Alphabetic order with files before directories
     134 */
     135function custom_folder_sort($a, $b)
     136{
     137  if (is_int($a) && is_int($b))
     138  {
     139    return $a-$b;
     140  }
     141  else if (is_string($a) && is_string($b))
     142  {
     143    return strnatcasecmp($a, $b);
     144  }
     145  else if (is_string($a) && is_int($b))
     146  {
     147    return 1;
     148  }
     149  else
     150  {
     151    return -1;
     152  }
    54153}
    55154
     
    76175    if (!is_dir(PHPWG_PLUGINS_PATH.$id.$path.$entry))
    77176    {
    78       $ext = strtolower(get_extension($entry));
    79       if ($ext == 'php')
     177      if (get_extension($entry) == 'php')
    80178      {
    81179        $data[ basename($entry, '.php') ] = $path.$entry;
     
    87185 
    88186  return $data;
     187}
     188
     189/**
     190 * Construct the list of all used strings in the plugin files
     191 * @param string $plugin
     192 * @param array $files
     193 * @return array multidimensional
     194 */
     195function analyze_files($plugin, $files, &$strings = array(), $path='')
     196{
     197  foreach ($files as $id => $file)
     198  {
     199    if (!isset($file['ignore']))
     200    {
     201      analyze_files($plugin, $file, $strings, $path.$id);
     202    }
     203    else
     204    {
     205      if ($file['ignore']) continue;
     206
     207      $file_strings = analyze_file($plugin.'/'.$path.$id);
     208     
     209      foreach ($file_strings as $string => $lines)
     210      {
     211        $strings[ $string ]['files'][ $path.$id ] = $file + array('lines' => $lines);
     212      }
     213    }
     214  }
     215 
     216  return $strings;
    89217}
    90218
     
    111239    }
    112240    // translate
    113     if (preg_match_all('#\\{\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}\\|@?translate#', $line, $matches))
     241    if (preg_match_all('#\\{\\\\?["\']{1}(.*?)\\\\?["\']{1}\\|@?translate#', $line, $matches))
    114242    {
    115243      for ($j=0; $j<count($matches[1]); ++$j)
     
    119247    }
    120248    // translate_dec
    121     if (preg_match_all('#translate_dec:\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}:\\\\{0,1}["\']{1}(.*?)\\\\{0,1}["\']{1}}#', $line, $matches))
     249    if (preg_match_all('#translate_dec:\\\\?["\']{1}(.*?)\\\\?["\']{1}:\\\\?["\']{1}(.*?)\\\\?["\']{1}}#', $line, $matches))
    122250    {
    123251      for ($j=0; $j<count($matches[1]); ++$j)
     
    179307  $files = array();
    180308 
    181   if (preg_match_all('#load_language\((?:\s*)(?:["\']{1})(.*?)(?:["\']{1})#', $content, $matches))
     309  if (preg_match_all('#load_language\\(\s*["\']{1}(.*?)["\']{1}#', $content, $matches))
    182310  {
    183311    $files = $matches[1];
     
    196324  return $lang;
    197325}
    198 
    199 ?>
Note: See TracChangeset for help on using the changeset viewer.