Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
LanguageDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.language.Language');
19 
20 class LanguageDAO extends DAO {
21 
25  function LanguageDAO() {
26  parent::DAO();
27  }
28 
33  function &_getCache($locale = null) {
34  if (is_null($locale)) {
35  $locale = AppLocale::getLocale();
36  }
37  $cache =& Registry::get('languageCache-'.$locale, true, null);
38  if ($cache === null) {
39  $cacheManager = CacheManager::getManager();
40  $cache = $cacheManager->getFileCache(
41  'languages', $locale,
42  array($this, '_cacheMiss')
43  );
44  $cacheTime = $cache->getCacheTime();
45  if ($cacheTime !== null && $cacheTime < filemtime($this->getLanguageFilename($locale))) {
46  $cache->flush();
47  }
48  }
49 
50  return $cache;
51  }
52 
53  function _cacheMiss($cache, $id) {
54  $allLanguages =& Registry::get('allLanguages-'.$cache->cacheId, true, null);
55  if ($allLanguages === null) {
56  // Add a locale load to the debug notes.
57  $notes =& Registry::get('system.debug.notes');
58  $locale = $cache->cacheId;
59  if ($locale == null) {
60  $locale = AppLocale::getLocale();
61  }
62  $filename = $this->getLanguageFilename($locale);
63  $notes[] = array('debug.notes.languageListLoad', array('filename' => $filename));
64 
65  // Reload locale registry file
66  $xmlDao = new XMLDAO();
67  $data = $xmlDao->parseStruct($filename, array('language'));
68 
69  // Build array with ($charKey => array(stuff))
70  if (isset($data['language'])) {
71  foreach ($data['language'] as $languageData) {
72  $allLanguages[$languageData['attributes']['code']] = array(
73  $languageData['attributes']['name'],
74  );
75  }
76  }
77  if (is_array($allLanguages)) {
78  asort($allLanguages);
79  }
80  $cache->setEntireCache($allLanguages);
81  }
82  if (isset($allLanguages[$id])) {
83  return $allLanguages[$id];
84  } else {
85  return null;
86  }
87  }
88 
94  function getLanguageFilename($locale) {
95  return "lib/pkp/locale/$locale/languages.xml";
96  }
97 
104  function getLanguageByCode($code, $locale = null) {
105  $cache = $this->_getCache($locale);
106  return $this->_returnLanguageFromRow($code, $cache->get($code));
107  }
108 
114  function getLanguages($locale = null) {
115  $cache = $this->_getCache($locale);
116  $returner = array();
117  foreach ($cache->getContents() as $code => $entry) {
118  $returner[] = $this->_returnLanguageFromRow($code, $entry);
119  }
120  return $returner;
121  }
122 
128  function getLanguageNames($locale = null) {
129  $cache = $this->_getCache($locale);
130  $returner = array();
131  $cacheContents = $cache->getContents();
132  if (is_array($cacheContents)) {
133  foreach ($cache->getContents() as $entry) {
134  $returner[] = $entry[0];
135  }
136  }
137  return $returner;
138  }
139 
144  function newDataObject() {
145  return new Language();
146  }
147 
153  function &_returnLanguageFromRow($code, &$entry) {
154  $language = $this->newDataObject();
155  $language->setCode($code);
156  $language->setName($entry[0]);
157 
158  HookRegistry::call('LanguageDAO::_returnLanguageFromRow', array(&$language, &$code, &$entry));
159 
160  return $language;
161  }
162 }
163 
164 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
Operations for retrieving and modifying Language objects.
getLanguageFilename($locale)
getLanguageByCode($code, $locale=null)
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
static getLocale()
static call($hookName, $args=null)
getLanguages($locale=null)
Basic class describing a language.
& _getCache($locale=null)
getLanguageNames($locale=null)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
& _returnLanguageFromRow($code, &$entry)