# ==================================================================
# Plugins::SecondDetailed - Auto Generated Program Module
#
# Plugins::SecondDetailed
# Author : Ivan Herger
# Version : 1.0.0
# Updated : Sat Apr 6 13:59:48 2002
#
# ==================================================================
#
package Plugins::SecondDetailed;
# ==================================================================
use strict;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use Links qw/$CFG $IN $DB/;
use vars qw/$USE_HTML $TIME_START $GRAND_TOTAL $SECOND/;
use Links::Plugins;
# Inherit from base class for debug and error methods
@Plugins::SecondDetailed::ISA = qw(GT::Base);
sub create_second_detailed {
# ------------------------------------------------------------------
# Generate one html page per link.
#
$USE_HTML = defined $ENV{REQUEST_METHOD} ? 1 : 0;
my $sdv = Links::Plugins->get_plugin_user_cfg('SecondDetailed');
my ($cond, $cust_page, $cust_limit);
if (ref $_[0] eq 'HASH') {
$cust_page = $_[0]->{page};
$cust_limit = $_[0]->{limit};
}
else {
$cond = shift;
}
my ($limit, $offset, $page, $count, $set_limit, $db, $url, $sth, $link, $exit_early);
if (!($sdv->{build_second_detailed} or ! $CFG->{build_detailed})) {
print "Skipping second set of Detailed Build (disabled).\n\n";
return;
}
_time_start();
$USE_HTML ?
print "Building second set of Detailed pages ... \n" :
print "Building second set of Detailed pages ... \n";
# Create the detailed directory.
($CFG->{build_detail_path} =~ m,^$CFG->{build_root_path}/(.*)$,) and _build_dir ($1);
# Loop through and build 1000 at a time.
$limit = 1000; $offset = 0; $count = 0;
print "\t";
$db = $DB->table('Links');
# Only build validated links.
$cond ||= new GT::SQL::Condition;
$cond->add ('isValidated', '=', 'Yes');
while (1) {
if ($cust_page or $cust_limit) {
$offset = ($cust_page-1) * $cust_limit;
$db->select_options ("LIMIT $offset, $cust_limit");
last if ($exit_early++);
}
else {
$set_limit = ($offset * $limit) . ",$limit";
$db->select_options ("LIMIT $set_limit");
}
$sth = $db->select ($cond);
last unless ($sth->rows);
while ($link = $sth->fetchrow_hashref) {
$page = $CFG->{build_detail_path} . "/" . $link->{'ID'} . '-2' . $CFG->{build_extension};
$url = $CFG->{build_detail_url} . "/" . $link->{'ID'} . '-2' . $CFG->{build_extension};
$SECOND = 1;
open (DETAIL, "> $page") or _cant_open($page, $!);
print DETAIL Links::Build::build ('detailed', $link);
close DETAIL;
my $perms = oct ($CFG->{build_file_per});
chmod ($perms, $page);
$USE_HTML ?
print "$link->{ID} " :
print "$link->{ID} ";
(++$count % 20) or print "\n\t";
}
$offset++;
}
print "\n";
print "Done (", _time_display(), " s)\n\n";
GT::Plugins->action ( CONTINUE );
return;
}
sub build_second_detailed {
# ------------------------------------------------------------------
# Builds a single detailed page, takes either a link hash ref, or
# a link id.
#
if (! $CFG->{build_detailed}) {
return Links::SiteHTML::display ('error', { error => "Detailed pages are not enabled in the admin." });
}
my $link_db = $DB->table('Links');
$GRAND_TOTAL ||= _grand_total();
my $link = shift;
if (ref $link ne 'HASH') {
return Links::SiteHTML::display ('error', { error => "Invalid argument passed to build_detailed: $link" });
}
$link->{ID} ||= $IN->param('ID');
if (exists $link->{ID}) {
my $id = $link->{ID};
$link = $link_db->get ($id, 'HASH');
$link_db->add_reviews([ $link ]);
if (! $link) {
return Links::SiteHTML::display ('error', { error => "Invalid link id passed to build_detailed: ($id)" });
}
if (! $link->{isValidated} eq 'Yes') {
return Links::SiteHTML::display ('error', { error => "The link has not yet been validated." });
}
}
my ($cat_id, $cat_name) = each %{$link_db->get_categories ($link->{ID})};
my $title = Links::Build::build ('title_unlinked', "$cat_name/$link->{Title}");
my $title_l = Links::Build::build ('title_linked', "$cat_name/$link->{Title}");
# Figure out the next/prev links.
my $catlnk_db = $DB->table ('Links', 'CatLinks');
$catlnk_db->select_options ("ORDER BY $CFG->{build_sort_order_category}") if ($CFG->{build_sort_order_category});
my $sth = $catlnk_db->select ( { CategoryID => $cat_id, isValidated => 'Yes' }, [ 'Links.ID' ] );
my ($next, $prev);
while (my ($id) = $sth->fetchrow_array) {
if ($id == $link->{ID}) {
($next) = $sth->fetchrow_array;
last;
}
else {
$prev = $id;
}
}
my ($next_url, $prev_url);
if ($next) {
$next_url = "$CFG->{build_detail_url}/$next$CFG->{build_extension}";;
}
if ($prev) {
$prev_url = "$CFG->{build_detail_url}/$prev$CFG->{build_extension}";;
}
my $cat_db = $DB->table ('Category');
$link->{Category_Template} = $cat_db->template_set ($cat_id);
GT::Plugins->action ( STOP );
my $templ;
if ($SECOND) {
$templ = 'detailed2';
}
else {
$templ = 'detailed';
}
$SECOND = 0;
# make the breadrumb
my @title_loop;
use Links::Build;
return Links::SiteHTML::display ($templ , {
%$link, title => $title, title_linked => $title_l, grand_total => $GRAND_TOTAL,
next => $next, prev => $prev, next_url => $next_url, prev_url => $prev_url,
title_loop => Links::Build::build('title', "$cat_name/$link->{Title}"),
});
}
# ==================================================================
sub _time_start {
# ------------------------------------------------------------------
# Start a timer.
#
$TIME_START = time;
}
sub _time_display {
# ------------------------------------------------------------------
# Return time results.
#
my $end = time;
my $elapsed = sprintf ("%.2f", $end - $TIME_START);
return $elapsed;
}
sub _build_dir {
# ------------------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
#
my $input = shift;
my $db = $DB->table('Category');
my $clean_input = $db->as_url ($input);
my ($dir, $path) = '';
my @dirs = split /\//, $clean_input;
foreach $dir (@dirs) {
$path .= "/$dir";
$path = _valid_dir ($CFG->{build_root_path}, $path);
# Build the directories.
unless (-e "$CFG->{build_root_path}$path") {
print "\tMaking Directory ($CFG->{build_dir_per}): '$CFG->{build_root_path}$path' ...";
if (mkdir ("$CFG->{build_root_path}$path", oct ($CFG->{build_dir_per}))) {
print "Made.";
}
else { print "mkdir failed! Reason: $!."; }
# Set the permissions
chmod (oct ($CFG->{build_dir_per}), "$CFG->{build_root_path}$path");
print "\n";
}
}
return "$CFG->{build_root_path}$path";
}
sub _valid_dir {
# ------------------------------------------------------------------
# Only allow a-z A-Z 0-9 / - in a directory.
#
my ($root, $dir) = @_;
if (! -e $root and -d _) {
die "Root directory: $root does not exist!";
}
if ($dir !~ m,^([\w\/\-]+)$,) {
die "Invalid characters in category name: $dir. Must contain only letters, numbers, _, / and -.";
}
return $1;
}
sub _cant_open {
# ------------------------------------------------------------------
# Display a more user-friendly error then Links::fatal.
#
my ($page, $err) = @_;
my $user = eval { getpwuid($>); } || 'webserver';
if ($err =~ /permission/i) {
print "\n\nERROR: Unable to open '$page' ($!)\n\n";
if (-e $page) {
print <table ('CatLinks')->total;
my $unval = $DB->table ('Links')->count ( { isValidated => 'No' } );
return $total - $unval;
}
# Always end with a 1.
1;