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