]>
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'
23 ENDPOINT
='http://mediawiki.banquise.tf/api.php'
26 USERNAME_PATH
='login_username'
27 PASSWORD_PATH
='login_password'
28 LOGIN_TOKEN_PATH
='login_token'
29 CSRF_TOKEN_PATH
='csrf_token'
30 COOKIE_JAR
='cookie_jar'
35 LOGIN_TOKEN
=$(cat "${LOGIN_TOKEN_PATH}")
36 LOGIN_TOKEN
="${LOGIN_TOKEN/+/%2B}"
37 LOGIN_TOKEN
="${LOGIN_TOKEN/\\/%5C}"
41 CSRF_TOKEN
=$(cat "${CSRF_TOKEN_PATH}")
42 CSRF_TOKEN
="${CSRF_TOKEN/+/%2B}"
43 CSRF_TOKEN
="${CSRF_TOKEN/\\/%5C}"
47 local -r body
=$
(curl
-X POST
-d 'action=query' -d 'meta=tokens' -d 'type=login' \
48 -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
49 local -r login_token
="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@logintoken -)"
54 local -r body
=$
(curl
-X POST
-d 'action=query' -d 'meta=tokens' \
55 -d 'format=xml' -b "${COOKIE_JAR}" -c "${COOKIE_JAR}" "${ENDPOINT}")
56 local -r csrf_token
="$(echo "${body}" | xmlstarlet sel -t -v //tokens/@csrftoken -)"
61 if [ ! -f "${LOGIN_TOKEN_PATH}" ]
63 requestLoginToken
> "${LOGIN_TOKEN_PATH}"
68 if [ ! -f "${USERNAME_PATH}" ]||[ ! -f "${PASSWORD_PATH}" ]
70 echo "What wiki account to use?"
71 read -p 'Username: ' USERNAME
72 read -sp 'Password: ' PASSWORD
74 USERNAME
="$(cat "${USERNAME_PATH}")"
75 PASSWORD
="$(cat "${PASSWORD_PATH}")"
78 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}")
79 local -r result
=$(echo "${body}" | xmlstarlet sel -t -v '//login/@result' -)
83 requestLoginToken
> "${LOGIN_TOKEN_PATH}"
87 echo "${USERNAME}" > "${USERNAME_PATH}"
88 echo "${PASSWORD}" > "${PASSWORD_PATH}"
92 echo 'Login failed. Wrong credentials?'
96 echo "Unknown login result: ${result}. Exiting."
101 if [ -z "${SOURCE}" ]
103 echo 'Upload files to Mediawiki.'
104 echo 'Usage: mediawiki_post.sh <source folder>'
110 requestCSRFToken
> "${CSRF_TOKEN_PATH}"
113 while IFS
= read -r -d '' -u 9
115 URI_PATH
=${NAMESPACE}:$(realpath --relative-to="${SOURCE}" "${REPLY}")
116 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}")
117 RESULT
=$(echo "${BODY}" | xmlstarlet sel -t -v '/api/edit/@result' -)
121 echo "Everything went right. Continue…"
124 echo "Unknown code: ${RESULT}. Exiting."
127 done 9< <( find "${SOURCE}" -type f
-name '*.tab' -print0 )