phpwiki-search.pl 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #! /usr/bin/perl
  2. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use CGI qw/:standard/;
  17. use CGI::Carp qw(fatalsToBrowser);
  18. use LWP::UserAgent;
  19. use Encode;
  20. if (not param('url')) {
  21. print header(-charset=>'utf-8'),
  22. start_html('PHP Wiki Search RSS 3.0'),
  23. h1('PHP Wiki Search RSS 3.0'),
  24. p('Translates a PHP Wiki Search result into RSS 3.0 usable by Oddmuse.'),
  25. start_form(-method=>'GET'),
  26. p('Search URL: ', textfield('url', '', 40), checkbox('latin-1'), submit()),
  27. end_form(),
  28. end_html();
  29. exit;
  30. }
  31. print header(-type=>'text/plain; charset=UTF-8');
  32. my $url = param('url');
  33. if (param('latin-1')) {
  34. $url =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/ige;
  35. $url = encode('latin-1', decode('utf-8', $url));
  36. my @letters = split(//, $url);
  37. my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')',
  38. ':', '/', '?', ';', '&', '=');
  39. foreach my $letter (@letters) {
  40. my $pattern = quotemeta($letter);
  41. if (not grep(/$pattern/, @safe)) {
  42. $letter = uc(sprintf("%%%02x", ord($letter)));
  43. }
  44. }
  45. $url = join('', @letters);
  46. }
  47. my $ua = new LWP::UserAgent;
  48. my $request = HTTP::Request->new('GET', $url);
  49. my $response = $ua->request($request);
  50. my $data = $response->content;
  51. $data = encode('utf-8', decode('latin-1', $data)) if param('latin-1');
  52. $data =~ /\<title\>([^<]*)/i;
  53. print "title: $1\n" if $1;
  54. print "link: " . param(url) . "\n";
  55. print "debug: $url\n"; # FIXME
  56. print "\n";
  57. while ($data =~ m|<dt>.*?<a href="([^"]*)".*\n((<dd>.*</dd>\n)*)|g) {
  58. my ($title, $desc) = ($1, $2);
  59. $title =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/ige;
  60. $title = encode('utf-8', decode('latin-1', $title)) if param('latin-1');
  61. print "title: $title\n";
  62. $_ = $desc;
  63. s|<dd>||g;
  64. s|<small[^>]*>||g;
  65. s|<strong[^>]*>||g;
  66. s|</strong>||g;
  67. s|</small>||g;
  68. s|</dd>||g;
  69. s|\n+$||g;
  70. s|\n|\n\t|g;
  71. print "description: $_\n";
  72. print "\n";
  73. }