Jonathan Druart
9d6d641d1f
On bug 17591 we discovered that there was something weird going on with the way we export and use subroutines/modules. This patch tries to standardize our EXPORT to use EXPORT_OK only. That way we will need to explicitely define the subroutine we want to use from a module. This patch is a squashed version of: Bug 17600: After export.pl Bug 17600: After perlimport Bug 17600: Manual changes Bug 17600: Other manual changes after second perlimports run Bug 17600: Fix tests And a lot of other manual changes. export.pl is a dirty script that can be found on bug 17600. "perlimport" is: git clone https://github.com/oalders/App-perlimports.git cd App-perlimports/ cpanm --installdeps . export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib" find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \; The ideas of this patch are to: * use EXPORT_OK instead of EXPORT * perltidy the EXPORT_OK list * remove '&' before the subroutine names * remove some uneeded use statements * explicitely import the subroutines we need within the controllers or modules Note that the private subroutines (starting with _) should not be exported (and not used from outside of the module except from tests). EXPORT vs EXPORT_OK (from https://www.thegeekstuff.com/2010/06/perl-exporter-examples/) """ Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members. @EXPORT and @EXPORT_OK are the two main variables used during export operation. @EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace. @EXPORT_OK does export of symbols on demand basis. """ If this patch caused a conflict with a patch you wrote prior to its push: * Make sure you are not reintroducing a "use" statement that has been removed * "$subroutine" is not exported by the C4::$MODULE module means that you need to add the subroutine to the @EXPORT_OK list * Bareword "$subroutine" not allowed while "strict subs" means that you didn't imported the subroutine from the module: - use $MODULE qw( $subroutine list ); You can also use the fully qualified namespace: C4::$MODULE::$subroutine Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
198 lines
5.3 KiB
Perl
198 lines
5.3 KiB
Perl
package Koha::Report;
|
|
|
|
# This file is part of Koha.
|
|
#
|
|
# Koha is free software; you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Koha is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Koha; if not, see <http://www.gnu.org/licenses>.
|
|
|
|
use Modern::Perl;
|
|
|
|
|
|
use Koha::Database;
|
|
use Koha::Reports;
|
|
use Koha::DateUtils qw( dt_from_string output_pref );
|
|
|
|
use base qw(Koha::Object);
|
|
#
|
|
# FIXME We could only return an error code instead of the arrayref
|
|
# Only 1 error is returned
|
|
# TODO Koha::Report->store should check this before saving
|
|
|
|
=head1 NAME
|
|
|
|
Koha::Report - Koha Report Object class
|
|
|
|
=head1 API
|
|
|
|
=head2 Class Methods
|
|
|
|
=head3 is_sql_valid
|
|
|
|
my ( $is_sql_valid, $errors ) = $report->is_sql_valid;
|
|
|
|
$errors is a arrayref of hashrefs, keys can be sqlerr or queryerr.
|
|
|
|
Validate SQL query string so it only contains a select,
|
|
not any of the harmful queries.
|
|
|
|
=cut
|
|
|
|
sub is_sql_valid {
|
|
my ($self) = @_;
|
|
|
|
my $sql = $self->savedsql;
|
|
$sql //= '';
|
|
my @errors = ();
|
|
|
|
if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
|
|
push @errors, { sqlerr => $1 };
|
|
} elsif ($sql !~ /^\s*SELECT\b\s*/i) {
|
|
push @errors, { queryerr => 'Missing SELECT' };
|
|
}
|
|
|
|
return ( @errors ? 0 : 1, \@errors );
|
|
}
|
|
|
|
=head3 get_search_info
|
|
|
|
Return search info
|
|
|
|
=cut
|
|
|
|
sub get_search_info {
|
|
my $self = shift;
|
|
my $sub_mana_info = { 'query' => shift };
|
|
return $sub_mana_info;
|
|
}
|
|
|
|
=head3 get_sharable_info
|
|
|
|
Return properties that can be shared.
|
|
|
|
=cut
|
|
|
|
sub get_sharable_info {
|
|
my $self = shift;
|
|
my $shared_report_id = shift;
|
|
my $report = Koha::Reports->find($shared_report_id);
|
|
my $sub_mana_info = {
|
|
'savedsql' => $report->savedsql,
|
|
'report_name' => $report->report_name,
|
|
'notes' => $report->notes,
|
|
'report_group' => $report->report_group,
|
|
'type' => $report->type,
|
|
};
|
|
return $sub_mana_info;
|
|
}
|
|
|
|
=head3 new_from_mana
|
|
|
|
Clear a Mana report to be imported in Koha?
|
|
|
|
=cut
|
|
|
|
sub new_from_mana {
|
|
my $self = shift;
|
|
my $data = shift;
|
|
|
|
$data->{mana_id} = $data->{id};
|
|
|
|
delete $data->{exportemail};
|
|
delete $data->{kohaversion};
|
|
delete $data->{creationdate};
|
|
delete $data->{lastimport};
|
|
delete $data->{id};
|
|
delete $data->{nbofusers};
|
|
delete $data->{language};
|
|
|
|
Koha::Report->new($data)->store;
|
|
}
|
|
|
|
=head3 prep_report
|
|
|
|
Prep the report and return executable sql with parameters embedded and a list of header types
|
|
for building batch action links in the template
|
|
|
|
=cut
|
|
|
|
sub prep_report {
|
|
my ( $self, $param_names, $sql_params ) = @_;
|
|
my $sql = $self->savedsql;
|
|
|
|
# First we split out the placeholders
|
|
# This part of the code supports using [[ table.field | alias ]] in the
|
|
# query and replaces it by table.field AS alias. This is used to build
|
|
# the batch action links foir cardnumbers, itemnumbers, and biblionumbers in the template
|
|
# while allowing the library to alter the column names
|
|
my @split = split /\[\[|\]\]/, $sql;
|
|
my $headers;
|
|
for ( my $i = 0 ; $i < $#split / 2 ; $i++ )
|
|
{ #The placeholders are always the odd elements of the array
|
|
my ( $type, $name ) = split /\|/,
|
|
$split[ $i * 2 + 1 ]; # We split them on '|'
|
|
$headers->{$name} = $type; # Store as a lookup for the template
|
|
$headers->{$name} =~
|
|
s/^\w*\.//; # strip the table name just as in $sth->{NAME} array
|
|
$split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g
|
|
; #Quote any special characters so we can replace the placeholders
|
|
$name = C4::Context->dbh->quote($name);
|
|
$sql =~ s/\[\[$split[$i*2+1]\]\]/$type AS $name/
|
|
; # Remove placeholders from SQL
|
|
}
|
|
|
|
my %lookup;
|
|
@lookup{@$param_names} = @$sql_params;
|
|
@split = split /<<|>>/, $sql;
|
|
for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) {
|
|
my $quoted =
|
|
@$param_names ? $lookup{ $split[ $i * 2 + 1 ] } : @$sql_params[$i];
|
|
|
|
# if there are special regexp chars, we must \ them
|
|
$split[ $i * 2 + 1 ] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
|
|
if ( $split[ $i * 2 + 1 ] =~ /\|\s*date\s*$/ ) {
|
|
$quoted = output_pref(
|
|
{
|
|
dt => dt_from_string($quoted),
|
|
dateformat => 'iso',
|
|
dateonly => 1
|
|
}
|
|
) if $quoted;
|
|
}
|
|
unless ( $split[ $i * 2 + 1 ] =~ /\|\s*list\s*$/ && $quoted ) {
|
|
$quoted = C4::Context->dbh->quote($quoted);
|
|
}
|
|
else {
|
|
my @list = split /\n/, $quoted;
|
|
my @quoted_list;
|
|
foreach my $item (@list) {
|
|
$item =~ s/\r//;
|
|
push @quoted_list, C4::Context->dbh->quote($item);
|
|
}
|
|
$quoted = "(" . join( ",", @quoted_list ) . ")";
|
|
}
|
|
$sql =~ s/<<$split[$i*2+1]>>/$quoted/;
|
|
}
|
|
return $sql, $headers;
|
|
}
|
|
|
|
=head3 _type
|
|
|
|
Returns name of corresponding DBIC resultset
|
|
|
|
=cut
|
|
|
|
sub _type {
|
|
return 'SavedSql';
|
|
}
|
|
|
|
1;
|