#!/usr/bin/env bash
#-----------------------------------------------------------------------
# call: tiki_get_data tiki_host data_source list_name item_name delimitor
#
# read data from tiki MySQL database on 'tiki_host' from 'data_source'
#
# implemented data sources:
# -------------
# - wiki page
# - blog post
# - file gallery
# ------------------------
# it is assumed that a tiki page contains blocks of text limited by the
# 'pseudo-sgml' tags, for example:
#
#
# aaa 125
# bbb 43256 53t6k5
#
#
# in addition to SGML blocks a page may contain comments and any other
# text which it will be ignored such that only content of the SGML block
# is returned to the user
# ----------------------------------------------------------------------
# examples:
# ---------
#
# . ./cdfopr/scripts/tiki_get_data www-cdf.fnal.gov wiki_page . \
# farm.bstn_prod_xpmm0i_bjps_71 \
# project.resources
#-----------------------------------------------------------------------
tiki_host=$1
data_source=$2
export LIST_NAME=$3
export ITEM_NAME=$4
export DELIMITOR=$5
old_tmp_file=$tmp_file
tmp_file=/tmp/$$.tmp
#-----------------------------------------------------------------------
if [ $tiki_host == "." ] ; then tiki_host=www-cdf.fnal.gov ; fi
#-----------------------------------------------------------------------
# call: tiki_mysql_query
# queries $tiki_host, query is stored in $tmp_file
#-----------------------------------------------------------------------
tiki_mysql_query() {
# echo mysql_connect
cmd="mysql -u cdf_reader --password=reader -A"
if [ $tiki_host != `hostname -f` ] ; then cmd=$cmd" -h $tiki_host" ; fi
$cmd < $tmp_file
}
#-----------------------------------------------------------------------
# query and cut
#-----------------------------------------------------------------------
query_and_cut() {
tiki_mysql_query | sed 's/
\\n//g' | tr "" "\n" | \
awk 'BEGIN {write = 0 ; delim = ENVIRON["DELIMITOR"] } \
{ if (write == 0) { \
if ($1 == "<"delim">") write = 1; \
} \
else { \
if ($1 != "</"delim">") print $0; \
else write = 0; \
} \
}' | sed 's/"/"/g' | \
sed 's/<//g'
}
#-----------------------------------------------------------------------
# cat a local file and cut
#-----------------------------------------------------------------------
cat_and_cut() {
# par0=`echo $tiki_host | awk -F: '{print $1}'`
# par1=`echo $tiki_host | awk -F: '{print $2}'`
# if [ "$par1" == "" ] ; then
# cmmd="cat $par0/$ITEM_NAME"
# else
# cmmd="ssh $par0 \"cat $par1/$ITEM_NAME\""
# fi
cmmd="cat $tiki_host/$ITEM_NAME"
$cmmd | \
awk 'BEGIN {write = 0 ; delim = ENVIRON["DELIMITOR"] } \
{ if (write == 0) { \
if ($1 == "<"delim">") write = 1; \
} \
else { \
if ($1 != ""delim">") print $0; \
else write = 0; \
} \
}'
}
#-----------------------------------------------------------------------
# just query
#-----------------------------------------------------------------------
query() {
tiki_mysql_query | sed 's/
//g' | sed 's/\\n//g' | tr "" "\n" | \
awk '{ if ($0 != "data") print $0}'
}
#-----------------------------------------------------------------------
# BLOG query
#-----------------------------------------------------------------------
get_blog_query() {
cat < $tmp_file
use tiki
select post.data from tiki_blog_posts post, tiki_blogs blog \
where blog.title='$LIST_NAME' and blog.blogId=post.blogId and \
post.title='$ITEM_NAME' ;
exit
EOF
}
#-----------------------------------------------------------------------
# FILE GALLERY query
#-----------------------------------------------------------------------
get_file_gallery_query() {
cat < $tmp_file
use tiki
select file.data from tiki_files file, tiki_file_galleries gal where gal.name='$LIST_NAME' and gal.galleryId=file.galleryId and file.filename='$ITEM_NAME' ;
exit
EOF
}
#-----------------------------------------------------------------------
# WIKI PAGE query
#-----------------------------------------------------------------------
get_wiki_page_query() {
cat < $tmp_file
use tiki
select page.data from tiki_pages page where page.pageName='$ITEM_NAME' ;
exit
EOF
}
#-----------------------------------------------------------------------
# here everything starts
#-----------------------------------------------------------------------
if [ $data_source == "blog" ] ; then
get_blog_query
query_and_cut
elif [ $data_source == "file_gallery" ] ; then
get_file_gallery_query
query
elif [ $data_source == "wiki_page" ] ; then
get_wiki_page_query
query_and_cut
elif [ $data_source == "file" ] ; then
cat_and_cut
else
echo "$source: not implemented yet"
fi
if [[ $? == 0 && -e $tmp_file ]] ; then rm $tmp_file ; fi
tmp_file=$old_tmp_file
#------------------------------------------------------------------------------