Open Journal Systems  2.4.8
 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  unset($result);
77 
78  return $returner;
79  }
80 
87  function &getVersionHistory($productType = null, $product = null) {
88  $versions = array();
89 
90  if(!$productType || !$product) {
91  $application = PKPApplication::getApplication();
92  $productType = 'core';
93  $product = $application->getName();
94  }
95 
96  $result =& $this->retrieve(
97  'SELECT * FROM versions WHERE product_type = ? AND product = ? ORDER BY date_installed DESC',
98  array($productType, $product)
99  );
100 
101  while (!$result->EOF) {
102  $versions[] = $this->_returnVersionFromRow($result->GetRowAssoc(false));
103  $result->MoveNext();
104  }
105 
106  $result->Close();
107  unset($result);
108 
109  return $versions;
110  }
111 
117  function &_returnVersionFromRow(&$row) {
118  $version = new Version(
119  $row['major'],
120  $row['minor'],
121  $row['revision'],
122  $row['build'],
123  $this->datetimeFromDB($row['date_installed']),
124  $row['current'],
125  (isset($row['product_type']) ? $row['product_type'] : null),
126  (isset($row['product']) ? $row['product'] : null),
127  (isset($row['product_class_name']) ? $row['product_class_name'] : ''),
128  (isset($row['lazy_load']) ? $row['lazy_load'] : 0),
129  (isset($row['sitewide']) ? $row['sitewide'] : 0)
130  );
131 
132  HookRegistry::call('VersionDAO::_returnVersionFromRow', array(&$version, &$row));
133 
134  return $version;
135  }
136 
142  function insertVersion(&$version, $isPlugin = false) {
143  $isNewVersion = true;
144 
145  if ($version->getCurrent()) {
146  // Find out whether the last installed version is the same as the
147  // one to be inserted.
148  $versionHistory =& $this->getVersionHistory($version->getProductType(), $version->getProduct());
149  $oldVersion =& array_shift($versionHistory);
150  if ($oldVersion) {
151  if ($version->compare($oldVersion) == 0) {
152  // The old and the new current versions are the same so we need
153  // to update the existing version entry.
154  $isNewVersion = false;
155  } elseif ($version->compare($oldVersion) == 1) {
156  // Version to insert is newer than the existing version entry.
157  // We reset existing entry.
158  $this->update('UPDATE versions SET current = 0 WHERE current = 1 AND product = ?', $version->getProduct());
159  } else {
160  // We do not support downgrades.
161  fatalError('You are trying to downgrade the product "'.$version->getProduct().'" from version ['.$oldVersion->getVersionString().'] to version ['.$version->getVersionString().']. Downgrades are not supported.');
162  }
163  }
164  }
165 
166  if ($isNewVersion) {
167  // We only change the install date when we insert new
168  // version entries.
169  if ($version->getDateInstalled() == null) {
170  $version->setDateInstalled(Core::getCurrentDate());
171  }
172 
173  // Insert new version entry
174  return $this->update(
175  sprintf('INSERT INTO versions
176  (major, minor, revision, build, date_installed, current, product_type, product, product_class_name, lazy_load, sitewide)
177  VALUES
178  (?, ?, ?, ?, %s, ?, ?, ?, ?, ?, ?)',
179  $this->datetimeToDB($version->getDateInstalled())),
180  array(
181  (int) $version->getMajor(),
182  (int) $version->getMinor(),
183  (int) $version->getRevision(),
184  (int) $version->getBuild(),
185  (int) $version->getCurrent(),
186  $version->getProductType(),
187  $version->getProduct(),
188  $version->getProductClassName(),
189  ($version->getLazyLoad()?1:0),
190  ($version->getSitewide()?1:0)
191  )
192  );
193  } else {
194  // Update existing version entry
195  return $this->update(
196  'UPDATE versions SET current = ?, product_class_name = ?, lazy_load = ?, sitewide = ?
197  WHERE product_type = ? AND product = ? AND major = ? AND minor = ? AND revision = ? AND build = ?',
198  array(
199  (int) $version->getCurrent(),
200  $version->getProductClassName(),
201  ($version->getLazyLoad()?1:0),
202  ($version->getSitewide()?1:0),
203  $version->getProductType(),
204  $version->getProduct(),
205  (int) $version->getMajor(),
206  (int) $version->getMinor(),
207  (int) $version->getRevision(),
208  (int) $version->getBuild()
209  )
210  );
211  }
212  }
213 
224  function &getCurrentProducts($context) {
225  if (count($context)) {
226  // Construct the where clause for the plugin settings
227  // context.
228  $contextNames = array_keys($context);
229  foreach ($contextNames as $contextLevel => $contextName) {
230  // Transform from camel case to ..._...
231  String::regexp_match_all('/[A-Z][a-z]*/', ucfirst($contextName), $words);
232  $contextNames[$contextLevel] = strtolower_codesafe(implode('_', $words[0]));
233  }
234  $contextWhereClause = 'AND (('.implode('_id = ? AND ', $contextNames).'_id = ?) OR v.sitewide = 1)';
235  } else {
236  $contextWhereClause = '';
237  }
238 
239  $result =& $this->retrieve(
240  'SELECT v.*
241  FROM versions v LEFT JOIN plugin_settings ps ON
242  lower(v.product_class_name) = ps.plugin_name
243  AND ps.setting_name = \'enabled\' '.$contextWhereClause.'
244  WHERE v.current = 1 AND (ps.setting_value = \'1\' OR v.lazy_load <> 1)', $context, false);
245 
246  $productArray = array();
247  while(!$result->EOF) {
248  $row =& $result->getRowAssoc(false);
249  $productArray[$row['product_type']][$row['product']] =& $this->_returnVersionFromRow($row);
250  $result->MoveNext();
251  }
252  $result->_close();
253  unset($result);
254 
255  return $productArray;
256  }
257 
263  function disableVersion($productType, $product) {
264  $this->update(
265  'UPDATE versions SET current = 0 WHERE current = 1 AND product_type = ? AND product = ?',
266  array($productType, $product)
267  );
268  }
269 }
270 
271 ?>
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)