temp(user/mw_create_redirect_day_month.sh):Save changes
[BK-2020-03.git] / user / mw_create_redirect_day_month.sh
CommitLineData
64b184ac
SBS
1#!/usr/bin/env bash
2# Desc: Create text files containing redirect wikicode: [[YYYY-mm-dd]] -> [[YYYY-mm#YYYY-mm-dd]]
3# Usage: script.sh [year]
4# Input: arg1: year (string)
5# Output: none
98c9cec8 6# Version: 0.1.0 (TODO: Support expanded year formats (i.e. ±YYYYYY-mm-dd)
1b4293c5 7# Ref/Attrib: ISO 8601:2004. 4.1.2.4. “Expanded Representations”
64b184ac
SBS
8
9# Example: Running 'mw_create_redirect_day_month.sh 2022' creates
10# directory 'wikicode' in working directory. It then populates it
11# with text files with names ranging from '2022-01-01' through
12# '2022-12-31', taking leap years into account. Each text file
13# contains one line of wikicode redirecting a [[YYYY-mm-dd]] link to
14# [[YYYY-mm#YYYY-mm-dd]]. For example:
15#
16# 2022-02-28.txt:
17# ```
18# #REDIRECT [[2022-02#2022-02-28]]
19# ```
20#
21# This is useful in combination with Mediawiki's
22# 'maintenance/importTextFiles.php' script that imports text files
23# into a wiki, using each text file's name as a new page title; by
24# default it does not clobber (overwrite) pages if they already
25# exist.
26
27yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
28die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
1b4293c5
SBS
29must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
30list_days_of_year_exp() {
31 # Desc: Lists days of year in expanded ISO-8601 ±Y̲Y̲YYYY-mm-dd format
32 # Usage: script.sh [str year]
33 # Input: arg1: year (≥4 digit gregorian calendar year)
34 # Output: stdout: iso-8601 dates (newline delimited)
35 # Depends: bash (5.1.16), GNU Coreutils (8.32; date, printf)
36 # Version: 0.0.1
37 # Ref/Attrib: [1] Linux Bash: How to print leading zeroes on a variable https://bytefreaks.net/gnulinux/bash/linux-bash-how-to-print-leading-zeroes-on-a-variable
38 # Ref/Attrib: ISO 8601:2004. 4.1.2.4. “Expanded Representations”
39
40 # Check arg
41 ## Validate argument count
42 if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi;
43 year="$1";
44
45 ## Validate arg as an integer year
46 pattern='^[0-9]{4,}$'; # !!!! STOPPED WORK HERE 2023-04-11T18:34+00
47 if [[ ! $year =~ $pattern ]]; then die "FATAL:Not a 4-digit year:$year"; fi;
48 if [[ ! $year -gt 1582 ]]; then die "FATAL:Not a gregorian calendar year:$year"; fi;
49
50 yyyy=$year;
51 for (( month = 1; month <= 12; month++ )); do
52 mm="$(printf %02d "$month")"; # two-digit month; See [1]
53 #yell "DEBUG:mm:$mm";
54 for (( day = 1; day <= 31; day++ )); do
55 dd="$(printf %02d "$day")"; # two-digit day; See [1]
56 #yell "DEBUG:dd:$dd";
57 date_iso8601="$yyyy-$mm-$dd";
58 if date --date="$date_iso8601" 1>/dev/random 2>&1; then
59 printf "%s\n" "$date_iso8601";
60 fi;
61 done;
62 done;
63}; # iso-8601 dates (yyyy-mm-dd) to stdout
64b184ac
SBS
64list_days_of_year() {
65 # Desc: Lists days of year in ISO-8601 YYYY-mm-dd format
66 # Usage: script.sh [str year]
67 # Input: arg1: year (4 digit gregorian calendar year)
68 # Output: stdout: iso-8601 dates (newline delimited)
69 # Depends: bash (5.1.16), GNU Coreutils (8.32; date, printf)
70 # Version: 0.0.1
71 # Ref/Attrib: [1] Linux Bash: How to print leading zeroes on a variable https://bytefreaks.net/gnulinux/bash/linux-bash-how-to-print-leading-zeroes-on-a-variable
72
73 # Check arg
74 ## Validate argument count
75 if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi;
76 year="$1";
77
78 ## Validate arg is a 4-digit year
79 pattern='^[0-9]{4}$';
80 if [[ ! $year =~ $pattern ]]; then die "FATAL:Not a 4-digit year."; fi;
81 if [[ ! $year -gt 1582 ]]; then die "FATAL:Not a gregorian calendar year."; fi;
82
83 yyyy=$year;
84 for (( month = 1; month <= 12; month++ )); do
85 mm="$(printf %02d "$month")"; # two-digit month; See [1]
86 #yell "DEBUG:mm:$mm";
87 for (( day = 1; day <= 31; day++ )); do
88 dd="$(printf %02d "$day")"; # two-digit day; See [1]
89 #yell "DEBUG:dd:$dd";
90 date_iso8601="$yyyy-$mm-$dd";
91 if date --date="$date_iso8601" 1>/dev/random 2>&1; then
92 printf "%s\n" "$date_iso8601";
93 fi;
94 done;
95 done;
96}; # iso-8601 dates (yyyy-mm-dd) to stdout
97main() {
98 dir_out="wikicode";
99 year="$1";
100
101 # check args
102 ## check output directory
103 if [[ ! -d $dir_out ]]; then mkdir "$dir_out"; fi;
104
105 ## check arg count
106 if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi;
107
108 ## check year
98c9cec8 109 pattern='^[0-9]{4}$';
64b184ac
SBS
110 if [[ ! $year =~ $pattern ]]; then die "FATAL:Not a 4-digit year."; fi;
111 if [[ ! $year -gt 1583 ]]; then die "FATAL:Not a gregorian calendar year."; fi;
112
113 # Cycle through each day of year
114 while read -r yyyymmdd; do
115 #yell "DEBUG:yyyymmdd:$yyyymmdd";
116 ## Form wikicode redirect line
117 wc_body="#REDIRECT [[${yyyymmdd%-*}#$yyyymmdd]]"; # e.g. "#REDIRECT [[2022-02#2022-02-28]"
118 #yell "DEBUG:wc_body:$wc_body";
119
120 ## Write wikicode to text file named with iso-8601 date (yyyy-mm-dd)
121 path_out="$dir_out/$yyyymmdd";
122 #yell "DEBUG:path_out:$path_out.txt";
123 printf "%s\n" "$wc_body" > "$path_out";
124 done < <(list_days_of_year "$year");
125
126 return 0;
127}; # main program
128
129main "$@";