#!/bin/sh
if [[ -n $VERBOSE ]]; then echo Verbose On; fi
# Maybe extend $PATH to include wikiput and wikiupload
PATH=$PATH:/home/alex/bin
# CGI script directory for faster maintenance
CGI_BIN=/home/alex/communitywiki.org/cgi-bin
# Data dir
DATA_DIR=/home/alex/oddwiki
# URL of the wiki.
SCRIPT_NAME=http://www.communitywiki.org/odd
# Source of the bad content page.
# BANNED_CONTENT=http://www.emacswiki.org/cgi-bin/wiki/raw/BannedContent
# BANNED_REGEXPS=http://www.emacswiki.org/cgi-bin/wiki/raw/StrangeBannedContent
# Extract the administrator password from the config file. Should
# look as follows: $AdminPass="foo";
PW=`sed -n 's/\$AdminPass="\(.*\)";/\1/p' < $DATA_DIR/config`
# Find all the namespaces in the "hive".
NS=`find $DATA_DIR/ -maxdepth 1 -type d -name '[A-Z]*' | sort`
# Get new BannedContent and link into every namespace. Just copy the
# lines containing regular expressions. Use symlinks for namespaces
# (ie. this change will not appear in their RecentChanges) and make
# sure to copy the BannedContent.lck file as well (so that nobody
# expects edits to persist).
# if [[ -n $VERBOSE ]]; then echo Installing BannedContent; fi
# wget -q -O - $BANNED_CONTENT \
# | grep '^[^#]' | sort \
# | wikiput -u 'CronJob' -p $PW \
# -s "Update from $BANNED_CONTENT" \
# "$SCRIPT_NAME/BannedContent"
# if [[ -n $VERBOSE ]]; then echo Installing BannedRegexps; fi
# wget -q -O - $BANNED_REGEXPS \
# | grep '^[^#]' | sort \
# | wikiput -u 'CronJob' -p $PW \
# -s "Update from $BANNED_REGEXPS" \
# "$SCRIPT_NAME/BannedRegexps"
# if [[ -n $VERBOSE ]]; then echo Copying BannedContent, BannedRegexps, and BannedHosts to namespaces; fi
# BANFILES="BannedContent.pg BannedContent.lck BannedRegexps.pg BannedRegexps.lck BannedHosts.pg BannedHosts.lck"
# for f in $NS; do
# DIR="$f/page/B"
# if [ ! -d $DIR ]; then
# mkdir -p $DIR
# chmod g+w $DIR
# fi
# for b in $BANFILES; do
# if [ ! -h "$DIR/$b" ]; then
# ln -sf "$DATA_DIR/page/B/$b" "$DIR"
# fi
# done
# rm -f "$f/pageidx"
# done
# Maintenance and despam action for all namespaces. This code used to
# sleep between calls to give other processes a chance to run. We're
# running perl nice, now, however.
cd $CGI_BIN
if [[ -z $NOMAINT ]]; then
if [[ -n $VERBOSE ]]; then echo Starting Maintenance; fi
MAINT=$DATA_DIR/maintenance
mkdir -p $MAINT
for f in $NS; do
f=`basename $f`
if [[ -n $VERBOSE ]]; then echo $f; fi
# nice perl odd.pl action=despam ns=$f | tail -n +7 > "$MAINT/$f-despam.html"
nice perl odd.pl action=maintain ns=$f | tail -n +7 > "$MAINT/$f.html"
done
# Run maintenance and despam on the main namespace (same as above
# without namespace). This will make sure that pages marked for
# deletion that are ready to be deleted are in fact deleted. We'll
# delete the wikis without pages down below.
if [[ -n $VERBOSE ]]; then echo Main; fi
# nice perl odd.pl action=despam | tail -n +7 > "$MAINT/Main-despam.html"
nice perl odd.pl action=maintain | tail -n +7 > "$MAINT/Main.html"
if [[ -n $VERBOSE ]]; then echo Done; fi
fi
cd
# Copy the default homepage.
if [[ -n $VERBOSE ]]; then echo Installing DefaultHomePage; fi
wget -O $DATA_DIR/README \
-q "$SCRIPT_NAME/raw/DefaultHomePage"
for hp in `find $DATA_DIR/page/ -name 'DefaultHomePage??.pg'`; do
page=`basename $hp .pg`
lang=`echo $page | sed -n 's/.*\(..\)$/\1/p' | tr '[A-Z]' '[a-z]'`
wget -O $DATA_DIR/README.$lang \
-q "$SCRIPT_NAME/raw/$page"
done
# overwrite the redirect in the english copy
cp -f $DATA_DIR/README $DATA_DIR/README.en
# Status -- find empty wikis by counting the number of page files.
# When counting, ignore symlinks because BannedContent.pg is a symlink
# (see above). Also post the data collected on the Status page. Make
# sure that nothing in the rest of this script visits the wikis lest
# their data directories be recreated.
if [[ -n $VERBOSE ]]; then echo Computing status and deleting old wikis; fi
STATUS='Current Status:\n'
for f in $NS; do
PG=`find $f/page -name '*.pg' -type f | wc -l`
NM=`basename $f`
if [ $PG == '0' ]; then
STATUS="$STATUS\n* $NM had 0 pages"
if rm -rf "$f"; then
STATUS="$STATUS and was deleted"
else
STATUS="$STATUS but deletion failed"
fi
else
# sort files by time, remove BannedContent, and take the first one.
# its modification time says when the wiki was modified. we cannot
# use the pageidx timestamp, because that file has been deleted after
# BannedContent was copied.
LAST_FILE=`ls -1 -t $f/page/*/*.pg|egrep -v 'Banned(Content|Hosts|Regexps).pg'|head -1`
MOD=`date -r $LAST_FILE --utc '+%Y-%m-%d %H:%M'`
STATUS="$STATUS\n* $NM:HomePage has $PG pages, last modified $MOD"
AGE=`date '+%s' -r $LAST_FILE`
NOW=`date '+%s'`
DAYS=$(( ($NOW-${AGE:-0}) / 60 / 60 / 24 ))
if [ $DAYS -gt 366 ]; then
STATUS="$STATUS, this was more than a year ago"
if rm -rf "$f"; then
STATUS="$STATUS and thus it was deleted"
else
STATUS="$STATUS but deletion failed"
fi
else
if [ $DAYS -gt 183 ]; then
STATUS="$STATUS, this was more than half a year ago -- this wiki will be deleted eventually"
fi
fi
fi
done
echo -e $STATUS \
| wikiput -u 'CronJob' -p "$PW" \
-s 'Page count for all namespaces' \
"$SCRIPT_NAME/Status"
# Documenting what we do: Upload the maintenance script and the config
# file as textfiles to the wiki.
if [[ -n $VERBOSE ]]; then echo Installing script and config files; fi
wikiput -u 'CronJob' -p $PW -s 'Script update' \
"$SCRIPT_NAME/maintenance" < $0
sed -e "s/$PW/*secret*/" < $DATA_DIR/config > /tmp/config.txt
wikiput -u 'CronJob' -p $PW -s 'Config update' \
"$SCRIPT_NAME/config" < /tmp/config.txt
# Write archive
if [[ -n $VERBOSE ]]; then echo Creating new archive; fi
cd
tar czf oddwiki.tar.gz --exclude oddwiki/config oddwiki
mv oddwiki.tar.gz communitywiki.org