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)
6 # Version: 0.1.0 (TODO: Support expanded year formats (i.e. ±YYYYYY-mm-dd)
7 # Ref/Attrib: ISO 8601:2004. 4.1.2.4. “Expanded Representations”
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:
18 # #REDIRECT [[2022-02#2022-02-28]]
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
27 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
28 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
29 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
30 list_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)
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”
41 ## Validate argument count
42 if [[ $# -ne 1 ]]; then die
"FATAL:Invalid number of arguments:$#"; fi;
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;
51 for (( month
= 1; month
<= 12; month
++ )); do
52 mm
="$(printf %02d "$month")"; # two-digit month; See [1]
54 for (( day
= 1; day
<= 31; day
++ )); do
55 dd="$(printf %02d "$day")"; # two-digit day; See [1]
57 date_iso8601
="$yyyy-$mm-$dd";
58 if date --date="$date_iso8601" 1>/dev
/random
2>&1; then
59 printf "%s\n" "$date_iso8601";
63 }; # iso-8601 dates (yyyy-mm-dd) to stdout
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)
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
74 ## Validate argument count
75 if [[ $# -ne 1 ]]; then die
"FATAL:Invalid number of arguments:$#"; fi;
78 ## Validate arg is a 4-digit year
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;
84 for (( month
= 1; month
<= 12; month
++ )); do
85 mm
="$(printf %02d "$month")"; # two-digit month; See [1]
87 for (( day
= 1; day
<= 31; day
++ )); do
88 dd="$(printf %02d "$day")"; # two-digit day; See [1]
90 date_iso8601
="$yyyy-$mm-$dd";
91 if date --date="$date_iso8601" 1>/dev
/random
2>&1; then
92 printf "%s\n" "$date_iso8601";
96 }; # iso-8601 dates (yyyy-mm-dd) to stdout
102 ## check output directory
103 if [[ ! -d $dir_out ]]; then mkdir
"$dir_out"; fi;
106 if [[ $# -ne 1 ]]; then die
"FATAL:Invalid number of arguments:$#"; fi;
109 pattern
='^[0-9]{4}$';
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;
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";
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");