/ojs/ojs-2.1.1/classes/help/Help.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite · PHP · 252 lines · 155 code · 29 blank · 68 comment · 31 complexity · 2d21efaa7d1ed399440d8434d02bc3ce MD5 · raw file

  1. <?php
  2. /**
  3. * Help.inc.php
  4. *
  5. * Copyright (c) 2003-2006 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package help
  9. *
  10. * Provides methods for translating help topic keys to their respected topic help id
  11. *
  12. * $Id: Help.inc.php,v 1.7 2006/06/12 23:25:51 alec Exp $
  13. */
  14. class Help {
  15. /**
  16. * Constructor.
  17. */
  18. function Help() {
  19. }
  20. /**
  21. * Get the locale to display help files in.
  22. * If help isn't available for the current locale,
  23. * defaults to en_US.
  24. */
  25. function getLocale() {
  26. $locale = Locale::getLocale();
  27. if (!file_exists("help/$locale/.")) {
  28. return 'en_US';
  29. }
  30. return $locale;
  31. }
  32. /**
  33. * Translate a help topic key to its numerical id.
  34. * @param $key string
  35. * @return string
  36. */
  37. function translate($key) {
  38. $key = trim($key);
  39. if (empty($key)) {
  40. return '';
  41. }
  42. $cache =& Help::_getMappingCache();
  43. $value = $cache->get($key);
  44. if (!isset($value)) {
  45. return '##' . $key . '##';
  46. }
  47. return $value;
  48. }
  49. function &_getMappingCache() {
  50. static $cache;
  51. if (!isset($cache)) {
  52. import('cache.CacheManager');
  53. $cacheManager =& CacheManager::getManager();
  54. $cache =& $cacheManager->getFileCache(
  55. 'help', 'mapping',
  56. array('Help', '_mappingCacheMiss')
  57. );
  58. // Check to see if the cache info is outdated.
  59. $cacheTime = $cache->getCacheTime();
  60. if ($cacheTime !== null && $cacheTime < filemtime(Help::getMappingFilename())) {
  61. // The cached data is out of date.
  62. $cache->flush();
  63. }
  64. }
  65. return $cache;
  66. }
  67. function &_getTocCache() {
  68. static $cache;
  69. if (!isset($cache)) {
  70. import('cache.CacheManager');
  71. $cacheManager =& CacheManager::getManager();
  72. $cache =& $cacheManager->getFileCache(
  73. 'help', 'toc',
  74. array('Help', '_tocCacheMiss')
  75. );
  76. // Check to see if the cache info is outdated.
  77. $cacheTime = $cache->getCacheTime();
  78. if ($cacheTime !== null && $cacheTime < Help::dirmtime('help/'.Help::getLocale().'/.', true)) {
  79. // The cached data is out of date.
  80. $cache->flush();
  81. }
  82. }
  83. return $cache;
  84. }
  85. function _mappingCacheMiss(&$cache, $id) {
  86. // Keep a secondary cache of the mappings so that a few
  87. // cache misses won't destroy the server
  88. static $mappings;
  89. if (!isset($mappings)) {
  90. $mappings =& Help::loadHelpMappings();
  91. $cache->setEntireCache($mappings);
  92. }
  93. return isset($mappings[$id])?$mappings[$id]:null;
  94. }
  95. function _tocCacheMiss(&$cache, $id) {
  96. // Keep a secondary cache of the TOC so that a few
  97. // cache misses won't destroy the server
  98. static $toc;
  99. if (!isset($toc)) {
  100. $helpToc = array();
  101. $topicId = 'index/topic/000000';
  102. $helpToc = Help::buildTopicSection($topicId);
  103. $toc =& Help::buildToc($helpToc);
  104. $cache->setEntireCache($toc);
  105. }
  106. return null;
  107. }
  108. function getMappingFilename() {
  109. return 'help/help.xml'; break;
  110. }
  111. /**
  112. * Load mappings of help page keys and their ids from an XML file
  113. * @return array associative array of page keys and ids
  114. */
  115. function &loadHelpMappings() {
  116. $mappings = array();
  117. // Reload help XML file
  118. $xmlDao = &new XMLDAO();
  119. $data = $xmlDao->parseStruct(Help::getMappingFilename(), array('topic'));
  120. // Build associative array of page keys and ids
  121. if (isset($data['topic'])) {
  122. foreach ($data['topic'] as $helpData) {
  123. $mappings[$helpData['attributes']['key']] = $helpData['attributes']['id'];
  124. }
  125. }
  126. return $mappings;
  127. }
  128. /**
  129. * Load table of contents from xml help topics and their tocs
  130. * (return cache, if available)
  131. * @return array associative array of topics and subtopics
  132. */
  133. function &getTableOfContents() {
  134. $cache =& Help::_getTocCache();
  135. return $cache->getContents();
  136. }
  137. /**
  138. * Modifies retrieved array of topics and arranges them into toc
  139. * @param $helpToc array
  140. * @return array
  141. */
  142. function &buildToc($helpToc) {
  143. $toc = array();
  144. foreach($helpToc as $topicId => $section) {
  145. $toc[$topicId] = array('title' => $section['title'], 'prefix' => '');
  146. Help::buildTocHelper($toc, $section['section'], '');
  147. }
  148. return $toc;
  149. }
  150. /**
  151. * Helper method for buildToc
  152. * @param $helpToc array
  153. * @param $section array
  154. * @param $num numbering of topic
  155. */
  156. function buildTocHelper(&$toc, $section, $prefix) {
  157. if (isset($section)) {
  158. $prefix = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$prefix";
  159. foreach($section as $topicId => $sect) {
  160. $toc[$topicId] = array('title' => $sect['title'], 'prefix' => $prefix);
  161. Help::buildTocHelper($toc, $sect['section'], $prefix);
  162. }
  163. }
  164. }
  165. /**
  166. * Helper method for getTableOfContents
  167. * @param $topicId int
  168. * @param $prevTocId int
  169. * @return array
  170. */
  171. function buildTopicSection($topicId, $prevTocId = null) {
  172. $topicDao = &DAORegistry::getDAO('HelpTopicDAO');
  173. $topic = $topicDao->getTopic($topicId);
  174. if ($topicId == 'index/topic/000000') {
  175. $tocId = $topic->getTocId();
  176. } else {
  177. $tocId = $topic->getSubTocId();
  178. }
  179. $section = array();
  180. if ($tocId && $tocId != $prevTocId) {
  181. $tocDao = &DAORegistry::getDAO('HelpTocDAO');
  182. $toc = $tocDao->getToc($tocId);
  183. $topics = $toc->getTopics();
  184. foreach($topics as $currTopic) {
  185. $currId = $currTopic->getId();
  186. $currTitle = $currTopic->getTitle();
  187. if ($currId != $topicId) {
  188. $section[$currId] = array('title' => $currTitle, 'section' => Help::buildTopicSection($currId, $tocId));
  189. }
  190. }
  191. }
  192. if (empty($section)) {
  193. $section = null;
  194. }
  195. return $section;
  196. }
  197. /**
  198. * Returns the most recent modified file in the specified directory
  199. * Taken from the php.net site under filemtime
  200. * @param $dirName string
  201. * @param $doRecursive bool
  202. * @return int
  203. */
  204. function dirmtime($dirName,$doRecursive) {
  205. $d = dir($dirName);
  206. $lastModified = 0;
  207. while($entry = $d->read()) {
  208. if ($entry != "." && $entry != "..") {
  209. if (!is_dir($dirName."/".$entry)) {
  210. $currentModified = filemtime($dirName."/".$entry);
  211. } else if ($doRecursive && is_dir($dirName."/".$entry)) {
  212. $currentModified = Help::dirmtime($dirName."/".$entry,true);
  213. }
  214. if ($currentModified > $lastModified){
  215. $lastModified = $currentModified;
  216. }
  217. }
  218. }
  219. $d->close();
  220. return $lastModified;
  221. }
  222. }
  223. ?>