#!/usr/bin/perl
#------------------------------------------------------------------------
# . . . regenerate RealGold indexes and
# ,-,-. ,-. | , ,-. ,-. ,-. | ,-| new stories from the database
# | | | ,-| |< |-' | | | | | | |
# ' ' ' `-^ ' ` `-' `-| `-' `' `-^
# `' by eric richardson
#------------------------------------------------------------------------
# $Id: make_gold,v 1.22 2000/09/17 11:05:40 eric Exp $
#
# makegold: generate RealGold stories and indexes
# Copyright (C) 2000 Eric Richardson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# This script looks in the RealGold stories database for new stories,
# and generates pages for the stories it finds. It then also
# regenerates the indexes for each of the topics.
#
#------------------------------------------------------------------------
use strict;
use vars qw($core $db %dbh %cfg %topics %stories %images @files %stored $date);
use eGold::core;
use Image::Magick;
use String::CRC32;
use Date::Format;
use Date::Language;
use LWP::UserAgent;
use Time::ParseDate;
use Storable;
#----------------#
# Initialization #
#----------------#
$core = eGold::core->start("make_gold");
MAIN: {
# set up some prepared queries
$dbh{get_image} = $db->prepare("
select title,image_text,location,description,photographer,image from image
where id = ?
");
foreach my $locale (&find_locales) {
local $date = Date::Language->new($cfg{locale}{$locale}{language});
warn "locale: $locale\n";
my @topics = &find_topics($locale);
warn "topics: @topics\n";
foreach my $topic (@topics) {
my @stories = &find_stories($topic);
warn "stories: @stories\n";
foreach my $story (@stories) {
&generate_story(
topic => $topic,
story => $story,
);
}
&generate_story_index(
topic => $topic,
stories => \@stories,
);
}
&generate_topic_index(
locale => $locale,
topics => \@topics,
);
}
$core->upload(@files);
$core->save_memory;
}
#-------------#
# Subroutines #
#-------------#
sub find_locales {
my @locales;
my $get_locales = $db->prepare("
select distinct locale from topic order by locale
");
$get_locales->execute;
my ($locale);
$get_locales->bind_columns(\$locale);
while ($get_locales->fetch) {
push @locales, $locale;
}
return @locales;
}
#----------
sub generate_story_index {
my %args = @_;
my $shtml;
my $html = $core->get_template(
"s_index.".$topics{$args{topic}}{locale}.".".$args{topic}
);
# fill in topic info
$html =~ s!#{t_name}!$args{topic}!gi;
$html =~ s!#{t_desc}!$topics{$args{topic}}{desc}!gi;
foreach my $story (@{$args{stories}}) {
my $lshtml = $core->get_template(
"s_index-story.".$topics{$args{topic}}{locale}.".".$args{topic}
);
foreach my $var (
'id','headline','subtitle','location','intro','author','released'
) {
$lshtml =~ s!#{$var}!$stories{$story}{$var}!gi;
}
$shtml .= $lshtml;
}
$html =~ s!#{stories}!$shtml!i;
$core->write_file(
name =>
$topics{$args{topic}}{locale}."/".$args{topic}."/topics.shtml",
data => $shtml,
);
$core->write_file(
name =>
$topics{$args{topic}}{locale}."/".$args{topic}."/index.shtml",
data => $html,
);
}
#----------
sub generate_topic_index {
my %args = @_;
my $thtml;
my $html = $core->get_template("t_index.".$args{locale});
foreach my $topic (@{$args{topics}}) {
my $lthtml = $core->get_template("t_index-topic.".$args{locale});
$lthtml =~ s!#{ctopic}!$core->clean_up($topic)!gei;
$lthtml =~ s!#{topic}!$topic!gi;
$lthtml =~ s!#{desc}!$topics{$topic}{desc}!gi;
$thtml .= $lthtml;
}
$html =~ s!#{topics}!$thtml!i;
$core->write_file(
name => $args{locale}."/index.shtml",
data => $html,
);
}
#----------
sub find_topics {
my $locale = shift;
my @topics;
my $get_topics = $db->prepare("
select topic_id,description from topic where locale = ?
order by description
");
$get_topics->execute($locale);
my ($t_id,$t_desc);
$get_topics->bind_columns(\$t_id,\$t_desc);
while ($get_topics->fetch) {
push @topics, $t_id;
%{$topics{$t_id}} = (
desc => $t_desc,
locale => $locale,
);
}
return @topics;
}
#----------
sub find_stories {
my $topic = shift;
my @stories;
my $get_stories = $db->prepare("
select
article.id,article.headline,article.subtitle,article.location,
article.intro,article.story,article.author,unix_timestamp(article.released),article.intercession
from
article,art_topic_lnk
where
art_topic_lnk.article_id = article.id
and released != 0
and article.state != 'WITHDRAWN'
and art_topic_lnk.topic_id = ?
order by article.written
");
$get_stories->execute($topic);
my ($id,$head,$sub,$loc,$intro,$story,$author,$released,$intercess);
$get_stories->bind_columns(
\$id,\$head,\$sub,\$loc,\$intro,\$story,\$author,\$released,\$intercess
);
while ($get_stories->fetch) {
push @stories, $id;
$story =~ s!\n!\n
!gi;
$intro =~ s!\n!\n
!gi;
# isn't this pretty much the most roundabout method of delivering a
# simple little date that you've ever seen? Gar.
$released = time2str($cfg{locale}{$topics{$topic}{locale}}{date},$released);
%{$stories{$id}} = (
id => $id,
headline => $head,
subtitle => $sub,
location => $loc,
intro => $intro,
story => $story,
author => $author,
released => $released,
locale => $topics{$topic}{locale},
intercession => $intercess,
);
}
return @stories;
}
#----------
sub generate_story {
my %args = @_;
my $outfile =
$topics{$args{topic}}{locale}."/".$args{topic}."/".$args{story}.".shtml";
my $html = $core->get_template(
"story.".$topics{$args{topic}}{locale}.".".$args{topic}
);
my @images = &load_images_for_story($args{story});
my @links = &load_links_for_story($args{story});
my @img_links;
foreach my $img (@images) {
&generate_image($img);
&generate_image_page($img,$topics{$args{topic}}{locale});
if ($html =~ m!#{thumb}!i) {
my $thumbnail = &generate_thumbnail(
img => $img,
locale => $topics{$args{topic}}{locale},
topic => $args{topic},
);
$html =~ s!#{thumb}!$thumbnail!i;
} else {
push @img_links, [$images{$img}{text},'../../photos/'.$img.'.shtml'];
}
}
unshift @links, @img_links;
foreach (
'headline','subtitle','location','intro','story','author','released','intercession'
) {
$html =~ s!#{$_}!$stories{$args{story}}{$_}!gi;
}
my $link_html = &make_links(@links);
$html =~ s!#{links}!$link_html!i;
my $fid;
if (!$stored{eThreads}{$args{story}}) {
# this is the first time we're creating this story... hopefully
$fid = &create_forum(
id => $args{story},
title => $stories{$args{story}}{headline},
);
$stored{eThreads}{$args{story}} = $fid;
} else {
$fid = $stored{eThreads}{$args{story}};
warn "using stored fid: $fid\n";
}
$html =~ s!#{fid}!$fid!gi;
$html =~ s!#{[^}]+}!!g;
$core->write_file(
name => $outfile,
data => $html,
);
}
#---------
sub generate_thumbnail {
my %args = @_;
my $html = $core->get_template("thumb.".$args{locale}.".".$args{topic});
my $image = "../../photos/".$args{img} . "-thumb.jpg";
my $url = "../../photos/".$args{img}.".shtml";
$html =~ s!#{image}!$image!gi;
$html =~ s!#{url}!$url!gi;
$html =~ s!#{width}!$images{$args{img}}{thumb}{width}!gi;
$html =~ s!#{height}!$images{$args{img}}{thumb}{height}!gi;
foreach my $key (keys %{$images{$args{img}}}) {
next if ($key eq"thumb");
$html =~ s!#{$key}!$images{$args{img}}{$key}!gi;
}
return $html;
}
#---------
sub load_links_for_story {
my $story = shift;
my @links;
my $get_links = $db->prepare("
select url_text,url from url where article_id = ?
");
$get_links->execute($story);
my ($text,$url);
$get_links->bind_columns(\$text,\$url);
push @links, [$text,$url] while ($get_links->fetch);
return @links;
}
#---------
sub make_links {
my @links = @_;
my $html = "