#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

# shellcheck source=../share/mediawiki-shell/common
source /usr/share/mediawiki-shell/common

usage() {
  printf '%s\n' "Usage: ${0##*/} WIKI PAGE
Example:
  ${0##*/} 'https://www.kicksecure.com/w' About
  ${0##*/} 'https://www.kicksecure.com/w' About" >&2
  exit 1
}

if [[ -z "${2-}" || "${1-}" =~ (-h|--help) ]]; then
  usage
fi

WIKI_URL="$1"
page_title="$2"

## Defense-in-depth: check page_title for malicious unicode before
## interpolating it into API URLs.
printf '%s\n' "$page_title" | unicode-show

# shellcheck source=../share/mediawiki-shell/wiki-config
source /usr/share/mediawiki-shell/wiki-config

log info "Checking for pending changes for file..."

page_pending_status_json="$(
  curl_run \
    "${curl_opts[@]}" \
    --get \
    --data-urlencode "titles=$page_title" \
    "${WIKI_API}?format=json&action=query&prop=info|flagged"
)"

log debug "$page_pending_status_json: '$page_pending_status_json'"

## Use a jq path query, not 'grep -- pending_since' against the
## pretty-printed JSON: the latter false-positive matches any page
## whose title contains the substring "pending_since", which would
## let an editor silently keep a page out of backups by naming it
## accordingly.
if jq -e '.query.pages[]? | .flagged?.pending_since? // empty' <<<"$page_pending_status_json" >/dev/null 2>&1; then
  die 10 "'$WIKI_URL' '$page_title' page has PENDING EDITS!"
fi

log info "No pending edits for file, ok."
