PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_akeeba/models/statistics.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 304 lines | 200 code | 48 blank | 56 comment | 24 complexity | fa5b964264bcd1930e6e2d3f7efcee35 MD5 | raw file
  1. <?php
  2. /**
  3. * @package AkeebaBackup
  4. * @version $Id: statistics.php 347 2010-12-20 13:20:20Z nikosdion $
  5. * @license GNU General Public License, version 2 or later
  6. * @author Nicholas K. Dionysopoulos
  7. * @copyright Copyright 2006-2009 Nicholas K. Dionysopoulos
  8. * @since 1.3
  9. */
  10. defined('_JEXEC') or die('Restricted access');
  11. jimport('joomla.application.component.model');
  12. /**
  13. * Akeeba statistics model class
  14. * used for all requirements of backup statistics in JP
  15. *
  16. */
  17. class AkeebaModelStatistics extends JModel
  18. {
  19. /** @var JPagination The JPagination object, used in the GUI */
  20. private $_pagination;
  21. /**
  22. * Constructor.
  23. */
  24. public function __construct()
  25. {
  26. global $mainframe;
  27. if(!is_object($mainframe)) {
  28. $app =& JFactory::getApplication();
  29. } else {
  30. $app = $mainframe;
  31. }
  32. parent::__construct();
  33. // Get the pagination request variables
  34. $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
  35. $limitstart = $app->getUserStateFromRequest(JRequest::getCmd('option','com_akeeba') .'profileslimitstart','limitstart',0);
  36. // Set the page pagination variables
  37. $this->setState('limit',$limit);
  38. $this->setState('limitstart',$limitstart);
  39. }
  40. /**
  41. * Returns the same list as getStatisticsList(), but includes an extra field
  42. * named 'meta' which categorises attempts based on their backup archive status
  43. *
  44. * @return array An object array of backup attempts
  45. */
  46. public function &getStatisticsListWithMeta($overrideLimits = false, $filters = null, $order = null)
  47. {
  48. $limitstart = $this->getState('limitstart');
  49. $limit = $this->getState('limit');
  50. if($overrideLimits)
  51. {
  52. $limitstart = 0;
  53. $limit = 0;
  54. $filters = null;
  55. }
  56. $allStats =& AEPlatform::get_statistics_list($limitstart, $limit, $filters, $order);
  57. $valid =& AEPlatform::get_valid_backup_records();
  58. if(empty($valid)) $valid = array();
  59. // This will hold the entries whose files are no longer present and are
  60. // not already marked as such in the database
  61. $updateNonExistent = array();
  62. if(!empty($allStats))
  63. {
  64. $new_stats = array();
  65. foreach($allStats as $stat)
  66. {
  67. $total_size = 0;
  68. if(in_array($stat['id'], $valid))
  69. {
  70. $archives = AEUtilStatistics::get_all_filenames($stat);
  71. $stat['meta'] = (count($archives) > 0) ? 'ok' : 'obsolete';
  72. if($stat['meta'] == 'ok')
  73. {
  74. if($stat['total_size']) {
  75. $total_size = $stat['total_size'];
  76. } else {
  77. $total_size = 0;
  78. foreach($archives as $filename)
  79. {
  80. $total_size += @filesize($filename);
  81. }
  82. }
  83. }
  84. else
  85. {
  86. if($stat['total_size']) {
  87. $total_size = $stat['total_size'];
  88. }
  89. if($stat['filesexist']) {
  90. $updateNonExistent[] = $stat['id'];
  91. }
  92. }
  93. $stat['size'] = $total_size;
  94. }
  95. else
  96. {
  97. switch($stat['status'])
  98. {
  99. case 'run':
  100. $stat['meta'] = 'pending';
  101. break;
  102. case 'fail':
  103. $stat['meta'] = 'fail';
  104. break;
  105. default:
  106. $stat['meta'] = 'obsolete';
  107. break;
  108. }
  109. }
  110. $new_stats[] = $stat;
  111. }
  112. }
  113. // Update records found as not having files any more
  114. if(count($updateNonExistent))
  115. {
  116. AEPlatform::invalidate_backup_records($updateNonExistent);
  117. }
  118. unset($valid);
  119. return $new_stats;
  120. }
  121. /**
  122. * Returns the details of the latest backup as HTML
  123. *
  124. * @return string HTML
  125. *
  126. * @todo Move this into a helper class
  127. */
  128. public function getLatestBackupDetails()
  129. {
  130. $db =& $this->getDBO();
  131. $query = 'SELECT max(id) FROM #__ak_stats';
  132. $db->setQuery($query);
  133. $id = $db->loadResult();
  134. $backup_types = AEUtilScripting::loadScripting();
  135. if(empty($id)) return '<p>'.JText::_('BACKUP_STATUS_NONE').'</p>';
  136. $record =& AEPlatform::get_statistics($id);
  137. jimport('joomla.utilities.date');
  138. switch($record['status'])
  139. {
  140. case 'run':
  141. $status = JText::_('STATS_LABEL_STATUS_PENDING');
  142. break;
  143. case 'fail':
  144. $status = JText::_('STATS_LABEL_STATUS_FAIL');
  145. break;
  146. case 'complete':
  147. $status = JText::_('STATS_LABEL_STATUS_OK');
  148. break;
  149. }
  150. switch($record['origin'])
  151. {
  152. case 'frontend':
  153. $origin = JText::_('STATS_LABEL_ORIGIN_FRONTEND');
  154. break;
  155. case 'backend':
  156. $origin = JText::_('STATS_LABEL_ORIGIN_BACKEND');
  157. break;
  158. case 'cli':
  159. $origin = JText::_('STATS_LABEL_ORIGIN_CLI');
  160. break;
  161. default:
  162. $origin = '&ndash;';
  163. break;
  164. }
  165. if(array_key_exists($record['type'],$backup_types['scripts']))
  166. {
  167. $type = AEPlatform::translate($backup_types['scripts'][ $record['type'] ]['text']);
  168. }
  169. else
  170. {
  171. $type = '';
  172. }
  173. $startTime = new JDate($record['backupstart']);
  174. $html = '<table>';
  175. if( AKEEBA_JVERSION == '16' ) {
  176. $html .= '<tr><td>'.JText::_('STATS_LABEL_START').'</td><td>'.$startTime->format(JText::_('DATE_FORMAT_LC4'), true).'</td></tr>';
  177. } else {
  178. $html .= '<tr><td>'.JText::_('STATS_LABEL_START').'</td><td>'.$startTime->toFormat(JText::_('DATE_FORMAT_LC4')).'</td></tr>';
  179. }
  180. $html .= '<tr><td>'.JText::_('STATS_LABEL_DESCRIPTION').'</td><td>'.$record['description'].'</td></tr>';
  181. $html .= '<tr><td>'.JText::_('STATS_LABEL_STATUS').'</td><td>'.$status.'</td></tr>';
  182. $html .= '<tr><td>'.JText::_('STATS_LABEL_ORIGIN').'</td><td>'.$origin.'</td></tr>';
  183. $html .= '<tr><td>'.JText::_('STATS_LABEL_TYPE').'</td><td>'.$type.'</td></tr>';
  184. $html .= '</table>';
  185. return $html;
  186. }
  187. /**
  188. * Delete the stats record whose ID is set in the model
  189. * @param int $id Backup record whose files we have to delete
  190. * @return bool True on success
  191. */
  192. public function delete($id)
  193. {
  194. $db =& $this->getDBO();
  195. if( (!is_numeric($id)) || ($id <= 0) )
  196. {
  197. $this->setError(JText::_('STATS_ERROR_INVALIDID'));
  198. return false;
  199. }
  200. // Try to delete files
  201. $this->deleteFile($id);
  202. if(!AEPlatform::delete_statistics($id))
  203. {
  204. $this->setError($db->getError());
  205. return false;
  206. }
  207. return true;
  208. }
  209. /**
  210. * Delete the backup file of the stats record whose ID is set in the model
  211. * @param int $id Backup record whose files we have to delete
  212. * @return bool True on success
  213. */
  214. public function deleteFile($id)
  215. {
  216. $db =& $this->getDBO();
  217. if( (!is_numeric($id)) || ($id <= 0) )
  218. {
  219. $this->setError(JText::_('STATS_ERROR_INVALIDID'));
  220. return false;
  221. }
  222. $stat = AEPlatform::get_statistics($id);
  223. $allFiles = AEUtilStatistics::get_all_filenames($stat, false);
  224. $registry =& AEFactory::getConfiguration();
  225. $status = true;
  226. jimport('joomla.filesystem.file');
  227. foreach($allFiles as $filename)
  228. {
  229. $new_status = JFile::delete($filename);
  230. $status = $status ? $new_status : false;
  231. }
  232. return $status;
  233. }
  234. /**
  235. * Get a pagination object
  236. *
  237. * @access public
  238. * @return JPagination
  239. *
  240. */
  241. public function &getPagination($filters = null)
  242. {
  243. if( empty($this->_pagination) )
  244. {
  245. // Import the pagination library
  246. jimport('joomla.html.pagination');
  247. // Prepare pagination values
  248. $total = AEPlatform::get_statistics_count($filters);
  249. $limitstart = $this->getState('limitstart');
  250. $limit = $this->getState('limit');
  251. // Create the pagination object
  252. $this->_pagination = new JPagination($total, $limitstart, $limit);
  253. }
  254. return $this->_pagination;
  255. }
  256. }