askpage.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2014 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  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('askpage.pl', 'Ask Page Extension');
  18. use Fcntl qw(:DEFAULT :flock);
  19. our ($DataDir, %Translate, @MyFooters);
  20. our ($AskPage, $QuestionPage, $NewQuestion);
  21. # Don't forget to set your $CommentsPattern to include both $AskPage and $QuestionPage
  22. $AskPage = 'Ask';
  23. $QuestionPage = 'Question_';
  24. $NewQuestion = 'Write your question here:';
  25. sub IncrementInFile {
  26. my $filename = shift;
  27. sysopen my $fh, encode_utf8($filename), O_RDWR|O_CREAT or die "can't open $filename: $!";
  28. flock $fh, LOCK_EX or die "can't flock $filename: $!";
  29. my $num = <$fh> || 1;
  30. seek $fh, 0, 0 or die "can't rewind $filename: $!";
  31. truncate $fh, 0 or die "can't truncate $filename: $!";
  32. (print $fh $num+1, "\n") or die "can't write $filename: $!";
  33. close $fh or die "can't close $filename: $!";
  34. return $num;
  35. }
  36. *OldAskPageDoPost = \&DoPost;
  37. *DoPost = \&NewAskPageDoPost;
  38. sub NewAskPageDoPost {
  39. my $id = FreeToNormal(shift);
  40. if ($id eq $AskPage and not GetParam('text', undef)) { # comment, not a regular edit
  41. my $currentQuestion = IncrementInFile("$DataDir/curquestion");
  42. $currentQuestion =~ s/[\s\n]//g;
  43. return OldAskPageDoPost($QuestionPage . $currentQuestion, @_); # hack page name
  44. }
  45. OldAskPageDoPost($id, @_); # keep original functionality for regular edits
  46. }
  47. *OldAskPageGetCommentForm = \&GetCommentForm;
  48. *GetCommentForm = \&NewAskPageGetCommentForm;
  49. @MyFooters = map { $_ == \&OldAskPageGetCommentForm ? \&NewAskPageGetCommentForm : $_ } @MyFooters;
  50. sub NewAskPageGetCommentForm {
  51. my ($id) = @_;
  52. $Translate{'Add your comment here:'} = $NewQuestion if $id eq $AskPage;
  53. OldAskPageGetCommentForm(@_);
  54. }
  55. *OldAskPageJournalSort = \&JournalSort;
  56. *JournalSort = \&NewAskPageJournalSort;
  57. sub NewAskPageJournalSort {
  58. return OldAskPageJournalSort() unless $a =~ m/^$QuestionPage\d+$/ and $b =~ m/^$QuestionPage\d+$/;
  59. ($b =~ m/$QuestionPage(\d+)/)[0] <=> ($a =~ m/$QuestionPage(\d+)/)[0];
  60. }