-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathChangeLogController.php
More file actions
131 lines (108 loc) · 4.62 KB
/
ChangeLogController.php
File metadata and controls
131 lines (108 loc) · 4.62 KB
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
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers;
use PhpMyAdmin\Config;
use PhpMyAdmin\Core;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Routing\Route;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use function __;
use function array_column;
use function array_keys;
use function basename;
use function file_get_contents;
use function htmlspecialchars;
use function is_readable;
use function ob_get_clean;
use function ob_start;
use function preg_match_all;
use function preg_replace;
use function preg_replace_callback;
use function readgzfile;
use function sprintf;
use function str_ends_with;
use const PREG_SET_ORDER;
#[Route('/changelog', ['GET'])]
final readonly class ChangeLogController implements InvocableController
{
public function __construct(
private ResponseRenderer $response,
private Config $config,
private ResponseFactory $responseFactory,
private Template $template,
) {
}
public function __invoke(ServerRequest $request): Response
{
$response = $this->responseFactory->createResponse();
foreach ($this->response->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
}
$filename = $this->config->getChangeLogFilePath();
/**
* Read changelog.
*/
// Check if the file is available, some distributions remove these.
if (! @is_readable($filename)) {
return $response->write(sprintf(
__('The %s file is not available on this system, please visit %s for more information.'),
basename($filename),
'<a href="' . Core::linkURL('https://www.phpmyadmin.net/')
. '" rel="noopener noreferrer" target="_blank">phpmyadmin.net</a>',
));
}
// Test if the file is in a compressed format
if (str_ends_with($filename, '.gz')) {
ob_start();
readgzfile($filename);
$changelog = ob_get_clean();
} else {
$changelog = file_get_contents($filename);
}
/**
* Whole changelog in variable.
*/
$changelog = htmlspecialchars((string) $changelog);
$githubUrl = 'https://github.com/phpmyadmin/phpmyadmin/';
$faqUrl = 'https://docs.phpmyadmin.net/en/latest/faq.html';
preg_match_all('@\n\[(\d+\.\d+\.\d+)]: (' . $githubUrl . '\S+)@', $changelog, $matches, PREG_SET_ORDER);
$releaseLinks = array_column($matches, 2, 1);
$changelog = (string) preg_replace_callback(
'/\n## \[(\d+\.\d+\.\d+)]/',
static fn (array $matches): string => "\n## [" . $matches[1] . '](' . $releaseLinks[$matches[1]] . ')',
$changelog,
);
$replaces = [
'/# Changes in phpMyAdmin (\d+\.\d+)/' => '<h1>Changes in phpMyAdmin \\1</h1>',
'@\[Keep a Changelog\]\(https://keepachangelog.com/\)@' => 'Keep a Changelog',
'/\n### (Added|Changed|Deprecated|Removed|Fixed|Security)/' => "\n" . '<h3>\\1</h3>',
// Add link to release title
'@\n## \[(\d+\.\d+\.\d+)\]\((' . $githubUrl . '\S+)\) \- (\d{4}|YYYY)-(\d{2}|MM)-(\d{2}|DD)\n@' => "\n"
. '<h2><a href="' . Url::getFromRoute('/url') . '&url=\\2">\\1</a> \\3-\\4-\\5</h2>' . "\n",
// Add link to GitHub issues/commits
'@\[(\#\d+|[a-z0-9]+)\]\((' . $githubUrl . '\S+)\)@' => '<a href="'
. Url::getFromRoute('/url') . '&url=\\2">\\1</a>',
// FAQ entries
'/FAQ ([0-9]+)\.([0-9a-z]+)/i' => '<a href="'
. Url::getFromRoute('/url') . '&url=' . $faqUrl . '#faq\\1-\\2">FAQ \\1.\\2</a>',
// CVE/CAN entries
'/((CAN|CVE)-[0-9]+-[0-9]+)/' => '<a href="' . Url::getFromRoute('/url') . '&url='
. 'https://www.cve.org/CVERecord?id=\\1">\\1</a>',
// PMASA entries
'/(PMASA-[0-9]+-[0-9]+)/' => '<a href="'
. Url::getFromRoute('/url') . '&url=https://www.phpmyadmin.net/security/\\1/">\\1</a>',
// Links target and rel
'/a href="/' => 'a target="_blank" rel="noopener noreferrer" href="',
// Remove release link references
'@\n\[(\d+\.\d+\.\d+)]: (' . $githubUrl . '\S+)@' => '',
'/YYYY-MM-DD/' => '(not yet released)',
];
return $response->write($this->template->render('changelog', [
'changelog' => preg_replace(array_keys($replaces), $replaces, $changelog),
]));
}
}