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)
8 # Example: Running 'mw_create_redirect_day_month.sh 2022' creates
9 # directory 'wikicode' in working directory. It then populates it
10 # with text files with names ranging from '2022-01-01' through
11 # '2022-12-31', taking leap years into account. Each text file
12 # contains one line of wikicode redirecting a [[YYYY-mm-dd]] link to
13 # [[YYYY-mm#YYYY-mm-dd]]. For example:
17 # #REDIRECT [[2022-02#2022-02-28]]
20 # This is useful in combination with Mediawiki's
21 # 'maintenance/importTextFiles.php' script that imports text files
22 # into a wiki, using each text file's name as a new page title; by
23 # default it does not clobber (overwrite) pages if they already
26 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
27 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
28 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
30 # Desc: Lists days of year in ISO-8601 YYYY-mm-dd format
31 # Usage: script.sh [str year]
32 # Input: arg1: year (4 digit gregorian calendar year)
33 # Output: stdout: iso-8601 dates (newline delimited)
34 # Depends: bash (5.1.16), GNU Coreutils (8.32; date, printf)
36 # 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
39 ## Validate argument count
40 if [[ $# -ne 1 ]]; then die
"FATAL:Invalid number of arguments:$#"; fi;
43 ## Validate arg is a 4-digit year
45 if [[ ! $year =~
$pattern ]]; then die
"FATAL:Not a 4-digit year."; fi;
46 if [[ ! $year -gt 1582 ]]; then die
"FATAL:Not a gregorian calendar year."; fi;
49 for (( month
= 1; month
<= 12; month
++ )); do
50 mm
="$(printf %02d "$month")"; # two-digit month; See [1]
52 for (( day
= 1; day
<= 31; day
++ )); do
53 dd="$(printf %02d "$day")"; # two-digit day; See [1]
55 date_iso8601
="$yyyy-$mm-$dd";
56 if date --date="$date_iso8601" 1>/dev
/random
2>&1; then
57 printf "%s\n" "$date_iso8601";
61 }; # iso-8601 dates (yyyy-mm-dd) to stdout
67 ## check output directory
68 if [[ ! -d $dir_out ]]; then mkdir
"$dir_out"; fi;
71 if [[ $# -ne 1 ]]; then die
"FATAL:Invalid number of arguments:$#"; fi;
75 if [[ ! $year =~
$pattern ]]; then die
"FATAL:Not a 4-digit year."; fi;
76 if [[ ! $year -gt 1583 ]]; then die
"FATAL:Not a gregorian calendar year."; fi;
78 # Cycle through each day of year
79 while read -r yyyymmdd
; do
80 #yell "DEBUG:yyyymmdd:$yyyymmdd";
81 ## Form wikicode redirect line
82 wc_body
="#REDIRECT [[${yyyymmdd%-*}#$yyyymmdd]]"; # e.g. "#REDIRECT [[2022-02#2022-02-28]"
83 #yell "DEBUG:wc_body:$wc_body";
85 ## Write wikicode to text file named with iso-8601 date (yyyy-mm-dd)
86 path_out
="$dir_out/$yyyymmdd";
87 #yell "DEBUG:path_out:$path_out.txt";
88 printf "%s\n" "$wc_body" > "$path_out";
89 done < <(list_days_of_year
"$year");