drafts.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2006, 2012 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. use File::Glob ':glob';
  17. our ($DraftDir);
  18. AddModuleDescription('drafts.pl', 'Draft Extension');
  19. our ($q, $Message, $Now, %Action, $DataDir, @MyInitVariables, @MyMaintenance, $FooterNote);
  20. $DraftDir = $DataDir."/draft"; # directory for drafts
  21. push(@MyInitVariables, \&DraftInit);
  22. sub DraftInit {
  23. if (GetParam('Draft', '')) {
  24. SetParam('action', 'draft') ; # Draft button used
  25. } elsif (IsFile("$DraftDir/" . GetParam('username', $q->remote_addr())) # draft exists
  26. and $FooterNote !~ /action=draft/) { # take care of mod_perl persistence
  27. $FooterNote = $q->p(ScriptLink('action=draft', T('Recover Draft'))) . $FooterNote;
  28. }
  29. }
  30. $Action{draft} = \&DoDraft;
  31. sub DoDraft {
  32. my $id = shift;
  33. my $draft = $DraftDir . '/' . GetParam('username', $q->remote_addr());
  34. if ($id) {
  35. my $text = GetParam('text', '');
  36. ReportError(T('No text to save'), '400 BAD REQUEST') unless $text;
  37. CreateDir($DraftDir);
  38. WriteStringToFile($draft, EncodePage(text=>$text, id=>$id));
  39. SetParam('msg', T('Draft saved')); # invalidate cache
  40. print GetHttpHeader('', T('Draft saved'), '204 NO CONTENT');
  41. } elsif (IsFile($draft)) {
  42. my $data = ParseData(ReadFileOrDie($draft));
  43. Unlink($draft);
  44. $Message .= $q->p(T('Draft recovered'));
  45. DoEdit($data->{id}, $data->{text}, 1);
  46. } else {
  47. ReportError(T('No draft available to recover'), '404 NOT FOUND');
  48. }
  49. }
  50. # add preview button to edit page (but not to GetCommentForm!)
  51. *DraftOldGetEditForm = \&GetEditForm;
  52. *GetEditForm = \&DraftNewGetEditForm;
  53. sub DraftNewGetEditForm {
  54. my $html = DraftOldGetEditForm(@_);
  55. # assume that the preview button html is the same for two calls
  56. my $draft_button = $q->submit(-name=>'Draft', -value=>T('Save Draft'));
  57. $html =~ s!(<input[^>]*name="Cancel"[^>]*>)!$1 $draft_button!;
  58. return $html;
  59. }
  60. # cleanup
  61. push(@MyMaintenance, \&DraftCleanup);
  62. sub DraftFiles {
  63. return map {
  64. substr($_, length($DraftDir) + 1);
  65. } Glob("$DraftDir/*"), Glob("$DraftDir/.*");
  66. }
  67. sub DraftCleanup {
  68. print '<p>' . T('Draft Cleanup');
  69. foreach my $draft (DraftFiles()) {
  70. next if $draft eq '.' or $draft eq '..';
  71. my $ts = Modified("$DraftDir/$draft");
  72. if ($Now - $ts < 1209600) { # 14*24*60*60
  73. print $q->br(), Tss("%1 was last modified %2 and was kept",
  74. $draft, CalcTimeSince($Now - $ts));
  75. } elsif (Unlink("$DraftDir/$draft")) {
  76. print $q->br(), Tss("%1 was last modified %2 and was deleted",
  77. $draft, CalcTimeSince($Now - $ts));
  78. } else {
  79. print $q->br(), Ts('Unable to delete draft %s', $draft);
  80. }
  81. }
  82. print '</p>';
  83. }