Open Journal Systems  3.3.0
VersionDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.site.Version');
19 
20 class VersionDAO extends DAO {
21 
29  function getCurrentVersion($productType = null, $product = null, $isPlugin = false) {
30  if(!$productType || !$product) {
32  $productType = 'core';
33  $product = $application->getName();
34  }
35 
36  // We only have to check whether we are on a version previous
37  // to the introduction of products when we're not looking for
38  // a product version anyway.
39  $returner = null;
40  if (!$isPlugin) {
41  $result = $this->retrieve(
42  'SELECT * FROM versions WHERE current = 1'
43  );
44  // If we only have one current version then this must be
45  // the application version before the introduction of products
46  // into the versions table.
47  if ($result->RecordCount() == 1) {
48  $oldVersion = $this->_returnVersionFromRow($result->GetRowAssoc(false));
49  if (isset($oldVersion)) $returner = $oldVersion;
50  }
51  $result->Close();
52  }
53 
54  if (!$returner) {
55  // From here on we can assume that we have the product type
56  // and product columns available in the versions table.
57  $result = $this->retrieve(
58  'SELECT * FROM versions WHERE current = 1 AND product_type = ? AND product = ?',
59  array($productType, $product)
60  );
61  $versionCount = $result->RecordCount();
62  if ($versionCount == 1) {
63  $returner = $this->_returnVersionFromRow($result->GetRowAssoc(false));
64  } elseif ($versionCount >1) {
65  fatalError('More than one current version defined for the product type "'.$productType.'" and product "'.$product.'"!');
66  }
67  }
68 
69  $result->Close();
70  return $returner;
71  }
72 
79  function getVersionHistory($productType = null, $product = null) {
80  $versions = array();
81 
82  if(!$productType || !$product) {
84  $productType = 'core';
85  $product = $application->getName();
86  }
87 
88  $result = $this->retrieve(
89  'SELECT * FROM versions WHERE product_type = ? AND product = ? ORDER BY date_installed DESC',
90  array($productType, $product)
91  );
92 
93  while (!$result->EOF) {
94  $versions[] = $this->_returnVersionFromRow($result->GetRowAssoc(false));
95  $result->MoveNext();
96  }
97 
98  $result->Close();
99  return $versions;
100  }
101 
107  function _returnVersionFromRow($row) {
108  $version = new Version(
109  $row['major'],
110  $row['minor'],
111  $row['revision'],
112  $row['build'],
113  $this->datetimeFromDB($row['date_installed']),
114  $row['current'],
115  (isset($row['product_type']) ? $row['product_type'] : null),
116  (isset($row['product']) ? $row['product'] : null),
117  (isset($row['product_class_name']) ? $row['product_class_name'] : ''),
118  (isset($row['lazy_load']) ? $row['lazy_load'] : 0),
119  (isset($row['sitewide']) ? $row['sitewide'] : 0)
120  );
121 
122  HookRegistry::call('VersionDAO::_returnVersionFromRow', array(&$version, &$row));
123 
124  return $version;
125  }
126 
132  function insertVersion($version, $isPlugin = false) {
133  $isNewVersion = true;
134 
135  if ($version->getCurrent()) {
136  // Find out whether the last installed version is the same as the
137  // one to be inserted.
138  $versionHistory = $this->getVersionHistory($version->getProductType(), $version->getProduct());
139 
140  $oldVersion = array_shift($versionHistory);
141  if ($oldVersion) {
142  if ($version->compare($oldVersion) == 0) {
143  // The old and the new current versions are the same so we need
144  // to update the existing version entry.
145  $isNewVersion = false;
146  } elseif ($version->compare($oldVersion) == 1) {
147  // Version to insert is newer than the existing version entry.
148  // We reset existing entry.
149  $this->update('UPDATE versions SET current = 0 WHERE current = 1 AND product = ?', $version->getProduct());
150  } else {
151  // We do not support downgrades.
152  fatalError('You are trying to downgrade the product "'.$version->getProduct().'" from version ['.$oldVersion->getVersionString(false).'] to version ['.$version->getVersionString(false).']. Downgrades are not supported.');
153  }
154  }
155  }
156 
157  if ($isNewVersion) {
158  // We only change the install date when we insert new
159  // version entries.
160  if ($version->getDateInstalled() == null) {
161  $version->setDateInstalled(Core::getCurrentDate());
162  }
163 
164  // Insert new version entry
165  return $this->update(
166  sprintf('INSERT INTO versions
167  (major, minor, revision, build, date_installed, current, product_type, product, product_class_name, lazy_load, sitewide)
168  VALUES
169  (?, ?, ?, ?, %s, ?, ?, ?, ?, ?, ?)',
170  $this->datetimeToDB($version->getDateInstalled())),
171  array(
172  (int) $version->getMajor(),
173  (int) $version->getMinor(),
174  (int) $version->getRevision(),
175  (int) $version->getBuild(),
176  (int) $version->getCurrent(),
177  $version->getProductType(),
178  $version->getProduct(),
179  $version->getProductClassName(),
180  ($version->getLazyLoad()?1:0),
181  ($version->getSitewide()?1:0)
182  )
183  );
184  } else {
185  // Update existing version entry
186  return $this->update(
187  'UPDATE versions SET current = ?, product_class_name = ?, lazy_load = ?, sitewide = ?
188  WHERE product_type = ? AND product = ? AND major = ? AND minor = ? AND revision = ? AND build = ?',
189  array(
190  (int) $version->getCurrent(),
191  $version->getProductClassName(),
192  ($version->getLazyLoad()?1:0),
193  ($version->getSitewide()?1:0),
194  $version->getProductType(),
195  $version->getProduct(),
196  (int) $version->getMajor(),
197  (int) $version->getMinor(),
198  (int) $version->getRevision(),
199  (int) $version->getBuild()
200  )
201  );
202  }
203  }
204 
215  function getCurrentProducts($context) {
216 
217  if (count($context)) {
218  assert(count($context)==1); // Context depth > 1 no longer supported here.
219  $contextWhereClause = 'AND (context_id = ? OR v.sitewide = 1)';
220  } else {
221  $contextWhereClause = '';
222  }
223 
224  $result = $this->retrieve(
225  'SELECT v.*
226  FROM versions v LEFT JOIN plugin_settings ps ON
227  lower(v.product_class_name) = ps.plugin_name
228  AND ps.setting_name = \'enabled\' '.$contextWhereClause.'
229  WHERE v.current = 1 AND (ps.setting_value = \'1\' OR v.lazy_load <> 1)',
230  $context, false
231  );
232 
233  $productArray = array();
234  while(!$result->EOF) {
235  $row = $result->getRowAssoc(false);
236  $productArray[$row['product_type']][$row['product']] = $this->_returnVersionFromRow($row);
237  $result->MoveNext();
238  }
239  $result->_close();
240  return $productArray;
241  }
242 
248  function disableVersion($productType, $product) {
249  $this->update(
250  'UPDATE versions SET current = 0 WHERE current = 1 AND product_type = ? AND product = ?',
251  array($productType, $product)
252  );
253  }
254 }
255 
256 
VersionDAO\getCurrentVersion
getCurrentVersion($productType=null, $product=null, $isPlugin=false)
Definition: VersionDAO.inc.php:29
$application
$application
Definition: index.php:65
VersionDAO\getVersionHistory
getVersionHistory($productType=null, $product=null)
Definition: VersionDAO.inc.php:79
Version
Describes system version history.
Definition: Version.inc.php:18
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
VersionDAO\insertVersion
insertVersion($version, $isPlugin=false)
Definition: VersionDAO.inc.php:132
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
DAO\datetimeToDB
datetimeToDB($dt)
Definition: DAO.inc.php:299
VersionDAO\_returnVersionFromRow
_returnVersionFromRow($row)
Definition: VersionDAO.inc.php:107
VersionDAO\getCurrentProducts
getCurrentProducts($context)
Definition: VersionDAO.inc.php:215
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
VersionDAO\disableVersion
disableVersion($productType, $product)
Definition: VersionDAO.inc.php:248
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
VersionDAO
Operations for retrieving and modifying Version objects.
Definition: VersionDAO.inc.php:20