#!/usr/bin/ksh93 ################################################################ #### Copyright 2010 By Dana French, All Rights Reserved ################################################################# function usagemsg_wikiAutoLoad { print " Program: ${1} Automatically upload files as page content to a Wiki Server. Usage: ${1##*/} [-?vVfzx] [-u WikiUserName] [-p WikiUserPassword] [-a URL] [-t PageTitle] [-c CookieDir] [-o OutputDir] [-C Category:...] [-e FileExension:...] FileNamesToUpload... Where: -v = Verbose mode - displays wikiAutoLoad function info -V = Very Verbose Mode - debug output displayed -u = WikiUser Login name (Default: WikiSysop) -p = WikiUser Password (Default: administrator) -a = Wiki API URL (Default: http://127.0.0.1/wiki/api.php ) -t = Page Title (only valid for single file upload) (Default: NULL) -c = Directory for storage of HTTP cookies (Default: .) -o = Directory for storage of output files (Default: .) -C = WikiCategories to assign to each uploaded page (Default: Upload) -z = Preview Mode, show steps but do not upload anything (Default: FALSE) -e = Delete matching file extension from upload files use remaining FileName suffix as the PageTitle -f = Insert Preformatted tags around file content (Default: FALSE) -x = Convert imbedded HTTP control characters to URL hex codes Example Usage: wikiAutoLoad -a "http://www.aixexpert.com/wiki/api.php" \\ -p administrator -C "Uploaded File" *.html Author: Dana French (dfrench@mtxia.com) Copyright 2010 By Dana French, All Rights Reserved \"AutoContent\" enabled " } ################################################################ #### #### Description: #### #### The purpose of this program is to provide the shell programmer or system #### administrator with an automated mechanism for uploading documentation #### and content to a MediaWiki server. This shell script can be run from #### any system in an organization to automatically upload information to a #### centralized wiki documentation server. #### #### Assumptions: #### #### It is assumed the content to be uploaded is stored in files on the local #### system. Each paged stored in a separate file. It is also assumed the #### filename will be used as the Page Title on the Wiki. When the file name #### is processed by this script, characters such as underscores "_", commas #### ",", periods ".", dashes "-" are replaced with spaces, and the file #### extension can be removed via a command line option. So the user can #### create files with names such as "My_Wiki_Page_to_Upload.html", and this #### script will upload this file to a wiki page named "My Wiki Page to #### Upload". #### #### Dependencies: #### #### This script requires the "wget" command to send files and receive #### cookies from the Wiki Server. #### #### Products: #### #### This script uploads the contents of a file and creates or updates a #### Mediwiki pages on a Wiki Server. #### #### Configured Usage: #### #### This script can be run from the command line or included in a library #### and called as a function. One or more filenames containing content to #### be uploaded to a Wiki Server must be specified on the command line or an #### error is generated. #### #### Page titles for single file uploads can be specified using the "-t" #### command line option. This option is only valid if a single file is #### specified on the command line. #### #### Bulk Uploads can be performed by specifying more than one filename on #### the command line. The page title for each page in a bulk upload will be #### the filename containing the content. #### #### Deleting File Extentions from page titles during Bulk file uploads: #### Multiple file extensions can be specified for the "-e" option by using #### the pipe "|" delimiter between each file extension. #### #### Multiple Categories can be specified on the command line as an argument #### to the "-C" option. To specify multiple categories, separate each #### category specified with a comma ",". #### #### Details: #### ################################################################ function wikiAutoLoad { typeset PROGNAME="${0}" typeset VERSION="1.0" typeset EXITCODE="0" TRUE="0" FALSE="1" VERBOSE="${FALSE}" VERYVERB="${FALSE}" typeset WIKIUSER="WikiSysop" typeset WIKIPASS="administrator" typeset APIURL="http://127.0.0.1/wiki/api.php" typeset PAGETITLE="" typeset CATEGORIES="Upload" typeset FILEEXTEN="html" typeset PRETAG="${FALSE}" typeset CONVERT="${FALSE}" PREVIEW="${FALSE}" DD_COOKIES="." DD_OUTFILE="." WGETOUT="${DD_OUTFILE}/wget${$}.out" #### #### Process the command line options and arguments, saving #### the values as appropriate. #### while getopts ":vVu:p:a:t:c:o:C:e:zfx" OPTION do case "${OPTION}" in 'u') WIKIUSER="${OPTARG}";; 'p') WIKIPASS="${OPTARG}";; 'a') APIURL="${OPTARG}";; 't') PAGETITLE="${OPTARG}";; 'c') DD_COOKIES="${OPTARG}";; 'o') DD_OUTFILE="${OPTARG}";; 'C') CATEGORIES="${OPTARG}";; 'e') FILEEXTEN="${OPTARG}";; 'z') PREVIEW="${TRUE}";; 'f') PRETAG="${TRUE}";; 'x') CONVERT="${TRUE}";; 'v') VERBOSE="${TRUE}";; 'V') VERYVERB="${TRUE}";; [?:#]) usagemsg_wikiAutoLoad "${0}" && return 1 ;; esac done shift $(( ${OPTIND} - 1 )) trap "usagemsg_wikiAutoLoad ${0}" EXIT if (( ${#@} == 0 )) then print -u 2 "# ERROR: Upload File(s) not specified" return 2 fi if (( ${#@} > 1 )) && [[ "_${PAGETITLE}" != "_" ]] then print -u 2 "# " print -u 2 "# WARNING: More than one file was specified on the command line." print -u 2 "# As a result the Page Title, that was also specified on" print -u 2 "# the command line, will not be used." print -u 2 "# " fi trap "-" EXIT ################################################################ #### #### Display some program info and the command line arguments specified #### if "VERBOSE" mode was specified. #### (( VERYVERB == TRUE )) && set -x (( VERBOSE == TRUE )) && print -u 2 "################################################################" (( VERBOSE == TRUE )) && print -u 2 "# Program Name.....: ${PROGNAME}" (( VERBOSE == TRUE )) && print -u 2 "# Version..........: ${VERSION}" (( VERBOSE == TRUE )) && print -u 2 "# True.............: ${TRUE}" (( VERBOSE == TRUE )) && print -u 2 "# False............: ${FALSE}" (( VERBOSE == TRUE )) && print -u 2 "# WikiUser.........: ${WIKIUSER}" (( VERBOSE == TRUE )) && print -u 2 "# WikiPass.........: ${WIKIPASS}" (( VERBOSE == TRUE )) && print -u 2 "# API URL..........: ${APIURL}" (( VERBOSE == TRUE )) && print -u 2 "# Cookie Dir.......: ${DD_COOKIES}" (( VERBOSE == TRUE )) && print -u 2 "# Output Dir.......: ${DD_OUTFILE}" # (( VERBOSE == TRUE )) && print -u 2 "# Categories.......: ${CATEGORIES}" (( VERBOSE == TRUE )) && print -u 2 "# File Extension...: ${FILEEXTEN}" (( PREVIEW == TRUE )) && TORF="TRUE" || TORF="FALSE" (( VERBOSE == TRUE )) && print -u 2 "# Preview Mode.....: ${TORF}" (( VERBOSE == TRUE )) && TORF="TRUE" || TORF="FALSE" (( VERBOSE == TRUE )) && print -u 2 "# Verbose Mode.....: ${TORF}" (( PRETAG == TRUE )) && TORF="TRUE" || TORF="FALSE" (( VERBOSE == TRUE )) && print -u 2 "# Preformatted.....: ${TORF}" (( CONVERT == TRUE )) && TORF="TRUE" || TORF="FALSE" (( VERBOSE == TRUE )) && print -u 2 "# Convert Chars....: ${TORF}" ################################################################ (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: wikiAutoLoad()" #### Retrieve a login token from the wikimedia server getWikiLoginToken "${APIURL}" "${WIKIUSER}" "${WIKIPASS}" (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: wikiAutoLoad()" #### Create a login session on the wikimedia server mkWikiLoginSession "${APIURL}" "${WIKIUSER}" "${WIKIPASS}" "${LGTOKEN}" #### Loop through each file specified on the command line and upload #### it to the Mediawiki server, creating a new page or updating an #### existing page. for FILE in "${@}" do (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: wikiAutoLoad()" (( VERBOSE == TRUE )) && print -u 2 "# File Name........: ${FILE}" #### If more than one upload file is specified on the command line, #### use the file name (minus the extension) as the wikimedia page name. (( ${#@} > 1 )) && PAGETITLE="${FILE%.@(${FILEEXTEN})}" [[ "_${PAGETITLE}" == "_" ]] && PAGETITLE="${FILE%.@(${FILEEXTEN})}" PAGETITLE="${PAGETITLE//[._,]/ }" #### extract the page contents from the file and store contents #### in a shell variable PAGETEXT=$( < "${FILE}" ) (( CONVERT == TRUE )) && PAGETEXT="${PAGETEXT//\/%26%67%74%3B}" (( PRETAG == TRUE )) && PAGETEXT="
${PAGETEXT}
" PAGETEXT="${PAGETEXT//\&/%26}" #### Loop through each category specified on the command line and #### add each category to the page contents. (( VERBOSE == TRUE )) && print -u 2 "# =================: ================================" IFS=$'.,:;|' CATS=( ${CATEGORIES} ) IFS=$' \t\n' for CAT in "${CATS[@]}" do (( VERBOSE == TRUE )) && print -u 2 "# ======== Category: ${CAT}" PAGETEXT="${PAGETEXT}"$'\n'"[[Category:${CAT}]]" done (( VERBOSE == TRUE )) && print -u 2 "# =================: ================================" (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: wikiAutoLoad()" (( VERBOSE == TRUE )) && print -u 2 "# wikiAutoLoad()...: PageTitle: ${PAGETITLE}" #### Retrieve an "editToken" from the wikimedia server. getWikiEditToken "${APIURL}" "${PAGETITLE}" (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: wikiAutoLoad()" #### Upload the page contents to the wikimedia Server. editWikiPage "${APIURL}" "${PAGETITLE}" "${PAGETEXT}" "${EDITTOKEN}" done #### Remove all temporary output and cookie files [[ -f "${DD_OUTFILE}/wget${$}.out" ]] && rm -f "${DD_OUTFILE}/wget${$}.out" [[ -f "${DD_OUTFILE}/loginToken${$}.xml" ]] && rm -f "${DD_OUTFILE}/loginToken${$}.xml" [[ -f "${DD_COOKIES}/loginTokenCookies${$}.txt" ]] && rm -f "${DD_COOKIES}/loginTokenCookies${$}.txt" [[ -f "${DD_OUTFILE}/wikiLogin${$}.xml" ]] && rm -f "${DD_OUTFILE}/wikiLogin${$}.xml" [[ -f "${DD_COOKIES}/editTokenCookies${$}.txt" ]] && rm -f "${DD_COOKIES}/editTokenCookies${$}.txt" [[ -f "${DD_OUTFILE}/editToken${$}.xml" ]] && rm -f "${DD_OUTFILE}/editToken${$}.xml" [[ -f "${DD_OUTFILE}/editPage${$}.xml" ]] && rm -f "${DD_OUTFILE}/editPage${$}.xml" return 0 } ################################################################ #### Retrieve a login token from the Wiki ################################################################ function getWikiLoginToken { (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: getWikiLoginToken()" typeset APIURL="${1}" typeset LGNAME="lgname=${2}" typeset LGPASS="lgpassword=${3}" typeset ACTION="action=login" typeset FORMAT="format=xml" typeset OUTFILE="${DD_OUTFILE}/loginToken${$}.xml" typeset SAVECOOKIES="${DD_COOKIES}/loginTokenCookies${$}.txt" if (( PREVIEW == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 "# Command >>>>>>>>>: wget ${APIURL}" wget -O "${OUTFILE}" \ --save-cookies "${SAVECOOKIES}" \ --keep-session-cookies \ --post-data="${ACTION}&${FORMAT}&${LGNAME}&${LGPASS}" \ "${APIURL}" 2>"${WGETOUT}" LGTOKEN=$( < "${OUTFILE}" ) LGTOKEN="${LGTOKEN#*token=\"}" LGTOKEN="${LGTOKEN%\"*}" (( VERBOSE == TRUE )) && print -u 2 "# getWikiLoginToken: LGTOKEN = ${LGTOKEN}" fi return ${?} } ################################################################ #### Login to Wiki and establish session ################################################################ function mkWikiLoginSession { (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: mkWikiLoginSession()" typeset APIURL="${1}" typeset LGNAME="lgname=${2}" typeset LGPASS="lgpassword=${3}" typeset LGTOKEN="lgtoken=${4}" typeset ACTION="action=login" typeset FORMAT="format=xml" typeset OUTFILE="${DD_OUTFILE}/wikiLogin${$}.xml" typeset LOADCOOKIES="${DD_COOKIES}/loginTokenCookies${$}.txt" typeset SAVECOOKIES="${DD_COOKIES}/editTokenCookies${$}.txt" (( VERBOSE == TRUE )) && print -u 2 "# mkWikiLoginSessio: WIKI LOGIN NAME = ${2}" if (( PREVIEW == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 "# Command >>>>>>>>>: wget ${APIURL}" wget --debug --no-check-certificate -O "${OUTFILE}" \ --load-cookies "${LOADCOOKIES}" \ --save-cookies "${SAVECOOKIES}" \ --keep-session-cookies \ --post-data="${ACTION}&${FORMAT}&${LGNAME}&${LGPASS}&${LGTOKEN}" \ "${APIURL}" 2>>"${WGETOUT}" fi return ${?} } ################################################################ #### Retrive edit token for each wiki page to be edited ################################################################ function getWikiEditToken { (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: getWikiEditToken()" typeset APIURL="${1}" typeset TITLES="titles=${2}" typeset ACTION="action=query" typeset FORMAT="format=xml" typeset PROP="prop=info" typeset INTOKEN="intoken=edit" typeset OUTFILE="${DD_OUTFILE}/editToken${$}.xml" typeset LOADCOOKIES="${DD_COOKIES}/editTokenCookies${$}.txt" if (( PREVIEW == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 "# Command >>>>>>>>>: wget ${APIURL}" wget --debug --no-check-certificate -O "${OUTFILE}" \ --load-cookies "${LOADCOOKIES}" \ --keep-session-cookies \ --post-data="${ACTION}&${FORMAT}&${PROP}&${TITLES}&${INTOKEN}" \ "${APIURL}" 2>>"${WGETOUT}" EDITTOKEN=$( < "${OUTFILE}" ) EDITTOKEN="${EDITTOKEN#*edittoken=\"}" EDITTOKEN="${EDITTOKEN%+*}" EDITTOKEN="${EDITTOKEN}%2B%5C" (( VERBOSE == TRUE )) && print -u 2 "# getWikiEditToken.: EDITTOKEN = ${EDITTOKEN}" fi return ${?} } ################################################################ #### Create or edit the wiki page. ################################################################ function editWikiPage { (( VERBOSE == TRUE )) && print -u 2 "# Function >>>>>>>>: editWikiPage()" typeset APIURL="${1}" typeset TITLE="title=${2}" typeset TEXT="text=${3}" typeset EDITTOKEN="token=${4}" typeset ACTION="action=edit" typeset RECREATE="recreate" typeset OUTFILE="${DD_OUTFILE}/editPage${$}.xml" typeset LOADCOOKIES="${DD_COOKIES}/editTokenCookies${$}.txt" (( VERBOSE == TRUE )) && print -u 2 "# editWikiPage.....: ${2}" if (( PREVIEW == FALSE )) then (( VERBOSE == TRUE )) && print -u 2 "# Command >>>>>>>>>: wget ${APIURL}" wget --debug --no-check-certificate -O "${OUTFILE}" \ --load-cookies "${LOADCOOKIES}" \ --post-data="${ACTION}&${TITLE}&${RECREATE}&${TEXT}&${EDITTOKEN}" \ "${APIURL}" 2>>"${WGETOUT}" fi return ${?} } ################################################################ ################################################################ ################################################################ wikiAutoLoad "${@}"