Sponsored Content
Full Discussion: Using Mediawiki API fails
Top Forums Web Development Using Mediawiki API fails Post 302888718 by bakunin on Monday 17th of February 2014 04:24:00 AM
Old 02-17-2014
Quote:
Originally Posted by bakunin
Can someone explain to me what i am doing wrong?
I finally found it out myself. This part of the MediaWiki documentation is somewhat poorly done, so i didn't understand immediately how to do it. IN hindsight it was a case of PEBKAC.

Solution is: once a script acquires an edit-token it has to confirm this token in a second pass to make it valid. I didn't do this in my first try, so here is a revised test-script which works against my Mediawiki (v 1.23rc3 on AIX 7.1).

Code:
#! /bin/ksh93

typeset WGET=$(which wget)
typeset chWikiInst="my_wiki"
typeset chWikiURL="http://<URL to Wiki here>/api.php"
typeset fTgt=/tmp/bakunin/wikitest2
typeset fOut="$fTgt/outfile"
typeset fTok="$fTgt/token"
typeset chUser="<place wiki user acc here>"
typeset chPwd="<password for wiki user acc>"
typeset chToken=""
typeset chSessionID=""
typeset chEditToken=""

rm outfile*
rm token*

# ----------------------- login1 --------------------
$WGET --post-data "action=login&lgname=${chUser}&lgpassword=${chPwd}&format=xml" \
      --save-cookies="${fTok}.login1" \
      --output-document="${fOut}.login1" \
      --keep-session-cookies \
      -q \
      "$chWikiURL"

                                                       # extract info
chToken="$(sed 's/.*\ token="\([^"]*\)".*/\1/' "${fOut}.login1")"
chSessionID="$(sed 's/.*\ sessionid="\([^"]*\)".*/\1/' "${fOut}.login1")"

print - "sessionID: $chSessionID \t Token: $chToken"

# ----------------------- confirm token --------------------
$WGET --post-data "action=login&lgname=${chUser}&lgpassword=${chPwd}&lgtoken=${chToken}&format=xml" \
      --load-cookies="${fTok}.login1" \
      --save-cookies="${fTok}.login2" \
      --output-document="${fOut}.login2" \
      --keep-session-cookies \
      -q \
      "$chWikiURL"

# ----------------------- get edit token --------------------
$WGET --post-data "action=tokens&type=edit&format=xml" \
      --load-cookies="${fTok}.login2" \
      --save-cookies="${fTok}.edit" \
      --output-document="${fOut}.edit" \
      --keep-session-cookies \
      -q \
      "$chWikiURL"

                                                       # extract info
chEditToken="$(sed 's/.*\ edittoken="\([^"]*\)+\\".*/\1/' "${fOut}.edit")"
                                                       # pseudo-URL-encode trailing "+\"
chEditToken="${chEditToken}%2B%5C"
print - "sessionID: $chSessionID\nToken....: $chToken\nEditToken: $chEditToken"

# ----------------------- create new page --------------------
$WGET --post-data "action=edit&title=MyTestPage&contentformat=text/x-wiki&format=xml&text='Hello-World'&token=${chEditToken}" \
      --load-cookies="${fTok}.edit" \
      --save-cookies="${fTok}.create" \
      --output-document="${fOut}.create" \
      --keep-session-cookies \
      -q \
      "$chWikiURL"

# ----------------------- logout -------------------
$WGET --post-data "action=logout&format=xml" \
      --load-cookies="$fTgt/wikitest.create" \
      --save-cookies="$fTgt/wikitest.sessionend" \
      --output-document="$fOut".sessionend \
      "$chWikiURL"
exit 0

This script will create/overwrite a page with the title "MyTestPage" and no text in it.

Going forward this leaves one question: how can i transmit more wiki-text? To put it all into a single URL is somewhat cumbersome and i have yet to figure out how to send the (suggested from the documentation) multipart-MIME-message with wget.

bakunin
 

5 More Discussions You Might Find Interesting

1. Solaris

how to install mediawiki

hi everybody.. i download and untar mediawiki ... i dont know how to install mediawiki.. there is no configure ... plz help me how to do it step bu step ... (1 Reply)
Discussion started by: coolboys
1 Replies

2. Red Hat

mediawiki page won't load - fedora 16, MediaWiki version 1.18.2

I created a mediawiki page and it was completely working and I had multiple pages within it. When I edited the $wgLogo = " "; to something I wanted (I put the link to the picture within " ") I had to edit the logo on the top right but now I can't even load any of my pages, I may have touched... (1 Reply)
Discussion started by: kelth
1 Replies

3. UNIX and Linux Applications

mediawiki page won't load - fedora 16, MediaWiki version 1.18.2

I created a mediawiki page and it was completely working and I had multiple pages within it. When I edited the $wgLogo = " "; to something I wanted (I put the link to the picture within " ") I had to edit the logo on the top right but now I can't even load any of my pages, I may have... (10 Replies)
Discussion started by: kelth
10 Replies

4. Shell Programming and Scripting

Need to run an API from a script and extract fields from output of API

Hi, I need to call an API (GetUsageDetails)from inside a shell script which takes an input argument acct_nbr. The output of API will be like : <usageAccum accumId="450" accumCaptn="PM_125" inclUnits="1410.00" inclUnitsUsed="744.00" shared="true" pooled="false" prorated="false"... (1 Reply)
Discussion started by: rkrish
1 Replies

5. Web Development

Face-api.js — JavaScript API for Face Recognition in the Browser with tensorflow.js

Ref: https://itnext.io/face-api-js-javascript-api-for-face-recognition-in-the-browser-with-tensorflow-js-bcc2a6c4cf07 (0 Replies)
Discussion started by: Neo
0 Replies
All times are GMT -4. The time now is 01:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy