User:Flubot/move.sh

This script (based on el:Χρήστης:AtouBot/μετακίνηση.sh) has already run in el and ro.wiktionary.

move.sh

 * 1) !/bin/bash

cookiesfile="enwikt-login-cookies.txt" wiki="en.wiktionary.org" lgname="Flubot" lgpassword="XXXXXXXXXXXXX" reason1=""
 * 1) username, password

curlargs="-s -S --retry 10 -H 'Expect:' -c $cookiesfile -b $cookiesfile -f" curlurl="http://$wiki/w/api.php?action=" if [ ! -z "$reason1" ]; then reason_arg="--data-urlencode reason=$reason1" #summary_arg="--data-urlencode 'summary=$reason1'" else reason_arg="" #summary_arg="" fi

function getxmlitem { xmlitemval=`echo $xmltext | tr '>' '\n' | grep ${tokenname}= | awk -F${tokenname}= '{ print $2 }' | awk -F'"' '{ print $2 }'` }

function loginerrcheck { if [ $loginresult -ne "0" ]; then echo "Login failed, maybe wrong password" if [ -e "$cookiesfile" ]; then rm -f $cookiesfile fi exit 1 fi   loginerr=`echo $login | grep Illegal` if [ ! -z "$loginerr" ]; then echo "Login failed, maybe wrong password" echo "  More information:" echo "  $loginerr" | sed -e "s/'/'/g;" if [ -e "$cookiesfile" ]; then rm -f $cookiesfile fi exit 1 fi   return }

function login { if [ -z "$lgname" -o -z "$lgpassword" ]; then echo "Edit this file and change variables" echo "lgname και lgpassword." exit 1 fi   login=`curl -s -S  --retry 10 -H 'Expect:' -c $cookiesfile -f --data-urlencode "lgname=$lgname" --data-urlencode "lgpassword=$lgpassword" --data-urlencode "format=xml"  "${curlurl}login"` loginresult=$? loginerrcheck loginneedtoken=`echo $login | grep NeedToken` if [ ! -z "$loginneedtoken" ]; then tokenname=token xmltext="$login" getxmlitem lgtoken="$xmlitemval" login=`curl -s -S --retry 10 -H 'Expect:' -c $cookiesfile -b $cookiesfile -f --data-urlencode "lgname=$lgname" --data-urlencode "lgpassword=$lgpassword" --data-urlencode "lgtoken=$lgtoken" --data-urlencode "format=xml"  "${curlurl}login"` loginresult=$? loginerrcheck loginneedtoken=`echo $login | grep NeedToken` if [ ! -z "$loginneedtoken" ]; then echo "Login failed (NeedToken after second attempt)" if [ -e "$cookiesfile" ]; then rm -f $cookiesfile fi		       exit 1 fi fi   echo "You are logged in as $lgname" }

function movepagefromtitle { xmltext=`curl $curlargs -d "prop=info" -d "format=xml" --data-urlencode "titles=$title" "${curlurl}query"` redir=`echo $xmltext | grep 'redirect' -c` if [ "$redir" -eq "0" ]; then xmltext=`curl $curlargs --data-urlencode "prop=info" --data-urlencode "intoken=move" --data-urlencode "format=xml" --data-urlencode "titles=$title" "${curlurl}query"` tokenname=movetoken getxmlitem movtoken="$xmlitemval" if [ -z "$movtoken" ]; then echo "Request for movetoken failed, you may have not rights" echo "  More information:" echo "  $xmltext" | sed -e "s/&#039;/'/g;" return 1 fi title1=`echo $title | sed -e 's/ţ/ț/g'` title1=`echo $title1 | sed -e 's/Ţ/Ț/g'` title1=`echo $title1 | sed -e 's/ş/ș/g'` title2=`echo $title1 | sed -e 's/Ş/Ș/g'`

echo "moving $title to $title2" result=`curl $curlargs --data-urlencode "format=xml" --data-urlencode "token=$movtoken" --data-urlencode "from=$title" --data-urlencode "to=$title2" $reason_arg "${curlurl}move" ` if [ "$?" -ne "0" ]; then echo "Moving failed" return 1 fi err=`echo $result | grep -i error` if [ ! -z "$err" ]; then echo "$title" "Moving failed... or made by someone else" echo "  More information:" echo "$err" | sed -e "s/&#039;/'/g;" | awk -F 'error' '{ print $2 }' | awk -F '/>' '{ print $1 }' fi else echo "$title" " : it is a redirect, not moved" return 1 fi }

function checkusage { if [ "$1" -eq "0" ]; then echo "Use: $0 1st_word 2nd_word 3rd..." echo "Your account must have moving rights." echo "Attention: Every headword including spaces must be written between quotes" echo "For example:" echo "$0 'mean vandal'  'super mean vandal'   'boring editwar type'" echo "Alternatively, you can call this script with" echo "cat file-with-words | $0 -" echo "and it will read the headwords' list from stdin." exit 1 fi }

checkusage $# if [ ! -e "$cookiesfile" ]; then login fi
 * 1) MAIN

if [ "$1" == "-" ]; then while read title; do	movepagefromtitle done else for title in "$@"; do	movepagefromtitle done fi