Open Monograph Press  3.3.0
ONIXCodelistItemDAO.inc.php
1 <?php
2 
18 import('classes.codelist.ONIXCodelistItem');
19 
20 class ONIXCodelistItemDAO extends DAO {
21 
22  /* The name of the codelist we are interested in */
23  var $_list;
24 
28  function __construct() {
29  parent::__construct();
30  }
31 
32  function &_getCache($locale = null) {
33  if ($locale == null) {
34  $locale = AppLocale::getLocale();
35  }
36 
37  $cacheName = 'Onix' . $this->getListName() . 'Cache';
38 
39  $cache =& Registry::get($cacheName, true, null);
40  if ($cache === null) {
41  $cacheManager = CacheManager::getManager();
42  $cache = $cacheManager->getFileCache(
43  $this->getListName() . '_codelistItems', $locale,
44  array($this, '_cacheMiss')
45  );
46  $cacheTime = $cache->getCacheTime();
47  if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename($locale))) {
48  $cache->flush();
49  }
50  }
51 
52  return $cache;
53  }
54 
55  function _cacheMiss($cache, $id) {
56  $allCodelistItems =& Registry::get('all' . $this->getListName() . 'CodelistItems', true, null);
57  if ($allCodelistItems === null) {
58  // Add a locale load to the debug notes.
59  $notes =& Registry::get('system.debug.notes');
60  $locale = $cache->cacheId;
61  if ($locale == null) {
62  $locale = AppLocale::getLocale();
63  }
64  $filename = $this->getFilename($locale);
65  $notes[] = array('debug.notes.codelistItemListLoad', array('filename' => $filename));
66 
67  // Reload locale registry file
68  $xmlDao = new XMLDAO();
69  $listName = $this->getListName(); // i.e., 'List30'
70  import('classes.codelist.ONIXParserDOMHandler');
71  $handler = new ONIXParserDOMHandler($listName);
72 
73  import('lib.pkp.classes.xslt.XSLTransformer');
74  import('lib.pkp.classes.file.FileManager');
75  import('lib.pkp.classes.file.TemporaryFileManager');
76 
77  $temporaryFileManager = new TemporaryFileManager();
78  $fileManager = new FileManager();
79 
80  // Ensure that the temporary file dir exists
81  $tmpDir = $temporaryFileManager->getBasePath();
82  if (!file_exists($tmpDir)) mkdir($tmpDir);
83 
84  $tmpName = tempnam($tmpDir, 'ONX');
85  $xslTransformer = new XSLTransformer();
86  $xslTransformer->setParameters(array('listName' => $listName));
87  $xslTransformer->setRegisterPHPFunctions(true);
88 
89  $xslFile = 'lib/pkp/xml/onixFilter.xsl';
90  $filteredXml = $xslTransformer->transform($filename, XSL_TRANSFORMER_DOCTYPE_FILE, $xslFile, XSL_TRANSFORMER_DOCTYPE_FILE, XSL_TRANSFORMER_DOCTYPE_STRING);
91  if (!$filteredXml) {assert(false);}
92 
93  $data = null;
94 
95  if (is_writeable($tmpName)) {
96  $fp = fopen($tmpName, 'wb');
97  fwrite($fp, $filteredXml);
98  fclose($fp);
99  $data = $xmlDao->parseWithHandler($tmpName, $handler);
100  $fileManager->deleteByPath($tmpName);
101  } else {
102  fatalError('misconfigured directory permissions on: ' . $tmpDir);
103  }
104 
105  // Build array with ($charKey => array(stuff))
106 
107  if (isset($data[$listName])) {
108  foreach ($data[$listName] as $code => $codelistData) {
109  $allCodelistItems[$code] = $codelistData;
110  }
111  }
112  if (is_array($allCodelistItems)) {
113  asort($allCodelistItems);
114  }
115 
116  $cache->setEntireCache($allCodelistItems);
117 
118  }
119  return null;
120  }
121 
128  function getFilename($locale) {
129  $masterLocale = MASTER_LOCALE;
130  $localizedFile = "locale/$locale/ONIX_BookProduct_CodeLists.xsd";
131  if (AppLocale::isLocaleValid($locale) && file_exists($localizedFile)) {
132  return $localizedFile;
133  }
134 
135  // Fall back on the version for the master locale.
136  return "locale/$masterLocale/ONIX_BookProduct_CodeLists.xsd";
137  }
138 
143  function setListName($list) {
144  $this->_list =& $list;
145  }
146 
151  function getListName() {
152  return $this->_list;
153  }
154 
159  function newDataObject() {
160  return new ONIXCodelistItem();
161  }
162 
169  function &getCodelistItems($list, $locale = null) {
170  $this->setListName($list);
171  $cache =& $this->_getCache($locale);
172  $returner = array();
173  foreach ($cache->getContents() as $code => $entry) {
174  $returner[] =& $this->_fromRow($code, $entry);
175  }
176  return $returner;
177  }
178 
187  function &getCodes($list, $codesToExclude = array(), $codesFilter = null, $locale = null) {
188  $this->setListName($list);
189  $cache =& $this->_getCache($locale);
190  $returner = array();
191  $cacheContents =& $cache->getContents();
192  if (is_array($cacheContents)) {
193  foreach ($cache->getContents() as $code => $entry) {
194  if ($code != '') {
195  if (!in_array($code, $codesToExclude) && (empty($codesFilter) || preg_match("/^" . preg_quote($codesFilter) . "/i", $entry[0])))
196  $returner[$code] =& $entry[0];
197  }
198  }
199  }
200  return $returner;
201  }
202 
207  function codeExistsInList($code, $list) {
208  $listKeys = array_keys($this->getCodes($list));
209  return ($code != null && in_array($code, $listKeys));
210  }
211 
216  function getCodeFromValue($value, $list) {
217  $codes = $this->getCodes($list);
218  $codes = array_flip($codes);
219  if (array_key_exists($value, $codes)) {
220  return $codes[$value];
221  }
222  return '';
223  }
224 
230  function &_fromRow($code, &$entry) {
231  $codelistItem = $this->newDataObject();
232  $codelistItem->setCode($code);
233  $codelistItem->setText($entry[0]);
234 
235  HookRegistry::call('ONIXCodelistItemDAO::_fromRow', array(&$codelistItem, &$code, &$entry));
236 
237  return $codelistItem;
238  }
239 }
240 
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
ONIXCodelistItem
Basic class describing a codelist item.
Definition: ONIXCodelistItem.inc.php:18
TemporaryFileManager
Definition: TemporaryFileManager.inc.php:19
XSLTransformer
Wrapper class for running XSL transformations using PHP 4.x or 5.x.
Definition: XSLTransformer.inc.php:24
ONIXCodelistItemDAO
Parent class for operations involving Codelist objects.
Definition: ONIXCodelistItemDAO.inc.php:20
ONIXCodelistItemDAO\codeExistsInList
codeExistsInList($code, $list)
Definition: ONIXCodelistItemDAO.inc.php:207
ONIXCodelistItemDAO\_getCache
& _getCache($locale=null)
Definition: ONIXCodelistItemDAO.inc.php:32
ONIXCodelistItemDAO\_cacheMiss
_cacheMiss($cache, $id)
Definition: ONIXCodelistItemDAO.inc.php:55
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
ONIXCodelistItemDAO\getFilename
getFilename($locale)
Definition: ONIXCodelistItemDAO.inc.php:128
Registry\get
static & get($key, $createIfEmpty=false, $createWithDefault=null)
Definition: Registry.inc.php:35
ONIXCodelistItemDAO\getCodelistItems
& getCodelistItems($list, $locale=null)
Definition: ONIXCodelistItemDAO.inc.php:169
ONIXCodelistItemDAO\$_list
$_list
Definition: ONIXCodelistItemDAO.inc.php:23
ONIXCodelistItemDAO\setListName
setListName($list)
Definition: ONIXCodelistItemDAO.inc.php:143
ONIXCodelistItemDAO\__construct
__construct()
Definition: ONIXCodelistItemDAO.inc.php:28
ONIXCodelistItemDAO\newDataObject
newDataObject()
Definition: ONIXCodelistItemDAO.inc.php:159
ONIXCodelistItemDAO\getCodes
& getCodes($list, $codesToExclude=array(), $codesFilter=null, $locale=null)
Definition: ONIXCodelistItemDAO.inc.php:187
ONIXCodelistItemDAO\_fromRow
& _fromRow($code, &$entry)
Definition: ONIXCodelistItemDAO.inc.php:230
ONIXCodelistItemDAO\getCodeFromValue
getCodeFromValue($value, $list)
Definition: ONIXCodelistItemDAO.inc.php:216
ONIXCodelistItemDAO\getListName
getListName()
Definition: ONIXCodelistItemDAO.inc.php:151
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
PKPLocale\isLocaleValid
static isLocaleValid($locale)
Definition: PKPLocale.inc.php:505
ONIXParserDOMHandler
This parser extracts a specific xs:simpleType based on a name attribute representing a code list with...
Definition: ONIXParserDOMHandler.inc.php:25
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31