| 4 | | use base 'Prlmnks::WWW'; |
|---|
| 5 | | |
|---|
| 6 | | use PrlMnks::Node; |
|---|
| 7 | | use PrlMnks::RSS; |
|---|
| 8 | | use CGI; |
|---|
| 9 | | use Data::Dumper; |
|---|
| 10 | | |
|---|
| 11 | | sub run { |
|---|
| 12 | | |
|---|
| 13 | | my $id = $self->cgi->url( -absolute => 1 ); |
|---|
| 14 | | $id =~ s/^\/rss\/(.*).xml$/$1/; |
|---|
| 15 | | |
|---|
| 16 | | $self->debug("Looking at '$id'"); |
|---|
| 17 | | |
|---|
| 18 | | # What sort of request is this? |
|---|
| 19 | | $id =~ m{ \A \d+ \z}xms # is it an id? |
|---|
| 20 | | ? process_id($id) # if so process it |
|---|
| 21 | | : process_category($id); # assume a category |
|---|
| 22 | | |
|---|
| 23 | | return 1; |
|---|
| 24 | | } |
|---|
| 25 | | |
|---|
| 26 | | ################################################################################ |
|---|
| 27 | | |
|---|
| 28 | | sub process_id { |
|---|
| 29 | | my $node_id = shift; |
|---|
| 30 | | my $node = PrlMnks::Node->retrieve($node_id); |
|---|
| 31 | | |
|---|
| 32 | | unless ($node) { |
|---|
| 33 | | require PrlMnks::Daemon; |
|---|
| 34 | | $node = |
|---|
| 35 | | PrlMnks::Daemon->new( DEBUG => $DEBUG ) |
|---|
| 36 | | ->fetch_and_store_node($node_id); |
|---|
| 37 | | |
|---|
| 38 | | die "Could not fetch the node id: '$node_id'"; |
|---|
| 39 | | } |
|---|
| 40 | | |
|---|
| 41 | | my $node_type = $node->node_type; |
|---|
| 42 | | warn "Node type is '$node_type'\n" if $DEBUG; |
|---|
| 43 | | |
|---|
| 44 | | my @nodes = (); |
|---|
| 45 | | |
|---|
| 46 | | # Get the nodes to xmlify. |
|---|
| 47 | | if ( $node_type =~ m/^user/ ) { |
|---|
| 48 | | @nodes = PrlMnks::Node->search( |
|---|
| 49 | | author_id => $node->node_id, |
|---|
| 50 | | { order_by => 'node_id desc' } |
|---|
| 51 | | ); |
|---|
| 52 | | } |
|---|
| 53 | | |
|---|
| 54 | | elsif ( $node_type eq 'note' ) { |
|---|
| 55 | | @nodes = ( |
|---|
| 56 | | $node, |
|---|
| 57 | | PrlMnks::Node->search( |
|---|
| 58 | | parent_id => $node->node_id, |
|---|
| 59 | | { order_by => 'node_id desc' } |
|---|
| 60 | | ) |
|---|
| 61 | | ); |
|---|
| 62 | | } |
|---|
| 63 | | |
|---|
| 64 | | else { |
|---|
| 65 | | @nodes = PrlMnks::Node->search( root_id => $node->node_id ); |
|---|
| 66 | | |
|---|
| 67 | | # Order the nodes by thread. |
|---|
| 68 | | @nodes = order_by_thread( $node->node_id, @nodes ); |
|---|
| 69 | | |
|---|
| 70 | | unshift @nodes, $node; |
|---|
| 71 | | } |
|---|
| 72 | | |
|---|
| 73 | | create_rss( |
|---|
| 74 | | title => $node->title, |
|---|
| 75 | | name => $node->node_id, |
|---|
| 76 | | nodes => \@nodes |
|---|
| 77 | | ); |
|---|
| 78 | | } |
|---|
| 79 | | |
|---|
| 80 | | sub order_by_thread { |
|---|
| 81 | | my $top_id = shift; |
|---|
| 82 | | my @nodes_in = @_; |
|---|
| 83 | | |
|---|
| 84 | | my %ids_to_nodes = map { $_->node_id => $_ } @nodes_in; |
|---|
| 85 | | |
|---|
| 86 | | #warn "top_id is '$top_id'"; |
|---|
| 87 | | |
|---|
| 88 | | my %hash = thread_build_hash(@nodes_in); |
|---|
| 89 | | |
|---|
| 90 | | my @ids = thread_sort_hash( $top_id, %hash ); |
|---|
| 91 | | |
|---|
| 92 | | #warn Dumper( \@ids); |
|---|
| 93 | | |
|---|
| 94 | | my @nodes = (); |
|---|
| 95 | | push @nodes, $ids_to_nodes{$_} for @ids; |
|---|
| 96 | | |
|---|
| 97 | | return @nodes; |
|---|
| 98 | | } |
|---|
| 99 | | |
|---|
| 100 | | sub thread_build_hash { |
|---|
| 101 | | |
|---|
| 102 | | # Build a hash |
|---|
| 103 | | my @nodes = @_; |
|---|
| 104 | | my %parents = (); |
|---|
| 105 | | |
|---|
| 106 | | foreach my $node (@nodes) { |
|---|
| 107 | | my $parent_id = $node->parent_id; |
|---|
| 108 | | |
|---|
| 109 | | #warn "adding '$parent_id' to \%parents"; |
|---|
| 110 | | push @{ $parents{$parent_id} }, $node->node_id; |
|---|
| 111 | | } |
|---|
| 112 | | |
|---|
| 113 | | #warn Dumper( \%parents ); |
|---|
| 114 | | |
|---|
| 115 | | return %parents; |
|---|
| 116 | | } |
|---|
| 117 | | |
|---|
| 118 | | sub thread_sort_hash { |
|---|
| 119 | | my $id = shift; |
|---|
| 120 | | my %hash = @_; |
|---|
| 121 | | |
|---|
| 122 | | my @sorted = (); |
|---|
| 123 | | |
|---|
| 124 | | my $ids = $hash{$id}; |
|---|
| 125 | | return @sorted unless $ids; |
|---|
| 126 | | |
|---|
| 127 | | foreach my $id ( sort { $a <=> $b } @$ids ) { |
|---|
| 128 | | push @sorted, $id; |
|---|
| 129 | | push @sorted, thread_sort_hash( $id, %hash ); |
|---|
| 130 | | } |
|---|
| 131 | | |
|---|
| 132 | | return @sorted; |
|---|
| 133 | | } |
|---|
| 134 | | |
|---|
| 135 | | sub process_category { |
|---|
| 136 | | my $category = shift; |
|---|
| 137 | | |
|---|
| 138 | | # Check to see if the category is good. |
|---|
| 139 | | die "Bad category '$category'" unless is_category_good($category); |
|---|
| 140 | | |
|---|
| 141 | | my $SQL = ""; |
|---|
| 142 | | |
|---|
| 143 | | if ( $category eq 'all' ) { |
|---|
| 144 | | $SQL = "actual_id is null"; |
|---|
| 145 | | |
|---|
| 146 | | } |
|---|
| 147 | | elsif ( $category eq 'top' ) { |
|---|
| 148 | | $SQL = "is_top = 1 "; |
|---|
| 149 | | |
|---|
| 150 | | } |
|---|
| 151 | | else { |
|---|
| 152 | | $SQL = "node_type = '$category'"; |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | $SQL .= " order by node_id desc limit " . $$conf{number_of_nodes}; |
|---|
| 156 | | |
|---|
| 157 | | my @nodes = PrlMnks::Node->retrieve_from_sql($SQL); |
|---|
| 158 | | |
|---|
| 159 | | create_rss( title => $category, name => $category, nodes => \@nodes ); |
|---|
| 160 | | } |
|---|
| 161 | | |
|---|
| 162 | | sub is_category_good { |
|---|
| 163 | | my $category = shift; |
|---|
| 164 | | return 0 unless $category; |
|---|
| 165 | | |
|---|
| 166 | | my %valid = map { $_ => 1 } |
|---|
| 167 | | qw( user qandasection sourcecode usergroup bookreview note |
|---|
| 168 | | perltutorial categorized_question poem monkdiscuss |
|---|
| 169 | | perlquestion cufp obfuscated snippet perlnews scratchpad |
|---|
| 170 | | perlmeditation pmdevtopic |
|---|
| 171 | | all top ); |
|---|
| 172 | | |
|---|
| 173 | | return $valid{$category} || 0; |
|---|
| 174 | | } |
|---|
| 175 | | |
|---|
| 176 | | sub create_rss { |
|---|
| 177 | | my %args = @_; |
|---|
| 178 | | my $nodes = $args{nodes}; |
|---|
| 179 | | |
|---|
| 180 | | # Create the RSS. |
|---|
| 181 | | my $rss = PrlMnks::RSS->new; |
|---|
| 182 | | |
|---|
| 183 | | $rss->set_title( $args{title} ); |
|---|
| 184 | | $rss->set_name( $args{name} ); |
|---|
| 185 | | |
|---|
| 186 | | $rss->add_node($_) for @$nodes; |
|---|
| 187 | | |
|---|
| 188 | | $rss->save; |
|---|
| 189 | | |
|---|
| 190 | | print $cgi->header( -type => 'application/xml', -status => '200 OK' ); |
|---|
| 191 | | print $rss->as_string; |
|---|
| 192 | | } |
|---|
| | 3 | # use strict; |
|---|
| | 4 | # use warnings; |
|---|
| | 5 | # |
|---|
| | 6 | # use base 'Prlmnks::WWW'; |
|---|
| | 7 | # |
|---|
| | 8 | # use PrlMnks::Node; |
|---|
| | 9 | # use PrlMnks::RSS; |
|---|
| | 10 | # use CGI; |
|---|
| | 11 | # use Data::Dumper; |
|---|
| | 12 | # |
|---|
| | 13 | # sub run { |
|---|
| | 14 | # |
|---|
| | 15 | # my $id = $self->cgi->url( -absolute => 1 ); |
|---|
| | 16 | # $id =~ s/^\/rss\/(.*).xml$/$1/; |
|---|
| | 17 | # |
|---|
| | 18 | # $self->debug("Looking at '$id'"); |
|---|
| | 19 | # |
|---|
| | 20 | # # What sort of request is this? |
|---|
| | 21 | # $id =~ m{ \A \d+ \z}xms # is it an id? |
|---|
| | 22 | # ? process_id($id) # if so process it |
|---|
| | 23 | # : process_category($id); # assume a category |
|---|
| | 24 | # |
|---|
| | 25 | # return 1; |
|---|
| | 26 | # } |
|---|
| | 27 | # |
|---|
| | 28 | # ################################################################################ |
|---|
| | 29 | # |
|---|
| | 30 | # sub process_id { |
|---|
| | 31 | # my $node_id = shift; |
|---|
| | 32 | # my $node = PrlMnks::Node->retrieve($node_id); |
|---|
| | 33 | # |
|---|
| | 34 | # unless ($node) { |
|---|
| | 35 | # require PrlMnks::Daemon; |
|---|
| | 36 | # $node = |
|---|
| | 37 | # PrlMnks::Daemon->new( DEBUG => $DEBUG ) |
|---|
| | 38 | # ->fetch_and_store_node($node_id); |
|---|
| | 39 | # |
|---|
| | 40 | # die "Could not fetch the node id: '$node_id'"; |
|---|
| | 41 | # } |
|---|
| | 42 | # |
|---|
| | 43 | # my $node_type = $node->node_type; |
|---|
| | 44 | # warn "Node type is '$node_type'\n" if $DEBUG; |
|---|
| | 45 | # |
|---|
| | 46 | # my @nodes = (); |
|---|
| | 47 | # |
|---|
| | 48 | # # Get the nodes to xmlify. |
|---|
| | 49 | # if ( $node_type =~ m/^user/ ) { |
|---|
| | 50 | # @nodes = PrlMnks::Node->search( |
|---|
| | 51 | # author_id => $node->node_id, |
|---|
| | 52 | # { order_by => 'node_id desc' } |
|---|
| | 53 | # ); |
|---|
| | 54 | # } |
|---|
| | 55 | # |
|---|
| | 56 | # elsif ( $node_type eq 'note' ) { |
|---|
| | 57 | # @nodes = ( |
|---|
| | 58 | # $node, |
|---|
| | 59 | # PrlMnks::Node->search( |
|---|
| | 60 | # parent_id => $node->node_id, |
|---|
| | 61 | # { order_by => 'node_id desc' } |
|---|
| | 62 | # ) |
|---|
| | 63 | # ); |
|---|
| | 64 | # } |
|---|
| | 65 | # |
|---|
| | 66 | # else { |
|---|
| | 67 | # @nodes = PrlMnks::Node->search( root_id => $node->node_id ); |
|---|
| | 68 | # |
|---|
| | 69 | # # Order the nodes by thread. |
|---|
| | 70 | # @nodes = order_by_thread( $node->node_id, @nodes ); |
|---|
| | 71 | # |
|---|
| | 72 | # unshift @nodes, $node; |
|---|
| | 73 | # } |
|---|
| | 74 | # |
|---|
| | 75 | # create_rss( |
|---|
| | 76 | # title => $node->title, |
|---|
| | 77 | # name => $node->node_id, |
|---|
| | 78 | # nodes => \@nodes |
|---|
| | 79 | # ); |
|---|
| | 80 | # } |
|---|
| | 81 | # |
|---|
| | 82 | # sub order_by_thread { |
|---|
| | 83 | # my $top_id = shift; |
|---|
| | 84 | # my @nodes_in = @_; |
|---|
| | 85 | # |
|---|
| | 86 | # my %ids_to_nodes = map { $_->node_id => $_ } @nodes_in; |
|---|
| | 87 | # |
|---|
| | 88 | # #warn "top_id is '$top_id'"; |
|---|
| | 89 | # |
|---|
| | 90 | # my %hash = thread_build_hash(@nodes_in); |
|---|
| | 91 | # |
|---|
| | 92 | # my @ids = thread_sort_hash( $top_id, %hash ); |
|---|
| | 93 | # |
|---|
| | 94 | # #warn Dumper( \@ids); |
|---|
| | 95 | # |
|---|
| | 96 | # my @nodes = (); |
|---|
| | 97 | # push @nodes, $ids_to_nodes{$_} for @ids; |
|---|
| | 98 | # |
|---|
| | 99 | # return @nodes; |
|---|
| | 100 | # } |
|---|
| | 101 | # |
|---|
| | 102 | # sub thread_build_hash { |
|---|
| | 103 | # |
|---|
| | 104 | # # Build a hash |
|---|
| | 105 | # my @nodes = @_; |
|---|
| | 106 | # my %parents = (); |
|---|
| | 107 | # |
|---|
| | 108 | # foreach my $node (@nodes) { |
|---|
| | 109 | # my $parent_id = $node->parent_id; |
|---|
| | 110 | # |
|---|
| | 111 | # #warn "adding '$parent_id' to \%parents"; |
|---|
| | 112 | # push @{ $parents{$parent_id} }, $node->node_id; |
|---|
| | 113 | # } |
|---|
| | 114 | # |
|---|
| | 115 | # #warn Dumper( \%parents ); |
|---|
| | 116 | # |
|---|
| | 117 | # return %parents; |
|---|
| | 118 | # } |
|---|
| | 119 | # |
|---|
| | 120 | # sub thread_sort_hash { |
|---|
| | 121 | # my $id = shift; |
|---|
| | 122 | # my %hash = @_; |
|---|
| | 123 | # |
|---|
| | 124 | # my @sorted = (); |
|---|
| | 125 | # |
|---|
| | 126 | # my $ids = $hash{$id}; |
|---|
| | 127 | # return @sorted unless $ids; |
|---|
| | 128 | # |
|---|
| | 129 | # foreach my $id ( sort { $a <=> $b } @$ids ) { |
|---|
| | 130 | # push @sorted, $id; |
|---|
| | 131 | # push @sorted, thread_sort_hash( $id, %hash ); |
|---|
| | 132 | # } |
|---|
| | 133 | # |
|---|
| | 134 | # return @sorted; |
|---|
| | 135 | # } |
|---|
| | 136 | # |
|---|
| | 137 | # sub process_category { |
|---|
| | 138 | # my $category = shift; |
|---|
| | 139 | # |
|---|
| | 140 | # # Check to see if the category is good. |
|---|
| | 141 | # die "Bad category '$category'" unless is_category_good($category); |
|---|
| | 142 | # |
|---|
| | 143 | # my $SQL = ""; |
|---|
| | 144 | # |
|---|
| | 145 | # if ( $category eq 'all' ) { |
|---|
| | 146 | # $SQL = "actual_id is null"; |
|---|
| | 147 | # |
|---|
| | 148 | # } |
|---|
| | 149 | # elsif ( $category eq 'top' ) { |
|---|
| | 150 | # $SQL = "is_top = 1 "; |
|---|
| | 151 | # |
|---|
| | 152 | # } |
|---|
| | 153 | # else { |
|---|
| | 154 | # $SQL = "node_type = '$category'"; |
|---|
| | 155 | # } |
|---|
| | 156 | # |
|---|
| | 157 | # $SQL .= " order by node_id desc limit " . $$conf{number_of_nodes}; |
|---|
| | 158 | # |
|---|
| | 159 | # my @nodes = PrlMnks::Node->retrieve_from_sql($SQL); |
|---|
| | 160 | # |
|---|
| | 161 | # create_rss( title => $category, name => $category, nodes => \@nodes ); |
|---|
| | 162 | # } |
|---|
| | 163 | # |
|---|
| | 164 | # sub is_category_good { |
|---|
| | 165 | # my $category = shift; |
|---|
| | 166 | # return 0 unless $category; |
|---|
| | 167 | # |
|---|
| | 168 | # my %valid = map { $_ => 1 } |
|---|
| | 169 | # qw( user qandasection sourcecode usergroup bookreview note |
|---|
| | 170 | # perltutorial categorized_question poem monkdiscuss |
|---|
| | 171 | # perlquestion cufp obfuscated snippet perlnews scratchpad |
|---|
| | 172 | # perlmeditation pmdevtopic |
|---|
| | 173 | # all top ); |
|---|
| | 174 | # |
|---|
| | 175 | # return $valid{$category} || 0; |
|---|
| | 176 | # } |
|---|
| | 177 | # |
|---|
| | 178 | # sub create_rss { |
|---|
| | 179 | # my %args = @_; |
|---|
| | 180 | # my $nodes = $args{nodes}; |
|---|
| | 181 | # |
|---|
| | 182 | # # Create the RSS. |
|---|
| | 183 | # my $rss = PrlMnks::RSS->new; |
|---|
| | 184 | # |
|---|
| | 185 | # $rss->set_title( $args{title} ); |
|---|
| | 186 | # $rss->set_name( $args{name} ); |
|---|
| | 187 | # |
|---|
| | 188 | # $rss->add_node($_) for @$nodes; |
|---|
| | 189 | # |
|---|
| | 190 | # $rss->save; |
|---|
| | 191 | # |
|---|
| | 192 | # print $cgi->header( -type => 'application/xml', -status => '200 OK' ); |
|---|
| | 193 | # print $rss->as_string; |
|---|
| | 194 | # } |
|---|