00001 <?php
00002
00016
00017
00018
00019 import('help.HelpTopic');
00020
00021 class HelpTopicDAO extends XMLDAO {
00022 function &_getCache($topicId) {
00023 static $cache;
00024 $locale = Help::getLocale();
00025 if (!isset($cache[$locale][$topicId])) {
00026 import('cache.CacheManager');
00027 $help =& Help::getHelp();
00028 $cacheManager =& CacheManager::getManager();
00029 $cache[$locale][$topicId] = $cacheManager->getFileCache('help-topic-' . $locale, $topicId, array($this, '_cacheMiss'));
00030
00031
00032 $cacheTime = $cache[$locale][$topicId]->getCacheTime();
00033 if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename($topicId))) {
00034
00035 $cache[$locale][$topicId]->flush();
00036 }
00037 }
00038 return $cache[$locale][$topicId];
00039 }
00040
00041 function &getMappingFile($topicId) {
00042 $help =& Help::getHelp();
00043 $mappingFiles =& $help->getMappingFiles();
00044
00045 for ($i = 0; $i < count($mappingFiles); $i++) {
00046
00047 $mappingFile =& $mappingFiles[$i];
00048 if ($mappingFile->containsTopic($topicId)) return $mappingFile;
00049 unset($mappingFile);
00050 }
00051 $returner = null;
00052 return $returner;
00053 }
00054
00055 function getFilename($topicId) {
00056 $mappingFile =& $this->getMappingFile($topicId);
00057 return $mappingFile?$mappingFile->getTopicFilename($topicId):null;
00058 }
00059
00060 function _cacheMiss(&$cache, $id) {
00061 static $data;
00062 if (!isset($data)) {
00063 $helpFile = $this->getFilename($cache->getCacheId());
00064 $data = &$this->parseStruct($helpFile);
00065
00066
00067 if ($data === false) {
00068 $returner = false;
00069 return $returner;
00070 }
00071 $cache->setEntireCache($data);
00072 }
00073 return null;
00074 }
00075
00081 function &getTopic($topicId) {
00082 $cache =& $this->_getCache($topicId);
00083 $data = $cache->getContents();
00084
00085
00086 if (!is_array($data)) {
00087 $returner = false;
00088 return $returner;
00089 }
00090
00091 $topic = &new HelpTopic();
00092
00093 $topic->setId($data['topic'][0]['attributes']['id']);
00094 $topic->setTitle($data['topic'][0]['attributes']['title']);
00095 $topic->setTocId($data['topic'][0]['attributes']['toc']);
00096 if (isset($data['topic'][0]['attributes']['subtoc'])) {
00097 $topic->setSubTocId($data['topic'][0]['attributes']['subtoc']);
00098 }
00099
00100 if (isset($data['section'])) {
00101 foreach ($data['section'] as $sectionData) {
00102 $section = &new HelpTopicSection();
00103 $section->setTitle(isset($sectionData['attributes']['title']) ? $sectionData['attributes']['title'] : null);
00104 $section->setContent($sectionData['value']);
00105 $topic->addSection($section);
00106 }
00107 }
00108
00109 if (isset($data['related_topic'])) {
00110 foreach ($data['related_topic'] as $relatedTopic) {
00111 $relatedTopicArray = array('id' => $relatedTopic['attributes']['id'], 'title' => $relatedTopic['attributes']['title']);
00112 $topic->addRelatedTopic($relatedTopicArray);
00113 }
00114 }
00115
00116 return $topic;
00117 }
00118
00124 function &getTopicsByKeyword($keyword) {
00125 $keyword = String::strtolower($keyword);
00126 $matchingTopics = array();
00127 $help =& Help::getHelp();
00128 foreach ($help->getSearchPaths() as $searchPath => $mappingFile) {
00129 $dir = opendir($searchPath);
00130 while (($file = readdir($dir)) !== false) {
00131 $currFile = $searchPath . DIRECTORY_SEPARATOR . $file;
00132 if (is_dir($currFile) && $file != 'toc' && $file != '.' && $file != '..') {
00133 HelpTopicDAO::searchDirectory($mappingFile, $matchingTopics,$keyword,$currFile);
00134 }
00135 }
00136 closedir($dir);
00137 }
00138
00139 krsort($matchingTopics);
00140 $topics = array_values($matchingTopics);
00141
00142 return $topics;
00143 }
00144
00153 function searchDirectory(&$mappingFile, &$matchingTopics,$keyword,$dir) {
00154 $currDir = opendir($dir);
00155 while (($file = readdir($currDir)) !== false) {
00156 $currFile = sprintf('%s/%s',$dir,$file);
00157 if (is_dir($currFile) && $file != '.' && $file != '..' && $file != 'toc') {
00158 HelpTopicDAO::searchDirectory($mappingFile, $matchingTopics,$keyword,$currFile);
00159 } else {
00160 HelpTopicDAO::scanTopic($mappingFile, $matchingTopics,$keyword,$dir,$file);
00161 }
00162 }
00163 closedir($currDir);
00164 }
00165
00175 function scanTopic(&$mappingFile, &$matchingTopics,$keyword,$dir,$file) {
00176 if (preg_match('/^\d{6,6}\.xml$/', $file)) {
00177 $topicId = $mappingFile->getTopicIdForFilename($dir . DIRECTORY_SEPARATOR . $file);
00178 $topic = &$this->getTopic($topicId);
00179
00180 if ($topic) {
00181 $numMatches = String::substr_count(String::strtolower($topic->getTitle()), $keyword);
00182
00183 foreach ($topic->getSections() as $section) {
00184 $numMatches += String::substr_count(String::strtolower($section->getTitle()), $keyword);
00185 $numMatches += String::substr_count(String::strtolower($section->getContent()), $keyword);
00186 }
00187
00188 if ($numMatches > 0) {
00189 $matchingTopics[($numMatches << 16) + count($matchingTopics)] = $topic;
00190 }
00191 }
00192 }
00193 }
00194 }
00195
00196 ?>