]>
Wikimedia Canada | Git repositories - eccc_to_commons.git/blob - mediawiki_post.sh
3 # mediawiki_post - Recursively send files in a directory to a Mediawiki instance
4 # Copyright (C) 2020 Pierre Choffet
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ENDPOINT
='https://commons.wikimedia.org/w/api.php'
25 USERNAME_PATH
='login_username'
26 PASSWORD_PATH
='login_password'
27 LOGIN_TOKEN_PATH
='login_token'
28 CSRF_TOKEN_PATH
='csrf_token'
29 COOKIE_JAR
='cookie_jar'
34 LOGIN_TOKEN
=$(cat "${LOGIN_TOKEN_PATH}")
35 LOGIN_TOKEN
="${LOGIN_TOKEN/+/%2B}"
36 LOGIN_TOKEN
="${LOGIN_TOKEN/\\/%5C}"
40 CSRF_TOKEN
=$(cat "${CSRF_TOKEN_PATH}")
41 CSRF_TOKEN
="${CSRF_TOKEN/+/%2B}"
42 CSRF_TOKEN
="${CSRF_TOKEN/\\/%5C}"
46 local -r body
=$
(curl
-X POST
-d 'action=query' -d 'meta=tokens' -d 'type=login' \
47 -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
48 local -r login_token
="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@logintoken -)"
53 local -r body
=$
(curl
-X POST
-d 'action=query' -d 'meta=tokens' \
54 -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
55 local -r csrf_token
="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@csrftoken -)"
60 if [ ! -f "${LOGIN_TOKEN_PATH}" ]
62 requestLoginToken
> "${LOGIN_TOKEN_PATH}"
67 if [ ! -f "${USERNAME_PATH}" ]||[ ! -f "${PASSWORD_PATH}" ]
69 echo "What wiki account to use?"
70 read -p 'Username: ' USERNAME
71 read -sp 'Password: ' PASSWORD
73 USERNAME
="$(cat "${USERNAME_PATH}")"
74 PASSWORD
="$(cat "${PASSWORD_PATH}")"
77 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}")
78 local -r result
=$(echo "${body}" | xmlstarlet sel -t -v '//login/@result' -)
82 requestLoginToken
> "${LOGIN_TOKEN_PATH}"
86 echo "${USERNAME}" > "${USERNAME_PATH}"
87 echo "${PASSWORD}" > "${PASSWORD_PATH}"
91 echo 'Login failed. Wrong credentials?'
95 echo "Unknown login result: ${result}. Exiting."
100 if [ -z "${SOURCE}" ]
102 echo 'Upload files to Mediawiki.'
103 echo 'Usage: mediawiki_post.sh <source folder>'
109 requestCSRFToken
> "${CSRF_TOKEN_PATH}"
112 while IFS
= read -r -d '' -u 9
114 URI_PATH
=${NAMESPACE}:$(realpath --relative-to="${SOURCE}" "${REPLY}")
115 BODY
=$(curl -X POST -d 'action=edit' --data-urlencode "title=${URI_PATH}" --data-urlencode "text@${REPLY}" -d "token=${CSRF_TOKEN}" -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
116 RESULT
=$(echo "${BODY}" | xmlstarlet sel -t -v '/api/edit/@result' -)
120 echo "Everything went right. Continue…"
123 echo "Unknown code: ${RESULT}. Exiting."
128 done 9< <( find "${SOURCE}" -type f
-print0 )