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