home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / FAQ / discus_admin_1357211388 / source / fcn-indx.pl < prev    next >
Text File  |  2009-11-06  |  10KB  |  334 lines

  1. # FILE: fcn-indx.pl
  2. # DESCRIPTION: Reindexing of board
  3. #-------------------------------------------------------------------------------
  4. # DISCUS COPYRIGHT NOTICE
  5. #
  6. # Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
  7. # The use of Discus is governed by the Discus License Agreement which is
  8. # available from the Discus WWW site at:
  9. #    http://www.discusware.com/discus/license
  10. #
  11. # Pursuant to the Discus License Agreement, this copyright notice may not be
  12. # removed or altered in any way.
  13. #-------------------------------------------------------------------------------
  14.  
  15. use strict;
  16. use vars qw($GLOBAL_OPTIONS $PARAMS $DCONF);
  17.  
  18. ###
  19. ### INDEXING_main
  20. ###
  21. ### Controls reindexing of your board
  22. ###
  23.  
  24. sub INDEXING_main {
  25.     my ($arg) = @_;
  26.     if ($arg->{position} eq "" || $arg->{position} eq "start") {
  27.         dreq("fcn-regn");
  28.         my $filelist = incremental( { topic => $arg->{topic}, operation => "build"} );
  29.         my %f;
  30.         foreach my $J (@{ $filelist->{data} }) {
  31.             chomp $J;
  32.             $f{$1} += 1 if $J =~ /\/(\d+)\.$DCONF->{ext}$/;
  33.         }
  34.         if (my @l = grep { $f{$_} > 1 } keys %f) {
  35.             my $subst = {};
  36.             $subst->{general}->{count} = scalar @l;
  37.             my @w = map { { filename => $_ } } @l;
  38.             $subst->{list} = \@w;
  39.             $subst->{arg}->{menuaction} = $arg->{menuaction};
  40.             $subst->{arg}->{username} = $arg->{username};
  41.             $subst->{arg}->{tempfile} = $filelist->{tempfile};
  42.             
  43.             
  44.             
  45.             screen_out("indxerr", $subst);
  46.         }        
  47.         my $subst = {};
  48.         $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?menu=$arg->{menuaction}&action=dr_indexing&username=$arg->{username}&tempfile=$filelist->{tempfile}";
  49.         $subst->{'gauge'}->{'operation'} = 1;
  50.         $subst->{'gauge'}->{'description'} = 100;
  51.         $subst->{'gauge'}->{'percent'} = 0;
  52.         screen_out("gauge", $subst);
  53.     }
  54.     if ($arg->{position} eq "continue") {
  55.         dreq("fcn-regn");
  56.         my $tempfile = incremental({ operation => "read", tempfile => $arg->{tempfile} });
  57.         my $timer = time;
  58.         my $ctr = $arg->{counter};
  59.         my $topic = 0;
  60.         my $fileopen = 0;
  61.         my @profile_changes = ();
  62.         while (1) {
  63.             my $file = shift(@{ $tempfile->{data} }) or last;
  64.             my ($tn, $pn) = split(/\D/, $file);
  65.             next if $tn == 0;
  66.             if ($topic != 0 && $topic != $tn) {
  67.                 unshift(@{ $tempfile->{data} }, $file);
  68.                 last;
  69.             }
  70.             if ($arg->{menuaction} == 1 || $arg->{menuaction} == 2) {
  71.                 if ($topic == 0) {
  72.                     open (TMP, ">> $DCONF->{admin_dir}/msg_index/$tn-tree.TMP") || error_message("File Write Error", "Could not create temporary tree index file in 'msg_index' directory!");
  73.                     $topic = $tn;
  74.                     $fileopen = 1;
  75.                 }
  76.                 my ($pdata, $subs) = build_page_data($tn, $pn);
  77.                 my $pdta1 = shift(@{ $pdata });
  78.                 chomp $pdta1;
  79.                 if (scalar(@{ $subs })) {
  80.                     print TMP $pdta1, "\t", join(",", @{ $subs }), "\n";
  81.                 } else {
  82.                     print TMP $pdta1, "\tX\n";
  83.                 }
  84.                 if (scalar(@{ $pdata })) {
  85.                     print TMP @{ $pdata };
  86.                 }
  87.             } elsif ($arg->{menuaction} == 3) {
  88.                 dreq("fcn-logs");
  89.                 if ($topic == 0) {
  90.                     open (TMP, ">> $DCONF->{admin_dir}/msg_index/$tn-search.TMP") || error_message("File Write Error", "Could not create temporary search index file in 'msg_index' directory!");
  91.                     $topic = $tn;
  92.                     $fileopen = 1;
  93.                 }
  94.                 my $messages = build_search_data($tn, $pn);
  95.                 foreach my $m (@{ $messages }) {
  96.                     print TMP search_hash_to_line($m);
  97.                 }
  98.             }
  99.             $ctr++;
  100.             if ($ctr % 10 == 0) {
  101.                 last if time > ($timer+2+$GLOBAL_OPTIONS->{reindex_secs});
  102.             }
  103.         }
  104.         close (TMP) if $fileopen;
  105.         my $res = incremental({ operation => "write", data => $tempfile->{data}, tempfile => $arg->{tempfile} });
  106.         my $subst = {};
  107.         $subst->{'gauge'}->{'operation'} = 1;
  108.         if ($res->{'continue'} == 1) {
  109.             $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?menu=$arg->{menuaction}&action=dr_indexing&username=$arg->{username}&tempfile=$arg->{tempfile}&counter=$ctr";
  110.             $subst->{'gauge'}->{'description'} = 100;
  111.             $subst->{'gauge'}->{'percent'} = int(100 * $ctr / ($ctr + scalar(@{ $tempfile->{data} }))) if ($ctr + scalar(@{ $tempfile->{data} })) > 0;
  112.         } else {
  113.             if ($arg->{menuaction} == 1 || $arg->{menuaction} == 2) {
  114.                 $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?menu=$arg->{menuaction}&action=dr_sorting&username=$arg->{username}&counter=0";
  115.                 $subst->{'gauge'}->{'description'} = 101;
  116.                 $subst->{'gauge'}->{'percent'} = 0;
  117.             } elsif ($arg->{menuaction} == 3) {
  118.                 $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?action=dr_sindxfin&username=$arg->{username}&counter=0";
  119.                 $subst->{'gauge'}->{'description'} = 103;
  120.                 $subst->{'gauge'}->{'percent'} = 0;
  121.             }
  122.         }
  123.         screen_out("gauge", $subst);
  124.     }
  125.     if ($arg->{position} eq "sort") {
  126.         my $counter = $arg->{counter};
  127.         my @dir = ();
  128.         opendir(DIR, "$DCONF->{admin_dir}/msg_index");
  129.         while (my $dir = readdir(DIR)) {
  130.             if ($dir =~ m|^(\d+)-tree.TMP$|) {
  131.                 push(@dir, $1);
  132.             }
  133.         }
  134.         closedir(DIR);
  135.         if (my $x = shift(@dir)) {
  136.             sorter($x);
  137.             $counter += 1;
  138.         } else {
  139.             my $tpcs = board_topics();
  140.             open (FILE, "> $DCONF->{admin_dir}/tree.txt");
  141.             foreach my $y (@{ $tpcs }) {
  142.                 open (SUB, "< $DCONF->{admin_dir}/msg_index/$y->{number}-tree.txt");
  143.                 print FILE (<SUB>)[0];
  144.                 close (SUB);
  145.             }
  146.             close (FILE);
  147.             dreq("topic-pg");
  148.             regenerate_topic_page();
  149.             return 1;
  150.         }
  151.         undef my $subst;
  152.         $subst->{'gauge'}->{'operation'} = 1;
  153.         $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?menu=$arg->{menuaction}&action=dr_sorting&username=$arg->{username}&counter=$counter";
  154.         $subst->{'gauge'}->{'description'} = 101;
  155.         $subst->{'gauge'}->{'percent'} = int(100 * $counter / ($counter + scalar(@dir))) if ($counter + scalar(@dir)) > 0;
  156.         screen_out("gauge", $subst);
  157.     }
  158.     if ($arg->{position} eq "search") {
  159.         dreq("fcn-logs");
  160.         my $counter = $arg->{counter};
  161.         my @dir = @{ directory_list("$DCONF->{admin_dir}/msg_index", '^\d+-search\.TMP', 1) };
  162.         if (my $x = shift(@dir)) {
  163.             my $num = $1 if $x =~ /^(\d+)-/;
  164.             if ($DCONF->{pro} && $GLOBAL_OPTIONS->{database}) {
  165.                 dreq("sql-imex-PRO", "dbint");
  166.                 my $dbh = db_connect();
  167.                 sql_dump_search_log_to_db("$DCONF->{admin_dir}/msg_index/$x", $dbh);
  168.             } else {
  169.                 open (FILE, "< $DCONF->{admin_dir}/msg_index/$num-search.TMP");
  170.                 open (FILE2, "> $DCONF->{admin_dir}/msg_index/$num-search.txt");
  171.                 while (<FILE>) {
  172.                     print FILE2;
  173.                 }
  174.                 close (FILE);
  175.                 close (FILE2);
  176.             }
  177.             unlink "$DCONF->{admin_dir}/msg_index/$x";
  178.             $counter += 1;
  179.         } else {
  180.             return 1;
  181.         }
  182.         undef my $subst;
  183.         $subst->{'gauge'}->{'operation'} = 1;
  184.         $subst->{'gauge'}->{'refresh_url'} = "$PARAMS->{cgiurl}?action=dr_sindxfin&username=$arg->{username}&counter=$counter";
  185.         $subst->{'gauge'}->{'description'} = 103;
  186.         $subst->{'gauge'}->{'percent'} = int(100 * $counter / ($counter + scalar(@dir))) if ($counter + scalar(@dir)) > 0;
  187.         screen_out("gauge", $subst);
  188.     }    
  189. }
  190.  
  191. ###
  192. ### sorter
  193. ###
  194. ### Provides sorting of entries in your tree file
  195. ###
  196.  
  197. sub sorter {
  198.     my ($topic) = @_;
  199.     undef my $tree;
  200.     open (FILE, "$DCONF->{admin_dir}/msg_index/$topic-tree.TMP");
  201.     while (<FILE>) {
  202.         chomp $_;
  203.         my @c = split(/\t/, $_);
  204.         $tree->{subs}->{$c[2]} = pop @c;
  205.         $tree->{data}->{$c[2]} = join("\t", @c);
  206.     }
  207.     close (FILE);
  208.     my $res = (childfill($topic, $topic, $tree))[0];
  209.     open (FILE, ">$DCONF->{admin_dir}/msg_index/$topic-tree.txt");
  210.     print FILE @{$res};
  211.     close (FILE);
  212.     unlink("$DCONF->{admin_dir}/msg_index/$topic-tree.TMP");
  213.     return 1;
  214. }
  215.  
  216. ###
  217. ### childfill
  218. ###
  219. ### Recursively fills in child page information
  220. ###
  221.  
  222. sub childfill {
  223.     my ($topic, $page, $treeref) = @_;
  224.     my @d = split(/\t/, $treeref->{data}->{$page});
  225.     my @c = split(/,/, $treeref->{subs}->{$page});
  226.     $treeref->{subs}->{$page} = undef;
  227.     my @res = ();
  228.     foreach my $subt (@c) {
  229.         next if $subt eq "X";
  230.         my ($cf, $posts, $subs, $lastmod, $lastposter) = childfill($topic, $subt, $treeref);
  231.         $d[7] += $posts;
  232.         $d[12] += $subs;
  233.         if ($lastposter =~ /\S/) {
  234.             $d[13] = $lastposter if $d[13] !~ /\S/;
  235.             $d[13] = $lastposter if $lastmod > $d[9];
  236.         }
  237.         $d[9] = $lastmod if $lastmod > $d[9];
  238.         next if ! defined $cf;
  239.         push @res, @{ $cf };
  240.     }
  241.     unshift(@res, join("", join("\t", @d), "\n"));
  242.     $treeref->{data}->{$page} = undef;
  243.     return (\@res, $d[7], $d[12], $d[9], $d[13]);
  244. }
  245.  
  246. ###
  247. ### build_page_data
  248. ###
  249. ### Creates a tree entry for a page by reading the actual file
  250. ###
  251.  
  252. sub build_page_data {
  253.     my ($topic, $page, $file) = @_;
  254.     $file = GetPage($topic, $page) if ! defined $file;
  255.     my @ret = ();
  256.     my @m = map { $_->{number} } @{ $file->{messages} };
  257.     my @stm = sort {$a->{'time'} <=> $b->{'time'}} @{ $file->{messages} };
  258.     my $rr = {};
  259.     $rr->{level} = (scalar(@{ $file->{head}->{levels} }) - 1);
  260.     $rr->{level} = 0 if $rr->{level} < 0;
  261.     $rr->{topic} = $topic;
  262.     $rr->{page} = $page;
  263.     $rr->{parent} = ( $topic == $page ? 0 : $file->{head}->{parent} );
  264.     $rr->{name} = $file->{head}->{me_name};
  265.     $rr->{param} = $file->{head}->{param};
  266.     $rr->{posts} = scalar(@{ $file->{messages} });
  267.     $rr->{properties} = $file->{head}->{properties};
  268.     $rr->{lastmod} = 0;
  269.     $rr->{lastmod} = $stm[ $#stm ]->{'time'} if $#stm >= 0;
  270.     $rr->{post_list} = join(",", @m);
  271.     $rr->{subs} = 1;
  272.     if (scalar(@stm)) {
  273.         for (my $i = $#stm; $i >= 0; $i--) {
  274.             my $lp = $stm[$i]->{author};
  275.             $lp = $` if $lp =~ /(?:\(|()(.*?)(?:\)|))/;
  276.             if ($lp =~ /\S/) {
  277.                 $rr->{last_poster} = trim($lp);
  278.                 last;
  279.             }
  280.         }
  281.         my $org = $stm[0]->{'author'};
  282.         $org = $` if $org =~ /(?:\(|()(.*?)(?:\)|))/;
  283.         $rr->{originator} = trim($org);
  284.     }
  285.     my @s = ();
  286.     foreach my $s (@{ $file->{sublist} }) {
  287.         push (@s, $s->{number});
  288.         if ($s->{islink} == 1) {
  289.             my $sl = {};
  290.             $sl->{level} = $rr->{level} + 1;
  291.             $sl->{topic} = $topic;
  292.             $sl->{page} = $s->{number};
  293.             $sl->{parent} = $page;
  294.             $sl->{name} = $s->{name};
  295.             $sl->{islink} = 1;
  296.             $sl->{target} = $s->{target};
  297.             $sl->{url} = $s->{url};
  298.             $sl->{properties} = $s->{props};
  299.             $sl->{subs} = 0;
  300.             $sl->{lastmod} = 0;
  301.             $rr->{subs} += 1;
  302.             my $k = (generate_tree_line($sl))[0];
  303.             chomp $k;
  304.             $k .= "\tX\n";
  305.             push @ret, $k;
  306.         }
  307.     }
  308.     unshift @ret, (generate_tree_line($rr))[0];
  309.     return (\@ret, \@s);
  310. }
  311.  
  312. ###
  313. ### build_search_data
  314. ###
  315. ### Creates a search entry for a page by reading the actual file
  316. ###
  317.  
  318. sub build_search_data {
  319.     my ($topic, $page, $file) = @_;
  320.     $file = GetPage($topic, $page) if ! defined $file;
  321.     my @u = ();
  322.     foreach my $l (@{ $file->{messages} }) {
  323.         undef my $j;
  324.         $j->{topic} = $topic;
  325.         $j->{page} = $page;
  326.         $j->{postindex} = $l->{number};
  327.         $j->{'time'} = $l->{'time'};
  328.         ($j->{text}) = search_stop($l->{text}, undef);
  329.         push @u, $j;
  330.     }
  331.     return \@u;
  332. }
  333. 1;
  334.