/doc/devtools/prefreport.php

https://gitlab.com/ElvisAns/tiki · PHP · 218 lines · 151 code · 31 blank · 36 comment · 8 complexity · 0e3671642621165431e9642f992c89f8 MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. // Usage: From the command line:
  8. // php doc/devtools/prefreport.php > prefreport.csv
  9. //
  10. // also check out doc/devtools/securitycheck.php to see in which files are
  11. // used each pref (and permission name too)
  12. //
  13. require_once 'tiki-setup.php';
  14. $prefslib = TikiLib::lib('prefs');
  15. $defaultValues = get_default_prefs();
  16. $fields = [
  17. 'preference' => '',
  18. 'hard_to_search' => false,
  19. 'duplicate_name' => 0,
  20. 'duplicate_description' => 0,
  21. 'word_count' => 0,
  22. 'filter' => '',
  23. 'name' => '',
  24. 'help' => '',
  25. 'default' => '',
  26. 'description' => '',
  27. 'locations' => '',
  28. 'dependencies' => '',
  29. 'type' => '',
  30. 'options' => '',
  31. 'admin' => '',
  32. 'module' => '',
  33. 'view' => '',
  34. 'permission' => '',
  35. 'plugin' => '',
  36. 'extensions' => '',
  37. 'tags' => '',
  38. 'parameters' => '',
  39. 'detail' => '',
  40. 'warning' => '',
  41. 'hint' => '',
  42. 'shorthint' => '',
  43. 'perspective' => '',
  44. 'separator' => '',
  45. ];
  46. $stopWords = ['', 'in', 'and', 'a', 'to', 'be', 'of', 'on', 'the', 'for', 'as', 'it', 'or', 'with', 'by', 'is', 'an'];
  47. $data = [];
  48. error_reporting(E_ALL);
  49. ini_set('display_errors', 'on');
  50. $data = collect_raw_data($fields);
  51. remove_fake_descriptions($data);
  52. set_default_values($data, $defaultValues);
  53. collect_locations($data);
  54. $index = [
  55. 'name' => index_data($data, 'name'),
  56. 'description' => index_data($data, 'description'),
  57. ];
  58. update_search_flag($data, $index, $stopWords);
  59. // Output results
  60. fputcsv(STDOUT, array_keys($fields));
  61. foreach ($data as $values) {
  62. fputcsv(STDOUT, array_values($values));
  63. }
  64. /**
  65. * @param $fields
  66. * @return array
  67. */
  68. function collect_raw_data($fields)
  69. {
  70. $data = [];
  71. foreach (glob('lib/prefs/*.php') as $file) {
  72. $name = substr(basename($file), 0, -4);
  73. $function = "prefs_{$name}_list";
  74. if ($name == 'index') {
  75. continue;
  76. }
  77. include $file;
  78. $list = $function();
  79. foreach ($list as $name => $raw) {
  80. $entry = $fields;
  81. $entry['preference'] = $name;
  82. $entry['name'] = isset($raw['name']) ? $raw['name'] : '';
  83. $entry['description'] = isset($raw['description']) ? $raw['description'] : '';
  84. $entry['filter'] = isset($raw['filter']) ? $raw['filter'] : '';
  85. $entry['help'] = isset($raw['help']) ? $raw['help'] : '';
  86. $entry['dependencies'] = ! empty($raw['dependencies']) ? implode(',', (array) $raw['dependencies']) : '';
  87. $entry['type'] = isset($raw['type']) ? $raw['type'] : '';
  88. $entry['options'] = isset($raw['options']) ? implode(',', $raw['options']) : '';
  89. $entry['admin'] = isset($raw['admin']) ? $raw['admin'] : '';
  90. $entry['module'] = isset($raw['module']) ? $raw['module'] : '';
  91. $entry['view'] = isset($raw['view']) ? $raw['view'] : '';
  92. $entry['permission'] = isset($raw['permission']) ? implode(',', $raw['permission']) : '';
  93. $entry['plugin'] = isset($raw['plugin']) ? $raw['plugin'] : '';
  94. $entry['extensions'] = isset($raw['extensions']) ? implode(',', $raw['extensions']) : '';
  95. $entry['tags'] = isset($raw['tags']) ? implode(',', $raw['tags']) : '';
  96. $entry['parameters'] = isset($raw['parameters']) ? implode(',', $raw['parameters']) : '';
  97. $entry['detail'] = isset($raw['detail']) ? $raw['detail'] : '';
  98. $entry['warning'] = isset($raw['warning']) ? $raw['warning'] : '';
  99. $entry['hint'] = isset($raw['hint']) ? $raw['hint'] : '';
  100. $entry['shorthint'] = isset($raw['shorthint']) ? $raw['shorthint'] : '';
  101. $entry['perspective'] = isset($raw['perspective']) ? $raw['perspective'] ? 'true' : 'false' : '';
  102. $entry['separator'] = isset($raw['separator']) ? $raw['separator'] : '';
  103. $data[] = $entry;
  104. }
  105. }
  106. return $data;
  107. }
  108. /**
  109. * @param $data
  110. */
  111. function remove_fake_descriptions(&$data)
  112. {
  113. foreach ($data as & $row) {
  114. if ($row['name'] == $row['description']) {
  115. $row['description'] = '';
  116. }
  117. }
  118. }
  119. /**
  120. * @param $data
  121. * @param $prefs
  122. */
  123. function set_default_values(&$data, $prefs)
  124. {
  125. foreach ($data as & $row) {
  126. $row['default'] = isset($prefs[$row['preference']]) ? $prefs[$row['preference']] : '';
  127. if (is_array($row['default'])) {
  128. $row['default'] = implode($row['separator'], $row['default']);
  129. }
  130. }
  131. }
  132. /**
  133. * @param $data
  134. * @param $field
  135. * @return array
  136. */
  137. function index_data($data, $field)
  138. {
  139. $index = [];
  140. foreach ($data as $row) {
  141. $value = strtolower($row[$field]);
  142. if (! isset($index[$value])) {
  143. $index[$value] = 0;
  144. }
  145. $index[$value]++;
  146. }
  147. return $index;
  148. }
  149. /**
  150. * @param $data
  151. */
  152. function collect_locations(&$data)
  153. {
  154. $prefslib = TikiLib::lib('prefs');
  155. foreach ($data as & $row) {
  156. $pages = $prefslib->getPreferenceLocations($row['preference']);
  157. foreach ($pages as & $page) {
  158. $page = $page[0] . '/' . $page[1];
  159. }
  160. $row['locations'] = implode(', ', $pages);
  161. }
  162. }
  163. /**
  164. * @param $data
  165. * @param $index
  166. * @param $stopWords
  167. */
  168. function update_search_flag(&$data, $index, $stopWords)
  169. {
  170. foreach ($data as & $row) {
  171. $name = strtolower($row['name']);
  172. $description = strtolower($row['description']);
  173. $words = array_diff(explode(' ', $name . ' ' . $description), $stopWords);
  174. $row['duplicate_name'] = $index['name'][$name];
  175. if (! empty($description)) {
  176. $row['duplicate_description'] = $index['description'][$description];
  177. }
  178. $row['word_count'] = count($words);
  179. if (count($words) < 5) {
  180. $row['hard_to_search'] = 'X';
  181. } elseif ($index['name'][$name] > 2) {
  182. $row['hard_to_search'] = 'X';
  183. } elseif ($index['description'][$description] > 2) {
  184. $row['hard_to_search'] = 'X';
  185. }
  186. }
  187. }