search-list.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (C) 2006–2023 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('search-list.pl', 'Search List Extension');
  18. our ($q, $bol, %Action, %Page, $OpenPageName, @MyRules);
  19. push(@MyRules, \&SearchListRule);
  20. sub SearchListRule {
  21. if ($bol && /\G(&lt;(list|titlelist) (.*?)&gt;)/cgis) {
  22. # <list regexp> (search page titles and page bodies)
  23. # <titlelist regexp> (search page titles only)
  24. Clean(CloseHtmlEnvironments());
  25. Dirty($1);
  26. my ($oldpos, $old_) = (pos, $_);
  27. my $original = $OpenPageName;
  28. my $variation = $2;
  29. my $term = $3;
  30. if ($term eq "") {
  31. $term = GetId();
  32. }
  33. local ($OpenPageName, %Page);
  34. my @found;
  35. if ($variation eq 'list') {
  36. @found = grep { $_ ne $original } SearchTitleAndBody($term);
  37. } elsif ($variation eq 'titlelist') {
  38. @found = grep { $_ ne $original } Matched($term, AllPagesList());
  39. }
  40. if (defined &PageSort) {
  41. @found = sort PageSort @found;
  42. } else {
  43. @found = sort(@found);
  44. }
  45. @found = map { $q->li(GetPageLink($_)) } @found;
  46. print $q->start_div({-class=>"search $variation"}),
  47. $q->ul(@found), $q->end_div;
  48. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  49. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  50. return '';
  51. }
  52. return;
  53. }
  54. # Add a new action list
  55. $Action{list} = \&DoList;
  56. sub DoList {
  57. my $id = shift;
  58. my $match = GetParam('match', '');
  59. my $search = GetParam('search', '');
  60. ReportError(T('The search parameter is missing.')) unless $match or $search;
  61. print GetHeader('', Ts('Page list for %s', $match||$search), '');
  62. local (%Page, $OpenPageName);
  63. my @found = Matched($match, $search ? SearchTitleAndBody($search) : AllPagesList());
  64. if (defined &PageSort) {
  65. @found = sort PageSort @found;
  66. } else {
  67. @found = sort(@found);
  68. }
  69. @found = map { $q->li(GetPageLink($_)) } @found;
  70. print $q->start_div({-class=>'search list'}), $q->ul(@found), $q->end_div;
  71. PrintFooter();
  72. }