dates.pl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2010 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('dates.pl', 'Dates Extension');
  18. our ($q, %Action, @MyAdminCode);
  19. push(@MyAdminCode, \&DatesMenu);
  20. sub DatesMenu {
  21. my ($id, $menuref, $restref) = @_;
  22. push(@$menuref,
  23. ScriptLink('action=dates',
  24. T('Extract all dates from the database'),
  25. 'dates'));
  26. }
  27. $Action{dates} = \&DoDates;
  28. my $regex = '([0-9][0-9][0-9][0-9]-([0-9][0-9])-([0-9][0-9]))';
  29. sub DoDates {
  30. print GetHeader('', T('Dates')), $q->start_div({-class=>'content dates'});
  31. print $q->p(T("No dates found.")) unless $q->p(SearchTitleAndBody($regex, \&DateCollector));
  32. DatesPrint();
  33. PrintFooter();
  34. }
  35. my %date_collection;
  36. my $date_page;
  37. *OldDatesSearchString = \&SearchString;
  38. *SearchString = \&NewDatesSearchString;
  39. sub NewDatesSearchString {
  40. $date_page = $_[1]; # save the page text!
  41. return OldDatesSearchString(@_);
  42. }
  43. sub DateCollector {
  44. my $id = shift;
  45. my $text = $date_page; # use the page text saved above!
  46. my ($ignore, $qtext) = split(/\n/, $text, 2);
  47. $qtext = QuoteHtml($qtext);
  48. while ($text =~ m/$regex/g) {
  49. my $date = $1;
  50. my $key = "$2-$3";
  51. my $context = SearchHighlight(SearchExtract($qtext, $date), $date);
  52. push(@{$date_collection{$key}}, [$id, $context]);
  53. }
  54. }
  55. sub DatesPrint {
  56. for my $key (sort keys %date_collection) {
  57. print $q->h2($key);
  58. print '<ul>';
  59. for my $item (@{$date_collection{$key}}) {
  60. my @item = @{$item};
  61. my $id = $item[0];
  62. my $context = $item[1];
  63. print $q->li(GetPageLink($id) . ': ' . $context);
  64. }
  65. print '</ul>';
  66. }
  67. }