#!/bin/bash

echo "$0: START"

#set -x
set -e

## example:
#[[ -v SOURCE_WIKI_URL ]] || SOURCE_WIKI_URL='https://www.whonix.org/w'

## XXX
source /usr/share/mediawiki-shell/common

## These variables should be set by the calling script as environment variables.
[[ -v SOURCE_WIKI_URL ]] || missing_variable SOURCE_WIKI_URL For example: SOURCE_WIKI_URL='https://www.whonix.org/w' $0
[[ -v SOURCE_TARGET_API ]] || SOURCE_TARGET_API="${SOURCE_WIKI_URL}/api.php"
[[ -v DESTINATION_WIKI_PAGE ]] || missing_variable DESTINATION_WIKI_PAGE For example: DESTINATION_WIKI_PAGE=File:10b.png $0

## XXX
WIKI_URL="$SOURCE_WIKI_URL"
source /usr/share/mediawiki-shell/wiki-config

echo "$0: INFO: SOURCE_WIKI_URL      : $SOURCE_WIKI_URL"
echo "$0: INFO: SOURCE_TARGET_API    : $SOURCE_TARGET_API"
echo "$0: INFO: DESTINATION_WIKI_PAGE: $DESTINATION_WIKI_PAGE"

## XXX
#WIKI_URL="$SOURCE_WIKI_URL" mw-logout
#WIKI_URL="$SOURCE_WIKI_URL" mw-login

echo "$0: INFO: Requesting revid...         ${TMPFOLDER}/revid.json"

$curl \
   $curl_opts \
   --output "${TMPFOLDER}/revid.json" \
   "${SOURCE_TARGET_API}?format=json&action=query&prop=info%7Cflagged&titles=${DESTINATION_WIKI_PAGE}"

lastrevid=$(cat "${TMPFOLDER}/revid.json" | jq -r ".query.pages[].lastrevid")

if echo "$lastrevid" | grep -qE '^[0-9]+$'; then
    true "Valid number."
else
    echo "$0 ERROR: lastrev not numeric!" >&2
    exit 1
fi

echo "$0: INFO: Requesting csrf_token...    ${TMPFOLDER}/csrf_token.json"

$curl \
   $curl_opts \
   --cookie "$cookie_jar" \
   --cookie-jar "$cookie_jar" \
   --header "Content-Type: application/json" \
   --header "Accept-Language: en-GB" \
   --header "Connection: keep-alive" \
   --output "${TMPFOLDER}/csrf_token.json" \
   --request "POST" \
   "${SOURCE_TARGET_API}?action=query&meta=tokens&type=csrf&format=json"

csrf_token=$(cat "${TMPFOLDER}/csrf_token.json" | jq --raw-output '.query.tokens.csrftoken')

## not login-token
#csrf_token=$(cat "${TMPFOLDER}/login-token.json" | jq --raw-output '.query.tokens.csrftoken')

## Sensitive?
#echo "INFO: XXX csrf_token: $csrf_token"

comment="mediawiki-shell-bot-flagged-revisions-mass-approve"

echo "$0: INFO: Requesting review-result... ${TMPFOLDER}/review-result.json"

$curl \
   $curl_opts \
   --cookie "$cookie_jar" \
   --cookie-jar "$cookie_jar" \
   --output "${TMPFOLDER}/review-result.json" \
   --silent \
   --data-urlencode "token=${csrf_token}" \
   "${SOURCE_TARGET_API}?format=json&action=review&revid=${lastrevid}&flag_accuracy=1&comment=${comment}"

## XXX: Sometimes API login timeout.
# {
#   "error": {
#     "code": "permissiondenied",
#     "info": "You don't have permission to review revisions.",
#     "*": "See https://www.kicksecure.com/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/postorius/lists/mediawiki-api-annoueprecations and breaking changes."
#   }
# }

# {
#   "error": {
#     "code": "internal_api_error_LogicException",
#     "info": "[1d001d90905f962f0a6548bf] Caught exception of type LogicException",
#     "errorclass": "LogicException"
#   }
# }

# $curl \
#    $curl_opts \
#    --cookie "$cookie_jar" \
#    --cookie-jar "$cookie_jar" \
#    --output "${TMPFOLDER}/review-result.json" \
#    --silent \
#    --data-urlencode "token=${csrf_token}" \
#    "${SOURCE_TARGET_API}?format=json&action=stabilize&title=${DESTINATION_WIKI_PAGE}&default=stable&&reason=${comment}"

review_result=$(cat "${TMPFOLDER}/review-result.json" | jq --raw-output '.review.result')

if [ "$review_result" = "Success" ]; then
   echo "$0: INFO: Success."
   exit 0
fi

echo "${TMPFOLDER}/review-result.json"
cat "${TMPFOLDER}/review-result.json" | jq

review_error_code=$(cat "${TMPFOLDER}/review-result.json" | jq --raw-output '.error.code')
if [ "$review_error_code" = "notreviewable" ]; then
   echo "$0: notreviewable"
   exit 0
fi

if [ "$review_error_code" = "permissiondenied" ]; then
   echo "$0: permissiondenied"
   exit 254
fi

echo "$0: Error." >&2
exit 1
