Utilizator:Flubot/rename.sh

De la Wikționar, dicționarul liber
Jump to navigation Jump to search
#!/bin/bash

cookiesfile="rowikt-login-cookies.txt"
wiki="ro.wiktionary.org"
#username, password
lgname="Flubot"
lgpassword="XXXXXXXXXXXXX"
reason1=""

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 "Apply for movtoken failed, you may have not rights"
		echo "   More information:"
		echo "   $xmltext" | sed -e "s/'/'/g;"
		return 1
    	fi
	title1=`echo $title | sed -e 's/ţ/ț/g'`
	title2=`echo $title1 | sed -e 's/ş/ș/g'`
	echo "$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/'/'/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 "Χρήση: $0 1η-λέξη 2η-λέξη 3η..."
	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
}


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

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