creationdate.pl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
  2. # Copyright (C) 2016 Alex Schroeder <alex@gnu.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. =head1 Creation Date Extension
  16. This module stores additional information about a page when it is first created:
  17. =over
  18. =item C<created> is the date the page is first saved
  19. =item C<originalAuthor> is the username that first created a page
  20. =back
  21. =cut
  22. use strict;
  23. use v5.10;
  24. AddModuleDescription('creationdate.pl', 'CreationDate Module');
  25. our (%Page, $Now, @MyAdminCode, %Action, $q, $FS, $RcOldFile, $RcFile);
  26. *CreationDateOldOpenPage = \&OpenPage;
  27. *OpenPage = \&CreationDateOpenPage;
  28. sub CreationDateOpenPage{
  29. CreationDateOldOpenPage(@_);
  30. $Page{created} = $Now unless $Page{created} or $Page{revision};
  31. $Page{originalAuthor} = GetParam('username','') unless $Page{originalAuthor}
  32. or $Page{revision};
  33. }
  34. # Allow administrators to add the 'created' item to page files, based on rc log
  35. # files.
  36. push(@MyAdminCode, \&CreationDateMenu);
  37. sub CreationDateMenu {
  38. my ($id, $menuref, $restref) = @_;
  39. push(@$menuref,
  40. ScriptLink('action=add-creation-date',
  41. T('Add creation date to page files'),
  42. 'creationdate')) if UserIsAdmin();
  43. }
  44. $Action{'add-creation-date'} = \&AddCreationDate;
  45. sub AddCreationDate {
  46. print GetHeader('', T('Add creation date to page files'));
  47. print $q->start_div({-class=>'creationdate'});
  48. print '<ul>';
  49. RequestLockOrError();
  50. for my $file ($RcOldFile, $RcFile) {
  51. open(my $F, '<:encoding(UTF-8)', encode_utf8($file)) or next;
  52. while (my $line = <$F>) {
  53. chomp($line);
  54. my ($ts, $id, $minor, $summary, $host, $username, $revision)
  55. = split(/$FS/, $line);
  56. next unless $revision == 1;
  57. print $q->li(NormalToFree($id));
  58. OpenPage($id);
  59. next unless $Page{revision}; # skip if page no longer exists
  60. next if $Page{created} and $Page{originalAuthor};
  61. $Page{created} = $ts unless $Page{created};
  62. $Page{originalAuthor} = $username unless $Page{originalAuthor};
  63. SavePage();
  64. }
  65. }
  66. ReleaseLock();
  67. print '</ul>';
  68. print $q->end_div();
  69. PrintFooter();
  70. }