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
|
|
|
'HelpfulRobotData' => 'Text', |
26
|
|
|
'HelpfulRobotScore' => 'Int', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
public static $has_one = array( |
30
|
|
|
'Vendor' => 'AddonVendor' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
public static $has_many = array( |
34
|
|
|
'Versions' => 'AddonVersion' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
public static $many_many = array( |
38
|
|
|
'Keywords' => 'AddonKeyword', |
39
|
|
|
'Screenshots' => 'Image', |
40
|
|
|
'CompatibleVersions' => 'SilverStripeVersion' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
public static $default_sort = 'Name'; |
44
|
|
|
|
45
|
|
|
public static $extensions = array( |
46
|
|
|
'SilverStripe\\Elastica\\Searchable' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the addon's versions sorted from newest to oldest. |
51
|
|
|
* |
52
|
|
|
* @return ArrayList |
53
|
|
|
*/ |
54
|
|
|
public function SortedVersions() |
55
|
|
|
{ |
56
|
|
|
$versions = $this->Versions()->toArray(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
usort($versions, function ($a, $b) { |
59
|
|
|
return version_compare($b->Version, $a->Version); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
return new ArrayList($versions); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function MasterVersion() |
66
|
|
|
{ |
67
|
|
|
return $this->Versions()->filter('PrettyVersion', array('dev-master', 'trunk'))->First(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function Authors() |
71
|
|
|
{ |
72
|
|
|
return $this->Versions()->relation('Authors'); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function VendorName() |
76
|
|
|
{ |
77
|
|
|
return substr($this->Name, 0, strpos($this->Name, '/')); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function VendorLink() |
81
|
|
|
{ |
82
|
|
|
return Controller::join_links( |
83
|
|
|
Director::baseURL(), |
84
|
|
|
'add-ons', |
85
|
|
|
$this->VendorName() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function PackageName() |
90
|
|
|
{ |
91
|
|
|
return substr($this->Name, strpos($this->Name, '/') + 1); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function Link() |
95
|
|
|
{ |
96
|
|
|
return Controller::join_links( |
97
|
|
|
Director::baseURL(), |
98
|
|
|
'add-ons', |
99
|
|
|
$this->Name |
|
|
|
|
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function DescriptionText() |
104
|
|
|
{ |
105
|
|
|
return $this->Description; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function RSSTitle() |
109
|
|
|
{ |
110
|
|
|
return sprintf('New module release: %s', $this->Name); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function PackagistUrl() |
114
|
|
|
{ |
115
|
|
|
return "https://packagist.org/packages/$this->Name"; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Remove the effect of code of conduct Helpful Robot measure that we currently don't include in the Supported module definition |
120
|
|
|
* |
121
|
|
|
* @return integer Adjusted Helpful Robot score |
122
|
|
|
*/ |
123
|
|
|
public function getAdjustedHelpfulRobotScore() |
124
|
|
|
{ |
125
|
|
|
return round(min(100, $this->HelpfulRobotScore / 92.9 * 100)); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getElasticaMapping() |
129
|
|
|
{ |
130
|
|
|
return new Mapping(null, array( |
131
|
|
|
'name' => array('type' => 'string'), |
132
|
|
|
'description' => array('type' => 'string'), |
133
|
|
|
'type' => array('type' => 'string'), |
134
|
|
|
'compatibility' => array('type' => 'string'), |
135
|
|
|
'vendor' => array('type' => 'string'), |
136
|
|
|
'tags' => array('type' => 'string'), |
137
|
|
|
'released' => array('type' => 'date'), |
138
|
|
|
'downloads' => array('type' => 'string'), |
139
|
|
|
'readme' => array('type' => 'string') |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getElasticaDocument() |
144
|
|
|
{ |
145
|
|
|
return new Document($this->ID, array( |
146
|
|
|
'name' => $this->Name, |
|
|
|
|
147
|
|
|
'description' => $this->Description, |
|
|
|
|
148
|
|
|
'type' => $this->Type, |
|
|
|
|
149
|
|
|
'compatibility' => $this->CompatibleVersions()->column('Name'), |
|
|
|
|
150
|
|
|
'vendor' => $this->VendorName(), |
151
|
|
|
'tags' => $this->Keywords()->column('Name'), |
|
|
|
|
152
|
|
|
'released' => $this->obj('Released')->Format('c'), |
153
|
|
|
'downloads' => (int)$this->Downloads, |
|
|
|
|
154
|
|
|
'readme' => strip_tags($this->Readme), |
|
|
|
|
155
|
|
|
)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function onBeforeDelete() |
159
|
|
|
{ |
160
|
|
|
parent::onBeforeDelete(); |
161
|
|
|
|
162
|
|
|
// Partially cascade delete. Leave author and keywords in place, |
163
|
|
|
// since they might be related to other addons. |
164
|
|
|
foreach ($this->Screenshots() as $image) { |
|
|
|
|
165
|
|
|
$image->delete(); |
166
|
|
|
} |
167
|
|
|
$this->Screenshots()->removeAll(); |
|
|
|
|
168
|
|
|
|
169
|
|
|
foreach ($this->Versions() as $version) { |
|
|
|
|
170
|
|
|
$version->delete(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->Keywords()->removeAll(); |
|
|
|
|
174
|
|
|
$this->CompatibleVersions()->removeAll(); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getDateCreated() |
178
|
|
|
{ |
179
|
|
|
return date('Y-m-d', strtotime($this->Created)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return ArrayData |
184
|
|
|
*/ |
185
|
|
|
public function HelpfulRobotData() |
186
|
|
|
{ |
187
|
|
|
$data = json_decode($this->HelpfulRobotData, true); |
|
|
|
|
188
|
|
|
|
189
|
|
|
return new ArrayData($data["inspections"][0]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* |
194
|
|
|
* @return bool|DateInterval |
195
|
|
|
*/ |
196
|
|
|
public function addonAge() |
197
|
|
|
{ |
198
|
|
|
$date = new DateTime(); |
199
|
|
|
$released = new DateTime($this->Released); |
|
|
|
|
200
|
|
|
|
201
|
|
|
return $date->diff($released); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Calculate the total amount of downloads per day |
206
|
|
|
* Based on the total amount of downloads divided by the age of the addon |
207
|
|
|
* |
208
|
|
|
* @return float |
209
|
|
|
*/ |
210
|
|
|
public function getRelativePopularity() |
211
|
|
|
{ |
212
|
|
|
return (int)$this->Downloads / max((int)$this->addonAge()->days, 1); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Format the relative popularity to a nicely readable number |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function relativePopularityFormatted() |
221
|
|
|
{ |
222
|
|
|
return number_format($this->getRelativePopularity(), 2); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
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.