#!/usr/bin/perl use strict; use warnings; use CGI; use FindBin qw($Bin); BEGIN { require $Bin.'/Skin/Universal.pm'; require $Bin.'/Skin/Text.pm'; require $Bin.'/Data/Config.pm'; require $Bin.'/Data/Source/Db.pm'; } use locale; use POSIX qw(locale_h strftime ceil); #my $locale = "ru_RU.CP1251"; #my $new_locale = setlocale(LC_ALL, $locale); #warn "Не установлена $locale" if ($new_locale ne $locale); use Cache::Memcached; my $memd = new Cache::Memcached { 'servers' => [ "localhost:11211" ], 'debug' => 0, 'compress_threshold' => 10_000, }; my $config = Config->new(); my $co = CGI->new(); $config->{_db_name} = $config->{_db_name_content}; my $db = Db->new(); $db->connect($config); $config->{WORK_PATH} = $Bin; my $id = $co->param('id') ? $co->param('id') : 'law'; my $file = $co->param('file') ? $co->param('file') : ''; my $page = $co->param('page') ? $co->param('page') : '1'; my $tag = $co->param('tag') ? $co->param('tag') : ''; my $mode = $co->param('mode') ? $co->param('mode') : ''; my $area = $co->param('area') ? $co->param('area') : ''; my $sql = ''; my $res = ''; my $orig_id = $id; # забираем тэги my $tags_array = $memd->get("tags"); if (!$tags_array){ $sql = 'select t_id, t_name, t_pics from tag'; #my $tags_array = $mode eq 'debug' ? $db->select_array_hashref_new($sql) : $db->select_array_hashref($sql); $tags_array = $db->select_array_hashref($sql); $memd->set("tags", $tags_array, 300); } my $TAGS = {}; my $TAGS_IDS = {}; my $TAGS_PICS = {}; $TAGS_IDS->{$_->{t_id}} = $_->{t_name}, $TAGS->{$_->{t_name}} = $_->{t_id}, $TAGS_PICS->{$_->{t_id}} = $_->{t_pics} foreach @{$tags_array}; $config->{TAGS} = $TAGS; $config->{TAGS_IDS} = $TAGS_IDS; $config->{TAGS_PICS} = $TAGS_PICS; $config->{AREA_TAGS} = {}; $config->{AREA_TAGS_IDS} = {}; foreach my $area (keys %{$config->{areas}}){ if (exists $config->{areas}->{$area}->{tag}) { $area =~ s/^([a-z]+)_.+/$1/; $config->{AREA_TAGS}->{ $area } = $TAGS->{ $config->{areas}->{$area}->{tag} }; $config->{AREA_TAGS_IDS}->{ $TAGS->{ $config->{areas}->{$area}->{tag} } } = $area; } } # Внимание! переписываем ID в случае запроса на показ списка, архива или главной страницы авто! if ($mode eq 'list'){ $id = $id.'_list'; } my $data = { 'CONFIG' => $config, 'AREA' => $id, 'MODE' => $mode, 'PAGE' => $page, }; # #$data->{TITLE} = $config->{areas}->{$id}->{name}; $data->{HEADER} = $config->{areas}->{$id}->{header}; if ($file =~ /^\d{10,12}$/){ $file =~ /^(\d{4})(\d\d)(\d\d)(\d+)$/; my $file_data = { year => $1, month => $2, day => $3, nr => $4 }; $data->{HEADER} = qq~$data->{HEADER}~; $sql = qq~ select * from article where date_format(a_date, '%Y-%m-%d') = '$file_data->{year}-$file_data->{month}-$file_data->{day}' -- a_date = '$file_data->{year}.$file_data->{month}.$file_data->{day}' and a_nr = '$file_data->{nr}' having a_tags like '%.$config->{AREA_TAGS}->{$id}.%' ~; $res = $db->select_single_hashref($sql); if ($res){ $res->{tag} = $config->{AREA_TAGS}->{$id}; $res->{id} = $id; $data->{PREV_NEXT_HTML} = Universal::PrevNext(prev_next($config, $db, $res)); $data->{TITLE} = $res->{a_title}; $data->{ANNOUNCE} = $res->{a_announce}; $res->{a_text} =~ s/\r//g; # $res->{a_text} =~ s/(\n+)/\n/g; my @text = split /\n/, $res->{a_text}; my @text_html; my $raw = 0; for (0 ... (scalar @text - 1)){ next if ($text[$_] =~ /(<--img-->)|(<--from-->)/ or $text[$_] =~ /class="subscr"/); $raw = 1 if $text[$_] =~ /
/; $text[$_] =~ s/www\.tks\.ru/pda.tks.ru/g; $text[$_] =~ s/(\/_pics)/http:\/\/static.tks.ru$1/g; if (!$raw){ $text[$_] = qq~$text[$_]
~ if $text[$_] =~ s/<--subtitle-->//; if ($text[$_] =~ s/<--source-->//){ $text[$_] = qq~// $text[$_]
~; } elsif ($text[$_] =~ s/<--head-->//){ $text[$_] = qq~$text[$_]
~; } else { $text[$_] = qq~$text[$_]
~; } } $raw = 0 if $text[$_] =~ /<\/pre>/; push @text_html, $text[$_]; } $data->{TEXT} .= join "\n", @text_html; } } elsif ($page =~ /^\d+$/){ my $per_page = $config->{areas}->{$id}->{per_page}; my $limit = ($config->{areas}->{$id}->{per_page} * ($page - 1) +1). ', '.$config->{areas}->{$id}->{per_page}; $data->{PREV_NEXT_HTML} = Universal::PrevNext(page_prev_next($config, $db, $page)); $sql = qq~ select a_title, a_title_1, a_date, a_nr, a_tags, date_format(a_date, '%Y') as date_y, date_format(a_date, '%y') as date_ys, date_format(a_date, '%m') as date_m, date_format(a_date, '%d') as date_d from article having a_tags like '%.$config->{AREA_TAGS}->{$orig_id}.%' order by a_date desc, a_nr desc limit $limit ~; $res = $db->select_array_hashref_old($sql); foreach my $record (@{$res}){ my @tags = split /\./, $record->{a_tags}; $record->{CLASS} = ''; foreach my $tag (@tags){ $record->{CLASS} = 'class="bold"', last if ($tag and $tag == 7); } $record->{LINK} = $config->{areas}->{$id}->{article_link}; $record->{LINK} =~ s/<%Y%>/$record->{date_y}/; $record->{LINK} =~ s/<%M%>/$record->{date_m}/; $record->{LINK} =~ s/<%D%>/$record->{date_d}/; $record->{LINK} =~ s/<%NR%>/$record->{a_nr}/; $data->{TEXT} .= Universal::FeedRow($record); } } my $Print = ''; $Print .= "Last-Modified: $data->{LAST_MODIFIED}\n" if $data->{LAST_MODIFIED}; $Print .= "Content-type: text/html\n\n"; $Print .= Universal::StartHTML($data); $Print .= Universal::Text($data); #use Data::Dumper; #$Print .= ''; #$Print .= Dumper($id); #$Print .= Dumper($data->{prev_next_links}); #$Print .= Dumper($res); #$Print .= $sql; #$Print .= Dumper($co); #$Print .= ''; $Print .= Universal::EndHTML(); print $Print; $db->disconnect(); sub page_prev_next { my ($config, $db, $page) = @_; my $ret = { prev => '', next => '' }; my $sql = qq~ select count(a_id) as hm from article where a_tags like '%.$config->{AREA_TAGS}->{$orig_id}.%' ~; my $hm = ceil($db->select_single_hashref($sql)->{hm}/$config->{areas}->{$id}->{per_page}); if ($page == 1){ $ret->{next} = $config->{areas}->{$id}->{area_link}.'/p'.($page+1); } elsif ($page < $hm){ $ret->{next} = $config->{areas}->{$id}->{area_link}.'/p'.($page+1); $ret->{prev} = $config->{areas}->{$id}->{area_link}.'/p'.($page-1); } elsif ($page == $hm){ $ret->{prev} = $config->{areas}->{$id}->{area_link}.'/p'.($page-1); } return $ret; } sub prev_next { my ($config, $db, $record) = @_; my $ret = { prev => '', next => '' }; my $sql = qq~ select a_nr, date_format(a_date, '%Y') as date_y, date_format(a_date, '%m') as date_m, date_format(a_date, '%d') as date_d from article where a_id < $record->{a_id} and a_tags like '%.$record->{tag}.%' order by a_date desc, a_nr desc limit 1 offset 0 ~; my $res = $db->select_single_hashref($sql); if ($res){ $ret->{prev} = $config->{areas}->{$id}->{article_link}; $ret->{prev} =~ s/<%Y%>/$res->{date_y}/; $ret->{prev} =~ s/<%M%>/$res->{date_m}/; $ret->{prev} =~ s/<%D%>/$res->{date_d}/; $ret->{prev} =~ s/<%NR%>/$res->{a_nr}/; } $sql = qq~ select a_nr, date_format(a_date, '%Y') as date_y, date_format(a_date, '%m') as date_m, date_format(a_date, '%d') as date_d from article where a_id > $record->{a_id} and a_tags like '%.$record->{tag}.%' order by a_date, a_nr limit 1 offset 0 ~; $res = $db->select_single_hashref($sql); if ($res){ $ret->{next} = $config->{areas}->{$id}->{article_link}; $ret->{next} =~ s/<%Y%>/$res->{date_y}/; $ret->{next} =~ s/<%M%>/$res->{date_m}/; $ret->{next} =~ s/<%D%>/$res->{date_d}/; $ret->{next} =~ s/<%NR%>/$res->{a_nr}/; } return $ret; }