search-list.pl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2006, 2007, 2008 Alex Schroeder <alex@emacswiki.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 %hash = ();
  35. if ($variation eq 'list') {
  36. foreach my $id (SearchTitleAndBody($term)) {
  37. $hash{$id} = 1 unless $id eq $original; # skip the page with the query
  38. }
  39. }
  40. if ($variation eq 'titlelist') {
  41. foreach my $id (grep(/$term/, AllPagesList())) {
  42. $hash{$id} = 1 unless $id eq $original; # skip the page with the query
  43. }
  44. }
  45. my @found = keys %hash;
  46. if (defined &PageSort) {
  47. @found = sort PageSort @found;
  48. } else {
  49. @found = sort(@found);
  50. }
  51. @found = map { $q->li(GetPageLink($_)) } @found;
  52. print $q->start_div({-class=>"search $variation"}),
  53. $q->ul(@found), $q->end_div;
  54. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  55. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  56. return '';
  57. }
  58. return;
  59. }
  60. # Add a new action list
  61. $Action{list} = \&DoList;
  62. sub DoList {
  63. my $id = shift;
  64. my $match = GetParam('match', '');
  65. my $search = GetParam('search', '');
  66. ReportError(T('The search parameter is missing.')) unless $match or $search;
  67. print GetHeader('', Ts('Page list for %s', $match||$search), '');
  68. local (%Page, $OpenPageName);
  69. my %hash = ();
  70. foreach my $id (grep(/$match/, $search
  71. ? SearchTitleAndBody($search)
  72. : AllPagesList())) {
  73. $hash{$id} = 1;
  74. }
  75. my @found = keys %hash;
  76. if (defined &PageSort) {
  77. @found = sort PageSort @found;
  78. } else {
  79. @found = sort(@found);
  80. }
  81. @found = map { $q->li(GetPageLink($_)) } @found;
  82. print $q->start_div({-class=>'search list'}),
  83. $q->ul(@found), $q->end_div;
  84. PrintFooter();
  85. }