sort.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2016 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. =head1 Sort Extension
  17. This extension allows you to sort search results based on last update date and
  18. based on creation date (if you have installed creationdate.pl).
  19. =cut
  20. AddModuleDescription('sort.pl', 'Sort Extension');
  21. our ($q, @InitVariables, %Action, %Page, $OpenPageName);
  22. my %SortUpdate;
  23. my %SortCreation;
  24. *OldSortSearchMenu = \&SearchMenu;
  25. *SearchMenu = \&NewSortSearchMenu;
  26. sub NewSortSearchMenu {
  27. my $sort = GetParam('sort');
  28. my $html = OldSortSearchMenu(@_);
  29. my $sort = GetParam('sort');
  30. my $string = UrlEncode(shift);
  31. $html .= ' ' . ($sort
  32. ? ScriptLink("search=$string", T('Sort alphabetically'))
  33. : $q->b(T('Sorted alphabetically')));
  34. $html .= ' ' . ($sort eq 'update'
  35. ? $q->b(T('Sorted by last update first'))
  36. : ScriptLink("search=$string;sort=update",
  37. T('Sort by last update')));
  38. if (defined(&CreationDateOpenPage)) {
  39. $html .= ' ' . ($sort eq 'creation'
  40. ? $q->b(T('Sorted by creation date'))
  41. : ScriptLink("search=$string;sort=creation",
  42. T('Sort by creation date')));
  43. }
  44. return $html;
  45. }
  46. *OldSortSearchTitleAndBody = \&SearchTitleAndBody;
  47. *SearchTitleAndBody = \&NewSortSearchTitleAndBody;
  48. sub NewSortSearchTitleAndBody {
  49. my ($regex, $func, @args) = @_;
  50. %SortUpdate = ();
  51. %SortCreation = ();
  52. my @found = OldSortSearchTitleAndBody($regex);
  53. my $sort = GetParam('sort');
  54. if ($sort eq 'update') {
  55. # last updated means first
  56. @found = sort { $SortUpdate{$b} cmp $SortUpdate{$a} } @found;
  57. } elsif ($sort eq 'creation') {
  58. # first created means first
  59. @found = sort { $SortCreation{$a} cmp $SortCreation{$b} } @found;
  60. }
  61. for my $id (@found) {
  62. $func->($id, @args) if $func;
  63. }
  64. return @found;
  65. }
  66. # Taking advantage of the fact that OpenPage is called for every page, we use it
  67. # to build our hashes.
  68. *OldSortOpenPage = \&OpenPage;
  69. *OpenPage = \&NewSortOpenPage;
  70. sub NewSortOpenPage {
  71. my $value = OldSortOpenPage(@_);
  72. $SortUpdate{$OpenPageName} = $Page{ts};
  73. $SortCreation{$OpenPageName} = $Page{created};
  74. return $value; # I don't think anybody uses this?
  75. }