| 1 | package Kwiki::PageStats; | ||
| 2 | use Kwiki::Plugin -Base; | ||
| 3 | use mixin 'Kwiki::Installer'; | ||
| 4 | |||
| 5 | const class_id => 'page_stats'; | ||
| 6 | const class_title => 'PageStats'; | ||
| 7 | const lock_count => 10; | ||
| 8 | |||
| 9 | field count => 0; | ||
| 10 | field 'mtime'; | ||
| 11 | |||
| 12 | our $VERSION = '0.06'; | ||
| 13 | |||
| 14 | sub storage_directory { | ||
| 15 | $self->plugin_directory; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub register { | ||
| 19 | my $registry = shift; | ||
| 20 | $registry->add(status => 'page_stats', | ||
| 21 | template => 'page_stats.html', | ||
| 22 | show_for => 'display', | ||
| 23 | ); | ||
| 24 | $registry->add(toolbar => 'Page Status', | ||
| 25 | template => 'page_stats_button.html', | ||
| 26 | show_for => 'display' | ||
| 27 | ); | ||
| 28 | $registry->add(action => 'page_stats_list'); | ||
| 29 | |||
| 30 | $registry->add(preference => $self->show_page_stats); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub page_stats_list { | ||
| 34 | my @pages; | ||
| 35 | for my $page ($self->pages->all) { | ||
| 36 | my $io_file = io->catfile($self->plugin_directory, $page->id); | ||
| 37 | if ($io_file->exists) { | ||
| 38 | $page->{hits} = $io_file->all; | ||
| 39 | push @pages, $page; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | @pages = sort {$b->{hits} <=> $a->{hits}} @pages; | ||
| 43 | |||
| 44 | $self->render_screen(pages => \@pages); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub show_page_stats { | ||
| 48 | my $p = $self->new_preference('show_page_stats'); | ||
| 49 | $p->query('Show hits in status bar?'); | ||
| 50 | $p->type('boolean'); | ||
| 51 | $p->default(1); | ||
| 52 | return $p; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub increment_page_count { | ||
| 56 | my $file = io($self->data_file)->file; | ||
| 57 | my $previous = 0; | ||
| 58 | my $mtime; | ||
| 59 | if ($file->exists) { | ||
| 60 | $previous = $file->all; | ||
| 61 | $mtime = $file->mtime; | ||
| 62 | } | ||
| 63 | $file->close; | ||
| 64 | $previous++; | ||
| 65 | $self->count($previous); | ||
| 66 | $self->mtime($mtime) if $mtime; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub write_page_count { | ||
| 70 | my $file = io($self->data_file)->file; | ||
| 71 | $file->print($self->count); | ||
| 72 | $file->close; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub touch_ctime { | ||
| 76 | my $file = io($self->ctime_file); | ||
| 77 | $file->touch unless $file->exists; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub file_ctime { | ||
| 81 | my $time = io($self->ctime_file)->file->ctime; | ||
| 82 | return scalar gmtime($time); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub page_stats { | ||
| 86 | return unless $self->preferences->show_page_stats->value; | ||
| 87 | eval {$self->lock;}; | ||
| 88 | return 'X' if $@; | ||
| 89 | $self->increment_page_count; | ||
| 90 | $self->write_page_count; | ||
| 91 | $self->touch_ctime; | ||
| 92 | $self->unlock; | ||
| 93 | if ($self->hub->have_plugin('time_zone')) { | ||
| 94 | return { | ||
| 95 | count => $self->count, | ||
| 96 | ctime => $self->hub->time_zone->format( | ||
| 97 | io($self->ctime_file)->file->ctime), | ||
| 98 | $self->mtime ? | ||
| 99 | (mtime => $self->hub->time_zone->format($self->mtime)) : () | ||
| 100 | }; | ||
| 101 | } else { | ||
| 102 | return { | ||
| 103 | count => $self->count, | ||
| 104 | ctime => $self->file_ctime, | ||
| 105 | $self->mtime ? (mtime => scalar gmtime($self->mtime)) : () | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | sub ctime_file { | ||
| 111 | $self->data_file . '.time'; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub data_file { | ||
| 115 | my $id = $self->hub->pages->current->id; | ||
| 116 | $self->storage_directory . '/' . $id; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub lock_directory { | ||
| 120 | $self->data_file . '.lck'; | ||
| 121 | } | ||
| 122 | |||
| 123 | # taken from PurpleWiki and Kwiki-Purple | ||
| 124 | sub lock { | ||
| 125 | my $tries = 0; | ||
| 126 | while (!mkdir($self->lock_directory, 0555)) { | ||
| 127 | die "unable to create page counting lock directory" | ||
| 128 | if ($! != 17); | ||
| 129 | $tries++; | ||
| 130 | die "timeout attempting to lock page count" | ||
| 131 | if ($tries > $self->lock_count); | ||
| 132 | sleep 1; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub unlock { | ||
| 137 | rmdir($self->lock_directory) or | ||
| 138 | die "unable to remove page count locking directory"; | ||
| 139 | } | ||
| 140 | |||
| 141 | __DATA__ |