PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/translations/prune.php

https://bitbucket.org/valmy/openx
PHP | 199 lines | 134 code | 21 blank | 44 comment | 53 complexity | da07f9e4de1e6af83b9f3f35a02bf279 MD5 | raw file
  1. #!/usr/bin/php -q
  2. <?php
  3. if (function_exists ("DebugBreak")) {
  4. DebugBreak ();
  5. }
  6. /*
  7. +---------------------------------------------------------------------------+
  8. | OpenX v2.8 |
  9. | ========== |
  10. | |
  11. | Copyright (c) 2003-2009 OpenX Limited |
  12. | For contact details, see: http://www.openx.org/ |
  13. | |
  14. | This program is free software; you can redistribute it and/or modify |
  15. | it under the terms of the GNU General Public License as published by |
  16. | the Free Software Foundation; either version 2 of the License, or |
  17. | (at your option) any later version. |
  18. | |
  19. | This program is distributed in the hope that it will be useful, |
  20. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  21. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  22. | GNU General Public License for more details. |
  23. | |
  24. | You should have received a copy of the GNU General Public License |
  25. | along with this program; if not, write to the Free Software |
  26. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  27. +---------------------------------------------------------------------------+
  28. $Id$
  29. */
  30. // Require the initialisation file
  31. // Done this way so that it works in CLI PHP
  32. $path = dirname(__FILE__);
  33. require_once $path .'/../../init.php';
  34. // Set longer time out, and ignore user abort
  35. if (!ini_get('safe_mode')) {
  36. @set_time_limit($conf['maintenance']['timeLimitScripts']);
  37. @ignore_user_abort(true);
  38. }
  39. $langFilePath = MAX_PATH . '/lib/max/language/en';
  40. $staleTransFile = MAX_PATH . '/var/stale.trans.log';
  41. $allTransFile = MAX_PATH . '/var/all.trans.log';
  42. $existingTransFile = MAX_PATH . '/var/existing.trans.log';
  43. $aFilterPattern = array('/^phpAds_hlp_/', '/^str/');
  44. $transSourceVar = $GLOBALS;
  45. // load all master translation files
  46. $result = loadLangFiles($langFilePath);
  47. // filter translations
  48. $aTranslation = $aOrigTrans = filterTranslations($aFilterPattern, $transSourceVar);
  49. // write list of all translation keys to file
  50. $result = writeTranslationToFile($allTransFile, $aTranslation);
  51. // finds stale translation in code base
  52. $aStaleKey = findStaleTranslations(MAX_PATH, $aTranslation);
  53. // create report of remaining translation keys that are not in the code base
  54. $result = writeTranslationToFile($staleTransFile, $aTranslation);
  55. // create report of transaltion that exist in code base
  56. $aResult = array_diff($aOrigTrans, $aTranslation);
  57. $result = writeTranslationToFile($existingTransFile, $aResult);
  58. function array_preg($pattern, $haystack)
  59. {
  60. foreach ($haystack as $key => $value) {
  61. if (preg_match($pattern, $key)) {
  62. $aMatches[] = $key;
  63. }
  64. }
  65. return $aMatches;
  66. }
  67. function filterTranslations($aFilterPattern, $aTranslation)
  68. {
  69. if (!is_array($aFilterPattern) || !is_array($aTranslation)) {
  70. (!is_array($aFilterPattern))
  71. ? die('$aFilterPattern must be an array')
  72. : die('$aTranslation must be an array');
  73. } else {
  74. $aResult = array();
  75. foreach ($aFilterPattern as $pattern) {
  76. $aFilteredResult = array_preg($pattern, $aTranslation);
  77. $aResult = array_merge($aResult, $aFilteredResult);
  78. }
  79. }
  80. return $aResult;
  81. }
  82. function findStaleTranslations($dir, &$aTranslation)
  83. {
  84. if (is_dir($dir) && is_array($aTranslation)) {
  85. // load list of directories
  86. $oDir = dir($dir);
  87. // interate through directories
  88. while (($file = $oDir->read()) !== false) {
  89. if ($file != "." && $file != ".." && $file != ".svn" && !strstr($file, '.lang.php')) {
  90. $file = $oDir->path .'/'. $file;
  91. if (is_dir($file) && $file != $oDir->path .'/pear' && $file != $oDir->path .'/var') {
  92. // interate through subdirectories and files
  93. findStaleTranslations($file, $aTranslation);
  94. } else if (is_readable($file)) {
  95. $aDisallowed = array(
  96. '.jpg', '.png', '.gif', '.sh', '.sql', '.crt',
  97. '.xml', '.bin');
  98. $fileExt = substr($file, strrpos($file, '.'));
  99. if (!in_array($fileExt, $aDisallowed)) {
  100. // parse current file for use of translation keys
  101. findTranslations($file, $aTranslation);
  102. }
  103. }
  104. }
  105. }
  106. } else if (!is_dir($dir)) {
  107. die('Specified directory does not exist: '. $dir);
  108. } else {
  109. die('$aTranslation must be an array');
  110. }
  111. return true;
  112. }
  113. function findTranslations($file, &$aTranslation)
  114. {
  115. if (!is_array($aTranslation)) {
  116. die('$aTranslation must be an array');
  117. }
  118. if (file_exists($file) && $fp = fopen($file, 'r')) {
  119. // load file into variable
  120. $data = '';
  121. while (!feof($fp)) {
  122. $data .= fread($fp, 2048);
  123. }
  124. // parse file for all remaining translation keys
  125. foreach ($aTranslation as $key => $value) {
  126. // check if key is in file
  127. if (strstr($data, $value)) {
  128. // remove transaltion from array of translation keys
  129. unset($aTranslation[$key]);
  130. }
  131. }
  132. fclose($fp);
  133. } else if (!file_exists($file)) {
  134. die ('File does not exits: '. $file);
  135. } else {
  136. die ('Unable to open file: '. $file);
  137. }
  138. return true;
  139. }
  140. function loadLangFiles($dir)
  141. {
  142. // retrieve listing of all files in directory
  143. if (is_dir($dir) && $oDir = dir($dir)) {
  144. while (($file = $oDir->read()) !== false) {
  145. // ensure it's a file
  146. if (!is_dir($oDir->path . '/'. $file) && !($file == '.' || $file == '..')) {
  147. include_once $oDir->path . '/'. $file;
  148. }
  149. }
  150. $oDir->close();
  151. } else if (!is_dir($dir)) {
  152. die('Specified directory is not a directory: '. $dir);
  153. } else {
  154. die('Unable to access directory: '. $dir);
  155. }
  156. return true;
  157. }
  158. function writeTranslationToFile($file, $aTranslation)
  159. {
  160. if (!is_array($aTranslation)) {
  161. die('$aTranslation must be an array');
  162. }
  163. if (!file_exists($file) && $fp = fopen($file, 'x+')) {
  164. $translation = implode("\n", $aTranslation);
  165. $str = 'Total Translation: '. count($aTranslation) ."\n\n";
  166. while (fwrite($fp, $str) === false) {
  167. die('Unable to write to file: '. $file);
  168. }
  169. while (fwrite($fp, $translation) === false) {
  170. die('Unable to write to file: '. $file);
  171. }
  172. fclose($fp);
  173. } else if (file_exists($file)) {
  174. die('File already exists: '. $file);
  175. }
  176. return true;
  177. }
  178. ?>