blib/lib/Slovo.pm

filter: Statementstmt 93.4% 185 / 198 Branchbran 66.0% 33 / 50 Conditioncond 50.9% 27 / 53 MC/DCmcdc 17.8% 5 / 28 Subroutinesub 100% 34 / 34 Podpod 100% 3 / 3 totaltotal 78.4% 287 / 366 CC 54 SCAR 20.2
CC
54
Cov
81%
_load_config
17.7
home
14.1
_around_dispatch
8.5
CRAP 7.5
« blib/lib/Slovo/Plugin/CGI.pm ↑ Summary blib/lib/Test/Mojo/Role/Slovo.pm »
1 package Slovo;
2 15 ▶use feature ':5.26';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
3 15 ▶use Mojo::Base 'Mojolicious', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 15 ▶use Mojo::Util 'class_to_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6 15 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
7 15 ▶use Mojo::Collection 'c';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 15 ▶use Slovo::Controller::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9 15 ▶use Slovo::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
10 15 ▶use Slovo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
11 15 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 15 our $AUTHORITY = 'cpan:BEROV';
14 15 our $VERSION = '2023.03.22';
15 15 our $CODENAME = 'U+2C15 GLAGOLITIC CAPITAL LETTER TVRIDO (Ⱅ)';
16 15 my $CLASS = __PACKAGE__;
17  
18 1 ▶has resources => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 path($INC{class_to_path $CLASS})->sibling("$CLASS/resources")->realpath;
20 15 };
21 13 ▶has validator => sub { Slovo::Validator->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 # We prefer $MOJO_HOME to be the folder where the folders bin or script
24 # reside.
25 157 ▶sub home {
Subroutine home: called CC=13 SCAR=26.5
Pod documented
26 157 my $class_or_self = shift;
27 153 ▶ if (ref($class_or_self) && $class_or_self->{home}) { return $class_or_self->{home}; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self and $class_or_self->{"home"}) 138 15 153
Condition100%3/3
ref $class_or_self and $class_or_self->{"home"}
!ll&&!rl&&r
114138
MC/DC100%2/2
ref $class_or_self and $class_or_self->{"home"}
ref $class_or_self $class_or_self->{"home"}
Truth table100%3/3
A and B
A= ref $class_or_selfB= $class_or_self->{"home"}
ABresult
0X0
100
111
28 15 ▶ if ($ENV{MOJO_HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOJO_HOME"}) 1 14 15
29 return
30 ref($class_or_self)
31 ? ($class_or_self->{home} = Mojo::Home->new($ENV{MOJO_HOME}))
32 1 ▶ : Mojo::Home->new($ENV{MOJO_HOME});
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 1 0 1
33 }
34 14 my $r = Mojo::Home->new($INC{class_to_path $CLASS})->dirname->to_abs;
35 14 my $m = lc $CLASS; # moniker *should* have the same name
36 14 ▶ while (($r = $r->dirname) && @{$r->to_array} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r = $r->dirname and @{$r->to_array;} > 2) 27 13 40
Condition66%2/3
$r = $r->dirname and @{$r->to_array;} > 2
!ll&&!rl&&r
01327
MC/DC50%1/2
$r = $r->dirname and @{$r->to_array;} > 2
$r = $r->dirname @{$r->to_array;} > 2
Truth table66%2/3
A and B
A= $r = $r->dirnameB= @{$r->to_array;} > 2
ABresult
0X0
100
111
37 27 ▶ if ( -x $r->child("script/$m")
Branch: 1 branch
BranchTrueFalseTotal
if (-x $r->child("script/$m") or -x $r->child("bin/$m") or -x $r->child("script/$m.pl") or -x $r->child("bin/$m.pl")) 1 26 27
Condition66%6/9
-x $r->child("script/$m") or -x $r->child("bin/$m") or -x $r->child("script/$m.pl") or -x $r->child("bin/$m.pl")
l!l&&r!l&&!r
1026
-x $r->child("script/$m") or -x $r->child("bin/$m") or -x $r->child("script/$m.pl")
l!l&&r!l&&!r
1026
-x $r->child("script/$m") or -x $r->child("bin/$m")
l!l&&r!l&&!r
1026
MC/DC25%1/4
-x $r->child("script/$m") or -x $r->child("bin/$m") or -x $r->child("script/$m.pl") or -x $r->child("bin/$m.pl")
-x $r->child("script/$m") -x $r->child("bin/$m") -x $r->child("script/$m.pl") -x $r->child("bin/$m.pl")
Truth table40%2/5
A or B or C or D
A= -x $r->child("script/$m")B= -x $r->child("bin/$m")C= -x $r->child("script/$m.pl")D= -x $r->child("bin/$m.pl")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
38 || -x $r->child("bin/$m")
39 || -x $r->child("script/$m.pl")
40 || -x $r->child("bin/$m.pl"))
41 {
42 0 ▶ if (ref($class_or_self)) { return $class_or_self->{home} = $r; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_self) { } 0 1 1
43 else {
44 1 return $r;
45 }
46 }
47 ;
48 }
49 return
50 ref($class_or_self)
51 13 ▶ ? ($class_or_self->{home} = $class_or_self->SUPER::home)
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 13 0 13
52 : $class_or_self->SUPER::home;
53 }
54  
55 # Writes to $home/log/slovo.log if $home/log/ exists and is writable.
56 1 ▶has log => sub {
Subroutine __ANON__: called CC=5 SCAR=20.3
57 1 my $self = shift;
58  
59 1 my $mode = $self->mode;
60 1 my $log = Mojo::Log->new;
61  
62 1 my $home = $self->home;
63 1 ▶ if (-d $home->child('log') && -w _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $home->child("log") and -w _) 0 1 1
Condition33%1/3
-d $home->child("log") and -w _
!ll&&!rl&&r
100
MC/DC0%0/2
-d $home->child("log") and -w _
-d $home->child("log") -w _
Truth table33%1/3
A and B
A= -d $home->child("log")B= -w _
ABresult
0X0
100
111
64 0 $log->path($home->child('log', $self->moniker . ".log"));
65 }
66  
67 # Reduced log output outside of development mode
68 1 ▶ return $log->level($ENV{MOJO_LOG_LEVEL}) if $ENV{MOJO_LOG_LEVEL};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJO_LOG_LEVEL"} 1 0 1
69 0 ▶ return $mode eq 'development' ? $log : $log->level('info');
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'development' ? : 0 0 0
70 15 };
71  
72 # This method will run once at server start
73 27 ▶sub startup ($app) {
Subroutine startup: called CC=1 SCAR=0.0
Pod documented
74 27 $app->log->debug("Starting $CLASS $VERSION|$CODENAME");
75 27 $app->controller_class('Slovo::Controller');
76 27 $app->commands->namespaces(
77 ['Slovo::Command::Author', 'Slovo::Command', 'Mojolicious::Command']);
78 ## no critic qw(Subroutines::ProtectPrivateSubs)
79 27 $app->hook(around_action => \&_around_action);
80 27 $app->hook(around_dispatch => \&_around_dispatch);
81 14 $app->hook(before_dispatch => \&_before_dispatch);
82 27 $app->_set_routes_attrs->_load_config->_load_pugins->_default_paths->_add_media_types();
83 14 my $cache = Slovo::Cache->new();
84 14 $app->renderer->cache($cache);
85 14 $app->routes->cache($cache);
86 14 $app->defaults(
87  
88 # layout => 'default'
89 boxes => $app->openapi_spec('/parameters/box/enum'),
90 data_formats => $app->openapi_spec('/parameters/data_format/enum'),
91 data_types => $app->openapi_spec('/parameters/data_type/enum'),
92 lang => 'bg-bg',
93 languages => $app->languages, # /parameters/language/enum
94 page_types => $app->openapi_spec('/parameters/page_type/enum'),
95 permissions => $app->openapi_spec('/parameters/permissions/enum'),
96 stranici_columns =>
97 $app->openapi_spec('/paths/~1stranici/get/parameters/4/items/enum'),
98 );
99 14 return $app;
100 }
101  
102 88 ▶sub _before_dispatch ($c) {
Subroutine _before_dispatch: called CC=2 SCAR=6.9
103 88 state $guest = $c->users->find_by_login_name('guest');
104 6 ▶ state $auth_config = c(@{$c->config('load_plugins')})->first(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
105 17 ▶ ref $_ eq 'HASH' and exists $_->{Authentication};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "HASH" and exists $_->{"Authentication"} 30 0 30
106 88 });
107 88 state $session_key = $auth_config->{Authentication}{session_key};
108 88 state $current_user_fn = $auth_config->{Authentication}{current_user_fn};
109 88 ▶ unless ($c->session->{$session_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->session->{$session_key}) 88 13 101
110  
111 #set the guest user as default to always have a user
112 88 $c->$current_user_fn($guest);
113 }
114 88 return;
115 }
116  
117 # Make some variables available to the templates being rendered, so
118 # these do not need to be set in the actions.
119 88 ▶sub _around_action ($next, $c, $action, $last) {
Subroutine _around_action: called CC=5 SCAR=17.5
120 88 ▶ if ($last && $c->current_route !~ /^api\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last and not $c->current_route =~ /^api\./u) 77 11 88
Condition66%2/3
$last and not $c->current_route =~ /^api\./u
!ll&&!rl&&r
24077
MC/DC50%1/2
$last and not $c->current_route =~ /^api\./u
$last not $c->current_route =~ /^api\./u
Truth table66%2/3
A and not(B)
A= $lastB= not $c->current_route =~ /^api\./u
ABresult
0X0
100
111
121 77 my $stash = $c->stash;
122 77 ▶ $stash->{l} //= $c->language; # current language of the text being edited
Condition33%1/3
$stash->{"l"} //= $c->language
l!l&&r!l&&!r
0770
MC/DC0%0/2
$stash->{"l"} //= $c->language
$stash->{"l"} $c->language
Truth table33%1/3
A //= B
A= $stash->{"l"}B= $c->language
ABresult
1X1
011
000
123 77 ▶ $stash->{user} //= $c->user; # current user
Condition33%1/3
$stash->{"user"} //= $c->user
l!l&&r!l&&!r
0770
MC/DC0%0/2
$stash->{"user"} //= $c->user
$stash->{"user"} $c->user
Truth table33%1/3
A //= B
A= $stash->{"user"}B= $c->user
ABresult
1X1
011
000
124 }
125 88 return $next->();
126 }
127  
128 # This code is executed on every request, so we try to save as much as possible
129 # method calls.
130 88 ▶sub _around_dispatch ($next, $c) {
Subroutine _around_dispatch: called CC=7 SCAR=21.3
131 88 state $app = $c->app;
132 88 state $root = $app->config('domove_root');
133  
134 88 state $s_paths = $app->static->paths;
135 88 state $r_paths = $app->renderer->paths;
136 88 state $cache = $app->renderer->cache;
137 my $dom
138 88 ▶ = eval { $c->domove->find_by_host($c->host_only) }
Condition50%1/2
eval { do { $c->domove->find_by_host($c->host_only) } } || die('No such Host (' . $c->host_only . ')! Looks like a Proxy Server misconfiguration, readonly' . " database file or a missing domain alias in table domove.\n")
l!l
880
MC/DC0%0/1
eval { do { $c->domove->find_by_host($c->host_only) } } || die('No such Host (' . $c->host_only . ')! Looks like a Proxy Server misconfiguration, readonly' . " database file or a missing domain alias in table domove.\n")
eval { do { $c->domove->find_by_host($c->host_only) } }
Truth table50%1/2
A
A= eval { do { $c->domove->find_by_host($c->host_only) } }
Aresult
11
00
139 || die 'No such Host ('
140 . $c->host_only
141 . ')! Looks like a Proxy Server misconfiguration, readonly'
142 . " database file or a missing domain alias in table domove.\n";
143  
144 # Use domain specific public and templates' paths with priority.
145 88 unshift @{$s_paths}, "$root/$dom->{domain}/public";
146 88 ▶ if (my $tpls = $dom->{templates}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tpls = $dom->{'templates'}) { } 88 0 88
147  
148 # absolute path
149 88 ▶ if ($tpls =~ m|^/| && -d $tpls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tpls =~ m[^/]u and -d $tpls) { } 0 88 88
Condition33%1/3
$tpls =~ m[^/]u and -d $tpls
!ll&&!rl&&r
8800
MC/DC0%0/2
$tpls =~ m[^/]u and -d $tpls
$tpls =~ m[^/]u -d $tpls
Truth table33%1/3
A and B
A= $tpls =~ m[^/]uB= -d $tpls
ABresult
0X0
100
111
150 0 unshift @{$r_paths}, $tpls;
151 }
152 else {
153 88 unshift @{$r_paths}, "$root/$dom->{domain}/templates";
154  
155 # try to find the relative path to the theme in the list of paths
156 88 for my $path (@{$r_paths}) {
157 352 ▶ if (-d "$path/$tpls") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$path/$tpls") 0 352 352
158 0 unshift @{$r_paths}, "$path/$tpls";
159 0 last;
160 }
161 }
162 }
163 }
164 else {
165 0 unshift @{$r_paths}, "$root/$dom->{domain}/templates";
166 }
167  
168 # Templates and routes are cached per domain. By the 'key_prefix' trick we
169 # can provide different templates with the same name to the renderer. This is
170 # how a long running application can switch to different themes per domain
171 # and have different templates although looking like having "the same" path.
172 # This is transparent for the renderer.
173 88 $cache->key_prefix($dom->{domain});
174 88 $c->stash(domain => $dom);
175 88 $next->();
176 88 shift @{$s_paths};
177 88 shift @{$r_paths};
178 88 return;
179 }
180  
181 14 ▶sub _load_config ($app) {
Subroutine _load_config: called CC=12 SCAR=28.7
182 14 my $etc = $app->resources->child('etc');
183 14 my $moniker = $app->moniker;
184 14 my $mode = $app->mode;
185 14 my $home = $app->home;
186  
187 # Load configuration from hash returned by "slovo.conf"
188 14 my $file = $etc->child("$moniker.conf");
189 14 my $mode_file = $etc->child("$moniker.$mode.conf");
190 14 my $home_file = $home->child("$moniker.conf");
191 14 my $home_mode_file = $home->child("$moniker.$mode.conf");
192 $ENV{MOJO_CONFIG}
193 14 ▶ //= (-f $home_mode_file && $home_mode_file)
Branch: 1 branch
BranchTrueFalseTotal
-f $mode_file ? : 0 12 12
Condition40%6/15
$ENV{"MOJO_CONFIG"} //= -f $home_mode_file && $home_mode_file || -f $home_file && $home_file || (-f $mode_file ? $mode_file : $file)
l!l&&r!l&&!r
2120
-f $home_mode_file && $home_mode_file || -f $home_file && $home_file || (-f $mode_file ? $mode_file : $file)
l!l&&r!l&&!r
0120
-f $home_mode_file and $home_mode_file or -f $home_file and $home_file
l!l&&r!l&&!r
0012
-f $home_mode_file and $home_mode_file
!ll&&!rl&&r
1200
-f $home_file and $home_file
!ll&&!rl&&r
1200
MC/DC0%0/6
$ENV{"MOJO_CONFIG"} //= -f $home_mode_file && $home_mode_file || -f $home_file && $home_file || (-f $mode_file ? $mode_file : $file)
$ENV{"MOJO_CONFIG"} -f $home_mode_file $home_mode_file -f $home_file $home_file (-f $mode_file ? $mode_file : $file)
Truth table0%0/12
A //= B and C or D and E || F
A= $ENV{"MOJO_CONFIG"}B= -f $home_mode_fileC= $home_mode_fileD= -f $home_fileE= $home_fileF= (-f $mode_file ? $mode_file : $file)
ABCDEFresult
1XXXXX1
011XXX1
00X11X1
01011X1
00X0X11
00X1011
0100X11
0101011
00X0X00
00X1000
0100X00
0101000
194 || (-f $home_file && $home_file)
195 || (-f $mode_file ? $mode_file : $file);
196  
197 14 my $config = $app->plugin('Config');
198 14 ▶ for my $class (@{$config->{load_classes} // []}) {
Condition50%1/2
$config->{"load_classes"} // []
l!l
140
MC/DC0%0/1
$config->{"load_classes"} // []
$config->{"load_classes"}
Truth table50%1/2
A
A= $config->{"load_classes"}
Aresult
11
00
199 0 $app->load_class($class);
200 }
201 14 $app->secrets($config->{secrets});
202  
203 # Enable response compression
204 14 ▶ if ($config->{response_compression}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"response_compression"}) 0 14 14
205 0 $app->renderer->compress(1);
206 }
207  
208 14 ▶ for my $setting (@{$config->{sessions} // []}) {
Condition50%1/2
$config->{"sessions"} // []
l!l
140
MC/DC0%0/1
$config->{"sessions"} // []
$config->{"sessions"}
Truth table50%1/2
A
A= $config->{"sessions"}
Aresult
11
00
209 28 my ($a, $v) = (keys %$setting, values %$setting);
210 28 $app->sessions->$a($v);
211 }
212  
213 14 return $app;
214 }
215  
216 14 ▶sub _load_pugins ($app) {
Subroutine _load_pugins: called CC=8 SCAR=20.8
217  
218 # Namespaces to load plugins from
219 # See /perldoc/Mojolicious#plugins
220 # See /perldoc/Mojolicious/Plugins#PLUGINS
221 14 $app->plugins->namespaces(['Slovo::Plugin', 'Slovo', 'Mojolicious::Plugin']);
222 14 ▶ my $plugins = $app->config('load_plugins') // [];
Condition50%1/2
$app->config("load_plugins") // []
l!l
140
MC/DC0%0/1
$app->config("load_plugins") // []
$app->config("load_plugins")
Truth table50%1/2
A
A= $app->config("load_plugins")
Aresult
11
00
223 14 foreach my $plugin (@$plugins) {
224 166 ▶ my $name = (ref $plugin ? (keys %$plugin)[0] : $plugin);
Branch: 1 branch
BranchTrueFalseTotal
ref $plugin ? : 124 42 166
225  
226 # $app->log->debug('Loading Plugin ' . $name);
227 # some plugins return $self and we are going to abuse this.
228 166 my $plug;
229 166 ▶ if (ref $plugin eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $plugin eq 'HASH') { } 124 42 166
elsif (not ref $plugin) { } 42 0 42
230 124 my $value = (values %$plugin)[0];
231 124 ▶ $plug = $app->plugin($name => ref $value eq 'CODE' ? $value->() : $value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'CODE' ? : 28 96 124
232 }
233 elsif (!ref($plugin)) {
234 42 $plug = $app->plugin($name);
235 }
236  
237 # Make OpenAPI specification allways available!
238 166 ▶ if ($name eq 'OpenAPI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "OpenAPI") 14 152 166
239 286 $app->helper(
240 286 ▶ openapi_spec => sub ($c_or_app, $path = '/') {
Subroutine __ANON__: called CC=2 SCAR=6.9
241 286 $plug->validator->get($path);
242 14 });
243 }
244 }
245 14 $app->routes->any('/*page_alias')->to('stranici#execute')->name('catch_all');
246  
247 14 return $app;
248 }
249  
250 14 ▶sub _default_paths ($app) {
Subroutine _default_paths: called CC=1 SCAR=0.0
251  
252 # Fallback "public" directory
253 14 push @{$app->static->paths}, $app->resources->child('public')->to_string;
254  
255 # Fallback templates directory
256 # See /perldoc/Mojolicious/Renderer#paths
257 14 push @{$app->renderer->paths}, $app->resources->child('templates')->to_string;
258  
259 # Current heme
260 14 return $app;
261 }
262  
263  
264 # Set Mojolicious::Routes object attributes and types
265 # See Mojolicious::Routes#base_classes
266 # See Mojolicious::Guides::Routing#Placeholder-types
267 14 ▶sub _set_routes_attrs ($app) {
Subroutine _set_routes_attrs: called CC=1 SCAR=0.0
268 14 my $r = $app->routes;
269 14 push @{$r->base_classes}, $app->controller_class;
270 14 $r->namespaces($r->base_classes);
271 14 my $w = qr/[\w\-]+/;
272 14 @{$r->types}{qw(lng str cel fl_token)}
273 = (qr/[A-z]{2}(?:\-[A-z]{2})?/a, $w, $w, qr/[a-f0-9]{40}/);
274 14 return $app;
275 }
276  
277 # Add more media types
278 14 ▶sub _add_media_types ($app) {
Subroutine _add_media_types: called CC=1 SCAR=0.0
279 14 $app->types->type(woff => ['application/font-woff', 'font/woff']);
280 14 $app->types->type(woff2 => ['application/font-woff2', 'font/woff2']);
281 14 return $app;
282 }
283  
284 84 ▶sub load_class ($app, $class) {
Subroutine load_class: called CC=3 SCAR=15.4
Pod documented
285  
286 # state $log = $app->log;
287 # $log->debug("Loading $class");
288 84 ▶ if (my $e = Mojo::Loader::load_class $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = Mojo::Loader::load_class($class)) 0 84 84
289 0 ▶ Carp::croak ref $e ? "Exception: $e" : "$class - Not found!";
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
290 }
291 84 return;
292 }
293  
294 15 1;
295  
296 =encoding utf8
297  
298 =head1 NAME
299  
300 Slovo - Искони бѣ Слово
301  
302 =head1 SYNOPSIS
303  
304 Install Slovo locally with all dependencies in less than two minutes
305  
306 time curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org \
307 -q -n -l ~/opt/slovo Slovo
308  
309 Run slovo for the first time in debug mode
310  
311 morbo ~/opt/slovo/bin/slovo
312  
313 Visit L<http://127.0.0.1:3000>.
314 For help visit L<http://127.0.0.1:3000/perldoc>.
315  
316 =head1 DESCRIPTION
317  
318 L<Slovo> is a simple to install and extensible L<Mojolicious>
319 L<CMS|https://en.wikipedia.org/wiki/Web_content_management_system>
320 with nice core features, listed below.
321  
322 This is a usable release, yet B<full of creeping bugs> and B<half-implemented
323 pieces>! The project is in active development, so expect often breaking changes.
324  
325 =over
326  
327 =item * On the fly generation of static pages under Apache/CGI – perfect for
328 cheap shared hosting and blogging – BETA;
329  
330 =item * Multi-domain support - BETA;
331  
332 =item * Multi-language pages - WIP;
333  
334 =item * Cached published pages and content - DONE;
335  
336 =item * Multi-user support - DONE;
337  
338 =item * User onboarding - WIP;
339  
340 =item * User sign in - DONE;
341  
342 =item * Managing pages, content, domains, users - WIP;
343  
344 =item * Managing groups - BASIC;
345  
346 =item * Multiple groups per user - DONE;
347  
348 =item * Ownership and permissions management per page and it's content - BETA;
349  
350 =item * Automatic 301 and 308 (Moved Permanently) redirects for renamed pages
351 and content - DONE;
352  
353 =item * Embedded fonts for displaying all
354 L<Azbuka|https://en.wikipedia.org/wiki/Cyrillic_script> and
355 L<Glagolitsa|https://en.wikipedia.org/wiki/Glagolitic_script> characters -
356 DONE;
357  
358 =item * OpenAPI 2/3.0 (Swagger) REST API - BASIC;
359  
360 =item * Embedded Trumbowyg - L<A lightweight WYSIWYG editor|https://alex-d.github.io/Trumbowyg/>;
361  
362 =item * Embedded Editor.md - L<The open source embeddable online markdown editor
363 (component), based on CodeMirror & jQuery &
364 Marked|http://editor.md.ipandao.com/>;
365  
366 =item * Example startup scripts for slovo and slovo_minion services
367 for L<systemd|https://freedesktop.org/wiki/Software/systemd/>, L<Apache
368 2.4|https://httpd.apache.org/docs/2.4/> and NGINX vhost configuration files.
369  
370 =item * Inflatable embedded themes support - BETA;
371  
372 =item * and more to come…
373  
374 =back
375  
376 By default Slovo comes with SQLite database, but support for PostgreSQL or
377 MySQL is about to be added when needed. It is just a question of making
378 compatible and/or translating some limited number of SQL queries to the
379 corresponding SQL dialects. Contributors are welcome.
380  
381 The word "slovo" (слово) has one unchanged meaning during the last millennium
382 among all slavic languages. It is actually one language that started splitting
383 apart less than one thousand years ago. The meaning is "word" – the God's word
384 (when used with capital letter). Hence the self-naming of this group of people
385 C<qr/sl(o|a)v(e|a|i)n(i|y|e)/> - people who have been given the God's word or
386 people who can speak. All others were considered "mute", hence the naming
387 (немци)...
388  
389 =head1 INSTALL
390  
391 All you need is a one-liner, it takes less than a minute.
392  
393 $ curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org -n -l ~/opt/slovo Slovo
394  
395 We recommend the use of a L<Perlbrew|http://perlbrew.pl> environment.
396  
397 If you already downloaded it and you have L<cpanm>.
398  
399 $ cpanm -l ~/opt/slovo Slovo-XXXX.XX.XX.tar.gz
400  
401 Or even if you don't have C<cpanm>. Note that you need to install dependencies first.
402 Set C<INSTALL_BASE>, remove old Slovo installation, make, test, install, create
403 data directory for sqlite database and run slovo to see available commands.
404  
405 tar zxf Slovo-XXXX.XX.XX.tar.gz
406 cd Slovo-XXXX.XX.XX
407 INSTALL_BASE=~/opt/slovo && rm -rf $INSTALL_BASE && make distclean; \
408 perl Makefile.PL INSTALL_BASE=$INSTALL_BASE && make && make test && make install \
409 && $INSTALL_BASE/bin/slovo eval 'app->home->child("data")->make_path({mode => 0700});' \
410 && $INSTALL_BASE/bin/slovo
411  
412 Use cpanm to install or update into a custom location as self contained application and
413 run slovo to see how it's going.
414  
415 # From metacpan. org
416 export PREFIX=~/opt/slovo;
417 cpanm -M https://cpan.metacpan.org -n --self-contained -l $PREFIX Slovo \
418 $PREFIX/bin/slovo eval 'app->home->child("data")->make_path({mode => 0700});' \
419 $PREFIX/bin/slovo
420  
421 # From the directory where you unpacked Slovo
422 export PREFIX=~/opt/slovo;
423 cpanm . -n --self-contained -l $PREFIX Slovo
424 $PREFIX/bin/slovo eval 'app->home->child("data")->make_path({mode => 0700});'
425 $PREFIX/bin/slovo
426  
427 Start the development server and open a browser
428  
429 morbo ./script/slovo -l http://*:3000 & sleep 1 exo-open http://localhost:3000
430  
431 =head1 USAGE
432  
433 cd /path/to/installed/slovo
434 # ...and see various options
435 ./bin/slovo
436  
437 =head1 CONFIGURATION, PATHS and UPGRADING
438  
439 L<Slovo> is a L<Mojolicious> application which means that everything
440 applying to Mojolicious applies to it too. Slovo main configuration file is
441 in C<lib/Slovo/resourses/etc/slovo.conf>. You can use your own by setting
442 C<$ENV{MOJO_CONFIG}> or by just copying C<slovo.conf> to $ENV{MOJO_HOME} and
443 modify it as you wish. Routes can be added or removed in C<routes.conf>. See
444 L<Mojolicious::Plugin::RoutesConfig> for details and examples. New plugins can
445 be added per deployment in C<plugins> section in C<slovo.conf>.
446  
447 C<$ENV{MOJO_HOME}> (L<where you installed Slovo|/home>) is automatically
448 detected and used. All paths, used in the application, are expected to be its
449 children. You can add your own templates in C<$ENV{MOJO_HOME}/templates> and
450 they will be loaded and used with priority. You can theme your own instance of
451 Slovo by just copying C<$ENV{MOJO_HOME}/lib/Slovo/resources/templates> to
452 C<$ENV{MOJO_HOME}/templates> and modify them. You can add your own static files
453 to C<$ENV{MOJO_HOME}/public>. You can create custom themes by forking
454 L<Slovo::Themes::Malka> and using it as a starting point.
455  
456 You can have separate static files and templates per domain under
457 C<$ENV{MOJO_HOME}/domove/your.domain/public>,
458 C<$ENV{MOJO_HOME}/domove/your.other.domain/templates>, etc. See
459 C<$ENV{MOJO_HOME}/domove/localhost> for example.
460  
461 You can switch between different themes by just selecting the theme in the
462 form for editing domains.
463  
464 Last but not least, you can add your own classes into
465 C<$ENV{MOJO_HOME}/site/lib> and (why not) replace entirely some Slovo classes
466 or just extend them. C<$ENV{MOJO_HOME}/bin/slovo> will load them with priority.
467  
468 With all the above, you can upgrade L<Slovo> by just installing new versions
469 over it and your files will not be touched. And of course, we know that you are
470 using versioning just in case anything goes wrong. See L</home>.
471  
472 =head1 ATTRIBUTES
473  
474 L<Slovo> inherits all attributes from L<Mojolicious> and implements
475 the following new ones.
476  
477 =head2 home
478  
479 L<Slovo> detects where B<home> is not like L<Mojo::Home> by where
480 C<lib/Mojolicous.pm> is but by where the C<script/> or C<bin/> folder resides
481 starting from where C<lib/Slovo.pm> is and going up the tree. If in one of
482 these folders there is a C<slovo> executable, then the upper folder is the
483 home.
484  
485 Examples:
486  
487 berov@Skylake:Slovo$ pwd
488 /home/berov/opt/dev/Slovo
489 berov@Skylake:Slovo$ perl script/slovo eval 'say app->home'
490 /home/berov/opt/dev/Slovo
491  
492 berov@Skylake:Slovo$ cpanm . -n -l ~/opt/t.com/slovo
493 --> Working on .
494 Configuring /home/berov/opt/dev/Slovo ... OK
495 Building Slovo-v2019.06.09 ... OK
496 Successfully installed Slovo-v2019.06.09
497 1 distribution installed
498  
499 berov@Skylake:t.com$ pwd
500 /home/berov/opt/t.com
501 berov@Skylake:t.com$ slovo/bin/slovo eval 'say app->home'
502 /home/berov/opt/t.com/slovo
503  
504 berov@Skylake:t.com$ pwd
505 /home/berov/opt/t.com
506 berov@Skylake:t.com$ perl -Islovo/lib/perl5 -MSlovo -E 'say Slovo->new->home'
507 /home/berov/opt/t.com/slovo
508  
509 =head2 log
510  
511 Overrides L<Mojolicious/log>. Logs to C<self-E<gt>home-E<gt>child('log/slovo.log')>
512 if C<$self-E<gt>home-E<gt>child('log')> exists and is writable. Oderwise writes to
513 STDERR. The log-level will default to either the C<MOJO_LOG_LEVEL> environment
514 variable, C<debug> if the "mode" is C<development>, or C<info> otherwise.
515  
516 =head2 resources
517  
518 push @{$app->static->paths}, $app->resources->child('public');
519  
520 Returns a L<Mojo::File> instance for path L<Slovo/resources> next to where
521 C<Slovo.pm> is installed.
522  
523 =head2 validator
524  
525 my $validator = $app->validator;
526 $app = $app->validator(Slovo::Validator->new);
527  
528 Validate values, defaults to a L<Slovo::Validator> object.
529  
530 # Add validation check
531 $app->validator->add_check(foo => sub {
532 my ($v, $name, $value) = @_;
533 return $value ne 'foo';
534 });
535  
536 # Add validation filter
537 $app->validator->add_filter(quotemeta => sub {
538 my ($v, $name, $value) = @_;
539 return quotemeta $value;
540 });
541  
542 =head1 METHODS
543  
544 L<Slovo> inherits all methods from L<Mojolicious> and implements
545 the following new ones.
546  
547 =head2 load_class
548  
549 A convenient wrapper with check for L<Mojo::Loader/load_class>.
550 Loads a class and croaks if something is wrong. This could be a helper.
551  
552 for my $class (@{$config->{load_classes} // []}) {
553 $app->load_class($class);
554 }
555  
556 =head2 startup
557  
558 Starts the application. Adds hooks, prepares C<$app-E<gt>routes> for use, loads
559 configuration files and applies settings from them, loads plugins, sets default
560 paths, and returns the application instance. See also L<Mojolicious/startup>.
561  
562 =head1 HOOKS
563  
564 Slovo adds custom code to the following hooks.
565  
566 =head2 around_action
567  
568 On each request we set the following variables in the stash so they are
569 available in the respective templates. Here they are:
570  
571 $stash->{l} //= $c->language; # current language
572 $stash->{user} //= $c->user; # current user
573  
574 =head2 around_dispatch
575  
576 On each request we determine the current host and modify the static and
577 renderer paths accordingly. This is how each domain has its own templates and
578 static files.
579  
580 Also if the C<templates> field for the current domain is not empty, we
581 determine from it the templates root for the theme to be used for this domain
582 during this request. This is how the themes support for multiple domains in one
583 L<Slovo> instance work.
584  
585 It is also important to note that in a long running application (not CGI) the
586 templates are cached in memory and the relative path from the current
587 templates root to each template is used as the key in L<Mojo::Cache> cache. We
588 had to implement L<Slovo::Cache/key_prefix> to be able to differentiate between
589 templates having the same names, but found in different paths. All this is
590 possible thanks to L<Mojolicious>'s well decoupled components.
591  
592 Example: Let's suppose that the domain L<https://слово.бг> has the field
593 'templates' value set to C<themes/malka> (малка==small f. in Bulgarian).
594  
595 Renderer paths before the check is performed:
596  
597 [
598 "/home/berov/opt/dev/Slovo/templates",
599 "/home/berov/perl5/perlbrew/perls/perl-5.28.2/lib/site_perl/5.28.2/Mojolicious/Plugin/Minion/resources/templates",
600 "/home/berov/opt/dev/Slovo/lib/Slovo/resources/templates"
601 ]
602  
603 Static paths before the check:
604  
605 [
606 "/home/berov/opt/dev/Slovo/public",
607 "/home/berov/perl5/perlbrew/perls/perl-5.28.2/lib/site_perl/5.28.2/Mojolicious/Plugin/Minion/resources/public",
608 "/home/berov/opt/dev/Slovo/lib/Slovo/resources/public"
609 ]
610  
611 Renderer paths after the check is performed. The first path in the list will be
612 used with priority:
613  
614 [
615 "/home/berov/opt/dev/Slovo/lib/Slovo/resources/templates/themes/malka", # if exists!
616 "/home/berov/opt/dev/Slovo/domove/xn--b1arjbl.xn--90ae/templates", # if exists!
617 "/home/berov/opt/dev/Slovo/templates",
618 "/home/berov/perl5/perlbrew/perls/perl-5.28.2/lib/site_perl/5.28.2/Mojolicious/Plugin/Minion/resources/templates",
619 "/home/berov/opt/dev/Slovo/lib/Slovo/resources/templates"
620 ]
621  
622 Static paths after the check:
623  
624 [
625 "/home/berov/opt/dev/Slovo/domove/xn--b1arjbl.xn--90ae/public", # if exists!
626 "/home/berov/opt/dev/Slovo/public",
627 "/home/berov/perl5/perlbrew/perls/perl-5.28.2/lib/site_perl/5.28.2/Mojolicious/Plugin/Minion/resources/public",
628 "/home/berov/opt/dev/Slovo/lib/Slovo/resources/public"
629 ]
630  
631 In addition the current domain row from table C<domove> becomes available in
632 the stash as C<$domain>.
633  
634 # In a controller or model
635 $c->stash('domain')->{id};
636 $m->c->stash('domain')->{aliases};
637  
638 # In a template like
639 # lib/Slovo/resources/templates/stranici/_form.html.ep
640 <%=
641 select_box
642 dom_id => $domove,
643 required => 1,
644 label => 'Дом',
645 title => 'В кой сайт се намира страницата.',
646 readonly => '',
647 value => $domain->{id} #default value
648 %>
649  
650 =head2 before_dispatch
651  
652 On each request we check if we have a logged in user and set the current user
653 to C<guest> if we don't. This way every part of the application (including
654 newly developed plugins) can count on having a current user. The user is needed
655 to determine the permissions for any table that has column C<permissions>. The
656 current user is available as C<$c-E<gt>user>.
657  
658 =head1 HELPERS
659  
660 Slovo implements the following helpers.
661  
662 =head2 openapi_spec
663  
664 We need to have our OpenAPI API specification always at hand as a unified
665 source of truth so here it is.
666  
667 # anywhere via $app or $c, even not via a REST call
668 state $columns =
669 $c->openapi_spec('/paths/~1stranici/get/parameters/3/default');
670 [
671 "id",
672 "pid",
673 "alias",
674 "title",
675 "is_dir"
676 ]
677  
678 =head1 BUGS, SUPPORT, CONTRIBUTING, DISCUSS
679  
680 =for html <a href="https://travis-ci.org/kberov/Slovo"><img src="https://travis-ci.org/kberov/Slovo.svg?branch=master"></a>
681  
682 To report a bug, please create issues at
683 L<GitHub|https://github.com/kberov/Slovo/issues>, fork the project and make
684 pull requests.
685  
686  
687 =head1 AUTHOR
688  
689 Красимир Беров
690 CPAN ID: BEROV
691 berov на cpan точка org
692 http://i-can.eu
693  
694 =head1 CONTRIBUTORS
695  
696 Ordered by time of first commit.
697  
698 =over
699  
700 =item * MANWAR (Mohammad S Anwar)
701  
702 =item * KABANOID (Mikhail Katasonov)
703  
704 =item * 0xAF (Stanislav Lechev)
705  
706 =back
707  
708 =head1 COPYRIGHT
709  
710 This is free software, licensed under:
711  
712 The Artistic License 2.0 (GPL Compatible)
713  
714 The full text of the license can be found in the
715 LICENSE file included with this module.
716  
717 This distribution contains other free software which belongs to their
718 respective authors.
719  
720 =head1 TODO
721  
722 =over
723  
724 =item * Stop adding features. Stabilize what we have.
725  
726 =item * Gradually replace L<MUI CSS|https://www.muicss.com/> with L<Chota
727 CSS|https://jenil.github.io/chota/> - site part is done.
728  
729 =item * Considerably improve the Adminiastration UI - now it is quite
730 simplistic. Use ES6 directly as per L<browsers compatibility
731 table|https://kangax.github.io/compat-table/es6/>
732  
733 =item * Consider using L<Mithril|https://github.com/MithrilJS/mithril.js> or
734 L<Vue.js|https://vuejs.org/> or something light as frontend framework for
735 building UI. We already use jQuery distributed with the Mojolicious distro.
736  
737 =back
738  
739 =head1 SEE ALSO
740  
741 L<Slovo::Plugin::TagHelpers>, L<Slovo::Plugin::DefaultHelpers>,
742 L<Slovo::Validator>, L<Mojolicious>, L<Mojolicious::Guides>
743  
744 =cut
745  
« blib/lib/Slovo/Plugin/CGI.pm ↑ Summary blib/lib/Test/Mojo/Role/Slovo.pm »