API:Client code/Bash

From MediaWiki.org
Jump to navigation Jump to search

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)

  1 #!/usr/bin/env bash
  2 
  3 #Needs curl and jq
  4 
  5 USERNAME="testWiki"
  6 USERPASS="test"
  7 WIKI="http://wiki.serverdev-mediawiki2-v1"
  8 WIKIAPI="http://wiki.serverdev-mediawiki2-v1/api.php"
  9 folder="/tmp"
 10 
 11 PAGE="Title of an article"
 12 
 13 FILENAME="Wikidata_.png"
 14 FILEPATH="/var/www/images/6/68/Wikidata_.png"
 15 FILECOMMENT="comment"
 16 FILETEXT="text"
 17 
 18 
 19 cookie_jar="${folder}/wikicj"
 20 
 21 #Will store file in wikifile
 22 echo "UTF8 check: ☠"
 23 #################login
 24 echo "Logging into $WIKIAPI as $USERNAME..."
 25 
 26 ###############
 27 #Login part 1
 28 #printf "%s" "Logging in (1/2)..."
 29 echo "Get login token..."
 30 CR=$(curl -S \
 31 	--location \
 32 	--retry 2 \
 33 	--retry-delay 5\
 34 	--cookie $cookie_jar \
 35 	--cookie-jar $cookie_jar \
 36 	--user-agent "Curl Shell Script" \
 37 	--keepalive-time 60 \
 38 	--header "Accept-Language: en-us" \
 39 	--header "Connection: keep-alive" \
 40 	--compressed \
 41 	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=login&format=json")
 42 
 43 echo "$CR" | jq .
 44 
 45 rm ${folder}/login.json
 46 echo "$CR" > ${folder}/login.json
 47 TOKEN=$(jq --raw-output '.query.tokens.logintoken'  ${folder}/login.json)
 48 
 49 if [ "$TOKEN" == "null" ]; then
 50 	echo "Getting a login token failed."
 51 	exit
 52 else
 53 	echo "Login token is $TOKEN"
 54 	echo "-----"
 55 fi
 56 
 57 ###############
 58 #Login part 2
 59 
 60 echo "Logging in..."
 61 CR=$(curl -S \
 62 	--location \
 63 	--cookie $cookie_jar \
 64     --cookie-jar $cookie_jar \
 65 	--user-agent "Curl Shell Script" \
 66 	--keepalive-time 60 \
 67 	--header "Accept-Language: en-us" \
 68 	--header "Connection: keep-alive" \
 69 	--compressed \
 70 	--data-urlencode "username=${USERNAME}" \
 71 	--data-urlencode "password=${USERPASS}" \
 72 	--data-urlencode "rememberMe=1" \
 73 	--data-urlencode "logintoken=${TOKEN}" \
 74 	--data-urlencode "loginreturnurl=${WIKI}" \
 75 	--request "POST" "${WIKIAPI}?action=clientlogin&format=json")
 76 
 77 echo "$CR" | jq .
 78 
 79 STATUS=$(echo $CR | jq '.clientlogin.status')
 80 if [[ $STATUS == *"PASS"* ]]; then
 81 	echo "Successfully logged in as $USERNAME, STATUS is $STATUS."
 82 	echo "-----"
 83 else
 84 	echo "Unable to login, is logintoken ${TOKEN} correct?"
 85 	exit
 86 fi
 87 
 88 ###############
 89 #Get edit token
 90 echo "Fetching edit token..."
 91 CR=$(curl -S \
 92 	--location \
 93 	--cookie $cookie_jar \
 94 	--cookie-jar $cookie_jar \
 95 	--user-agent "Curl Shell Script" \
 96 	--keepalive-time 60 \
 97 	--header "Accept-Language: en-us" \
 98 	--header "Connection: keep-alive" \
 99 	--compressed \
100 	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=csrf&format=json")
101 
102 echo "$CR" | jq .
103 echo "$CR" > ${folder}/edittoken.json
104 EDITTOKEN=$(jq --raw-output '.query.tokens.csrftoken' ${folder}/edittoken.json)
105 rm ${folder}/edittoken.json
106 
107 #Remove carriage return!
108 if [[ $EDITTOKEN == *"+\\"* ]]; then
109 	echo "Edit token is: $EDITTOKEN"
110 else
111 	echo "Edit token not set."
112 	exit
113 fi
114 
115 ###############
116 echo "Make a test edit"
117 CR=$(curl -S \
118 	--location \
119 	--cookie $cookie_jar \
120 	--cookie-jar $cookie_jar \
121 	--user-agent "Curl Shell Script" \
122 	--keepalive-time 60 \
123 	--header "Accept-Language: en-us" \
124 	--header "Connection: keep-alive" \
125 	--compressed \
126 	--data-urlencode "title=${PAGE}" \
127 	--data-urlencode "appendtext={{nocat|2017|01|31}}" \
128 	--data-urlencode "token=${EDITTOKEN}" \
129 	--request "POST" "${WIKIAPI}?action=edit&format=json")
130 
131 echo "$CR" | jq .
132 
133 ###############
134 echo "Make a test upload"
135 
136 CR=$(curl -S \
137 	--cookie $cookie_jar \
138 	--cookie-jar $cookie_jar \
139 	--user-agent "Curl Shell Script" \
140 	--keepalive-time 60 \
141 	--header "Accept-Language: en-us" \
142 	--header "Connection: keep-alive" \
143 	--header "Expect:" \
144 	--form "token=${EDITTOKEN}" \
145 	--form "filename=${FILENAME}" \
146 	--form "text=${FILETEXT}" \
147 	--form "comment=${FILECOMMENT}" \
148 	--form "file=@${FILEPATH}" \
149 	--form "ignorewarnings=yes" \
150 	--request "POST" "${WIKIAPI}?action=upload&format=json&")
151 
152 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)

  1 #!/usr/bin/env bash
  2 
  3 #Needs curl and jq
  4 
  5 USERNAME="Smile4ever"
  6 USERPASS="******"
  7 PAGE="Title of an article"
  8 WIKIAPI="https://en.wikipedia.org/w/api.php"
  9 cookie_jar="wikicj"
 10 #Will store file in wikifile
 11 
 12 echo "UTF8 check: ☠"
 13 #################login
 14 echo "Logging into $WIKIAPI as $USERNAME..."
 15 
 16 ###############
 17 #Login part 1
 18 #printf "%s" "Logging in (1/2)..."
 19 echo "Get login token..."
 20 CR=$(curl -S \
 21 	--location \
 22 	--retry 2 \
 23 	--retry-delay 5\
 24 	--cookie $cookie_jar \
 25 	--cookie-jar $cookie_jar \
 26 	--user-agent "Curl Shell Script" \
 27 	--keepalive-time 60 \
 28 	--header "Accept-Language: en-us" \
 29 	--header "Connection: keep-alive" \
 30 	--compressed \
 31 	--request "GET" "${WIKIAPI}?action=query&meta=tokens&type=login&format=json")
 32 
 33 echo "$CR" | jq .
 34 	
 35 rm login.json
 36 echo "$CR" > login.json
 37 TOKEN=$(jq --raw-output '.query.tokens.logintoken' login.json)
 38 TOKEN="${TOKEN//\"/}" #replace double quote by nothing
 39 
 40 #Remove carriage return!
 41 printf "%s" "$TOKEN" > token.txt
 42 TOKEN=$(cat token.txt | sed 's/\r$//')
 43 
 44 
 45 if [ "$TOKEN" == "null" ]; then
 46 	echo "Getting a login token failed."
 47 	exit	
 48 else
 49 	echo "Login token is $TOKEN"
 50 	echo "-----"
 51 fi
 52 
 53 ###############
 54 #Login part 2
 55 echo "Logging in..."
 56 CR=$(curl -S \
 57 	--location \
 58 	--cookie $cookie_jar \
 59     --cookie-jar $cookie_jar \
 60 	--user-agent "Curl Shell Script" \
 61 	--keepalive-time 60 \
 62 	--header "Accept-Language: en-us" \
 63 	--header "Connection: keep-alive" \
 64 	--compressed \
 65 	--data-urlencode "username=${USERNAME}" \
 66 	--data-urlencode "password=${USERPASS}" \
 67 	--data-urlencode "rememberMe=1" \
 68 	--data-urlencode "logintoken=${TOKEN}" \
 69 	--data-urlencode "loginreturnurl=http://en.wikipedia.org" \
 70 	--request "POST" "${WIKIAPI}?action=clientlogin&format=json")
 71 
 72 echo "$CR" | jq .
 73 
 74 STATUS=$(echo $CR | jq '.clientlogin.status')
 75 if [[ $STATUS == *"PASS"* ]]; then
 76 	echo "Successfully logged in as $USERNAME, STATUS is $STATUS."
 77 	echo "-----"
 78 else
 79 	echo "Unable to login, is logintoken ${TOKEN} correct?"
 80 	exit
 81 fi
 82 
 83 ###############
 84 #Get edit token
 85 echo "Fetching edit token..."
 86 CR=$(curl -S \
 87 	--location \
 88 	--cookie $cookie_jar \
 89 	--cookie-jar $cookie_jar \
 90 	--user-agent "Curl Shell Script" \
 91 	--keepalive-time 60 \
 92 	--header "Accept-Language: en-us" \
 93 	--header "Connection: keep-alive" \
 94 	--compressed \
 95 	--request "GET" "${WIKIAPI}?action=query&meta=tokens&format=json")
 96 
 97 echo "$CR" | jq .
 98 echo "$CR" > edittoken.json
 99 EDITTOKEN=$(jq --raw-output '.query.tokens.csrftoken' edittoken.json)
100 rm edittoken.json
101 
102 EDITTOKEN="${EDITTOKEN//\"/}" #replace double quote by nothing
103 
104 #Remove carriage return!
105 printf "%s" "$EDITTOKEN" > edittoken.txt
106 EDITTOKEN=$(cat edittoken.txt | sed 's/\r$//')
107 
108 if [[ $EDITTOKEN == *"+\\"* ]]; then
109 	echo "Edit token is: $EDITTOKEN"
110 else
111 	echo "Edit token not set."
112 	exit
113 fi
114 
115 ###############
116 #Make a test edit
117 #EDITTOKEN="d55014d69f1a8c821073bb6724aced7658904018+\\"
118 CR=$(curl -S \
119 	--location \
120 	--cookie $cookie_jar \
121 	--cookie-jar $cookie_jar \
122 	--user-agent "Curl Shell Script" \
123 	--keepalive-time 60 \
124 	--header "Accept-Language: en-us" \
125 	--header "Connection: keep-alive" \
126 	--compressed \
127 	--data-urlencode "title=${PAGE}" \
128 	--data-urlencode "appendtext={{nocat|2017|01|31}}" \
129 	--data-urlencode "token=${EDITTOKEN}" \
130 	--request "POST" "${WIKIAPI}?action=edit&format=json")
131 	
132 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