-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag-adder.php
More file actions
53 lines (38 loc) · 1.26 KB
/
tag-adder.php
File metadata and controls
53 lines (38 loc) · 1.26 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
<?php
require 'loader.php';
// must be run from the command line
if (php_sapi_name() != 'cli') {
die('CLI only');
}
$directories = glob('web/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$just_slug = str_replace('web/', '', $directory);
$yaml_file = "$directory/$just_slug.yaml";
$yaml = Spyc::YAMLLoad(file_get_contents($yaml_file));
if (!isset($yaml['tags'])) {
$yaml['tags'] = [];
}
$tag_count = count($yaml['tags']);
if ($tag_count === 0) {
printf("No tags for %s\n", $just_slug);
print "Page is at " . $yaml['canonical_url'] . "\n";
print "Enter tags, separated by commas, or just enter to skip, q to exit: ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) == 'q') {
die("Exiting\n");
}
if (trim($line) == '') {
continue;
}
$tags = explode(',', $line);
$tags = array_map('trim', $tags);
$tags = array_unique($tags);
sort($tags);
$yaml['tags'] = $tags;
file_put_contents($yaml_file, Spyc::YAMLDump($yaml));
printf("Tags added for %s\n", $just_slug);
} else {
// printf("Tags already exist for %s\n", $just_slug);
}
}