blib/lib/Galileo.pm

filter: Statementstmt 32.3% 44 / 136 Branchbran 9.0% 4 / 44 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 12 / 24 Podpod 50.0% 1 / 2 totaltotal 28.1% 63 / 224 CC 28 SCAR 34.9
CC
28
Cov
26%
load_config
110.8
__ANON__
20.0
__ANON__
12.0
CRAP 32.9
« blib/lib/Galileo/Command/setup.pm ↑ Summary blib/lib/Galileo/Admin.pm »
1 package Galileo;
2 2 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '0.041';
5 2 $VERSION = eval $VERSION;
6  
7 2 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Functions qw'rel2abs catdir';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has db => sub {
16 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
17 0 ▶ my $schema_class = $self->config->{db_schema} or die "Unknown DB Schema Class";
Branch: 1 branch
BranchTrueFalseTotal
my $schema_class = $self->config->{"db_schema"} or die "Unknown DB Schema Class" 0 0 0
18 0 ▶ eval "require $schema_class" or die "Could not load Schema Class ($schema_class). $@\n";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $schema_class" or die "Could not load Schema Class ($schema_class). $@\n" 0 0 0
19  
20 my $schema = $schema_class->connect(
21 0 @{ $self->config }{ qw/db_dsn db_username db_password db_options/ }
22 0 ▶ ) or die "Could not connect to $schema_class using DSN " . $self->config->{db_dsn};
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $schema_class->connect(@{$self->config;}{"db_dsn", "db_username", "db_password", "db_options"}) or die "Could not connect to $schema_class using DSN " . $self->config->{"db_dsn"} 0 0 0
23  
24 0 return $schema;
25 2 };
26  
27 has home => sub {
28 2 ▶ my $path = $ENV{GALILEO_HOME} || getcwd;
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition66%2/3
$ENV{'GALILEO_HOME'} || getcwd()
l!l&&r!l&&!r
110
MC/DC0%0/2
$ENV{'GALILEO_HOME'} || getcwd()
$ENV{'GALILEO_HOME'} getcwd()
Truth table66%2/3
A || B
A= $ENV{'GALILEO_HOME'}B= getcwd()
ABresult
1X1
011
000
29 2 return Mojo::Home->new(File::Spec->rel2abs($path));
30 2 };
31  
32 has config_file => sub {
33 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
34 2 ▶ return $ENV{GALILEO_CONFIG} if $ENV{GALILEO_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GALILEO_CONFIG"} 0 2 2
35 2 return $self->home->rel_file('galileo.conf');
36 2 };
37  
38 sub load_config {
39 2 ▶ my $app = shift;
Subroutine load_config: called CC=13 SCAR=47.1
Pod undocumented
40  
41 2 $app->plugin( Config => {
42 file => $app->config_file,
43 default => {
44 db_schema => 'Galileo::DB::Schema',
45 db_dsn => 'dbi:SQLite:dbname=' . $app->home->rel_file('galileo.db'),
46 db_username => undef,
47 db_password => undef,
48 db_options => { sqlite_unicode => 1 },
49 extra_css => [ '/themes/standard.css' ],
50 extra_js => [],
51 extra_static_paths => ['static'],
52 sanitize => 1,
53 pagedown_extra_options => {
54 highlighter => 'highlight',
55 table_class => 'table table-striped',
56 extensions => [qw/
57 tables
58 fenced_code_gfm
59 def_list
60 attr_list
61 footnotes
62 smartypants
63 strikethrough
64 newlines
65 /],
66 },
67 secrets => [],
68 upload_path => 'uploads',
69 },
70 });
71  
72 # handle deprecated db_connect
73 2 ▶ if ( my $db_connect = delete $app->config->{db_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db_connect = delete $app->config->{"db_connect"}) 0 2 2
74 0 warn "### Configuration key db_connect is deprecated ###\n";
75 0 ▶ if ( ref $db_connect ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $db_connect) { } 0 0 0
76 0 @{ $app->config }{ qw/db_dsn db_username db_password db_options/ } = @$db_connect;
77 } else {
78 0 $app->config->{db_dsn} = $db_connect;
79 }
80 }
81  
82 # handle deprecated files key
83 2 ▶ if (my $files = $app->config->{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $files = $app->config->{"files"}) 0 2 2
84 0 warn "### 'files' config key is deprecated, use extra_static_paths ###\n";
85 0 ▶ push @{ $app->config->{extra_static_paths} }, (ref $files ? @$files : $files);
Branch: 1 branch
BranchTrueFalseTotal
ref $files ? : 0 0 0
86 }
87  
88 # add the files directories to array of static content folders
89 # TODO don't repeat
90 2 foreach my $dir ( @{$app->config->{extra_static_paths}} ) {
91 # convert relative paths to relative one (to home dir)
92 2 $dir = $app->_to_abs($dir);
93 0 ▶ push @{ $app->static->paths }, $dir if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 0 0 0
94 }
95  
96 # normalize and make helper for upload directory
97 0 my $upload;
98 0 ▶ if ( my $dir = $app->config->{upload_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $app->config->{"upload_path"}) 0 0 0
99 0 $upload = $app->_to_abs($dir);
100 }
101 0 ▶ if ( -d $upload ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $upload) { } 0 0 0
102 0 push @{ $app->static->paths }, $upload;
103 0 $app->config->{upload_path} = $upload;
104 0 ▶ $app->helper( upload_path => sub { $upload } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 } else {
106 0 ▶ $app->helper( upload_path => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 }
108  
109  
110 0 ▶ if ( my $secrets = $app->config->{secrets} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $secrets = $app->config->{'secrets'}) { } 0 0 0
elsif (my $secret = $app->config->{'secret'}) { } 0 0 0
111 0 ▶ $app->secrets($secrets) if @$secrets;
Branch: 1 branch
BranchTrueFalseTotal
if @$secrets 0 0 0
112 } elsif ( my $secret = $app->config->{secret} ) {
113 0 warn "### 'secret' config key is deprecated, use 'secrets' instead ###\n";
114 0 ▶ $app->secrets([$secret]) if $secret;
Branch: 1 branch
BranchTrueFalseTotal
if $secret 0 0 0
115 }
116 }
117  
118 sub _to_abs {
119 2 ▶ my ($self, $dir) = @_;
Subroutine _to_abs: called CC=2 SCAR=7.6
120 2 ▶ unless ( File::Spec->file_name_is_absolute( $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($dir)) 2 0 2
121 2 $dir = $self->home->rel_dir( $dir );
122 }
123 0 return $dir;
124 }
125  
126 sub startup {
127 2 ▶ my $app = shift;
Subroutine startup: called CC=3 SCAR=24.3
Pod documented
128  
129 2 $app->load_config;
130  
131 {
132 # use content from directories under lib/Galileo/files or using File::ShareDir
133 0 my $lib_base = catdir(dirname(rel2abs(__FILE__)), 'Galileo', 'files');
134  
135 0 my $public = catdir($lib_base, 'public');
136 0 ▶ $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('Galileo'), 'public');
Branch: 1 branch
BranchTrueFalseTotal
-d $public ? : 0 0 0
137  
138 0 my $templates = catdir($lib_base, 'templates');
139 0 ▶ $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('Galileo'), 'templates');
Branch: 1 branch
BranchTrueFalseTotal
-d $templates ? : 0 0 0
140 }
141  
142 # use commands from Galileo::Command namespace
143 0 push @{$app->commands->namespaces}, 'Galileo::Command';
144  
145 ## Helpers ##
146  
147 0 ▶ $app->helper( schema => sub { shift->app->db } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
148  
149 0 ▶ $app->helper( 'home_page' => sub{ '/page/home' } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
150  
151 $app->helper( 'auth_fail' => sub {
152 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
153 0 ▶ my $message = shift || "Not Authorized";
Condition0%0/2
shift() || 'Not Authorized'
l!l
00
MC/DC0%0/1
shift() || 'Not Authorized'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
154 0 $self->humane_flash( $message );
155 0 $self->redirect_to( $self->home_page );
156 0 return 0;
157 0 });
158  
159 $app->helper( 'get_user' => sub {
160 0 ▶ my ($self, $name) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
161 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
162 0 $name = $self->session->{username};
163 }
164 0 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
165 0 return $self->schema->resultset('User')->single({name => $name});
166 0 });
167  
168 $app->helper( 'is_author' => sub {
169 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 my $user = $self->get_user(@_);
171 0 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
172 0 return $user->is_author;
173 0 });
174 $app->helper( 'is_admin' => sub {
175 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
176 0 my $user = $self->get_user(@_);
177 0 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
178 0 return $user->is_admin;
179 0 });
180  
181 0 $app->plugin('Memorize');
182 0 $app->plugin('Galileo::Plugin::Modal');
183 0 $app->plugin('Humane', auto => 0);
184  
185 ## Routing ##
186  
187 0 my $r = $app->routes;
188  
189 0 ▶ $r->any( '/' => sub { my $self = shift; $self->redirect_to( $self->home_page ) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 $r->any( '/page/:name' )->to('page#show')->name('show_page');
191 0 $r->post( '/login' )->to('user#login');
192 0 $r->any( '/logout' )->to('user#logout');
193  
194 my $if_author = $r->under( sub {
195 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
196 0 ▶ return $self->is_author || $self->auth_fail;
Condition0%0/3
$self->is_author || $self->auth_fail
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_author || $self->auth_fail
$self->is_author $self->auth_fail
Truth table0%0/3
A || B
A= $self->is_authorB= $self->auth_fail
ABresult
1X1
011
000
197 0 });
198  
199 0 $if_author->any( '/admin/menu' )->to('menu#edit');
200 0 $if_author->any( '/edit/:name' )->to('page#edit')->name('edit_page');
201 0 $if_author->websocket( '/store/page' )->to('page#store');
202 0 $if_author->websocket( '/store/menu' )->to('menu#store');
203 0 $if_author->websocket( '/files/list' )->to('file#list');
204  
205 my $if_admin = $r->under( sub {
206 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
207 0 ▶ return $self->is_admin || $self->auth_fail;
Condition0%0/3
$self->is_admin || $self->auth_fail
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_admin || $self->auth_fail
$self->is_admin $self->auth_fail
Truth table0%0/3
A || B
A= $self->is_adminB= $self->auth_fail
ABresult
1X1
011
000
208 0 });
209  
210 0 $if_admin->any( '/admin/users' )->to('admin#users');
211 0 $if_admin->any( '/admin/pages' )->to('admin#pages');
212 0 $if_admin->any( '/admin/user/:name' )->to('admin#user');
213 0 $if_admin->websocket( '/store/user' )->to('admin#store_user');
214 0 $if_admin->websocket( '/remove/page' )->to('admin#remove_page');
215  
216 }
217  
218 2 1;
219  
220 __END__
221  
222 =head1 NAME
223  
224 Galileo - A simple modern CMS built on Mojolicious
225  
226 =head1 SYNOPSIS
227  
228 $ galileo setup
229 $ galileo daemon
230  
231 =head1 DESCRIPTION
232  
233 L<Galileo> is a Perl CMS with some modern features. It uses client-side markdown rendering and websockets for saving page data without reloading. L<Galileo> relies on many other great open-source projects, see more in the L</"TECHNOLOGIES USED"> section.
234  
235 This release is very young, don't expect anything not to break, for now. Bug reports very welcome.
236  
237 =head1 INSTALLATION
238  
239 L<Galileo> uses well-tested and widely-used CPAN modules, so installation should be as simple as
240  
241 $ cpanm Galileo
242  
243 when using L<App::cpanminus>. Of course you can use your favorite CPAN client or install manually by cloning the L</"SOURCE REPOSITORY">.
244  
245 =head1 SETUP
246  
247 =head2 Environment
248  
249 Although most of L<Galileo> is controlled by a configuration file, a few properties must be set before that file can be read. These properties are controlled by the following environment variables.
250  
251 =over
252  
253 =item C<GALILEO_HOME>
254  
255 This is the directory where L<Galileo> expects additional files. These include the configuration file and log files. The default value is the current working directory (C<cwd>).
256  
257 =item C<GALILEO_CONFIG>
258  
259 This is the full path to a configuration file. The default is a file named F<galileo.conf> in the C<GALILEO_HOME> path, however this file need not actually exist, defaults may be used instead. This file need not be written by hand, it can be generated as a result of running the C<galileo config> command.
260  
261 =back
262  
263 =head2 The F<galileo> command line application
264  
265 L<Galileo> installs a command line application, C<galileo>. It inherits from the L<mojo> command and so provides all those commands, but it provides extra functions specifically for use with Galileo.
266  
267 =head3 setup
268  
269 $ galileo setup
270  
271 This command starts the app in setup mode. It can write a configuration file, and setup or upgrade the database.
272  
273 This step is required after both installation and upgrading Galileo, because the database page will deploy or upgrade the database used by your Galileo site. It will use the default DBI settings (SQLite) or whatever is setup in the C<GALILEO_CONFIG> configuration file.
274  
275 Warning: As usual, proper care should be taken when upgrading a database. This mechanism is rather new and while it should be safe, the author makes no promises about anything yet! Backup files and database before upgrading!
276  
277 Although L<Galileo> does not need to be configured, it is recommended to do so to set your application's secret. The secret can be any string, however stronger is better. You do not need to memorize it or even remember it. This secret protects the cookies employed by Galileo from being tampered with on the client side.
278  
279 Notes:
280  
281 =over
282  
283 =item *
284  
285 The database deployment tools may emit debugging information unexpectedly to your terminal, especially messages about "overwriting" and some internal "peek" information. These message are harmless, but as yet cannot be suppressed.
286  
287 =item *
288  
289 Upgrading database schemas from before Galileo version 0.012 (when schema versioning was introduced) is no longer supported as of version 0.037. The process wasn't testable anyway.
290  
291 =back
292  
293 =head3 dump
294  
295 $ galileo dump
296 $ galileo dump --directory pages -t
297 $ galileo dump --directory pages -t --encoding utf-8
298  
299 This tool dumps all the pages in your galileo site as markdown files. The directory for exporting to may be specifed with the C<--directory> or C<-d> flag, by default it exports to the current working directory.
300  
301 The title of the page is by default includes as an HTML comment. To include the title as an C<< <h1> >> level directive pass C<--title> or C<-t> without an option. Any other option given to C<--title> will be used as an C<sprintf> format for rendering the title (at the top of the article).
302  
303 The document will be encoded as UTF-8 by default, if other encoding of the content is desired, use C<--encoding> or C<-e> flag. Available value of the C<--encoding> is same as L<Encode> module of Perl.
304  
305 =head1 RUNNING THE APPLICATION
306  
307 $ galileo daemon
308  
309 After the database has been setup, you can run C<galileo daemon> to start the server.
310  
311 You may also use L<morbo> (Mojolicious' development server) or L<hypnotoad> (Mojolicious' production server). You may even use any other server that Mojolicious supports, however for full functionality it must support websockets. When doing so you will need to know the full path to the C<galileo> application. A useful recipe might be
312  
313 $ hypnotoad `which galileo`
314  
315 where you may replace C<hypnotoad> with your server of choice.
316  
317 =head2 Logging
318  
319 Logging in L<Galileo> is the same as in L<Mojolicious|Mojolicious::Lite/Logging>. Messages will be printed to C<STDERR> unless a directory named F<log> exists in the C<GALILEO_HOME> path, in which case messages will be logged to a file in that directory.
320  
321 =head2 Extra Static Paths
322  
323 By default, if Galileo detects a folder named F<static> inside the C<GALILEO_HOME> path, that path is added to the list of folders for serving static files. The name of this folder may be changed in the configuration file via the key C<extra_static_paths>, which expects an array reference of strings representing paths. If any path is relative it will be relative to C<GALILEO_HOME>.
324  
325 =head2 Upload Path
326  
327 By default, if Galileo detects a folder named F<uploads> inside the C<GALILEO_HOME> path, that path is used for uploads, specifically user-uploaded images. This path is added to the static files. The name of this folder may be changed in the configuration file via the key C<upload_path>, which expects a string representing the path. If the path is relative it will be relative to C<GALILEO_HOME>.
328  
329 NOTE: as for this writing, this option doesn't do anything but append to the static files paths. Eventually it will be where uploads go, but NOT YET!
330  
331 =head1 CUSTOMIZING
332  
333 The L</config> keys C<extra_css> and C<extra_js> take array references pointing to CSS or Javascript files (respectively) within a L<static directory|/"Static files folder">. As an example, the default C<extra_css> key contains the path to a simple theme css file which adds a gray background and border to the main container.
334  
335 As yet there are no widgets/plugins as such, however a clever bit of javascript might be able to load something.
336  
337 =head1 TECHNOLOGIES USED
338  
339 =over
340  
341 =item *
342  
343 L<Mojolicious|http://mojolicio.us> - a next generation web framework for the Perl programming language
344  
345 =item *
346  
347 L<DBIx::Class|http://www.dbix-class.org/> - an extensible and flexible Object/Relational Mapper written in Perl
348  
349 =item *
350  
351 L<PageDown|http://code.google.com/p/pagedown/> (Markdown engine) - the version of Attacklab's Showdown and WMD as used on Stack Overflow and the other Stack Exchange sites
352  
353 =item *
354  
355 L<PageDown Extra|https://github.com/jmcmanus/pagedown-extra> - Markdown Extra Plugins for Pagedown
356  
357 =item *
358  
359 L<Bootstrap|http://twitter.github.com/bootstrap> - the beautiful CSS/JS library from Twitter
360  
361 =item *
362  
363 L<jQuery|http://jquery.com/> - because everything uses jQuery
364  
365 =item *
366  
367 L<html5sortable|https://github.com/voidberg/html5sortable> - Lightweight jQuery plugin to create sortable lists and grids using native HTML5 drag and drop API
368  
369 =item *
370  
371 L<HumaneJS|http://wavded.github.com/humane-js/> - A simple, modern, browser notification system
372  
373 =back
374  
375 =head1 SEE ALSO
376  
377 =over
378  
379 =item *
380  
381 L<Contenticious> - File-based Markdown website application
382  
383 =back
384  
385 =head1 SOURCE REPOSITORY
386  
387 L<http://github.com/jberger/Galileo>
388  
389 =begin html
390  
391 <p>
392 <img src="https://travis-ci.org/jberger/Galileo.png?branch=master" alt="Travis CI build status">
393 </p>
394  
395 =end html
396  
397 =head1 AUTHOR
398  
399 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
400  
401 =head1 COPYRIGHT AND LICENSE
402  
403 Copyright (C) 2012-2014 by Joel Berger
404  
405 This library is free software; you can redistribute it and/or modify
406 it under the same terms as Perl itself.
407  
408 =cut
409  
410  
411  
« blib/lib/Galileo/Command/setup.pm ↑ Summary blib/lib/Galileo/Admin.pm »