| 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 |
| 6 | # Version: 0.0.1 |
| 7 | |
| 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: |
| 14 | # |
| 15 | # 2022-02-28.txt: |
| 16 | # ``` |
| 17 | # #REDIRECT [[2022-02#2022-02-28]] |
| 18 | # ``` |
| 19 | # |
| 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 |
| 24 | # exist. |
| 25 | |
| 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 |
| 29 | list_days_of_year() { |
| 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) |
| 35 | # Version: 0.0.1 |
| 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 |
| 37 | |
| 38 | # Check arg |
| 39 | ## Validate argument count |
| 40 | if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi; |
| 41 | year="$1"; |
| 42 | |
| 43 | ## Validate arg is a 4-digit year |
| 44 | pattern='^[0-9]{4}$'; |
| 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; |
| 47 | |
| 48 | yyyy=$year; |
| 49 | for (( month = 1; month <= 12; month++ )); do |
| 50 | mm="$(printf %02d "$month")"; # two-digit month; See [1] |
| 51 | #yell "DEBUG:mm:$mm"; |
| 52 | for (( day = 1; day <= 31; day++ )); do |
| 53 | dd="$(printf %02d "$day")"; # two-digit day; See [1] |
| 54 | #yell "DEBUG:dd:$dd"; |
| 55 | date_iso8601="$yyyy-$mm-$dd"; |
| 56 | if date --date="$date_iso8601" 1>/dev/random 2>&1; then |
| 57 | printf "%s\n" "$date_iso8601"; |
| 58 | fi; |
| 59 | done; |
| 60 | done; |
| 61 | }; # iso-8601 dates (yyyy-mm-dd) to stdout |
| 62 | main() { |
| 63 | dir_out="wikicode"; |
| 64 | year="$1"; |
| 65 | |
| 66 | # check args |
| 67 | ## check output directory |
| 68 | if [[ ! -d $dir_out ]]; then mkdir "$dir_out"; fi; |
| 69 | |
| 70 | ## check arg count |
| 71 | if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi; |
| 72 | |
| 73 | ## check year |
| 74 | pattern='^[0-9]{4}$'; |
| 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; |
| 77 | |
| 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"; |
| 84 | |
| 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"); |
| 90 | |
| 91 | return 0; |
| 92 | }; # main program |
| 93 | |
| 94 | main "$@"; |