1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm) #
3 # Database module for mainipulating SQLite databases in the database directory. #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk> #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
9 # This program is free software; you can redistribute it and/or modify it under #
10 # the terms of the GNU General Public License as published by the Free #
11 # Software Foundation; as version 2 of the License. #
13 # This program is distributed in the hope that it will be useful, but WITHOUT #
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
17 # You should have received a copy of the GNU General Public License along with #
18 # this program; if not, write to the Free Software Foundation, Inc., 51 #
19 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
20 #################################################################################
22 # Define the package (perl module) name.
24 package Kiriwrite::Database::SQLite;
26 # Enable strict and use warnings.
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION = "0.2.0";
38 my ($options, %options);
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_statement_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_statement_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_statement_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines. #
59 #################################################################################
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite #
67 # $dbmodule = Kiriwrite::Database::SQLite->new(); #
68 #################################################################################
70 # Get the perl module name.
75 return bless($self, $class);
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module #
85 # $dbmodule->loadsettings(Directory, options); #
87 # options Specifies the following options (in any order). #
89 # Directory Specifies the directory to use for getting databases. #
90 # DateTime Specifies the date and time format to use. #
91 # Server Specifies the server to use. #
92 # Database Specifies the database to use. #
93 # Username Specifies the username to use. #
94 # Password Specifies the password to use. #
95 # HashType Specifies the password hash type to use. #
96 # Port Specifies the server port to use. #
97 # Protocol Specifies the protocol to use. #
98 # TablePrefix Specifies the table prefix to use. #
99 #################################################################################
101 # Get the data passed to the subroutine.
104 my ($passedoptions) = @_;
106 # Add the directory setting to the list of options (as it's the only
107 # one needed for this database module).
110 "Directory" => $passedoptions->{"Directory"},
111 "DateTime" => $passedoptions->{"DateTime"},
117 #################################################################################
118 # convert: Converts data into SQL formatted data. #
122 # $dbmodule->convert(data); #
124 # data Specifies the data to convert. #
125 #################################################################################
127 # Get the data passed to the subroutine.
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date. #
149 # $dbmodule->dateconvert(date); #
151 # date Specifies the date to convert. #
152 #################################################################################
154 # Get the date passed to the subroutine.
159 # Convert the date given into the proper date.
161 # Create the following varialbes to be used later.
188 # Split the date and time.
190 $length = length($data);
196 # Get the character and check if it is a space.
198 $char = substr($data, $seek, 1);
202 # The character is a space, so get the date and time.
204 $date = substr($data, 0, $seek);
205 $timelength = $length - $seek - 1;
206 $time = substr($data, $seek + 1, $timelength);
212 } until ($seek eq $length);
214 # Get the year, month and date.
216 $length = length($date);
221 # Get the character and check if it is a dash.
223 $char = substr($date, $seek, 1);
227 # The character is a dash, so get the year, month or day.
229 $datelength = $seek - $startchar;
233 # Get the year from the date.
235 $year = substr($date, 0, $datelength) + 1900;
239 # Get the last two characters to get the short year
242 $year_short = substr($year, 2, 2);
244 } elsif ($count eq 1){
246 # Get the month and day from the date.
248 $month = substr($date, $startchar + 1, $datelength - 1) + 1;
250 # Check if the month is less then 10, if it is
251 # add a zero to the value.
255 $month_full = '0' . $month;
259 $month_full = $month;
266 $daylength = $length - $seek + 1;
267 $day = substr($date, $startchar + 1, $daylength);
269 # Check if the day is less than 10, if it is
270 # add a zero to the value.
274 $day_full = '0' . $day;
288 } until ($seek eq $length);
290 # Get the length of the time value and reset certain
293 $length = length($time);
300 # Get the character and check if it is a colon.
302 $char = substr($time, $seek, 1);
306 # The character is a colon, so get the hour, minute and day.
308 $timelength = $seek - $startchar;
312 # Get the hour from the time.
314 $hour = substr($time, 0, $timelength);
318 # If the hour is less than ten then add a
323 $hour_full = '0' . $hour;
331 } elsif ($count eq 1){
333 # Get the minute and second from the time.
335 $minute = substr($time, $startchar + 1, $timelength - 1);
338 # If the minute is less than ten then add a
343 $minute_full = '0' . $minute;
347 $minute_full = $minute;
353 $secondlength = $length - $seek + 1;
354 $second = substr($time, $startchar + 1, $secondlength);
356 # If the second is less than ten then add a
361 $second_full = '0' . $second;
365 $second_full = $second;
375 } until ($seek eq $length);
377 # Get the setting for displaying the date and time.
379 $data = $options{"DateTime"};
381 # Process the setting for displaying the date and time
382 # using regular expressions
384 $data =~ s/DD/$day_full/g;
386 $data =~ s/MM/$month_full/g;
387 $data =~ s/M/$month/g;
388 $data =~ s/YY/$year/g;
389 $data =~ s/Y/$year_short/g;
391 $data =~ s/hh/$hour_full/g;
392 $data =~ s/h/$hour/g;
393 $data =~ s/mm/$minute_full/g;
394 $data =~ s/m/$minute/g;
395 $data =~ s/ss/$second_full/g;
396 $data =~ s/s/$second/g;
405 #################################################################################
406 # geterror: Gets the error message (or extended error message). #
410 # $dbmodule->geterror(extended); #
412 # Extended Specifies if the extended error should be retrieved. #
413 #################################################################################
415 # Get the data passed to the subroutine.
418 my $extended = shift;
432 # Check to see if extended information should be returned.
436 # Extended information should be returned.
442 # Basic information should be returned.
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid. #
456 # $database->dbpermissions(dbname, read, write); #
458 # dbname Specifies the database name to check. #
459 # read Check to see if the database can be read. #
460 # write Check to see if the database can be written. #
461 #################################################################################
463 # Get the database name, read setting and write setting.
465 my ($class, $dbname, $readper, $writeper) = @_;
467 # Check if the database can be read.
471 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473 # The database can be read.
477 # The database cannot be read, so return a value
486 # Check if the database can be written.
490 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492 # The database can be read.
496 # The database cannot be read, so return a value
505 # No errors have occured while checking so return a value
513 #################################################################################
514 # dbexists: Check if the database exists. #
518 # $dbmodule->dbexists(dbname); #
520 # dbname Specifies the database name to check. #
521 #################################################################################
523 # Get the value that was passed to the subroutine.
528 # Check if the filename exists, if it does, return a value of 1, else
529 # return a value of 0, meaning that the file was not found.
531 if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
533 # Specified file does exist so return a value of 0.
539 # Specified file does not exist so return a value of 1.
547 #################################################################################
548 # Database Subroutines. #
549 #################################################################################
552 #################################################################################
553 # getdblist: Gets the list of available databases. #
557 # $dbmodule->getdblist(); #
558 #################################################################################
560 # Get the list of databases.
563 my @data_directory_final;
565 my $database_filename_length;
566 my $database_filename_friendly;
568 # Check if the database directory has valid permission settings.
570 if (-e $options{"Directory"}){
572 # The database directory does exist. So check if
573 # the permission settings are valid.
575 if (-r $options{"Directory"}){
577 # The permission settings for reading the directory
582 # The permission settings for reading the directory
583 # are invalid so return an error value.
585 $error = "DataDirInvalidPermissions";
592 # The database directory does not exist, so return an
595 $error = "DataDirMissing";
600 opendir(DATADIR, $options{"Directory"});
601 @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
604 # Process the list of databases.
606 foreach $database (@data_directory){
608 $database =~ s/.db.sqlite$//og;
609 $database_filename_friendly = $database;
611 #$database_filename_length = length($database);
612 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
613 push(@data_directory_final, $database_filename_friendly);
617 # Return the list of databases.
619 return @data_directory_final;
624 #################################################################################
625 # getdatabaseinfo: Get information about the database. #
629 # $dbmodule->getdatabaseinfo(); #
630 #################################################################################
632 # Get the database information.
635 my ($databaseinfo, %databaseinfo);
636 my ($sqldata, @sqldata);
641 $statement_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
642 $error = "DatabaseError", $errorext = $database_handle->errstr, return
644 $statement_handle->execute();
646 @sqldata = $statement_handle->fetchrow_array();
648 # Process the database information into a hash.
651 "DatabaseName" => $sqldata[0],
652 "Description" => $sqldata[1],
653 "Notes" => $sqldata[2],
654 "Categories" => $sqldata[3],
655 "Major" => $sqldata[4],
656 "Minor" => $sqldata[5],
657 "Revision" => $sqldata[6]
660 $statement_handle->finish();
661 undef $statement_handle;
663 return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be #
670 # moved or copied to. #
674 # $dbmodule->getseconddatabaseinfo(); #
675 #################################################################################
677 # Get the database information.
680 my ($databaseinfo, %databaseinfo);
681 my ($sqldata, @sqldata);
686 $second_statement_handle = $second_database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
687 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
689 $second_statement_handle->execute();
691 @sqldata = $second_statement_handle->fetchrow_array();
693 # Process the database information into a hash.
696 "DatabaseName" => $sqldata[0],
697 "Description" => $sqldata[1],
698 "Notes" => $sqldata[2],
699 "Categories" => $sqldata[3],
700 "Major" => $sqldata[4],
701 "Minor" => $sqldata[5],
702 "Revision" => $sqldata[6]
705 $second_statement_handle->finish();
706 undef $second_statement_handle;
708 return %databaseinfo;
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database. #
718 # $dbmodule->adddatabase(options); #
720 # options Specifies the following options in any order. #
722 # DatabaseFilename Specifies the database file/shortname to use. #
723 # DatabaseName Specifies the database name to use. #
724 # DatabaseDescription Specifies the database description to use. #
725 # DatabaseNotes Specifies the database notes to use. #
726 # DatabaseCategories Specifies the database categories to use. #
727 # VersionMajor Specifies the major version. #
728 # VersionMinor Specifies the minor version. #
729 # VersionRevision Specifies the revision version. #
730 #################################################################################
732 # Get the database that was passed to the subroutine.
738 my ($passedoptions) = @_;
740 my $dbfilename = $passedoptions->{"DatabaseFilename"};
741 my $dbname = $passedoptions->{"DatabaseName"};
742 my $dbdescription = $passedoptions->{"DatabaseDescription"};
743 my $dbnotes = $passedoptions->{"DatabaseNotes"};
744 my $dbcategories = $passedoptions->{"DatabaseCategories"};
745 my $dbmajorver = $passedoptions->{"VersionMajor"};
746 my $dbminorver = $passedoptions->{"VersionMinor"};
747 my $dbrevisionver = $passedoptions->{"VersionRevision"};
749 # Check if the database with the filename given already exists.
751 my $database_exists = $class->dbexists($dbfilename);
753 if ($database_exists eq 0){
755 # The database filename exists so set the error value.
757 $error = "DatabaseExists";
762 # Create the database structure.
764 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765 $database_handle->{unicode} = 1;
766 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767 name varchar(256) primary key,
768 description varchar(512),
770 categories varchar(512),
771 kiriwrite_version_major int(4),
772 kiriwrite_version_minor int(4),
773 kiriwrite_version_revision int(4)
774 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775 $statement_handle->execute();
777 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778 filename varchar(256) primary key,
779 pagename varchar(512),
780 pagedescription varchar(512),
781 pagesection varchar(256),
782 pagetemplate varchar(64),
785 lastmodified datetime
786 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787 $statement_handle->execute();
789 # Convert the values into SQL query formatted values and add an entry
790 # to the kiriwrite_database_info table.
792 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793 \'' . $class->convert($dbname) . '\',
794 \'' . $class->convert($dbdescription) . '\',
795 \'' . $class->convert($dbnotes) . '\',
796 \'' . $class->convert($dbcategories) . '\',
797 \'' . $class->convert($dbmajorver) . '\',
798 \'' . $class->convert($dbminorver) . '\',
799 \'' . $class->convert($dbrevisionver) . '\'
800 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801 $statement_handle->execute();
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database. #
811 # $dbmodule->editdatabase(options); #
813 # options Specifies the following options in any order. #
815 # NewDatabaseFilename Specifies the new database filename to use. #
816 # DatabaseName Specifies the new database name. #
817 # DatabaseDescription Specifies the new database description. #
818 # DatabaseNotes Specifies the new database notes. #
819 # DatabaseCategories Specifies the new database categories. #
820 #################################################################################
826 my ($passedoptions) = @_;
828 my $dbnewfilename = $passedoptions->{"DatabaseNewFilename"};
829 my $dbname = $passedoptions->{"DatabaseName"};
830 my $dbdescription = $passedoptions->{"DatabaseDescription"};
831 my $dbnotes = $passedoptions->{"DatabaseNotes"};
832 my $dbcategories = $passedoptions->{"DatabaseCategories"};
834 # Check if a new database filename has been specified and if a
835 # new database filename has been specified then change the
838 if ($database_filename ne $dbnewfilename){
840 # A new database filename has been given so check if the output
841 # directory has write access.
843 if (-r $options{"Directory"}){
845 # The directory is readable.
849 # The directory is not readable so set the error value.
851 $error = "DataDirInvalidPermissions";
857 if (-w $options{"Directory"}){
859 # The directory is writeable.
863 # The directory is not writeable so set the error value.
865 $error = "DataDirInvalidPermissions";
871 # Check if a database filename already exists before using the
874 my $database_newexists = $class->dbexists($dbnewfilename);
876 if ($database_newexists eq 0){
878 # The database filename exists so set the error value.
880 $error = "DatabaseExists";
885 # Check if the database can be renamed (has write access).
887 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
889 if ($database_permissions eq 1){
891 # The database filename exists so set the error value.
893 $error = "InvalidPermissionsSet";
898 # "Disconnect" from the database.
900 $database_handle->disconnect();
902 # Rename the database.
904 ($database_filename) = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
905 ($dbnewfilename) = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
907 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
909 # Reload the database from the new filename.
911 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
912 $database_handle->{unicode} = 1;
913 $database_filename = $dbnewfilename;
917 # Check if the database can be altered with the new data.
919 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
921 if ($database_permissions eq 1){
923 # The database filename exists so set the error value.
925 $error = "InvalidPermissionsSet";
930 # Get the current database information.
932 $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933 $statement_handle->execute();
935 my @database_oldinfo = $statement_handle->fetchrow_array();
937 my $dboldname = $database_oldinfo[0];
939 # Update the database information.
941 $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942 description = \'' . $class->convert($dbdescription) . '\',
943 notes = \'' . $class->convert($dbnotes) . '\',
944 categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945 $statement_handle->execute();
947 undef $statement_handle;
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database. #
958 # $dbmodule->deletedatabase(options); #
960 # options Specifies the following options in any order. #
962 # DatabaseName Specifies the Kiriwrite database to delete. #
963 #################################################################################
968 # Get the database filename.
971 my ($passedoptions) = shift;
973 my $databasename = $passedoptions->{"DatabaseName"};
975 # Check if the database exists.
977 my $database_exists = $class->dbexists($databasename);
979 if ($database_exists eq 1){
981 # The database does not exist so set the error value.
983 $error = "DoesNotExist";
988 # Check if the database permissions are valid.
990 my $database_permissions = $class->dbpermissions($databasename);
992 if ($database_permissions eq 1){
994 # The database permissions are invalid so set the error
997 $error = "InvalidPermissionsSet";
1002 # Delete the database.
1004 ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006 unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database. #
1016 # $dbmodule->connect(options); #
1018 # options Specifies the following options in any order. #
1020 # DatabaseName Specifies the Kiriwrite database to use. #
1021 #################################################################################
1023 # Get the database name.
1029 my ($passedoptions) = @_;
1030 my (%database, $database);
1032 my $dbname = $passedoptions->{"DatabaseName"};
1034 # Check if the database exists.
1036 my $database_exists = $class->dbexists($dbname);
1038 if ($database_exists eq 1){
1040 # The database does not exist so return an error value
1041 # saying that the database does not exist.
1043 $error = "DoesNotExist";
1049 # Check if the database has valid permissions set.
1051 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1053 if ($database_permissions eq 1){
1055 # The database has invalid permissions set so return
1056 # an error value saying that the database has invalid
1059 $error = "InvalidPermissionsSet";
1065 # Connect to the database.
1067 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1068 $database_handle->{unicode} = 1;
1069 $database_filename = $dbname;
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying #
1080 # $dbmodule->selectseconddb(options); #
1082 # options Specifies the following options in any order. #
1084 # DatabaseName Specifies the Kiriwrite database to use. #
1085 #################################################################################
1087 # Get the database name.
1093 my ($passedoptions) = @_;
1094 my (%database, $database);
1096 my $dbname = $passedoptions->{"DatabaseName"};
1098 # Check if the database exists.
1100 my $database_exists = $class->dbexists($dbname);
1102 if ($database_exists eq 1){
1104 # The database does not exist so return an error value
1105 # saying that the database does not exist.
1107 $error = "DoesNotExist";
1113 # Check if the database has valid permissions set.
1115 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1117 if ($database_permissions eq 1){
1119 # The database has invalid permissions set so return
1120 # an error value saying that the database has invalid
1123 $error = "InvalidPermissionsSet";
1129 # Connect to the database.
1131 $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1132 $second_database_handle->{unicode} = 1;
1133 $second_database_filename = $dbname;
1137 #################################################################################
1138 # Page subroutines. #
1139 #################################################################################
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database. #
1147 # $dbmodule->getpagelist(options); #
1149 # options Specifies the following options in any order. #
1151 # StartFrom Start from the specified page in the database. #
1152 # Limit Get the amount of pages given. #
1153 #################################################################################
1159 my ($passedoptions) = shift;
1161 my $start_from = $passedoptions->{"StartFrom"};
1162 my $limit = $passedoptions->{"Limit"};
1164 if (defined($start_from)){
1172 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1173 $statement_handle->execute();
1177 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1178 $statement_handle->execute();
1182 my @database_pagefilenames;
1183 my @database_pagefilenames_final;
1185 # Process the collected pages.
1187 while (@database_pagefilenames = $statement_handle->fetchrow_array){
1189 # Add each page to the list of pages in the database.
1191 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1195 undef $statement_handle;
1196 return @database_pagefilenames_final;
1201 #################################################################################
1202 # getpagecount: Get the count of pages that are in the database. #
1206 # $dbmodule->getpagecount(); #
1207 #################################################################################
1214 $statement_handle = $database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1215 $statement_handle->execute();
1217 my $count = $statement_handle->fetchrow_array();
1224 #################################################################################
1225 # getpageinfo: Gets the page information from the filename passed. #
1229 # $dbmodule->getpageinfo(options); #
1231 # options Specifies the following options in any order. #
1233 # PageFilename Specifies the page filename to get the page information from. #
1234 # Reduced Specifies if the reduced version of the page information should #
1236 #################################################################################
1242 my ($passedoptions) = shift;
1243 my (%database_page, $database_page);
1244 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1249 # Get the page from the database.
1251 my $page_filename = $passedoptions->{"PageFilename"};
1252 my $page_reduced = $passedoptions->{"Reduced"};
1254 if (!$page_reduced){
1260 if ($page_reduced eq 1){
1262 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1264 $statement_handle->execute();
1266 # Check if the page exists in the database.
1268 while (@data_page = $statement_handle->fetchrow_array()){
1270 # Get the values from the array.
1272 $pagefilename = $data_page[0];
1273 $pagename = $data_page[1];
1274 $pagedescription = $data_page[2];
1275 $pagelastmodified = $data_page[3];
1277 # Put the values into the page hash.
1280 "PageFilename" => $pagefilename,
1281 "PageName" => $pagename,
1282 "PageDescription" => $pagedescription,
1283 "PageLastModified" => $class->dateconvert($pagelastmodified),
1293 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1295 $statement_handle->execute();
1297 # Check if the page exists in the database.
1299 while (@data_page = $statement_handle->fetchrow_array()){
1301 # Get the values from the array.
1303 $pagefilename = $data_page[0];
1304 $pagename = $data_page[1];
1305 $pagedescription = $data_page[2];
1306 $pagesection = $data_page[3];
1307 $pagetemplate = $data_page[4];
1308 $pagedata = $data_page[5];
1309 $pagesettings = $data_page[6];
1310 $pagelastmodified = $data_page[7];
1312 # Put the values into the page hash.
1315 "PageFilename" => $pagefilename,
1316 "PageName" => $pagename,
1317 "PageDescription" => $pagedescription,
1318 "PageSection" => $pagesection,
1319 "PageTemplate" => $pagetemplate,
1320 "PageContent" => $pagedata,
1321 "PageSettings" => $pagesettings,
1322 "PageLastModified" => $class->dateconvert($pagelastmodified),
1331 # Check if the page did exist.
1335 $error = "PageDoesNotExist";
1340 undef $statement_handle;
1341 return %database_page;
1346 #################################################################################
1347 # addpage: Add a page to the selected database. #
1351 # $dbmodule->addpage(options); #
1353 # options Specifies the following options in any order. #
1355 # PageFilename Specifies the page filename to use. #
1356 # PageName Specifies the page name to use. #
1357 # PageDescription Specifies the page description to use. #
1358 # PageSection Specifies the page section to use. #
1359 # PageTemplate Specifies the page template to use. #
1360 # PageContent Specifies the page content to use. #
1361 # PageSettings Specifies the page settings to use. #
1362 #################################################################################
1364 # Get the data that was passed to the subroutine.
1370 my ($passedoptions) = shift;
1375 # Get the values passed to the hash.
1377 my $page_filename = $passedoptions->{"PageFilename"};
1378 my $page_name = $passedoptions->{"PageName"};
1379 my $page_description = $passedoptions->{"PageDescription"};
1380 my $page_section = $passedoptions->{"PageSection"};
1381 my $page_template = $passedoptions->{"PageTemplate"};
1382 my $page_content = $passedoptions->{"PageContent"};
1383 my $page_settings = $passedoptions->{"PageSettings"};
1385 # Check to see if the filename given already exists
1386 # in the page database.
1388 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1389 $statement_handle->execute();
1391 # Check if a page with the filename given really does
1394 while (@database_page = $statement_handle->fetchrow_array()){
1396 # A page does exist so increment the count to 1.
1402 if ($page_count ne 0){
1404 # The page does exist so set the error value.
1406 $error = "PageExists";
1411 # Check if certain values are undefined.
1419 if (!$page_description){
1421 $page_description = "";
1425 if (!$page_section){
1431 if (!$page_content){
1437 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1438 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1440 # Add the page to the selected database.
1442 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1443 \'' . $class->convert($page_filename) . '\',
1444 \'' . $class->convert($page_name) . '\',
1445 \'' . $class->convert($page_description) . '\',
1446 \'' . $class->convert($page_section) . '\',
1447 \'' . $class->convert($page_template) . '\',
1448 \'' . $class->convert($page_content) . '\',
1449 \'' . $class->convert($page_settings) . '\',
1450 \'' . $class->convert($page_date) . '\'
1451 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1452 $statement_handle->execute();
1454 undef $statement_handle;
1459 #################################################################################
1460 # deletepage: Delete a page from the selected database. #
1464 # $dbmodule->deletepage(options) #
1466 # options Specifies the following options in any order. #
1468 # PageFilename Specifies the page filename to delete. #
1469 #################################################################################
1474 # Get the data that was passed to the subroutine.
1477 my ($passedoptions) = @_;
1481 # Get the page filename.
1483 my $page_filename = $passedoptions->{"PageFilename"};
1485 # Check if the page exists before deleting it.
1487 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1488 $statement_handle->execute();
1490 while (@page_info = $statement_handle->fetchrow_array()){
1496 # Check if the page really does exist.
1500 $error = "PageDoesNotExist";
1507 $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1508 $statement_handle->execute();
1513 #################################################################################
1514 # editpage: Edit a page from the selected database. #
1518 # $dbmodule->editpage(options); #
1520 # options Specifies the following options in any order. #
1522 # PageFilename Specifies the filename to edit. #
1523 # PageNewFilename Specifies the new filename to use. #
1524 # PageNewName Specifies the new page name to use. #
1525 # PageNewDescription Specifies the new page description to use. #
1526 # PageNewSection Specifies the new page section to use. #
1527 # PageNewTemplate Specifies the new page template to use. #
1528 # PageNewContent Specifies the new page content to use. #
1529 # PageNewSettings Specifies the new page settings to use. #
1530 #################################################################################
1535 # Get the values passed to the subroutine.
1538 my ($passedoptions) = @_;
1542 # Get the data that was passed to the subroutine.
1544 my $page_filename = $passedoptions->{"PageFilename"};
1545 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1546 my $page_newname = $passedoptions->{"PageNewName"};
1547 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1548 my $page_newsection = $passedoptions->{"PageNewSection"};
1549 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1550 my $page_newcontent = $passedoptions->{"PageNewContent"};
1551 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1553 # Check if the page with the filename given exists.
1555 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1556 $statement_handle->execute();
1558 # Check if the page really does exist.
1560 while (@page_info = $statement_handle->fetchrow_array()){
1562 # The page information is found.
1568 # Check if the page really does exist.
1572 $error = "PageDoesNotExist";
1577 # Check if there is a page that already exists with the new
1582 if ($page_filename ne $page_newfilename){
1584 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1585 $statement_handle->execute();
1587 # Check if a page really is using the new filename.
1589 while (@page_info = $statement_handle->fetchrow_array()){
1591 # The page information is found.
1597 if ($page_found eq 1){
1599 $error = "PageExists";
1606 # Get the current date.
1608 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1609 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1611 # Edit the selected page.
1613 $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_pages SET filename = \'' . $class->convert($page_newfilename) . '\', pagename = \'' . $class->convert($page_newname) . '\', pagedescription = \'' . $class->convert($page_newdescription) . '\', pagesection = \'' . $class->convert($page_newsection) . '\', pagetemplate = \'' . $class->convert($page_newtemplate) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagesettings = \'' . $class->convert($page_newsettings) . '\', lastmodified = \'' . $page_date . '\' WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1614 $statement_handle->execute();
1619 #################################################################################
1620 # movepage: Moves a page from the old database to the new database. #
1624 # $dbmodule->movepage(options); #
1626 # options Specifies the following options in any order. #
1628 # PageFilename Specifies the page with the filename to move. #
1629 #################################################################################
1634 # Get the values passed to the subroutine.
1637 my ($passedoptions) = @_;
1639 my (%database_page, $database_page);
1640 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1644 # Get the data that was passed to the subroutine.
1646 my $page_filename = $passedoptions->{"PageFilename"};
1648 # Check if the page with the filename given exists.
1650 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1651 $statement_handle->execute();
1653 # Check if the page really does exist.
1655 while (@page_info = $statement_handle->fetchrow_array()){
1657 # Get the values from the array.
1659 $pagefilename = $page_info[0];
1660 $pagename = $page_info[1];
1661 $pagedescription = $page_info[2];
1662 $pagesection = $page_info[3];
1663 $pagetemplate = $page_info[4];
1664 $pagedata = $page_info[5];
1665 $pagesettings = $page_info[6];
1666 $pagelastmodified = $page_info[7];
1668 # Put the values into the page hash.
1671 "PageFilename" => $pagefilename,
1672 "PageName" => $pagename,
1673 "PageDescription" => $pagedescription,
1674 "PageSection" => $pagesection,
1675 "PageTemplate" => $pagetemplate,
1676 "PageContent" => $pagedata,
1677 "PageSettings" => $pagesettings,
1678 "PageLastModified" => $pagelastmodified,
1681 # The page information is found.
1687 # Check if the page really does exist.
1691 $error = "PageDoesNotExist";
1696 # Check if the page with the filename given already exists in
1697 # the database the page is being moved to.
1702 $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1703 $second_statement_handle->execute();
1705 while (@page_info = $second_statement_handle->fetchrow_array()){
1711 # Check if the page really does exist.
1715 $error = "PageAlreadyExists";
1720 # Add the page to the new database.
1722 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1723 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1724 \'' . $class->convert($database_page{"PageName"}) . '\',
1725 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1726 \'' . $class->convert($database_page{"PageSection"}) . '\',
1727 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1728 \'' . $class->convert($database_page{"PageContent"}) . '\',
1729 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1730 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1731 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1732 $second_statement_handle->execute();
1734 # Delete the page from the old database.
1736 $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1737 $statement_handle->execute();
1742 #################################################################################
1743 # copypage: Copies a page from the old database to the new database. #
1747 # $dbmodule->copypage(options); #
1749 # options Specifies the following options in any order. #
1751 # PageFilename Specifies the page with the filename to copy. #
1752 #################################################################################
1757 # Get the values passed to the subroutine.
1760 my ($passedoptions) = @_;
1762 my (%database_page, $database_page);
1763 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1767 # Get the data that was passed to the subroutine.
1769 my $page_filename = $passedoptions->{"PageFilename"};
1771 # Check if the page with the filename given exists.
1773 $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1774 $statement_handle->execute();
1776 # Check if the page really does exist.
1778 while (@page_info = $statement_handle->fetchrow_array()){
1780 # Get the values from the array.
1782 $pagefilename = $page_info[0];
1783 $pagename = $page_info[1];
1784 $pagedescription = $page_info[2];
1785 $pagesection = $page_info[3];
1786 $pagetemplate = $page_info[4];
1787 $pagedata = $page_info[5];
1788 $pagesettings = $page_info[6];
1789 $pagelastmodified = $page_info[7];
1791 # Put the values into the page hash.
1794 "PageFilename" => $pagefilename,
1795 "PageName" => $pagename,
1796 "PageDescription" => $pagedescription,
1797 "PageSection" => $pagesection,
1798 "PageTemplate" => $pagetemplate,
1799 "PageContent" => $pagedata,
1800 "PageSettings" => $pagesettings,
1801 "PageLastModified" => $pagelastmodified,
1804 # The page information is found.
1810 # Check if the page really does exist.
1814 $error = "PageDoesNotExist";
1819 # Check if the page with the filename given already exists in
1820 # the database the page is being moved to.
1825 $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1826 $second_statement_handle->execute();
1828 while (@page_info = $second_statement_handle->fetchrow_array()){
1834 # Check if the page really does exist.
1838 $error = "PageAlreadyExists";
1843 # Add the page to the new database.
1845 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1846 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1847 \'' . $class->convert($database_page{"PageName"}) . '\',
1848 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1849 \'' . $class->convert($database_page{"PageSection"}) . '\',
1850 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1851 \'' . $class->convert($database_page{"PageContent"}) . '\',
1852 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1853 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1854 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1855 $second_statement_handle->execute();
1859 #################################################################################
1860 # Filter subroutines. #
1861 #################################################################################
1864 #################################################################################
1865 # connectfilter: Connect to the filter database. #
1869 # $dbmodule->connectfilter(missingignore); #
1871 # missingignore Ignore error about database being missing. #
1872 #################################################################################
1878 my $ignoremissing = shift;
1880 # Check if the template database exists.
1882 my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1884 if ($filterdatabase_exists eq 1){
1886 $filterdb_exists = 0;
1888 if (!$ignoremissing){
1890 $error = "FilterDatabaseDoesNotExist";
1897 # Check if the permission settings for the template database are valid.
1899 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1901 if ($filterdb_permissions eq 1){
1903 # The template database has invalid permissions set
1904 # so return an error value.
1906 if (!$ignoremissing){
1908 $error = "FilterDatabaseInvalidPermissionsSet";
1915 # Connect to the template database.
1917 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1918 $filterdb_database_handle->{unicode} = 1;
1919 $filterdb_loaded = 1;
1923 sub disconnectfilter{
1924 #################################################################################
1925 # disconnectfilter: Disconnect from the filter database. #
1929 # $dbmodule->disconnectfilter(); #
1930 #################################################################################
1932 # Disconnect the template database.
1934 if ($filterdb_loaded eq 1){
1936 undef $filterdb_statement_handle;
1937 $filterdb_database_handle->disconnect();
1944 #################################################################################
1945 # getfiltercount: Gets the count of filters in the filters database. #
1949 # $dbmodule->getfiltercount(); #
1950 #################################################################################
1957 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_filters') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return);
1958 $filterdb_statement_handle->execute();
1960 my $count = $filterdb_statement_handle->fetchrow_array();
1967 #################################################################################
1968 # getfilterlist: Gets the list of filters in the filter database. #
1972 # $dbmodule->getfilterlist(options); #
1974 # StartFrom Specifies where the list of filters should start from. #
1975 # Limit Specifies the amount of the filters to get. #
1976 #################################################################################
1982 my ($passedoptions) = shift;
1987 my $start_from = $passedoptions->{"StartFrom"};
1988 my $limit = $passedoptions->{"Limit"};
1990 if (defined($start_from)){
1998 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC LIMIT ' . $start_from . ',' . $limit) or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1999 $filterdb_statement_handle->execute();
2003 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2004 $filterdb_statement_handle->execute();
2008 # Get the list of filters available.
2010 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2012 # Add the filter to the list of available filters.
2014 push(@filter_list, $filter_data[0]);
2018 undef $filterdb_statement_handle;
2019 return @filter_list;
2024 #################################################################################
2025 # getfilterinfo: Gets information about the filter. #
2029 # $dbmodule->getfilterinfo(options); #
2031 # options Specifies the following options in any order. #
2033 # FilterID Specifies the filter ID number to get information from. #
2034 # Reduced Specifies if the reduced version of the filter information #
2035 # should be retrieved. #
2036 #################################################################################
2041 # Get the values passed to the subroutine.
2044 my ($passedoptions) = @_;
2047 my $filter_exists = 0;
2050 # Get the values that are in the hash.
2052 my $filter_id = $passedoptions->{"FilterID"};
2053 my $reduced = $passedoptions->{"Reduced"};
2055 if ($reduced && $reduced eq 1){
2057 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2058 $filterdb_statement_handle->execute();
2062 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2063 $filterdb_statement_handle->execute();
2067 # Get the filter information.
2069 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2071 $filter_info{"FilterID"} = $filter_data[0];
2072 $filter_info{"FilterPriority"} = $filter_data[1];
2073 $filter_info{"FilterFind"} = $filter_data[2];
2074 $filter_info{"FilterReplace"} = $filter_data[3];
2075 $filter_info{"FilterEnabled"} = $filter_data[4];
2076 $filter_info{"FilterNotes"} = $filter_data[5];
2082 # Check if the filter exists.
2084 if (!$filter_exists){
2086 # The filter does not exist so return
2089 $error = "FilterDoesNotExist";
2094 # Return the filter information.
2096 undef $filterdb_statement_handle;
2097 return %filter_info;
2102 #################################################################################
2103 # addfilter: Adds a filter to the filter database. #
2107 # $dbmodule->addfilter(options); #
2109 # options Specifies the following options in any order. #
2111 # FindFilter Specifies the find filter to add. #
2112 # ReplaceFilter Specifies the replace filter to add. #
2113 # Priority Specifies the filter priority to use. #
2114 # Notes Specifies the notes to use. #
2115 #################################################################################
2120 # Get the values passed to the subroutine.
2123 my ($passedoptions) = @_;
2125 # Define some variables for later.
2127 my @database_filters;
2130 my $nofiltertable = 0;
2131 my $filter_found = 0;
2132 my $filter_count = 0;
2136 # Get the values from the hash.
2138 my $filter_find = $passedoptions->{"FindFilter"};
2139 my $filter_replace = $passedoptions->{"ReplaceFilter"};
2140 my $filter_priority = $passedoptions->{"Priority"};
2141 my $filter_enabled = $passedoptions->{"Enabled"};
2142 my $filter_notes = $passedoptions->{"Notes"};
2144 # Check if the filter database permissions are valid.
2146 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2147 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2149 if ($filterdb_permissions eq 1){
2151 if ($filterdb_exists eq 0){
2152 $error = "FilterDatabaseInvalidPermissionsSet";
2158 # Check if certain values are undefined and if they
2159 # are then set them blank.
2167 if (!$filter_replace){
2169 $filter_replace = "";
2173 if (!$filter_priority){
2175 $filter_priority = 1;
2179 if (!$filter_notes){
2185 if (!$filter_enabled){
2187 $filter_enabled = "";
2191 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2193 if ($directory_permissions eq 1 && $filterdb_exists){
2195 # The template database cannot be created because of invalid directory
2196 # permissions so return an error value.
2198 $error = "FilterDatabaseFileUncreateable";
2203 # Check if the filter table exists.
2205 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2207 # Check if there is really no filter table.
2209 if ($nofiltertable){
2211 # Create the filter database table.
2213 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2214 id int(7) primary key,
2216 findsetting varchar(1024),
2217 replacesetting varchar(1024),
2220 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2221 $filterdb_statement_handle->execute();
2225 # Find the lowest filter identification number available.
2227 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2228 $filterdb_statement_handle->execute();
2230 while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2232 $filter_id = $database_filters[0];
2234 # Add the filter identification to the list of filter IDs.
2236 push(@filterid_list, $filter_id);
2242 # Process each filter looking for a blank available filter.
2244 foreach $filter_id (@filterid_list){
2246 # Check the next filter ID to see if it's blank.
2248 $new_id = $filter_id + 1;
2250 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2251 $filterdb_statement_handle->execute();
2253 # Get the filter identification number.
2255 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2261 # Check if a filter was found.
2263 if (!$filter_found){
2265 # No filter was found using this ID so exit the loop.
2271 # Increment the filter count and reset the filter found value.
2279 # Check if there were any filters in the filter database.
2281 if (!$filter_count && !$new_id){
2283 # There were no filters in the filter database so set
2284 # the new filter identification value to 1.
2290 # Add the filter to the filter database.
2292 $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, enabled, notes) VALUES (
2293 \'' . $class->convert($new_id) . '\',
2294 \'' . $class->convert($filter_priority) . '\',
2295 \'' . $class->convert($filter_find) . '\',
2296 \'' . $class->convert($filter_replace) .'\',
2297 \'' . $class->convert($filter_enabled) . '\',
2298 \'' . $class->convert($filter_notes) . '\'
2299 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2300 $filterdb_statement_handle->execute();
2305 #################################################################################
2306 # editfilter: Edits a filter in the filter database. #
2310 # $dbmodule->editfilter(options); #
2312 # options Specifies the following options in any order. #
2314 # FilterID Specifies the filter to edit. #
2315 # NewFindFilter Specifies the new find filter setting. #
2316 # NewReplaceFilter Specifies the new replace filter setting. #
2317 # NewFilterPriority Specifies the new filter priority setting. #
2318 # NewEnabled Specifies if the filter should be enabled. #
2319 # NewFilterNotes Specifies the new notes for the filter. #
2320 #################################################################################
2325 # Get the values passed to the subroutine.
2328 my ($passedoptions) = @_;
2331 my $filter_exists = 1;
2334 # Get the values from the hash.
2336 my $filter_id = $passedoptions->{"FilterID"};
2337 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2338 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2339 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2340 my $filter_enabled = $passedoptions->{"NewEnabled"};
2341 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2343 # Check if the filter database permissions are valid.
2345 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2346 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2348 if ($filterdb_permissions eq 1){
2350 if ($filterdb_exists eq 0){
2351 $error = "FilterDatabaseInvalidPermissionsSet";
2357 # Check if the filter exists before editing it.
2359 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2360 $filterdb_statement_handle->execute();
2362 # Check if the filter exists.
2364 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2370 # Check if the filter really does exist.
2372 if (!$filter_exists){
2374 # The filter does not exist so return
2377 $error = "FilterDoesNotExist";
2382 # Edit the selected filter.
2384 $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2385 findsetting = \'' . $class->convert($filter_newfind) . '\',
2386 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2387 priority = \'' . $class->convert($filter_newpriority) . '\',
2388 enabled = \'' . $class->convert($filter_enabled) . '\',
2389 notes = \'' . $class->convert($filter_newnotes) . '\'
2390 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2391 $filterdb_statement_handle->execute();
2393 undef $filterdb_statement_handle;
2399 #################################################################################
2400 # deletefilter: Deletes a filter from the filter database. #
2404 # $dbmodule->deletefilter(options); #
2406 # options Specifies the following options in any order. #
2408 # FilterID Specifies the filter to delete from the filter database. #
2409 #################################################################################
2414 # Get the values passed to the subroutine.
2417 my ($passedoptions) = @_;
2419 my $filter_exists = 0;
2422 # Get the values from the hash.
2424 my $filter_id = $passedoptions->{"FilterID"};
2426 # Check if the filter exists before deleting.
2428 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2429 $filterdb_statement_handle->execute();
2431 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2437 # Check to see if the filter really does exist.
2439 if (!$filter_exists){
2441 $error = "FilterDoesNotExist";
2446 # Delete the filter from the filter database.
2448 $filterdb_statement_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2449 $filterdb_statement_handle->execute();
2451 undef $filterdb_statement_handle;
2455 #################################################################################
2456 # Template subroutines. #
2457 #################################################################################
2459 sub connecttemplate{
2460 #################################################################################
2461 # connecttemplate: Connect to the template database. #
2465 # $dbmodule->connecttemplate(missingignore); #
2467 # missingignore Ignore errror about database being missing. #
2468 #################################################################################
2474 my $ignoremissing = shift;
2476 # Check if the template database exists.
2478 my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2480 if ($templatedatabase_exists eq 1){
2482 $templatedb_exists = 0;
2484 if (!$ignoremissing){
2486 $error = "TemplateDatabaseDoesNotExist";
2493 # Check if the permission settings for the template database are valid.
2495 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2497 if ($templatedb_permissions eq 1){
2499 # The template database has invalid permissions set
2500 # so return an error value.
2502 if (!$ignoremissing){
2504 $error = "TemplateDatabaseInvalidPermissionsSet";
2511 # Connect to the template database.
2513 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2514 $template_database_handle->{unicode} = 1;
2515 $templatedb_loaded = 1;
2519 sub disconnecttemplate{
2520 #################################################################################
2521 # disconnecttemplate: Disconnect from the template database. #
2525 # $dbmodule->disconnecttemplate(); #
2526 #################################################################################
2528 # Disconnect the template database.
2530 if ($templatedb_loaded eq 1){
2532 undef $template_statement_handle;
2533 $template_database_handle->disconnect();
2539 sub gettemplatecount{
2540 #################################################################################
2541 # gettemplatecount: Gets the count of templates in the template database. #
2545 # $dbmodule->gettemplatecount(); #
2546 #################################################################################
2553 $template_statement_handle = $template_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_templates') or ( $error = "FilterDatabaseError", $errorext = $template_database_handle->errstr, return);
2554 $template_statement_handle->execute();
2556 my $count = $template_statement_handle->fetchrow_array();
2562 sub gettemplatelist{
2563 #################################################################################
2564 # gettemplatelist: Gets the list of templates. #
2568 # $dbmodule->gettemplatelist(options); #
2570 # options Specifies the following options as a hash (in any order). #
2572 # StartFrom Specifies where the list of templates will start from. #
2573 # Limit Specifies how many templates should be retrieved. #
2574 #################################################################################
2580 my ($passedoptions) = @_;
2582 my $start_from = $passedoptions->{"StartFrom"};
2583 my $limit = $passedoptions->{"Limit"};
2585 if (defined($start_from)){
2593 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC LIMIT ' . $start_from . ',' . $limit ) or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2594 $template_statement_handle->execute();
2598 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2599 $template_statement_handle->execute();
2603 my @database_template;
2605 my $template_filename;
2607 while (@database_template = $template_statement_handle->fetchrow_array()){
2609 # Get certain values from the array.
2611 $template_filename = $database_template[0];
2613 # Add the template to the list of templates.
2615 push(@templates_list, $template_filename);
2619 return @templates_list;
2623 sub gettemplateinfo{
2624 #################################################################################
2625 # gettemplateinfo: Get information on a template. #
2629 # $dbmodule->gettemplateinfo(options); #
2631 # options Specifies the following options in any order. #
2633 # TemplateFilename Specifies the template filename to use. #
2634 # Reduced Specifies a reduced version of template information to #
2636 #################################################################################
2641 # Get the data passed to the subroutine.
2644 my ($passedoptions) = @_;
2649 my $template_filename;
2651 my $template_description;
2652 my $template_datemodified;
2653 my $template_layout;
2655 my $template_found = 0;
2657 my $filename = $passedoptions->{"TemplateFilename"};
2658 my $reduced = $passedoptions->{"Reduced"};
2660 if ($reduced && $reduced eq 1){
2662 $template_statement_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2663 $template_statement_handle->execute();
2667 $template_statement_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2668 $template_statement_handle->execute();
2672 while (@template_data = $template_statement_handle->fetchrow_array()){
2674 # Get certain values from the array.
2676 $template_filename = $template_data[0];
2677 $template_name = $template_data[1];
2678 $template_description = $template_data[2];
2679 $template_layout = $template_data[3];
2680 $template_datemodified = $template_data[4];
2682 # Process them into the hash.
2685 "TemplateFilename" => $template_filename,
2686 "TemplateName" => $template_name,
2687 "TemplateDescription" => $template_description,
2688 "TemplateLayout" => $template_layout,
2689 "TemplateLastModified" => $template_datemodified
2692 $template_found = 1;
2696 if ($template_found eq 0){
2698 # The template was not found in the template database so
2699 # write an error value.
2701 $error = "TemplateDoesNotExist";
2711 #################################################################################
2712 # addtemplate: Adds a template to the template database. #
2716 # $dbmodule->addtemplate(); #
2718 # options Specifies the following options in any order. #
2720 # TemplateFilename Specifies the new template filename. #
2721 # TemplateName Specifies the new template name. #
2722 # TemplateDescription Specifies the new template description. #
2723 # TemplateLayout Specifies the new template layout. #
2724 #################################################################################
2729 # Get the data passed to the subroutine.
2732 my ($passedoptions) = @_;
2735 my $notemplatetable;
2738 my $template_filename = $passedoptions->{"TemplateFilename"};
2739 my $template_name = $passedoptions->{"TemplateName"};
2740 my $template_description = $passedoptions->{"TemplateDescription"};
2741 my $template_layout = $passedoptions->{"TemplateLayout"};
2743 # Check if the template database permissions are valid.
2745 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2746 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2748 if ($templatedb_permissions eq 1){
2750 if ($templatedb_exists eq 0){
2751 $error = "TemplateDatabaseInvalidPermissionsSet";
2757 # Check if the template already exists before adding.
2759 if ($templatedb_exists eq 0){
2761 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2763 if ($blankfile eq 0){
2765 $template_statement_handle->execute();
2767 while (@page_exists = $template_statement_handle->fetchrow_array()){
2769 $error = "TemplatePageExists";
2778 # Get the current date.
2780 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2782 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2784 # Check if certain values are undefined and if they
2785 # are then set them blank.
2787 if (!$template_name){
2789 $template_name = "";
2793 if (!$template_description){
2795 $template_description = "";
2799 if (!$template_layout){
2801 $template_layout = "";
2805 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2807 if ($directory_permissions eq 1 && $templatedb_exists){
2809 # The template database cannot be created because of invalid directory
2810 # permissions so return an error value.
2812 $error = "TemplateDatabaseUncreateable";
2817 # Check to see if a template can be added.
2819 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2820 \'' . $class->convert($template_filename) . '\',
2821 \'' . $class->convert($template_name) . '\',
2822 \'' . $class->convert($template_description) . '\',
2823 \'' . $class->convert($template_layout) . '\',
2824 \'' . $class->convert($template_date) . '\'
2825 )') or ( $notemplatetable = 1 );
2827 if (!$notemplatetable){
2829 $template_statement_handle->execute();
2833 # Check to see if there is no template table and attempt to create one.
2835 if ($notemplatetable){
2837 # Create a template table.
2839 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2841 if ($directory_permissions eq 1){
2843 # The template database cannot be created because of invalid directory
2844 # permissions so return an error.
2846 $error = "TemplateDatabaseFileUncreateable";
2851 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2852 filename varchar(256) primary key,
2853 templatename varchar(512),
2854 templatedescription varchar(512),
2855 templatelayout text,
2856 datemodified datetime
2857 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2858 $template_statement_handle->execute();
2860 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2861 \'' . $class->convert($template_filename) . '\',
2862 \'' . $class->convert($template_name) . '\',
2863 \'' . $class->convert($template_description) . '\',
2864 \'' . $class->convert($template_layout) . '\',
2865 \'' . $class->convert($template_date) . '\'
2866 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2867 $template_statement_handle->execute();
2874 #################################################################################
2875 # deletetemplate: Deletes a template from the template database. #
2879 # $dbmodule->deletetemplate(options); #
2881 # options Specifies the following options in any order. #
2883 # TemplateFilename Specifies the template filename to delete. #
2884 #################################################################################
2889 # Get the data passed to the subroutine.
2892 my ($passedoptions) = @_;
2895 my $template_filename = $passedoptions->{"TemplateFilename"};
2896 my $template_count = 0;
2898 # Check if the template exists.
2900 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2901 $template_statement_handle->execute();
2903 while (@pagedata = $template_statement_handle->fetchrow_array()){
2909 if ($template_count eq 0){
2911 # No pages were returned so return an error value.
2913 $error = "TemplateDoesNotExist";
2918 # Delete the template from the template database.
2920 $template_statement_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2921 $template_statement_handle->execute();
2926 #################################################################################
2927 # editttemplate: Edits a Kiriwrite template. #
2931 # $dbmodule->edittemplate(options); #
2933 # options Specifies the following options in any order. #
2935 # TemplateFilename Specifies the template filename to edit. #
2936 # NewTemplateFilename Specifies the new template filename. #
2937 # NewTemplateName Specifies the new template name. #
2938 # NewTemplateDescription Specifies the new template description. #
2939 # NewTemplateLayout Specifies the new template layout. #
2940 #################################################################################
2942 # Get the values passed.
2945 my ($passedoptions) = @_;
2946 my $template_found = 0;
2949 # Process the values passed.
2951 my $template_filename = $passedoptions->{"TemplateFilename"};
2952 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2953 my $new_template_name = $passedoptions->{"NewTemplateName"};
2954 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2955 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2957 # Check if the template database permissions are valid.
2959 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2960 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2962 if ($templatedb_permissions eq 1){
2964 if ($templatedb_exists eq 0){
2965 $error = "TemplateDatabaseInvalidPermissionsSet";
2971 # Check if the template exists.
2973 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2974 $template_statement_handle->execute();
2976 while (@template_info = $template_statement_handle->fetchrow_array()){
2978 $template_found = 1;
2982 # Check to see if the template was found and set an error value if
2985 if ($template_found eq 0){
2987 $error = "TemplateDoesNotExist";
2992 # Get the date and time.
2994 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2995 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2997 # Update the template information.
2999 $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
3000 filename = \'' . $class->convert($new_template_filename) . '\',
3001 templatename = \'' . $class->convert($new_template_name) . '\',
3002 templatedescription = \'' . $class->convert($new_template_description) . '\',
3003 templatelayout = \'' . $class->convert($new_template_layout) . '\',
3004 datemodified = \'' . $class->convert($templatenewdate) . '\'
3005 WHERE filename = \'' . $class->convert($template_filename) . '\'
3006 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
3007 $template_statement_handle->execute();
3011 #################################################################################
3012 # General subroutines. #
3013 #################################################################################
3016 #################################################################################
3017 # connect: Connect to the server. #
3021 # $dbmodule->connect(); #
3022 #################################################################################
3024 # This function is not needed in this database module.
3029 #################################################################################
3030 # connect: Disconnect from the server. #
3034 # $dbmodule->disconnect(); #
3035 #################################################################################
3037 # This function is not needed in this database module.
3039 undef $statement_handle;