1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Elastica\Document; |
4
|
|
|
use Elastica\Type\Mapping; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* An add-on with one or more versions. |
8
|
|
|
*/ |
9
|
|
|
class Addon extends DataObject |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
public static $db = array( |
13
|
|
|
'Name' => 'Varchar(255)', |
14
|
|
|
'Description' => 'Text', |
15
|
|
|
'Type' => 'Varchar(100)', |
16
|
|
|
'Readme' => 'HTMLText', |
17
|
|
|
'Released' => 'SS_Datetime', |
18
|
|
|
'Repository' => 'Varchar(255)', |
19
|
|
|
'Downloads' => 'Int', |
20
|
|
|
'DownloadsMonthly' => 'Int', |
21
|
|
|
'Favers' => 'Int', |
22
|
|
|
'LastUpdated' => 'SS_Datetime', |
23
|
|
|
'LastBuilt' => 'SS_Datetime', |
24
|
|
|
'BuildQueued' => 'Boolean', |
25
|
|
|
// Module rating information |
26
|
|
|
'Rating' => 'Int', |
27
|
|
|
'RatingDetails' => 'Text', |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
public static $has_one = array( |
31
|
|
|
'Vendor' => 'AddonVendor', |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
public static $has_many = array( |
35
|
|
|
'Versions' => 'AddonVersion' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
public static $many_many = array( |
39
|
|
|
'Keywords' => 'AddonKeyword', |
40
|
|
|
'Screenshots' => 'Image', |
41
|
|
|
'CompatibleVersions' => 'SilverStripeVersion' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
public static $default_sort = 'Name'; |
45
|
|
|
|
46
|
|
|
public static $extensions = array( |
47
|
|
|
'SilverStripe\\Elastica\\Searchable' |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the addon's versions sorted from newest to oldest. |
52
|
|
|
* |
53
|
|
|
* @return ArrayList |
54
|
|
|
*/ |
55
|
|
|
public function SortedVersions() |
56
|
|
|
{ |
57
|
|
|
$versions = $this->Versions()->toArray(); |
|
|
|
|
58
|
|
|
|
59
|
|
|
usort($versions, function ($a, $b) { |
60
|
|
|
return version_compare($b->Version, $a->Version); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
return new ArrayList($versions); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function MasterVersion() |
67
|
|
|
{ |
68
|
|
|
return $this->Versions()->filter('PrettyVersion', array('dev-master', 'trunk'))->First(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function Authors() |
72
|
|
|
{ |
73
|
|
|
return $this->Versions()->relation('Authors'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function VendorName() |
77
|
|
|
{ |
78
|
|
|
return substr($this->Name, 0, strpos($this->Name, '/')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function VendorLink() |
82
|
|
|
{ |
83
|
|
|
return Controller::join_links( |
84
|
|
|
Director::baseURL(), |
85
|
|
|
'add-ons', |
86
|
|
|
$this->VendorName() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function PackageName() |
91
|
|
|
{ |
92
|
|
|
return substr($this->Name, strpos($this->Name, '/') + 1); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function Link() |
96
|
|
|
{ |
97
|
|
|
return Controller::join_links( |
98
|
|
|
Director::baseURL(), |
99
|
|
|
'add-ons', |
100
|
|
|
$this->Name |
|
|
|
|
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function DescriptionText() |
105
|
|
|
{ |
106
|
|
|
return $this->Description; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function RSSTitle() |
110
|
|
|
{ |
111
|
|
|
return sprintf('New module release: %s', $this->Name); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function PackagistUrl() |
115
|
|
|
{ |
116
|
|
|
return "https://packagist.org/packages/$this->Name"; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getElasticaMapping() |
120
|
|
|
{ |
121
|
|
|
return new Mapping(null, array( |
122
|
|
|
'name' => array('type' => 'string'), |
123
|
|
|
'description' => array('type' => 'string'), |
124
|
|
|
'type' => array('type' => 'string'), |
125
|
|
|
'compatibility' => array('type' => 'string'), |
126
|
|
|
'vendor' => array('type' => 'string'), |
127
|
|
|
'tags' => array('type' => 'string'), |
128
|
|
|
'released' => array('type' => 'date'), |
129
|
|
|
'downloads' => array('type' => 'string'), |
130
|
|
|
'readme' => array('type' => 'string') |
131
|
|
|
)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getElasticaDocument() |
135
|
|
|
{ |
136
|
|
|
return new Document($this->ID, array( |
137
|
|
|
'name' => $this->Name, |
|
|
|
|
138
|
|
|
'description' => $this->Description, |
|
|
|
|
139
|
|
|
'type' => $this->Type, |
|
|
|
|
140
|
|
|
'compatibility' => $this->CompatibleVersions()->column('Name'), |
|
|
|
|
141
|
|
|
'vendor' => $this->VendorName(), |
142
|
|
|
'tags' => $this->Keywords()->column('Name'), |
|
|
|
|
143
|
|
|
'released' => $this->obj('Released')->Format('c'), |
144
|
|
|
'downloads' => (int)$this->Downloads, |
|
|
|
|
145
|
|
|
'readme' => strip_tags($this->Readme), |
|
|
|
|
146
|
|
|
)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function onBeforeDelete() |
150
|
|
|
{ |
151
|
|
|
parent::onBeforeDelete(); |
152
|
|
|
|
153
|
|
|
// Partially cascade delete. Leave author and keywords in place, |
154
|
|
|
// since they might be related to other addons. |
155
|
|
|
foreach ($this->Screenshots() as $image) { |
|
|
|
|
156
|
|
|
$image->delete(); |
157
|
|
|
} |
158
|
|
|
$this->Screenshots()->removeAll(); |
|
|
|
|
159
|
|
|
|
160
|
|
|
foreach ($this->Versions() as $version) { |
|
|
|
|
161
|
|
|
$version->delete(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->Keywords()->removeAll(); |
|
|
|
|
165
|
|
|
$this->CompatibleVersions()->removeAll(); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getDateCreated() |
169
|
|
|
{ |
170
|
|
|
return date('Y-m-d', strtotime($this->Created)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns unserialised result data from the ratings check suite |
175
|
|
|
* |
176
|
|
|
* {@see \SilverStripe\ModuleRatings\CheckSuite} |
177
|
|
|
* |
178
|
|
|
* @return ArrayData |
179
|
|
|
*/ |
180
|
|
|
public function RatingData() |
181
|
|
|
{ |
182
|
|
|
if ($this->RatingDetails) { |
|
|
|
|
183
|
|
|
$data = (array)json_decode($this->RatingDetails, true); |
184
|
|
|
return ArrayData::create($data); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns a list of whether rating metrics have passed for this addon, and a description of the metric |
190
|
|
|
* |
191
|
|
|
* @return ArrayList |
192
|
|
|
*/ |
193
|
|
|
public function RatingDescriptions() |
194
|
|
|
{ |
195
|
|
|
$metrics = $this->RatingData(); |
196
|
|
|
|
197
|
|
|
return ArrayList::create([ |
198
|
|
|
['Metric' => $metrics->has_readme, 'Description' => 'Readme'], |
199
|
|
|
['Metric' => $metrics->has_license, 'Description' => 'FOSS License'], |
200
|
|
|
['Metric' => $metrics->has_code_or_src_folder, 'Description' => 'Structured correctly'], |
201
|
|
|
['Metric' => $metrics->has_contributing_file, 'Description' => 'Contributing file'], |
202
|
|
|
['Metric' => $metrics->has_gitattributes_file, 'Description' => 'Git attributes file'], |
203
|
|
|
['Metric' => $metrics->has_editorconfig_file, 'Description' => 'Editor config file'], |
204
|
|
|
['Metric' => $metrics->good_code_coverage, 'Description' => 'Good code coverage (>40%)'], |
205
|
|
|
['Metric' => $metrics->great_code_coverage, 'Description' => 'Great code coverage (>75%)'], |
206
|
|
|
['Metric' => $metrics->has_documentation, 'Description' => 'Documentation'], |
207
|
|
|
['Metric' => $metrics->travis_passing, 'Description' => 'Travis passing'], |
208
|
|
|
['Metric' => $metrics->good_scrutinizer_score, 'Description' => 'Scrutinizer >6.5'], |
209
|
|
|
['Metric' => $metrics->coding_standards, 'Description' => 'PSR-2 standards'], |
210
|
|
|
]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @return bool|DateInterval |
218
|
|
|
*/ |
219
|
|
|
public function addonAge() |
220
|
|
|
{ |
221
|
|
|
$date = new DateTime(); |
222
|
|
|
$released = new DateTime($this->Released); |
|
|
|
|
223
|
|
|
|
224
|
|
|
return $date->diff($released); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Calculate the total amount of downloads per day |
229
|
|
|
* Based on the total amount of downloads divided by the age of the addon |
230
|
|
|
* |
231
|
|
|
* @return float |
232
|
|
|
*/ |
233
|
|
|
public function getRelativePopularity() |
234
|
|
|
{ |
235
|
|
|
return (int)$this->Downloads / max((int)$this->addonAge()->days, 1); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Format the relative popularity to a nicely readable number |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function relativePopularityFormatted() |
244
|
|
|
{ |
245
|
|
|
return number_format($this->getRelativePopularity(), 2); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.