PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin_cssUtils/cssUtilSrvr.php

https://bitbucket.org/mstetson/obiblio-10-wip
PHP | 136 lines | 119 code | 7 blank | 10 comment | 51 complexity | 5302a3282c0eb361ec20e08ac2af4b7e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
  3. * See the file COPYRIGHT.html for more details.
  4. */
  5. require_once("../shared/common.php");
  6. //print_r($_REQUEST);echo "<br />";
  7. function getClassList() {
  8. $lines = file("../themes/default/style.css");
  9. $classes = [];
  10. foreach ($lines as $lineNum => $line) {
  11. preg_match('/((\D|\A)\.)(\w+)([\s,])/', $line,$grp);
  12. if (isset($grp[2])) {
  13. $classes[($lineNum)+1] = $grp[3];
  14. }
  15. }
  16. return $classes;
  17. }
  18. function moduleList () {
  19. $handl = opendir("..");
  20. while (false !== ($file = readdir($handl))) {
  21. if ($file != '.' && $file != '..') {
  22. if ((is_dir('../'.$file)) &&
  23. (substr($file,0,1) != '.') &&
  24. ($file != 'images') &&
  25. ($file != 'font') &&
  26. ($file != 'photos')
  27. ) {
  28. $mods[] = $file;
  29. }
  30. }
  31. }
  32. closedir($handl);
  33. sort($mods,SORT_LOCALE_STRING);
  34. return $mods;
  35. };
  36. function array_flat($array) {
  37. $tmp = [];
  38. foreach($array as $a) {
  39. if(is_array($a)) {
  40. $tmp = array_merge($tmp, array_flat($a));
  41. } else {
  42. $tmp[] = $a;
  43. }
  44. }
  45. return $tmp;
  46. }
  47. function getFileList($dir) {
  48. $files = array();
  49. if ($handle = opendir($dir)) {
  50. while (false !== ($file = readdir($handle))) {
  51. if ($file != "." && $file != ".." &&
  52. $file != 'sql' &&
  53. $file != 'legacy' &&
  54. $file != 'defs' &&
  55. $file != 'themes') {
  56. if(is_dir($dir.'/'.$file)) {
  57. $dir2 = $dir.'/'.$file;
  58. $files[] = getFileList($dir2);
  59. } else {
  60. $aFile = $dir.'/'.$file;
  61. $files[] = $aFile;
  62. }
  63. }
  64. }
  65. closedir($handle);
  66. }
  67. return array_flat($files);
  68. }
  69. switch ($_REQUEST['mode']){
  70. case 'ck4CssUnused':
  71. ## collct all 'class' selectors in CSS file
  72. $classes = getClassList();
  73. echo "there are ".count($classes)." class selectors in the CSS file.";
  74. ## create array of classes with empty values;
  75. foreach ($classes as $lineNmbr=>$className) {
  76. $cList[$className] = $lineNmbr;
  77. }
  78. $modules = moduleList();
  79. $found = [];
  80. foreach ($modules as $module) {
  81. $files = getFileList('../'.$module);
  82. foreach ($files as $file) {
  83. ## capture file suffixfor later use
  84. $ext = substr($file,-6);
  85. $lines = file("../$module/$file");
  86. foreach ($lines as $lineNum => $line) {
  87. if ($ext != 'Js.php') {
  88. preg_match_all('/(class=")(\w(?<!\d)[\w\'-]*)(["])/',$line,$out, PREG_PATTERN_ORDER);
  89. } else {
  90. preg_match_all('/(\$\([\"\']\.)(.*?)([\"\'])/',$line,$out, PREG_PATTERN_ORDER);
  91. //print_r($out);echo"<br />";
  92. }
  93. foreach ($out[2] as $key) {
  94. if ((substr($key,-3) == 'Btn') || (substr($key,-4) == 'Btns')) continue;
  95. if ((substr($key,0,3) == "'.$") || (substr($key,0,3) == "'.H")) continue;
  96. if (isset($found[$key])) {
  97. continue;
  98. } elseif (isset($cList[$key])) {
  99. $found[$key] = array('note'=>'Used');
  100. } else {
  101. //echo "$key<br />";
  102. $found[$key] = array('note'=>'Missing','file'=>$file,'lineNum'=>($lineNum+1));
  103. }
  104. }
  105. }
  106. }
  107. }
  108. if (count($cList) == count($found)) {
  109. echo T("All css class entries are being used.");
  110. }
  111. echo "<p>The following are not being used and may possibly be removed.</p>";
  112. foreach ($cList as $className=>$lineNmbr) {
  113. if ($found[$className]['note'] == 'used') {
  114. continue;
  115. } else {
  116. echo "$className on $lineNmbr<br />";
  117. }
  118. }
  119. echo "<p>The following classes assigned to html elements are not defined in the CSS file.</p>";
  120. foreach ($found as $className=>$data) {
  121. if ($data['note'] == 'Missing') echo "$className at line ".$data['lineNum']." of file ".$data['file']."<br />";
  122. }
  123. //print_r($found);echo "<br />";
  124. break;
  125. default:
  126. echo "<h4>".T("invalid mode").": &gt;$_REQUEST[mode]&lt;</h4><br />";
  127. break;
  128. }
  129. ?>