Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
VersionDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.site.Version');
19 
20 class VersionDAO extends DAO {
24  function VersionDAO() {
25  parent::DAO();
26  }
27 
35  function &getCurrentVersion($productType = null, $product = null, $isPlugin = false) {
36  if(!$productType || !$product) {
37  $application = PKPApplication::getApplication();
38  $productType = 'core';
39  $product = $application->getName();
40  }
41 
42  // We only have to check whether we are on a version previous
43  // to the introduction of products when we're not looking for
44  // a product version anyway.
45  $returner = null;
46  if (!$isPlugin) {
47  $result =& $this->retrieve(
48  'SELECT * FROM versions WHERE current = 1'
49  );
50  // If we only have one current version then this must be
51  // the application version before the introduction of products
52  // into the versions table.
53  if ($result->RecordCount() == 1) {
54  $oldVersion =& $this->_returnVersionFromRow($result->GetRowAssoc(false));
55  if (isset($oldVersion)) $returner =& $oldVersion;
56  }
57  }
58 
59  if (!$returner) {
60  // From here on we can assume that we have the product type
61  // and product columns available in the versions table.
62  $result =& $this->retrieve(
63  'SELECT * FROM versions WHERE current = 1 AND product_type = ? AND product = ?',
64  array($productType, $product)
65  );
66  $versionCount = $result->RecordCount();
67  if ($versionCount == 1) {
68  $returner =& $this->_returnVersionFromRow($result->GetRowAssoc(false));
69  } elseif ($versionCount >1) {
70  fatalError('More than one current version defined for the product type "'.$productType.'" and product "'.$product.'"!');
71  }
72  }
73 
74  $result->Close();
75  unset($result);
76 
77  return $returner;
78  }
79 
86  function &getVersionHistory($productType = null, $product = null) {
87  $versions = array();
88 
89  if(!$productType || !$product) {
90  $application = PKPApplication::getApplication();
91  $productType = 'core';
92  $product = $application->getName();
93  }
94 
95  $result =& $this->retrieve(
96  'SELECT * FROM versions WHERE product_type = ? AND product = ? ORDER BY date_installed DESC',
97  array($productType, $product)
98  );
99 
100  while (!$result->EOF) {
101  $versions[] = $this->_returnVersionFromRow($result->GetRowAssoc(false));
102  $result->MoveNext();
103  }
104 
105  $result->Close();
106  unset($result);
107 
108  return $versions;
109  }
110 
116  function &_returnVersionFromRow(&$row) {
117  $version = new Version(
118  $row['major'],
119  $row['minor'],
120  $row['revision'],
121  $row['build'],
122  $this->datetimeFromDB($row['date_installed']),
123  $row['current'],
124  (isset($row['product_type']) ? $row['product_type'] : null),
125  (isset($row['product']) ? $row['product'] : null),
126  (isset($row['product_class_name']) ? $row['product_class_name'] : ''),
127  (isset($row['lazy_load']) ? $row['lazy_load'] : 0),
128  (isset($row['sitewide']) ? $row['sitewide'] : 0)
129  );
130 
131  HookRegistry::call('VersionDAO::_returnVersionFromRow', array(&$version, &$row));
132 
133  return $version;
134  }
135 
141  function insertVersion(&$version, $isPlugin = false) {
142  $isNewVersion = true;
143 
144  if ($version->getCurrent()) {
145  // Find out whether the last installed version is the same as the
146  // one to be inserted.
147  $versionHistory =& $this->getVersionHistory($version->getProductType(), $version->getProduct());
148  $oldVersion =& array_shift($versionHistory);
149  if ($oldVersion) {
150  if ($version->compare($oldVersion) == 0) {
151  // The old and the new current versions are the same so we need
152  // to update the existing version entry.
153  $isNewVersion = false;
154  } elseif ($version->compare($oldVersion) == 1) {
155  // Version to insert is newer than the existing version entry.
156  // We reset existing entry.
157  $this->update('UPDATE versions SET current = 0 WHERE current = 1 AND product = ?', $version->getProduct());
158  } else {
159  // We do not support downgrades.
160  fatalError('You are trying to downgrade the product "'.$version->getProduct().'" from version ['.$oldVersion->getVersionString().'] to version ['.$version->getVersionString().']. Downgrades are not supported.');
161  }
162  }
163  }
164 
165  if ($isNewVersion) {
166  // We only change the install date when we insert new
167  // version entries.
168  if ($version->getDateInstalled() == null) {
169  $version->setDateInstalled(Core::getCurrentDate());
170  }
171 
172  // Insert new version entry
173  return $this->update(
174  sprintf('INSERT INTO versions
175  (major, minor, revision, build, date_installed, current, product_type, product, product_class_name, lazy_load, sitewide)
176  VALUES
177  (?, ?, ?, ?, %s, ?, ?, ?, ?, ?, ?)',
178  $this->datetimeToDB($version->getDateInstalled())),
179  array(
180  (int) $version->getMajor(),
181  (int) $version->getMinor(),
182  (int) $version->getRevision(),
183  (int) $version->getBuild(),
184  (int) $version->getCurrent(),
185  $version->getProductType(),
186  $version->getProduct(),
187  $version->getProductClassName(),
188  ($version->getLazyLoad()?1:0),
189  ($version->getSitewide()?1:0)
190  )
191  );
192  } else {
193  // Update existing version entry
194  return $this->update(
195  'UPDATE versions SET current = ?, product_class_name = ?, lazy_load = ?, sitewide = ?
196  WHERE product_type = ? AND product = ? AND major = ? AND minor = ? AND revision = ? AND build = ?',
197  array(
198  (int) $version->getCurrent(),
199  $version->getProductClassName(),
200  ($version->getLazyLoad()?1:0),
201  ($version->getSitewide()?1:0),
202  $version->getProductType(),
203  $version->getProduct(),
204  (int) $version->getMajor(),
205  (int) $version->getMinor(),
206  (int) $version->getRevision(),
207  (int) $version->getBuild()
208  )
209  );
210  }
211  }
212 
223  function &getCurrentProducts($context) {
224  if (count($context)) {
225  // Construct the where clause for the plugin settings
226  // context.
227  $contextNames = array_keys($context);
228  foreach ($contextNames as $contextLevel => $contextName) {
229  // Transform from camel case to ..._...
230  String::regexp_match_all('/[A-Z][a-z]*/', ucfirst($contextName), $words);
231  $contextNames[$contextLevel] = strtolower_codesafe(implode('_', $words[0]));
232  }
233  $contextWhereClause = 'AND (('.implode('_id = ? AND ', $contextNames).'_id = ?) OR v.sitewide = 1)';
234  } else {
235  $contextWhereClause = '';
236  }
237 
238  $result =& $this->retrieve(
239  'SELECT v.*
240  FROM versions v LEFT JOIN plugin_settings ps ON
241  lower(v.product_class_name) = ps.plugin_name
242  AND ps.setting_name = \'enabled\' '.$contextWhereClause.'
243  WHERE v.current = 1 AND (ps.setting_value = \'1\' OR v.lazy_load <> 1)', $context, false);
244 
245  $productArray = array();
246  while(!$result->EOF) {
247  $row =& $result->getRowAssoc(false);
248  $productArray[$row['product_type']][$row['product']] =& $this->_returnVersionFromRow($row);
249  $result->MoveNext();
250  }
251  $result->_close();
252  unset($result);
253 
254  return $productArray;
255  }
256 
262  function disableVersion($productType, $product) {
263  $this->update(
264  'UPDATE versions SET current = 0 WHERE current = 1 AND product_type = ? AND product = ?',
265  array($productType, $product)
266  );
267  }
268 }
269 
270 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
Operations for retrieving and modifying Version objects.
disableVersion($productType, $product)
Describes system version history.
Definition: Version.inc.php:18
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
& _returnVersionFromRow(&$row)
getCurrentDate($ts=null)
Definition: Core.inc.php:94
datetimeFromDB($dt)
Definition: DAO.inc.php:316
insertVersion(&$version, $isPlugin=false)
call($hookName, $args=null)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
regexp_match_all($pattern, $subject, &$matches)
Definition: String.inc.php:342
& getCurrentProducts($context)
& getVersionHistory($productType=null, $product=null)
& getCurrentVersion($productType=null, $product=null, $isPlugin=false)