Open Journal Systems  3.0.0
 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  return $returner;
76  }
77 
84  function &getVersionHistory($productType = null, $product = null) {
85  $versions = array();
86 
87  if(!$productType || !$product) {
88  $application = PKPApplication::getApplication();
89  $productType = 'core';
90  $product = $application->getName();
91  }
92 
93  $result = $this->retrieve(
94  'SELECT * FROM versions WHERE product_type = ? AND product = ? ORDER BY date_installed DESC',
95  array($productType, $product)
96  );
97 
98  while (!$result->EOF) {
99  $versions[] = $this->_returnVersionFromRow($result->GetRowAssoc(false));
100  $result->MoveNext();
101  }
102 
103  $result->Close();
104  return $versions;
105  }
106 
112  function &_returnVersionFromRow($row) {
113  $version = new Version(
114  $row['major'],
115  $row['minor'],
116  $row['revision'],
117  $row['build'],
118  $this->datetimeFromDB($row['date_installed']),
119  $row['current'],
120  (isset($row['product_type']) ? $row['product_type'] : null),
121  (isset($row['product']) ? $row['product'] : null),
122  (isset($row['product_class_name']) ? $row['product_class_name'] : ''),
123  (isset($row['lazy_load']) ? $row['lazy_load'] : 0),
124  (isset($row['sitewide']) ? $row['sitewide'] : 0)
125  );
126 
127  HookRegistry::call('VersionDAO::_returnVersionFromRow', array(&$version, &$row));
128 
129  return $version;
130  }
131 
137  function insertVersion(&$version, $isPlugin = false) {
138  $isNewVersion = true;
139 
140  if ($version->getCurrent()) {
141  // Find out whether the last installed version is the same as the
142  // one to be inserted.
143  $versionHistory =& $this->getVersionHistory($version->getProductType(), $version->getProduct());
144  $oldVersion =& array_shift($versionHistory);
145  if ($oldVersion) {
146  if ($version->compare($oldVersion) == 0) {
147  // The old and the new current versions are the same so we need
148  // to update the existing version entry.
149  $isNewVersion = false;
150  } elseif ($version->compare($oldVersion) == 1) {
151  // Version to insert is newer than the existing version entry.
152  // We reset existing entry.
153  $this->update('UPDATE versions SET current = 0 WHERE current = 1 AND product = ?', $version->getProduct());
154  } else {
155  // We do not support downgrades.
156  fatalError('You are trying to downgrade the product "'.$version->getProduct().'" from version ['.$oldVersion->getVersionString(false).'] to version ['.$version->getVersionString(false).']. Downgrades are not supported.');
157  }
158  }
159  }
160 
161  if ($isNewVersion) {
162  // We only change the install date when we insert new
163  // version entries.
164  if ($version->getDateInstalled() == null) {
165  $version->setDateInstalled(Core::getCurrentDate());
166  }
167 
168  // Insert new version entry
169  return $this->update(
170  sprintf('INSERT INTO versions
171  (major, minor, revision, build, date_installed, current, product_type, product, product_class_name, lazy_load, sitewide)
172  VALUES
173  (?, ?, ?, ?, %s, ?, ?, ?, ?, ?, ?)',
174  $this->datetimeToDB($version->getDateInstalled())),
175  array(
176  (int) $version->getMajor(),
177  (int) $version->getMinor(),
178  (int) $version->getRevision(),
179  (int) $version->getBuild(),
180  (int) $version->getCurrent(),
181  $version->getProductType(),
182  $version->getProduct(),
183  $version->getProductClassName(),
184  ($version->getLazyLoad()?1:0),
185  ($version->getSitewide()?1:0)
186  )
187  );
188  } else {
189  // Update existing version entry
190  return $this->update(
191  'UPDATE versions SET current = ?, product_class_name = ?, lazy_load = ?, sitewide = ?
192  WHERE product_type = ? AND product = ? AND major = ? AND minor = ? AND revision = ? AND build = ?',
193  array(
194  (int) $version->getCurrent(),
195  $version->getProductClassName(),
196  ($version->getLazyLoad()?1:0),
197  ($version->getSitewide()?1:0),
198  $version->getProductType(),
199  $version->getProduct(),
200  (int) $version->getMajor(),
201  (int) $version->getMinor(),
202  (int) $version->getRevision(),
203  (int) $version->getBuild()
204  )
205  );
206  }
207  }
208 
219  function &getCurrentProducts($context) {
220 
222  if (count($context)) {
223  assert(count($context)==1); // Context depth > 1 no longer supported here.
224  $contextWhereClause = 'AND (' . $contextColumn . ' = ? OR v.sitewide = 1)';
225  } else {
226  $contextWhereClause = '';
227  }
228 
229  $result = $this->retrieve(
230  'SELECT v.*
231  FROM versions v LEFT JOIN plugin_settings ps ON
232  lower(v.product_class_name) = ps.plugin_name
233  AND ps.setting_name = \'enabled\' '.$contextWhereClause.'
234  WHERE v.current = 1 AND (ps.setting_value = \'1\' OR v.lazy_load <> 1)',
235  $context, false
236  );
237 
238  $productArray = array();
239  while(!$result->EOF) {
240  $row = $result->getRowAssoc(false);
241  $productArray[$row['product_type']][$row['product']] =& $this->_returnVersionFromRow($row);
242  $result->MoveNext();
243  }
244  $result->_close();
245  return $productArray;
246  }
247 
253  function disableVersion($productType, $product) {
254  $this->update(
255  'UPDATE versions SET current = 0 WHERE current = 1 AND product_type = ? AND product = ?',
256  array($productType, $product)
257  );
258  }
259 }
260 
261 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
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:84
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
static getPluginSettingsContextColumnName()
insertVersion(&$version, $isPlugin=false)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
& getCurrentProducts($context)
& getVersionHistory($productType=null, $product=null)
& getCurrentVersion($productType=null, $product=null, $isPlugin=false)
& _returnVersionFromRow($row)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95