Using Mediawiki API fails


 
Thread Tools Search this Thread
Top Forums Web Development Using Mediawiki API fails
# 1  
Old 10-24-2013
Using Mediawiki API fails

I am trying to create an automated documentation feature for our servers by using a Mediawiki and its API and feeding scripts on each server, but failed so far. The idea is to collect several vital data about a server by a script, create a wiki page and feed this into a central Mediawiki instance using wget as a means of transfer.

I set up a test-Wiki using Mediawiki 1.21.2 using a simple XAMPP stack on SLES11 (i intend to work on the software integration part later) and set up a user account with all rights (bureaucrat, administrator) to avoid any authentication issues for the test.

I finally wrote the following script on a test server to try creation of a wiki page:

Code:
#! /bin/ksh93

typeset WGET=$(which wget)
typeset chWikiInst="my_wiki"
typeset chWikiURL="http://<wikiserver>/wiki/api.php"
typeset fTgt=/tmp/wikitest
typeset fOut="$fTgt/outfile"
typeset chUser="wikiroot"
typeset chPwd="<password>"
typeset chToken=""
typeset chEditToken=""

rm outfile*
rm wikitest.login
rm wikitest.session*

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

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

$WGET --post-data "action=login&lgname=${chUser}&lgpassword=${chPwd}&lgtoken=${chToken}&format=xml" \
      --load-cookies="$fTgt/wikitest.login" \
      --save-cookies="$fTgt/wikitest.session" \
      --output-document="$fOut".1 \
      "$chWikiURL"

# ----------------------- get edit token -----------
wget --post-data "action=query&prop=info|revisions&intoken=edit&titles=Mypage&format=xml" \
     --load-cookies="$fTgt/wikitest.session" \
      --save-cookies="$fTgt/wikitest.sessionedit" \
      --output-document="$fOut".2 \
      "$chWikiURL"

chEditToken="$(sed -n 's/.*\ edittoken="\([^"]*\)".*/\1/p' "${fOut}.2")"

print - "------------------------------------------------"
print - "chEditToken=${chEditToken}"
print - "------------------------------------------------"


# ----------------------- edit page ----------------
wget --post-data "action=edit&title=Mypage&summary=wikitest%20summary&text=wikitest%20Text&token=${chEditToken}" \
     --load-cookies="$fTgt/wikitest.sessionedit" \
      --save-cookies="$fTgt/wikitest.sessionedit" \
      --output-document="$fOut".3 \
      "$chWikiURL"


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

exit 0

What worked was the login- the query- and the logout-part. I get the correct answers back, the cookies/tokens are set correctly and all works correctly. Somehow, though, when i try to use the edit-token to create the new page (it doesn't exist yet) i get back the API-documentation, which indicates an error.

I checked if the API is allowed to be used at all in LocalSettings.php and include/DefaultSettings.php, but everything seems to be as it should be.

wget and OS version is as follows:

Code:
# wget --version
GNU Wget 1.14 built on aix5.1.0.0.

Locale: /opt/freeware/share/locale 
Compile: xlc_r -D_LARGE_FILES -DHAVE_CONFIG_H 
    -DSYSTEM_WGETRC="/opt/freeware/etc/wgetrc" 
    -DLOCALEDIR="/opt/freeware/share/locale" -I. -I../lib -I../lib 
    -D_THREAD_SAFE -I/opt/freeware/include -I/opt/freeware/include 
    -qmaxmem=16384 -DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 
    -D_ALL_SOURCE -DFUNCPROTO=15 -O -I/opt/freeware/include 
Link: xlc_r -D_LARGE_FILES -qmaxmem=16384 -DSYSV -D_AIX -D_AIX32 -D_AIX41 
    -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O 
    -I/opt/freeware/include -L/opt/freeware/lib 
    -Wl,-bmaxdata:0x80000000 -L/opt/freeware/lib 
    /opt/freeware/lib/libiconv.a /opt/freeware/lib/libintl.a 
    /opt/freeware/lib/libiconv.a /opt/freeware/lib/libssl.so 
    /opt/freeware/lib/libcrypto.so /opt/freeware/lib/libz.a -ldl -lz 
    -lz -lidn -lpcre ftp-opie.o openssl.o http-ntlm.o ../lib/libgnu.a 
    -liconv 

# oslevel -s
5300-10-01-0921

# uname -a
AIX myTestHost 3 5 <####>

Can someone explain to me what i am doing wrong?

bakunin
# 2  
Old 10-24-2013
Normally, FWIW, I would do this kind of task with curl in a PHP script.
# 3  
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
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. 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

2. 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

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. 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

5. 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
Login or Register to Ask a Question

Featured Tech Videos