This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ExtensionUpdatesHelper.php
164 lines (138 loc) · 3.91 KB
/
ExtensionUpdatesHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* @package Joomla.Administrator
* @subpackage mod_extension_updates
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Module\ExtensionUpdates\Administrator\Helper;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Extension\ExtensionHelper;
/**
* mod_extension_updates_helper helper class for the module
*
* @since 4.0.0
*/
abstract class ExtensionUpdatesHelper
{
/**
* Extract all the extensions contents i.e. Total number of extensions,
* Total updatable extensions, Joomla! core update status
*
* @return array Extensions contents
*
* @since 4.0.0
*/
public static function extractExtensionsContent() : array
{
$totalExtensions = static::getTotalInstalledExtensions();
$updatableExtensions = static::getUpdatableExtensions();
$content = array();
$content['percentage'] = static::calculatePercentage($totalExtensions, $updatableExtensions);
$content['updatableCount'] = count($updatableExtensions);
$content['updatableInfo'] = static::groupExtensions($updatableExtensions);
$content['updateJoomla'] = static::checkJoomlaUpdate();
return $content;
}
/**
* Group extensions by it's types
*
* @param array $extensions Updatable extensions
*
* @return array Grouped updatable extensions
*
* @since 4.0.0
*/
private static function groupExtensions($extensions) : array
{
$result = array(
'component' => 0,
'plugin' => 0,
'template' => 0,
'module' => 0,
'library' => 0,
'package' => 0
);
if (empty($extensions))
{
return $result;
}
foreach ($extensions as $extension)
{
$result[$extension->type]++;
}
return $result;
}
/**
* Calculation of updated extensions percentage.
* This is assuming that the Joomla! alone takes 50% of the total extensions,
* That means if Joomla! is outdated then the 50% of the system is outdated
*
* @param int $total Total number of extensions
* @param int $updatable Total number of updatable extensions
*
* @return float Percentage of system update status
*
* @since 4.0.0
*/
private static function calculatePercentage($total, $updatable) : float
{
$updateJoomla = static::checkJoomlaUpdate();
$updatedExtensions = $total - count($updatable);
$updatePercentage = (float) ($updatedExtensions * .5 * 100) / $total;
if (!$updateJoomla)
{
$updatePercentage = $updatePercentage + 50;
}
return floor($updatePercentage);
}
/**
* Check Joomla! core update status
*
* @return boolean Joomla! is updated or not
*
* @since 4.0.0
*/
private static function checkJoomlaUpdate()
{
$updateModel = Factory::getApplication()->bootComponent('com_installer')
->getMVCFactory()->createModel('Update', 'Administrator', ['ignore_request' => true]);
$eid = ExtensionHelper::getExtensionRecord('files_joomla')->extension_id;
$updateModel->setState('filter.extension_id', $eid);
$extensions = $updateModel->getItems();
return !empty($extensions) ? $extensions[0]->version : false;
}
/**
* Get updatable extensions
*
* @return array Updatable extensions
*
* @since 4.0.0
*/
private static function getUpdatableExtensions() : array
{
$updateModel = Factory::getApplication()->bootComponent('com_installer')
->getMVCFactory()->createModel('Update', 'Administrator', ['ignore_request' => true]);
$extensions = $updateModel->getItems();
return $extensions;
}
/**
* Get total installed extensions
*
* @return array Total installed extensions
*
* @since 4.0.0
*/
private static function getTotalInstalledExtensions() : int
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT(extension_id)')
->from($db->quoteName('#__extensions'))
->where($db->quoteName('name') . ' <> ' . $db->quote('files_joomla'));
$db->setQuery($query);
return $db->loadResult();
}
}