forked from joomla/joomla-platform
/
changelog.php
executable file
·448 lines (389 loc) · 10.9 KB
/
changelog.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/php
<?php
/**
* An example command line application built on the Joomla Platform.
*
* To run this example, adjust the executable path above to suite your operating system,
* make this file executable and run the file.
*
* Alternatively, run the file using:
*
* php -f run.php
*
* @package Joomla.Examples
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
// We are a valid Joomla entry point.
define('_JEXEC', 1);
// Setup the path related constants.
define('JPATH_BASE', dirname(__FILE__));
// Bootstrap the application.
require realpath('../libraries/import.php');
/**
* Joomla Platform Changelog builder.
*
* This application builds the HTML version of the Joomla Platform change log from the Github API
* that is used in news annoucements.
*
* @package Joomla.Examples
* @subpackage Changlog
* @since 12.1
*/
class Changelog extends JApplicationCli
{
/**
* The github API object.
*
* @var JGithub
* @since 12.1
*/
protected $api;
/**
* Debug mode switch.
*
* @var boolean
* @since 12.1
*/
protected $debug = false;
/**
* An array of output buffers.
*
* @var array
* @since 12.1
*/
protected $buffers = array();
/**
* Overrides JGithub constructor to initialise the api property.
*
* @param mixed $input An optional argument to provide dependency injection for the application's
* input object. If the argument is a JInputCli object that object will become
* the application's input object, otherwise a default input object is created.
* @param mixed $config An optional argument to provide dependency injection for the application's
* config object. If the argument is a JRegistry object that object will become
* the application's config object, otherwise a default config object is created.
* @param mixed $dispatcher An optional argument to provide dependency injection for the application's
* event dispatcher. If the argument is a JDispatcher object that object will become
* the application's event dispatcher, if it is null then the default event dispatcher
* will be created based on the application's loadDispatcher() method.
*
* @see loadDispatcher()
* @since 11.1
*/
public function __construct(JInputCli $input = null, JRegistry $config = null, JDispatcher $dispatcher = null)
{
parent::__construct($input, $config, $dispatcher);
$options = new JRegistry;
$options->set('headers.Accept', 'application/vnd.github.html+json');
$this->api = new JGithub($options);
}
/**
* Execute the application.
*
* @return void
*
* @since 12.1
*/
public function execute()
{
// Check if we just want the help page.
if ($this->input->get('h'))
{
$this->help();
return;
}
// Check for debug mode.
$this->debug = $this->input->getBool('d');
// Set the maximum number of pages (and runaway failsafe).
$cutoff = 100;
$page = 1;
// Check if we only want to get the latest version information.
$latestOnly = $this->input->get('l');
// Initialise the version cutoffs.
$versions = array(
0 => '12.1',
653 => '11.4',
310 => '11.3',
140 => '11.2',
72 => '11.1',
);
// Initialise arrays and metrics.
$log = array();
$userCount = array();
$pullCount = 0;
$mergedBy = array();
$labelled = array();
// Set the current version.
$version = $versions[0];
while ($cutoff--)
{
// Get a page of the closed issues.
$issues = $this->getIssues($page++);
// Check if we've gone past the last page.
if (empty($issues))
{
break;
}
// Loop through each pull.
foreach ($issues as $i => $issue)
{
$this->out(sprintf(' %03d ', $i + 1), false);
// Check if the issue is a pull request.
if (empty($issue->pull_request->html_url))
{
$this->out('Skipped; no pull request url.');
continue;
}
// Change the version
if (isset($versions[$issue->number]) || ($this->debug && $pullCount > 0))
{
// Populate buffers.
$this->setBuffer("$version.userCount", $userCount);
$this->setBuffer("$version.pullCount", $pullCount, false);
$this->setBuffer("$version.mergedBy", $mergedBy);
$this->setBuffer("$version.labelled", $labelled);
// Reset counters.
$pullCount = 0;
$userCount = array();
$mergedBy = array();
$labelled = array();
// Break if we only want the latest version.
if ($latestOnly || $this->debug)
{
break 2;
}
// Increment version.
$version = $versions[$issue->number];
}
// Get specific information about the pull request.
$data = $this->getPull($issue->number);
// Check if merged.
if (!$data->merged || $data->commits == 0)
{
$this->out(' - not merged.');
continue;
}
// Check if the issue is labelled.
foreach ($issue->labels as $label)
{
if (!isset($labelled[$label->name]))
{
$labelled[$label->name] = array();
}
$labelled[$label->name][] = '<a href="' . $issue->html_url . '">' . $issue->title . '</a>';
}
// Prepare the link to the pull.
$html = '[<a href="' . $issue->html_url . '" title="Closed ' . $issue->closed_at . '">';
$html .= '#' . $issue->number;
$html .= '</a>] <strong>' . $issue->title . '</strong>';
$html .= ' (<a href="https://github.com/' . $issue->user->login . '">' . $issue->user->login . '</a>)';
if (trim($data->body_html))
{
$html .= $data->body_html;
}
$this->setBuffer("$version.log", $html);
if (!isset($userCount[$issue->user->login]))
{
$userCount[$issue->user->login] = 0;
}
$userCount[$issue->user->login]++;
$pullCount++;
if (!isset($mergedBy[$data->merged_by->login]))
{
$mergedBy[$data->merged_by->login] = 0;
}
$mergedBy[$data->merged_by->login]++;
$this->out(' - ok');
}
}
// Check if the output folder exists.
if (!is_dir('./docs'))
{
mkdir('./docs');
}
// Write the file.
file_put_contents('./docs/changelog.html', $this->render($latestOnly ? array($versions[0]) : $versions));
// Close normally.
$this->close();
}
/**
* Display the help text for the app.
*
* @return void
*
* @since 12.1
*/
public function help()
{
$this->out();
$this->out('Joomla Platform Changelog');
$this->out('-------------------------');
$this->out();
$this->out('Usage: ./changelog [-l]');
$this->out();
$this->out(' -l Build the changelog for the latest version only.');
$this->out(' -d Debug mode. Test one issue.');
$this->out();
$this->out('Output is sent to docs/changelog.html');
$this->out();
}
/**
* Gets a named buffer.
*
* @param string $name the name of the buffer.
*
* @return string
*
* @since 12.1
*/
protected function getBuffer($name)
{
if (isset($this->buffers[$name]))
{
return $this->buffers[$name];
}
else
{
return '';
}
}
/**
* Get a page of issue data.
*
* @param integer $page The page number.
*
* @return array
*
* @since 12.1
*/
protected function getIssues($page)
{
$this->out(sprintf('Getting issues page #%02d.', $page));
$this->out(str_pad('', 40, '-'));
$issues = $this->api->issues
->getListByRepository('joomla', 'joomla-platform', null, 'closed', null, null, null, 'updated', 'desc', null, $page, 100);
$this->out(sprintf('Got %s issues.', count($issues)));
return $issues;
}
/**
* Get information about a specific pull request.
*
* @return object
*
* @since 12.1
*/
protected function getPull($id)
{
$this->out(sprintf('Getting info for pull %6d', $id), false);
return $this->api->pulls->get('joomla', 'joomla-platform', $id);
}
/**
* Renders the output.
*
* @param array $versions An array of the versions to render.
*
* @return string
*
* @since 12.1
*/
protected function render(array $versions)
{
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Joomla Platform pull request log</title>
</head>
<body>';
foreach ($versions as $version)
{
// Print the version number.
$html .= PHP_EOL . ' <h1>' . $version . '</h1>';
// Print out the labelled version of the changelog first.
$labelled = $this->getBuffer("$version.labelled");
if ($labelled)
{
foreach ($labelled as $label => $links)
{
$html .= PHP_EOL . "<h2>$label</h2>";
$html .= PHP_EOL . '<ul>';
foreach ($links as $link)
{
$html .= PHP_EOL . "<li>$link</li>";
}
$html .= PHP_EOL . '</ul>';
}
}
// Print out the detailed version of the changelog.
$log = $this->getBuffer("$version.log");
$html .= PHP_EOL . '<h2>The following pull requests made by community contributors were merged:</h2>';
$html .= PHP_EOL . '<ol>';
foreach ($log as $issue)
{
$html .= PHP_EOL . "<li>$issue</li>";
}
$html .= PHP_EOL . '</ol>';
// Print out the user-pull statistics.
$userCount = $this->getBuffer("$version.userCount");
arsort($userCount);
$pullCount = $this->getBuffer("$version.pullCount");
$html .= PHP_EOL . sprintf('<h4>%d pull requests.</h4>', $pullCount);
$html .= PHP_EOL . '<ol>';
foreach ($userCount as $user => $count)
{
$html .= PHP_EOL . sprintf('<li><a href="https://github.com/%1$s">%1$s</a>: %2$d</li>', $user, $count);
}
$html .= PHP_EOL . ' </ol>';
// Print out the admin-merge statistics.
$mergedBy = $this->getBuffer("$version.mergedBy");
arsort($mergedBy);
$html .= PHP_EOL . '<h4>Merged by:</h4>';
$html .= PHP_EOL . '<ol>';
foreach ($mergedBy as $user => $count)
{
$html .= PHP_EOL . sprintf('<li><a href="https://github.com/%1$s">%1$s</a>: %2$d</li>', $user, $count);
}
$html .= PHP_EOL . '</ol>';
}
$html .= PHP_EOL . '</body>';
$html .= PHP_EOL . '</html>';
return $html;
}
/**
* Sets a named buffer.
*
* @param string $name The name of the buffer.
* @param mixed $text The text to put into/append to the buffer.
* @param boolean $append Append to the array buffer.
*
* @return void
*
* @since 12.1
*/
protected function setBuffer($name, $text, $append = true)
{
if (!isset($this->buffers[$name]))
{
$this->buffers[$name] = array();
}
if (is_array($text) || !$append)
{
$this->buffers[$name] = $text;
}
else
{
$this->buffers[$name][] = $text;
}
}
}
// Catch any exceptions thrown.
try
{
JCli::getInstance('Changelog')->execute();
}
catch (Exception $e)
{
// An exception has been caught, just echo the message.
fwrite(STDOUT, $e->getMessage() . "\n");
exit($e->getCode());
}