X-Git-Url: https://git.wikimedia.ca/?p=eccc_to_commons.git;a=blobdiff_plain;f=mediawiki_post.sh;fp=mediawiki_post.sh;h=b0d0127dea98f366acf7258281de918998b2e93f;hp=0000000000000000000000000000000000000000;hb=ea933e3293cb98e64656291c69d8f098104b8256;hpb=c4fdff03a380cd239b55e8d5f57817b967d1449d
diff --git a/mediawiki_post.sh b/mediawiki_post.sh
new file mode 100755
index 0000000..b0d0127
--- /dev/null
+++ b/mediawiki_post.sh
@@ -0,0 +1,128 @@
+#!/bin/bash
+
+# mediawiki_post - Recursively send files in a directory to a Mediawiki instance
+# Copyright (C) 2020 Pierre Choffet
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+
+set -ex
+set -o pipefail
+
+ENDPOINT='https://commons.wikimedia.org/w/api.php'
+NAMESPACE='Data'
+
+USERNAME_PATH='login_username'
+PASSWORD_PATH='login_password'
+LOGIN_TOKEN_PATH='login_token'
+CSRF_TOKEN_PATH='csrf_token'
+COOKIE_JAR='cookie_jar'
+
+SOURCE="${1}"
+
+readLoginToken() {
+ LOGIN_TOKEN=$(cat "${LOGIN_TOKEN_PATH}")
+ LOGIN_TOKEN="${LOGIN_TOKEN/+/%2B}"
+ LOGIN_TOKEN="${LOGIN_TOKEN/\\/%5C}"
+}
+
+readCSRFToken() {
+ CSRF_TOKEN=$(cat "${CSRF_TOKEN_PATH}")
+ CSRF_TOKEN="${CSRF_TOKEN/+/%2B}"
+ CSRF_TOKEN="${CSRF_TOKEN/\\/%5C}"
+}
+
+requestLoginToken() {
+ local -r body=$(curl -X POST -d 'action=query' -d 'meta=tokens' -d 'type=login' \
+ -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
+ local -r login_token="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@logintoken -)"
+ echo "${login_token}"
+}
+
+requestCSRFToken() {
+ local -r body=$(curl -X POST -d 'action=query' -d 'meta=tokens' \
+ -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
+ local -r csrf_token="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@csrftoken -)"
+ echo "${csrf_token}"
+}
+
+login() {
+ if [ ! -f "${LOGIN_TOKEN_PATH}" ]
+ then
+ requestLoginToken > "${LOGIN_TOKEN_PATH}"
+ fi
+
+ readLoginToken
+
+ if [ ! -f "${USERNAME_PATH}" ]||[ ! -f "${PASSWORD_PATH}" ]
+ then
+ echo "What wiki account to use?"
+ read -p 'Username: ' USERNAME
+ read -sp 'Password: ' PASSWORD
+ else
+ USERNAME="$(cat "${USERNAME_PATH}")"
+ PASSWORD="$(cat "${PASSWORD_PATH}")"
+ fi
+
+ local -r body=$(curl -X POST -d 'action=login' --data-urlencode "lgname=${USERNAME}" --data-urlencode "lgpassword=${PASSWORD}" -d "lgtoken=${LOGIN_TOKEN}" -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
+ local -r result=$(echo "${body}" | xmlstarlet sel -t -v '//login/@result' -)
+
+ case "${result}" in
+ NeedToken)
+ requestLoginToken > "${LOGIN_TOKEN_PATH}"
+ readLoginToken
+ ;;
+ Success)
+ echo "${USERNAME}" > "${USERNAME_PATH}"
+ echo "${PASSWORD}" > "${PASSWORD_PATH}"
+ echo 'Logged in.'
+ ;;
+ Failed)
+ echo 'Login failed. Wrong credentials?'
+ exit 1
+ ;;
+ *)
+ echo "Unknown login result: ${result}. Exiting."
+ exit 1
+ esac
+}
+
+if [ -z "${SOURCE}" ]
+then
+ echo 'Upload files to Mediawiki.'
+ echo 'Usage: mediawiki_post.sh