User:Flubot/move2.sh

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This script is a variation of User:Flubot/move.sh. It moves Romanian entries from cedillas to commas and appends an additional text to the redirect created by the moving.

Please note that what seems to be "s/'/'/g;" is actually "s/& # 0 3 9 ;/'/g;"

move.sh[edit]

#!/bin/bash

cookiesfile="enwikt-login-cookies.txt"
wiki="en.wiktionary.org"
#username, password
lgname="Flubot"
lgpassword="XXXXXXXXXXXXX"
reason1="according_to_[[WT:RONV]];redirect_should_be_kept"
              # do not insert spaces into reason1, reason2 variables
reason2="add_text_to_redirect"

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

tosee=""

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/'/'/g;"
		return 1
    	fi
	err2=0
	title1=`echo $title | sed -e 's/ţ/ț/g'`
	title1=`echo $title1 | sed -e 's/Ţ/Ț/g'`
	title2=`echo $title1 | sed -e 's/ş/ș/g'`
	title2=`echo $title2 | 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 
		err2=1
		echo "Moving failed for some reason"
		return 1
    	fi
    	err=`echo $result | grep -i error`
    	if [ ! -z "$err" ]; then
		err2=1
		echo "$title" "Moving failed..."
		echo "   More information:"
		echo "$err" | sed -e "s/'/'/g;" | awk -F 'error' '{ print $2 }' | awk -F '/>' '{ print $1 }'
	fi
	if [ $err2 = 1 ]; then
			echo 'no additional text, error while moving'
			tosee=$tosee", "$title
	else
		if [ "$title1" = "$title2" ]; then
			echo 'no additional text, no ş'
		else
			appendtext
		fi
	fi
    else
	echo "$title" " : it is a redirect, not moved, no additional text for it"
	return 1
    fi
}

function appendtext() {
    xmltext=`curl $curlargs -d "prop=info" -d "format=xml" --data-urlencode "titles=$title" "${curlurl}query"`
    redir=`echo $xmltext | grep 'redirect' -c`
    if [ "$redir" -ne "0" ]; then
        xmltext=`curl $curlargs --data-urlencode "prop=info" --data-urlencode "intoken=edit" --data-urlencode "format=xml" --data-urlencode "titles=$title" --data-urlencode "bot=1" "${curlurl}query"`
    	tokenname=edittoken
    	getxmlitem
    	editoken="$xmlitemval"
    	if [ -z "$editoken" ]; then 
		echo "Request for edittoken failed, you may have not rights"
		echo "   More information:"
		echo "   $xmltext" | sed -e "s/'/'/g;"
		return 1
    	fi
	addtext=`echo ' This redirect from a cedilla form to a Romanian spelling is in accordance with Wiktionary policy. However, if another language has the word '$title' with a cedilla, then this page should be made an entry, and [[template:also]] used to link to the Romanian entry.'`
	echo "adding text to $title"
    	result=`curl $curlargs --data-urlencode "format=xml" --data-urlencode "title=$title" --data-urlencode "token=$editoken" --data-urlencode "appendtext=$addtext" --data-urlencode summary=$reason2 "${curlurl}edit" `
    	if [ "$?" -ne "0" ]; then 
		echo "Edit failed"
		return 1
    	fi
    	err1=`echo $result | grep -i error`
    	if [ ! -z "$err1" ]; then
		echo "$title" "Not modified for some reason"
		echo "   More information:"
		echo "$err1" | sed -e "s/'/'/g;" | awk -F 'error' '{ print $2 }' | awk -F '/>' '{ print $1 }'
    	fi
    else
	echo "$title: it is not a redirect, not modified"
	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
}


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

if [ "$1" == "-" ]; then
    while read title; do
	movepagefromtitle
	sleep 3
	echo
    done
else 
    for title in "$@"; do
	movepagefromtitle
	sleep 5
	echo
    done
fi
echo $tosee >> move-log.txt