-
Notifications
You must be signed in to change notification settings - Fork 4
/
fields.php
93 lines (70 loc) · 2.9 KB
/
fields.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
<?php
return [
'gitLog' => [
'computed' => [
'gitLog' => function () {
// Gather all commits and format as valid JSOn
$log = site()->git()->getRepo()->log('{%n \"hash\": \"%h\",%n \"date\": \"%at\",%n \"message\": \"%s\",%n \"author\": \"%an\"},');
$log = rtrim($log,",");
$log = "[{$log}]";
$log = json_decode($log, true);
foreach($log as $idx => $commit) {
// Format date
$date = $commit['date'];
$log[$idx]['dateFormatted'] = date("Y-m-d, H:i", $date);
}
return $log;
}
]
],
'gitRevisions' => [
'computed' => [
// 'blueprintFields' => function () {
// return $this->model()->blueprint();
// },
'gitRevisions' => function () {
$gitHelper = site()->git();
$parent = $this->model();
// Get the relative content file path
$contentFile = $parent->contentFile();
$contentFile = substr($contentFile, strlen($gitHelper->path()) + 1);
// Get all commits where the content file was modified
$commitFormat = '{%n \"hash\": \"%h\",%n \"date\" : \"%at\"%n,%n \"message\": \"%s\",%n \"author\": \"%an\"},';
$logCommand = 'log --follow --name-only --pretty=format:"' . $commitFormat . '" -- ' . $contentFile;
$revisions = $gitHelper->getRepo()->run($logCommand);
// Gather all former paths/filenames
$matches = [];
$pattern = '/\},\s*(.*)\s*/';
preg_match_all($pattern, $revisions, $matches, PREG_SET_ORDER);
$revisions = preg_replace($pattern, '},' , $revisions);
// Format as valid Object
$revisions = rtrim($revisions,",");
$revisions = "[{$revisions}]";
$revisions = json_decode($revisions, true);
foreach($revisions as $idx => $revision) {
// Map gathered names
$formerContentFile = $matches[$idx][1];
$revisions[$idx]['path'] = $formerContentFile;
// Format date
$date = $revision['date'];
$revisions[$idx]['dateFormatted'] = date("Y-m-d, H:i", $date);
// Get (former) template from filename
$formerTemplate = basename(basename($formerContentFile, ".txt"), ".md");
$revisions[$idx]['template'] = $formerTemplate;
// Gather and decode content
$revisionCommand = "show {$revision['hash']}:{$formerContentFile}";
$revisionContent = $gitHelper->getRepo()->run($revisionCommand);
$revisionContent = Kirby\Data\Txt::decode($revisionContent);
$virtualPage = new Kirby\Cms\Page([
'template' => $formerTemplate,
'content' => $revisionContent,
'slug' => 'totally-irrelevant'
]);
$revisionContent = Kirby\Cms\Form::for($virtualPage)->values();
$revisions[$idx]['content'] = $revisionContent;
}
return $revisions;
}
]
],
];