Le mirroring des dépôts vers Github est maintenant natif dans Gitlab (et donc Framagit). Merci de passer à la fonction native pour que nous puissions couper notre solution maison. Détails sur https://docs.gitlab.com/ee/workflow/repository_mirroring.html#pushing-to-a-remote-repository (pensez à supprimer le webhook de notre solution maison).

Commit 95adc29c by Luc Didry

Document abstraction layer. Fix #22

parent ac6000f4
Pipeline #10803 passed with stage
in 5 minutes 3 seconds
......@@ -2,7 +2,7 @@ image: debian:jessie
test:
script:
- apt-get update
- apt-get install -y build-essential libssl-dev openssl
- apt-get install -y build-essential libssl-dev openssl perl-doc
- echo yes | cpan Carton
- carton install
- cp lstu.conf.template lstu.conf
......
......@@ -12,7 +12,10 @@ locales:
$(XGETTEXT) -f $(EXTRACTFILES) -o $(FR) 2>/dev/null
$(XGETTEXT) -f $(EXTRACTFILES) -o $(OC) 2>/dev/null
test:
podcheck:
podchecker lib/Lstu/DB/Ban.pm lib/Lstu/DB/Session.pm lib/Lstu/DB/URL.pm
test: podcheck
$(CARTON) $(REAL_LSTU) test
dev:
......
......@@ -7,6 +7,51 @@ has 'until';
has 'strike' => 0;
has 'dbtype';
=head1 NAME
Lstu::DB::Ban - Abstraction layer for Lstu ban system
=head1 Contributing
When creating a new database accessor, make sure that it provides the following subroutines.
After that, modify this file and modify the C<new> subroutine to allow to use your accessor.
Have a look at Lstu::DB::Ban::SQLite's code: it's simple and may be more understandable that this doc.
=head1 Attributes
=over 1
=item B<ip> : string, an IP address
=item B<until> : unix timestamp
=item B<strike> : integer
=item B<dbtype> : string
=back
=head1 Sub routines
=head2 new
=over 1
=item B<Usage> : C<$c = Lstu::DB::Ban-E<gt>new(dbtype =E<gt> 'sqlite');>
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<ip> attribute is provided, it have to load the informations from the database
=item B<Returns> : the db accessor object
=item B<Info> : the dbtype argument is used by Lstu::DB::Ban to choose which db accessor will be used, you don't need to use it
=back
=cut
sub new {
my $c = shift;
......@@ -32,4 +77,67 @@ sub to_hash {
};
}
=head2 is_banned
=over 1
=item B<Usage> : C<$c-E<gt>is_banned(3)>
=item B<Arguments> : an integer. Will be config('ban_min_strike'), which is the number of strike before being banned (default: 3)
=item B<Purpose> : check the db record with C<ip> equal to the object's ip attribute if C<until> is superior to current time and if C<strike> is superior or equal to the argument.
eg: C<WHERE ip = ? AND until E<gt> ? AND strike E<gt>= ?', $c->ip, time, $argument>
=item B<Returns> : the db accessor object if the ip is banned, undef otherwise
=back
=head2 increment_ban_delay
=over 1
=item B<Usage> : C<$c-E<gt>increment_ban_delay(3600)>
=item B<Arguments> : an integer. This number is a penalty (in second) that will be added to the C<until> attribute of the db accessor object
=item B<Purpose> : add penalty to the C<until> attribute of the db accessor object, increment the C<strike> attribute by one and write the db accessor object's attribute to the database.
Update the database record if one already exists, create one otherwise.
=item B<Returns> : the db accessor object
=back
=head2 clear
=over 1
=item B<Usage> : C<$c-E<gt>clear>
=item B<Arguments> : none
=item B<Purpose> : delete all ban records from database where until < time
eg: C<WHERE until E<lt> ?, time>
=item B<Returns> : nothing is expected
=back
=head2 delete_all
=over 1
=item B<Usage> : C<$c-E<gt>delete_all>
=item B<Arguments> : none
=item B<Purpose> : delete all ban records from database unconditionnally
=item B<Returns> : nothing is expected
=back
=cut
1;
......@@ -15,19 +15,6 @@ sub new {
return $c;
}
sub clear {
my $c = shift;
Lstu::DB::SQLite::Ban->delete_where('until < ?', time);
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Ban->delete_where('1 = 1');
}
sub is_banned {
my $c = shift;
my $ban_min_strike = shift;
......@@ -73,6 +60,19 @@ sub increment_ban_delay {
return $c;
}
sub clear {
my $c = shift;
Lstu::DB::SQLite::Ban->delete_where('until < ?', time);
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Ban->delete_where('1 = 1');
}
sub _slurp {
my $c = shift;
......
......@@ -6,6 +6,49 @@ has 'token';
has 'until';
has 'dbtype';
=head1 NAME
Lstu::DB::Session - Abstraction layer for Lstu session system
=head1 Contributing
When creating a new database accessor, make sure that it provides the following subroutines.
After that, modify this file and modify the C<new> subroutine to allow to use your accessor.
Have a look at Lstu::DB::Session::SQLite's code: it's simple and may be more understandable that this doc.
=head1 Attributes
=over 1
=item B<token> : random string
=item B<until> : unix timestamp
=item B<dbtype> : string
=back
=head1 Sub routines
=head2 new
=over 1
=item B<Usage> : C<$c = Lstu::DB::Session-E<gt>new(dbtype =E<gt> 'sqlite');>
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<token> attribute is provided, it have to load the informations from the database
=item B<Returns> : the db accessor object
=item B<Info> : the dbtype argument is used by Lstu::DB::Session to choose which db accessor will be used, you don't need to use it
=back
=cut
sub new {
my $c = shift;
......@@ -36,4 +79,64 @@ sub to_hash {
}
}
=head2 delete
=over 1
=item B<Usage> : C<$c-E<gt>delete>
=item B<Arguments> : none
=item B<Purpose> : delete the session record from the database
=item B<Returns> : the db accessor object
=back
=head2 write
=over 1
=item B<Usage> : C<$c-E<gt>write>
=item B<Arguments> : none
=item B<Purpose> : create or update the object in the database
=item B<Returns> : the db accessor object
=back
=head2 clear
=over 1
=item B<Usage> : C<$c-E<gt>clear>
=item B<Arguments> : none
=item B<Purpose> : delete all session records from database where until < time
eg: C<WHERE until E<lt> ?, time>
=item B<Returns> : nothing is expected
=back
=head2 delete_all
=over 1
=item B<Usage> : C<$c-E<gt>delete_all>
=item B<Arguments> : none
=item B<Purpose> : delete all session records from database unconditionnally
=item B<Returns> : nothing is expected
=back
=cut
1;
......@@ -15,19 +15,6 @@ sub new {
return $c;
}
sub clear {
my $c = shift;
Lstu::DB::SQLite::Sessions->delete_where('until < ?', time);
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Sessions->delete_where('1 = 1');
}
sub delete {
my $c = shift;
......@@ -62,6 +49,19 @@ sub write {
return $c;
}
sub clear {
my $c = shift;
Lstu::DB::SQLite::Sessions->delete_where('until < ?', time);
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Sessions->delete_where('1 = 1');
}
sub _slurp {
my $c = shift;
......
......@@ -8,6 +8,53 @@ has 'counter' => 0;
has 'timestamp';
has 'dbtype';
=head1 NAME
Lstu::DB::URL - Abstraction layer for Lstu session system
=head1 Contributing
When creating a new database accessor, make sure that it provides the following subroutines.
After that, modify this file and modify the C<new> subroutine to allow to use your accessor.
Have a look at Lstu::DB::URL::SQLite's code: it's simple and may be more understandable that this doc.
=head1 Attributes
=over 1
=item B<short> : random string
=item B<url> : string, valid URL
=item B<counter> : integer
=item B<timestamp> : unix timestamp
=item B<dbtype> : string
=back
=head1 Sub routines
=head2 new
=over 1
=item B<Usage> : C<$c = Lstu::DB::URL-E<gt>new(dbtype =E<gt> 'sqlite');>
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<short> or the C<url> attribute is provided, it have to load the informations from the database. In the case of multiple records for the same C<url>, choose the first.
=item B<Returns> : the db accessor object
=item B<Info> : the dbtype argument is used by Lstu::DB::URL to choose which db accessor will be used, you don't need to use it
=back
=cut
sub new {
my $c = shift;
......@@ -34,4 +81,162 @@ sub to_hash {
};
}
=head2 increment_counter
=over 1
=item B<Usage> : C<$c-E<gt>increment_counter>
=item B<Arguments> : none
=item B<Purpose> : increment the C<counter> attribute of the db accessor object and update the database record
=item B<Returns> : the db accessor object
=back
=head2 write
=over 1
=item B<Usage> : C<$c-E<gt>write>
=item B<Arguments> : none
=item B<Purpose> : create or update the object in the database
=item B<Returns> : the db accessor object
=back
=head2 delete
=over 1
=item B<Usage> : C<$c-E<gt>delete>
=item B<Arguments> : none
=item B<Purpose> : delete the URL record from the database
=item B<Returns> : the db accessor object
=back
=head2 exist
=over 1
=item B<Usage> : C<$c-E<gt>exist('short')>
=item B<Arguments> : string
=item B<Purpose> : count how many database record there is with C<short> equal to the argument.
eg: COUNT(short) WHERE short = ?, $argument
=item B<Returns> : integer. Should be 0 or 1
=back
=head2 choose_empty
=over 1
=item B<Usage> : C<$c-E<gt>choose_empty>
=item B<Arguments> : none
=item B<Purpose> : choose an unassigned short string in the database
=item B<Returns> : string, an unassigned short string
=back
=head2 count_empty
=over 1
=item B<Usage> : C<$c-E<gt>count_empty>
=item B<Arguments> : none
=item B<Purpose> : count how many unassigned short string there is in the database
eg: C<COUNT(short) WHERE url IS NULL>
=item B<Returns> : integer
=back
=head2 paginate
=over 1
=item B<Usage> : C<$c-E<gt>paginate($page, $page_offset)>
=item B<Arguments> : two integers.
=over 2
=item B<$page> : the number of the page you want
=item B<$page_offset> : the number of records per page.
=back
=item B<Purpose> : returns all the URL records, page per page, ordered by the C<counter> attribute
eg: SELECT * WHERE url IS NOT NULL ORDER BY counter DESC LIMIT ? OFFSET ?, $page_offset, $page * $page_offset
=item B<Returns> : an array of hash references, containing all the Lstu::DB::URL attributes, except C<dbtype>
=back
=head2 get_a_lot
=over 1
=item B<Usage> : C<$c-E<gt>paginate(['short1', 'short2'])>
=item B<Arguments> : an array reference of strings, which are C<short> attributes
=item B<Purpose> : returns all the URL records which C<short> attribute are in the array ref
=item B<Returns> : an array of hash references, containing all the Lstu::DB::URL attributes, except C<dbtype>
=back
=head2 total
=over 1
=item B<Usage> : C<$c-E<gt>total>
=item B<Arguments> : none
=item B<Purpose> : count how many shorten links there is in the database.
eg: C<COUNT(short) WHERE url IS NOT NULL>
=item B<Returns> : integer
=back
=head2 delete_all
=over 1
=item B<Usage> : C<$c-E<gt>delete_all>
=item B<Arguments> : none
=item B<Purpose> : delete all URL records from database unconditionnally
=item B<Returns> : nothing is expected
=back
=cut
1;
......@@ -15,26 +15,6 @@ sub new {
return $c;
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Lstu->delete_where('1 = 1');
}
sub delete {
my $c = shift;
my $result;
if (Lstu::DB::SQLite->begin) {
$result = $c->record->delete;
$c->record(undef);
Lstu::DB::SQLite->commit;
}
return $result;
}
sub increment_counter {
my $c = shift;
......@@ -44,21 +24,6 @@ sub increment_counter {
return $c;
}
sub count_empty {
my $c = shift;
return Lstu::DB::SQLite::Lstu->count('WHERE url IS NULL');
}
sub exist {
my $c = shift;
my $short = shift;
return undef unless $short;
return Lstu::DB::SQLite::Lstu->count('WHERE short = ?', $short);
}
sub write {
my $c = shift;
......@@ -85,6 +50,28 @@ sub write {
return $c;
}
sub delete {
my $c = shift;
my $result;
if (Lstu::DB::SQLite->begin) {
$result = $c->record->delete;
$c->record(undef);
Lstu::DB::SQLite->commit;
}
return $result;
}
sub exist {
my $c = shift;
my $short = shift;
return undef unless $short;
return Lstu::DB::SQLite::Lstu->count('WHERE short = ?', $short);
}
sub choose_empty {
my $c = shift;
......@@ -99,10 +86,10 @@ sub choose_empty {
}
}
sub total {
sub count_empty {
my $c = shift;
return Lstu::DB::SQLite::Lstu->count("WHERE url IS NOT NULL");
return Lstu::DB::SQLite::Lstu->count('WHERE url IS NULL');
}
sub paginate {
......@@ -121,6 +108,19 @@ sub get_a_lot {
return Lstu::DB::SQLite::Lstu->select("WHERE short IN ($p) ORDER BY counter DESC", @{$u});
}
sub total {
my $c = shift;
return Lstu::DB::SQLite::Lstu->count("WHERE url IS NOT NULL");
}
sub delete_all {
my $c = shift;
# Rotten syntax, but prevents "Static Lstu::DB::SQLite->delete has been deprecated"
Lstu::DB::SQLite::Lstu->delete_where('1 = 1');
}
sub _slurp {
my $c = shift;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment