API:Client code/Bash

Append text to a page and upload an image (Mediawiki version 1.31.1) edit

I merged and updated the outdated examples in this page.

You need curl and jq.

--Karima Rafes (talk) 13:00, 27 December 2018 (UTC)

#!/usr/bin/env bash

#Needs curl and jq

USERNAME="testWiki"
USERPASS="test"
WIKI="http://wiki.serverdev-mediawiki2-v1"
WIKIAPI="http://wiki.serverdev-mediawiki2-v1/api.php"
folder="/tmp"

PAGE="Title of an article"

FILENAME="Wikidata_.png"
FILEPATH="/var/www/images/6/68/Wikidata_.png"
FILECOMMENT="comment"
FILETEXT="text"


cookie_jar="${folder}/wikicj"

#Will store file in wikifile
echo "UTF8 check: ☠"
#################login
echo "Logging into $WIKIAPI as $USERNAME..."

###############
#Login part 1
#printf "%s" "Logging in (1/2)..."
echo "Get login token..."
CR=$(curl -S \
	--location \
	--retry 2 \
	--retry-delay 5\
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=login&format=json")

echo "$CR" | jq .

rm ${folder}/login.json
echo "$CR" > ${folder}/login.json
TOKEN=$(jq --raw-output '.query.tokens.logintoken'  ${folder}/login.json)

if [ "$TOKEN" == "null" ]; then
	echo "Getting a login token failed."
	exit
else
	echo "Login token is $TOKEN"
	echo "-----"
fi

###############
#Login part 2

echo "Logging in..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
    --cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "username=${USERNAME}" \
	--data-urlencode "password=${USERPASS}" \
	--data-urlencode "rememberMe=1" \
	--data-urlencode "logintoken=${TOKEN}" \
	--data-urlencode "loginreturnurl=${WIKI}" \
	--request "POST" "${WIKIAPI}?action=clientlogin&format=json")

echo "$CR" | jq .

STATUS=$(echo $CR | jq '.clientlogin.status')
if [[ $STATUS == *"PASS"* ]]; then
	echo "Successfully logged in as $USERNAME, STATUS is $STATUS."
	echo "-----"
else
	echo "Unable to login, is logintoken ${TOKEN} correct?"
	exit
fi

###############
#Get edit token
echo "Fetching edit token..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=csrf&format=json")

echo "$CR" | jq .
echo "$CR" > ${folder}/edittoken.json
EDITTOKEN=$(jq --raw-output '.query.tokens.csrftoken' ${folder}/edittoken.json)
rm ${folder}/edittoken.json

#Remove carriage return!
if [[ $EDITTOKEN == *"+\\"* ]]; then
	echo "Edit token is: $EDITTOKEN"
else
	echo "Edit token not set."
	exit
fi

###############
echo "Make a test edit"
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "title=${PAGE}" \
	--data-urlencode "appendtext={{nocat|2017|01|31}}" \
	--data-urlencode "token=${EDITTOKEN}" \
	--request "POST" "${WIKIAPI}?action=edit&format=json")

echo "$CR" | jq .

###############
echo "Make a test upload"

CR=$(curl -S \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--header "Expect:" \
	--form "token=${EDITTOKEN}" \
	--form "filename=${FILENAME}" \
	--form "text=${FILETEXT}" \
	--form "comment=${FILECOMMENT}" \
	--form "file=@${FILEPATH}" \
	--form "ignorewarnings=yes" \
	--request "POST" "${WIKIAPI}?action=upload&format=json&")

echo "$CR" | jq .

Append text to a page edit

The file upload example didn't work anymore. I've updated the code and changed it so it can append text to a page. Smile4ever (talk) 10:29, 31 January 2017 (UTC)

#!/usr/bin/env bash

#Needs curl and jq

USERNAME="Smile4ever"
USERPASS="******"
PAGE="Title of an article"
WIKIAPI="https://en.wikipedia.org/w/api.php"
cookie_jar="wikicj"
#Will store file in wikifile

echo "UTF8 check: ☠"
#################login
echo "Logging into $WIKIAPI as $USERNAME..."

###############
#Login part 1
#printf "%s" "Logging in (1/2)..."
echo "Get login token..."
CR=$(curl -S \
	--location \
	--retry 2 \
	--retry-delay 5\
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=login&format=json")

echo "$CR" | jq .
	
rm login.json
echo "$CR" > login.json
TOKEN=$(jq --raw-output '.query.tokens.logintoken' login.json)
TOKEN="${TOKEN//\"/}" #replace double quote by nothing

#Remove carriage return!
printf "%s" "$TOKEN" > token.txt
TOKEN=$(cat token.txt | sed 's/\r$//')


if [ "$TOKEN" == "null" ]; then
	echo "Getting a login token failed."
	exit	
else
	echo "Login token is $TOKEN"
	echo "-----"
fi

###############
#Login part 2
echo "Logging in..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
    --cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "username=${USERNAME}" \
	--data-urlencode "password=${USERPASS}" \
	--data-urlencode "rememberMe=1" \
	--data-urlencode "logintoken=${TOKEN}" \
	--data-urlencode "loginreturnurl=http://en.wikipedia.org" \
	--request "POST" "${WIKIAPI}?action=clientlogin&format=json")

echo "$CR" | jq .

STATUS=$(echo $CR | jq '.clientlogin.status')
if [[ $STATUS == *"PASS"* ]]; then
	echo "Successfully logged in as $USERNAME, STATUS is $STATUS."
	echo "-----"
else
	echo "Unable to login, is logintoken ${TOKEN} correct?"
	exit
fi

###############
#Get edit token
echo "Fetching edit token..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--request "GET" "${WIKIAPI}?action=query&meta=tokens&format=json")

echo "$CR" | jq .
echo "$CR" > edittoken.json
EDITTOKEN=$(jq --raw-output '.query.tokens.csrftoken' edittoken.json)
rm edittoken.json

EDITTOKEN="${EDITTOKEN//\"/}" #replace double quote by nothing

#Remove carriage return!
printf "%s" "$EDITTOKEN" > edittoken.txt
EDITTOKEN=$(cat edittoken.txt | sed 's/\r$//')

if [[ $EDITTOKEN == *"+\\"* ]]; then
	echo "Edit token is: $EDITTOKEN"
else
	echo "Edit token not set."
	exit
fi

###############
#Make a test edit
#EDITTOKEN="d55014d69f1a8c821073bb6724aced7658904018+\\"
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "title=${PAGE}" \
	--data-urlencode "appendtext={{nocat|2017|01|31}}" \
	--data-urlencode "token=${EDITTOKEN}" \
	--request "POST" "${WIKIAPI}?action=edit&format=json")
	
echo "$CR" | jq .

Upload a file (outdated example) edit

The following is an example of how to use w:Bash (Unix shell) to login and upload a file.

You will need the curl package which you can get with:

sudo apt-get install curl

Curl will need to write 2 files, one for the cookiejar, and one to store files if you need to upload.

Bash is not very efficient, but it is widely available.

The following example is released into Public Domain.Smallman12q (talk) 00:00, 7 January 2013 (UTC)

#!/usr/bin/env bash

#Needs curl
USERNAME="Username"
USERPASS="UserPass"
WIKIAPI="http://commons.wikimedia.org/w/api.php"
cookie_jar="wikicj"
#Will store file in wikifile

echo "UTF8 check: ☠"
#################login
echo "Logging into $WIKIAPI as $USERNAME..."
#Login part 1
#printf "%s" "Logging in (1/2)..."
echo "Logging in (1/2)..."
CR=$(curl -S \
	--location \
	--retry 2 \
	--retry-delay 5\
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "lgname=${USERNAME}" \
	--data-urlencode "lgpassword=${USERPASS}" \
	--request "POST" "${WIKIAPI}?action=login&format=txt")

CR2=($CR)
if [ "${CR2[9]}" = "[token]" ]; then
	TOKEN=${CR2[11]}
	echo "Logging in (1/2)...Complete"
else
	echo "Login part 1 failed."
	echo $CR
	exit
fi

#Login part 2
echo "Logging in (2/2)..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
    --cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--data-urlencode "lgname=${USERNAME}" \
	--data-urlencode "lgpassword=${USERPASS}" \
	--data-urlencode "lgtoken=${TOKEN}" \
	--request "POST" "${WIKIAPI}?action=login&format=txt")
	
#TODO-Add login part 2 check
echo "Successfully logged in as $USERNAME."

###############
#Get edit token
echo "Fetching edit token..."
CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--compressed \
	--request "POST" "${WIKIAPI}?action=tokens&format=txt")

CR2=($CR)
EDITTOKEN=${CR2[8]}
if [ ${#EDITTOKEN} = 34 ]; then
	echo "Edit token is: $EDITTOKEN"
else
	echo "Edit token not set."
	echo $CR
	exit
fi
#########################

curl "http://bits.wikimedia.org/images/wikimedia-button.png" >wikifile

CR=$(curl -S \
	--location \
	--cookie $cookie_jar \
	--cookie-jar $cookie_jar \
	--user-agent "Curl Shell Script" \
	--keepalive-time 60 \
	--header "Accept-Language: en-us" \
	--header "Connection: keep-alive" \
	--header "Expect:" \
	--form "token=${EDITTOKEN}" \
	--form "filename=filename.gif" \
	--form "text=Filedescription" \
	--form "comment=commentDetails" \
	--form "file=@wikifile" \
	--request "POST" "${WIKIAPI}?action=upload&format=txt&")

echo $CR
read -p "Done..."
exit