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 Modules::Database::SQLite;
26 # Enable strict and use warnings.
28 use Modules::System::Common;
32 # Load the following Perl modules.
34 use DBI qw(:sql_types);
36 # Set the following values.
38 our $VERSION = "0.2.0";
39 my ($options, %options);
44 my $database_filename;
45 my $second_database_filename;
46 my $second_database_handle;
47 my $second_statement_handle;
48 my $templatedb_loaded = 0;
49 my $templatedb_exists = 1;
50 my $template_statement_handle;
51 my $template_database_handle;
52 my $filterdb_loaded = 0;
53 my $filterdb_exists = 1;
54 my $filterdb_statement_handle;
55 my $filterdb_database_handle;
58 #################################################################################
59 # Generic Subroutines. #
60 #################################################################################
63 #################################################################################
64 # new: Create an instance of Kiriwrite::Database::SQLite #
68 # $dbmodule = Kiriwrite::Database::SQLite->new(); #
69 #################################################################################
71 # Get the perl module name.
76 return bless($self, $class);
81 #################################################################################
82 # loadsettings: Loads settings into the SQLite database module #
86 # $dbmodule->loadsettings(Directory, options); #
88 # options Specifies the following options (in any order). #
90 # Directory Specifies the directory to use for getting databases. #
91 # DateTime Specifies the date and time format to use. #
92 # Server Specifies the server to use. #
93 # Database Specifies the database to use. #
94 # Username Specifies the username to use. #
95 # Password Specifies the password to use. #
96 # HashType Specifies the password hash type to use. #
97 # Port Specifies the server port to use. #
98 # Protocol Specifies the protocol to use. #
99 # TablePrefix Specifies the table prefix to use. #
100 #################################################################################
102 # Get the data passed to the subroutine.
105 my ($passedoptions) = @_;
107 # Add the directory setting to the list of options (as it's the only
108 # one needed for this database module).
111 "Directory" => $passedoptions->{"Directory"},
112 "DateTime" => $passedoptions->{"DateTime"},
118 #################################################################################
119 # convert: Converts data into SQL formatted data. #
123 # $dbmodule->convert(data); #
125 # data Specifies the data to convert. #
126 #################################################################################
128 # Get the data passed to the subroutine.
145 #################################################################################
146 # dateconvert: Converts a SQL date into a proper date. #
150 # $dbmodule->dateconvert(date); #
152 # date Specifies the date to convert. #
153 #################################################################################
155 # Get the date passed to the subroutine.
160 # Convert the date given into the proper date.
162 # Create the following varialbes to be used later.
189 # Split the date and time.
191 $length = length($data);
197 # Get the character and check if it is a space.
199 $char = substr($data, $seek, 1);
203 # The character is a space, so get the date and time.
205 $date = substr($data, 0, $seek);
206 $timelength = $length - $seek - 1;
207 $time = substr($data, $seek + 1, $timelength);
213 } until ($seek eq $length);
215 # Get the year, month and date.
217 $length = length($date);
222 # Get the character and check if it is a dash.
224 $char = substr($date, $seek, 1);
228 # The character is a dash, so get the year, month or day.
230 $datelength = $seek - $startchar;
234 # Get the year from the date.
236 $year = substr($date, 0, $datelength) + 1900;
240 # Get the last two characters to get the short year
243 $year_short = substr($year, 2, 2);
245 } elsif ($count eq 1){
247 # Get the month and day from the date.
249 $month = substr($date, $startchar + 1, $datelength - 1) + 1;
251 # Check if the month is less then 10, if it is
252 # add a zero to the value.
256 $month_full = '0' . $month;
260 $month_full = $month;
267 $daylength = $length - $seek + 1;
268 $day = substr($date, $startchar + 1, $daylength);
270 # Check if the day is less than 10, if it is
271 # add a zero to the value.
275 $day_full = '0' . $day;
289 } until ($seek eq $length);
291 # Get the length of the time value and reset certain
294 $length = length($time);
301 # Get the character and check if it is a colon.
303 $char = substr($time, $seek, 1);
307 # The character is a colon, so get the hour, minute and day.
309 $timelength = $seek - $startchar;
313 # Get the hour from the time.
315 $hour = substr($time, 0, $timelength);
319 # If the hour is less than ten then add a
324 $hour_full = '0' . $hour;
332 } elsif ($count eq 1){
334 # Get the minute and second from the time.
336 $minute = substr($time, $startchar + 1, $timelength - 1);
339 # If the minute is less than ten then add a
344 $minute_full = '0' . $minute;
348 $minute_full = $minute;
354 $secondlength = $length - $seek + 1;
355 $second = substr($time, $startchar + 1, $secondlength);
357 # If the second is less than ten then add a
362 $second_full = '0' . $second;
366 $second_full = $second;
376 } until ($seek eq $length);
378 # Get the setting for displaying the date and time.
380 $data = $options{"DateTime"};
382 # Process the setting for displaying the date and time
383 # using regular expressions
385 $data =~ s/DD/$day_full/g;
387 $data =~ s/MM/$month_full/g;
388 $data =~ s/M/$month/g;
389 $data =~ s/YY/$year/g;
390 $data =~ s/Y/$year_short/g;
392 $data =~ s/hh/$hour_full/g;
393 $data =~ s/h/$hour/g;
394 $data =~ s/mm/$minute_full/g;
395 $data =~ s/m/$minute/g;
396 $data =~ s/ss/$second_full/g;
397 $data =~ s/s/$second/g;
406 #################################################################################
407 # geterror: Gets the error message (or extended error message). #
411 # $dbmodule->geterror(extended); #
413 # Extended Specifies if the extended error should be retrieved. #
414 #################################################################################
416 # Get the data passed to the subroutine.
419 my $extended = shift;
433 # Check to see if extended information should be returned.
437 # Extended information should be returned.
443 # Basic information should be returned.
452 #################################################################################
453 # dbpermissions: Check if the permissions for the database are valid. #
457 # $database->dbpermissions(dbname, read, write); #
459 # dbname Specifies the database name to check. #
460 # read Check to see if the database can be read. #
461 # write Check to see if the database can be written. #
462 #################################################################################
464 # Get the database name, read setting and write setting.
466 my ($class, $dbname, $readper, $writeper) = @_;
468 # Check if the database can be read.
472 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
474 # The database can be read.
478 # The database cannot be read, so return a value
487 # Check if the database can be written.
491 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
493 # The database can be read.
497 # The database cannot be read, so return a value
506 # No errors have occured while checking so return a value
514 #################################################################################
515 # dbexists: Check if the database exists. #
519 # $dbmodule->dbexists(dbname); #
521 # dbname Specifies the database name to check. #
522 #################################################################################
524 # Get the value that was passed to the subroutine.
529 # Check if the filename exists, if it does, return a value of 1, else
530 # return a value of 0, meaning that the file was not found.
532 if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
534 # Specified file does exist so return a value of 0.
540 # Specified file does not exist so return a value of 1.
548 #################################################################################
549 # Database Subroutines. #
550 #################################################################################
553 #################################################################################
554 # getdblist: Gets the list of available databases. #
558 # $dbmodule->getdblist(); #
559 #################################################################################
561 # Get the list of databases.
564 my @data_directory_final;
566 my $database_filename_length;
567 my $database_filename_friendly;
569 # Check if the database directory has valid permission settings.
571 if (-e $options{"Directory"}){
573 # The database directory does exist. So check if
574 # the permission settings are valid.
576 if (-r $options{"Directory"}){
578 # The permission settings for reading the directory
583 # The permission settings for reading the directory
584 # are invalid so return an error value.
586 $error = "DataDirInvalidPermissions";
593 # The database directory does not exist, so return an
596 $error = "DataDirMissing";
601 opendir(DATADIR, $options{"Directory"});
602 @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
605 # Process the list of databases.
607 foreach $database (@data_directory){
609 $database =~ s/.db.sqlite$//og;
610 $database_filename_friendly = $database;
612 #$database_filename_length = length($database);
613 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
614 push(@data_directory_final, $database_filename_friendly);
618 # Return the list of databases.
620 return @data_directory_final;
625 #################################################################################
626 # getdatabaseinfo: Get information about the database. #
630 # $dbmodule->getdatabaseinfo(); #
631 #################################################################################
633 # Get the database information.
636 my ($databaseinfo, %databaseinfo);
637 my ($sqldata, @sqldata);
642 $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 (
643 $error = "DatabaseError", $errorext = $database_handle->errstr, return
645 $statement_handle->execute();
647 @sqldata = $statement_handle->fetchrow_array();
649 # Process the database information into a hash.
652 "DatabaseName" => $sqldata[0],
653 "Description" => $sqldata[1],
654 "Notes" => $sqldata[2],
655 "Categories" => $sqldata[3],
656 "Major" => $sqldata[4],
657 "Minor" => $sqldata[5],
658 "Revision" => $sqldata[6]
661 $statement_handle->finish();
662 undef $statement_handle;
664 return %databaseinfo;
668 sub getseconddatabaseinfo{
669 #################################################################################
670 # getseconddatabaseinfo: Get information about the database that pages will be #
671 # moved or copied to. #
675 # $dbmodule->getseconddatabaseinfo(); #
676 #################################################################################
678 # Get the database information.
681 my ($databaseinfo, %databaseinfo);
682 my ($sqldata, @sqldata);
687 $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 (
688 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
690 $second_statement_handle->execute();
692 @sqldata = $second_statement_handle->fetchrow_array();
694 # Process the database information into a hash.
697 "DatabaseName" => $sqldata[0],
698 "Description" => $sqldata[1],
699 "Notes" => $sqldata[2],
700 "Categories" => $sqldata[3],
701 "Major" => $sqldata[4],
702 "Minor" => $sqldata[5],
703 "Revision" => $sqldata[6]
706 $second_statement_handle->finish();
707 undef $second_statement_handle;
709 return %databaseinfo;
714 #################################################################################
715 # adddatabase: Adds a Kiriwrite database. #
719 # $dbmodule->adddatabase(options); #
721 # options Specifies the following options in any order. #
723 # DatabaseFilename Specifies the database file/shortname to use. #
724 # DatabaseName Specifies the database name to use. #
725 # DatabaseDescription Specifies the database description to use. #
726 # DatabaseNotes Specifies the database notes to use. #
727 # DatabaseCategories Specifies the database categories to use. #
728 # VersionMajor Specifies the major version. #
729 # VersionMinor Specifies the minor version. #
730 # VersionRevision Specifies the revision version. #
731 #################################################################################
733 # Get the database that was passed to the subroutine.
739 my ($passedoptions) = @_;
741 my $dbfilename = $passedoptions->{"DatabaseFilename"};
742 my $dbname = $passedoptions->{"DatabaseName"};
743 my $dbdescription = $passedoptions->{"DatabaseDescription"};
744 my $dbnotes = $passedoptions->{"DatabaseNotes"};
745 my $dbcategories = $passedoptions->{"DatabaseCategories"};
746 my $dbmajorver = $passedoptions->{"VersionMajor"};
747 my $dbminorver = $passedoptions->{"VersionMinor"};
748 my $dbrevisionver = $passedoptions->{"VersionRevision"};
750 # Check if the database with the filename given already exists.
752 my $database_exists = $class->dbexists($dbfilename);
754 if ($database_exists eq 0){
756 # The database filename exists so set the error value.
758 $error = "DatabaseExists";
763 # Create the database structure.
765 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
766 $database_handle->{unicode} = 1;
767 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
768 name varchar(256) primary key,
769 description varchar(512),
771 categories varchar(512),
772 kiriwrite_version_major int(4),
773 kiriwrite_version_minor int(4),
774 kiriwrite_version_revision int(4)
775 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
776 $statement_handle->execute();
778 $statement_handle = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
779 filename varchar(256) primary key,
780 pagename varchar(512),
781 pagedescription varchar(512),
782 pagesection varchar(256),
783 pagetemplate varchar(64),
786 lastmodified datetime
787 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
788 $statement_handle->execute();
790 # Convert the values into SQL query formatted values and add an entry
791 # to the kiriwrite_database_info table.
793 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
794 \'' . $class->convert($dbname) . '\',
795 \'' . $class->convert($dbdescription) . '\',
796 \'' . $class->convert($dbnotes) . '\',
797 \'' . $class->convert($dbcategories) . '\',
798 \'' . $class->convert($dbmajorver) . '\',
799 \'' . $class->convert($dbminorver) . '\',
800 \'' . $class->convert($dbrevisionver) . '\'
801 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
802 $statement_handle->execute();
807 #################################################################################
808 # editdatabase: Edits a Kiriwrite Database. #
812 # $dbmodule->editdatabase(options); #
814 # options Specifies the following options in any order. #
816 # NewDatabaseFilename Specifies the new database filename to use. #
817 # DatabaseName Specifies the new database name. #
818 # DatabaseDescription Specifies the new database description. #
819 # DatabaseNotes Specifies the new database notes. #
820 # DatabaseCategories Specifies the new database categories. #
821 #################################################################################
827 my ($passedoptions) = @_;
829 my $dbnewfilename = $passedoptions->{"DatabaseNewFilename"};
830 my $dbname = $passedoptions->{"DatabaseName"};
831 my $dbdescription = $passedoptions->{"DatabaseDescription"};
832 my $dbnotes = $passedoptions->{"DatabaseNotes"};
833 my $dbcategories = $passedoptions->{"DatabaseCategories"};
835 # Check if a new database filename has been specified and if a
836 # new database filename has been specified then change the
839 if ($database_filename ne $dbnewfilename){
841 # A new database filename has been given so check if the output
842 # directory has write access.
844 if (-r $options{"Directory"}){
846 # The directory is readable.
850 # The directory is not readable so set the error value.
852 $error = "DataDirInvalidPermissions";
858 if (-w $options{"Directory"}){
860 # The directory is writeable.
864 # The directory is not writeable so set the error value.
866 $error = "DataDirInvalidPermissions";
872 # Check if a database filename already exists before using the
875 my $database_newexists = $class->dbexists($dbnewfilename);
877 if ($database_newexists eq 0){
879 # The database filename exists so set the error value.
881 $error = "DatabaseExists";
886 # Check if the database can be renamed (has write access).
888 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
890 if ($database_permissions eq 1){
892 # The database filename exists so set the error value.
894 $error = "InvalidPermissionsSet";
899 # "Disconnect" from the database.
901 $database_handle->disconnect();
903 # Rename the database.
905 ($database_filename) = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
906 ($dbnewfilename) = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
908 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
910 # Reload the database from the new filename.
912 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
913 $database_handle->{unicode} = 1;
914 $database_filename = $dbnewfilename;
918 # Check if the database can be altered with the new data.
920 my $database_permissions = $class->dbpermissions($database_filename, 1, 1);
922 if ($database_permissions eq 1){
924 # The database filename exists so set the error value.
926 $error = "InvalidPermissionsSet";
931 # Get the current database information.
933 $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
934 $statement_handle->execute();
936 my @database_oldinfo = $statement_handle->fetchrow_array();
938 my $dboldname = $database_oldinfo[0];
940 # Update the database information.
942 $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
943 description = \'' . $class->convert($dbdescription) . '\',
944 notes = \'' . $class->convert($dbnotes) . '\',
945 categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
946 $statement_handle->execute();
948 undef $statement_handle;
954 #################################################################################
955 # deletedatabase: Deletes a Kiriwrite database. #
959 # $dbmodule->deletedatabase(options); #
961 # options Specifies the following options in any order. #
963 # DatabaseName Specifies the Kiriwrite database to delete. #
964 #################################################################################
969 # Get the database filename.
972 my ($passedoptions) = shift;
974 my $databasename = $passedoptions->{"DatabaseName"};
976 # Check if the database exists.
978 my $database_exists = $class->dbexists($databasename);
980 if ($database_exists eq 1){
982 # The database does not exist so set the error value.
984 $error = "DoesNotExist";
989 # Check if the database permissions are valid.
991 my $database_permissions = $class->dbpermissions($databasename);
993 if ($database_permissions eq 1){
995 # The database permissions are invalid so set the error
998 $error = "InvalidPermissionsSet";
1003 # Delete the database.
1005 ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1007 unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1012 #################################################################################
1013 # selectdb: Selects the Kiriwrite database. #
1017 # $dbmodule->connect(options); #
1019 # options Specifies the following options in any order. #
1021 # DatabaseName Specifies the Kiriwrite database to use. #
1022 #################################################################################
1024 # Get the database name.
1030 my ($passedoptions) = @_;
1031 my (%database, $database);
1033 my $dbname = $passedoptions->{"DatabaseName"};
1035 # Check if the database exists.
1037 my $database_exists = $class->dbexists($dbname);
1039 if ($database_exists eq 1){
1041 # The database does not exist so return an error value
1042 # saying that the database does not exist.
1044 $error = "DoesNotExist";
1050 # Check if the database has valid permissions set.
1052 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1054 if ($database_permissions eq 1){
1056 # The database has invalid permissions set so return
1057 # an error value saying that the database has invalid
1060 $error = "InvalidPermissionsSet";
1066 # Connect to the database.
1068 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1069 $database_handle->{unicode} = 1;
1070 $database_filename = $dbname;
1075 #################################################################################
1076 # selectseconddb: Selects a second Kiriwrite database for moving and copying #
1081 # $dbmodule->selectseconddb(options); #
1083 # options Specifies the following options in any order. #
1085 # DatabaseName Specifies the Kiriwrite database to use. #
1086 #################################################################################
1088 # Get the database name.
1094 my ($passedoptions) = @_;
1095 my (%database, $database);
1097 my $dbname = $passedoptions->{"DatabaseName"};
1099 # Check if the database exists.
1101 my $database_exists = $class->dbexists($dbname);
1103 if ($database_exists eq 1){
1105 # The database does not exist so return an error value
1106 # saying that the database does not exist.
1108 $error = "DoesNotExist";
1114 # Check if the database has valid permissions set.
1116 my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1118 if ($database_permissions eq 1){
1120 # The database has invalid permissions set so return
1121 # an error value saying that the database has invalid
1124 $error = "InvalidPermissionsSet";
1130 # Connect to the database.
1132 $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1133 $second_database_handle->{unicode} = 1;
1134 $second_database_filename = $dbname;
1138 #################################################################################
1139 # Page subroutines. #
1140 #################################################################################
1143 #################################################################################
1144 # getpagelist: Gets the list of pages from the database. #
1148 # $dbmodule->getpagelist(options); #
1150 # options Specifies the following options in any order. #
1152 # StartFrom Start from the specified page in the database. #
1153 # Limit Get the amount of pages given. #
1154 #################################################################################
1160 my ($passedoptions) = shift;
1162 my $start_from = $passedoptions->{"StartFrom"};
1163 my $limit = $passedoptions->{"Limit"};
1165 if (defined($start_from)){
1173 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1174 $statement_handle->execute();
1178 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1179 $statement_handle->execute();
1183 my @database_pagefilenames;
1184 my @database_pagefilenames_final;
1186 # Process the collected pages.
1188 while (@database_pagefilenames = $statement_handle->fetchrow_array){
1190 # Add each page to the list of pages in the database.
1192 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1196 undef $statement_handle;
1197 return @database_pagefilenames_final;
1202 #################################################################################
1203 # getpagecount: Get the count of pages that are in the database. #
1207 # $dbmodule->getpagecount(); #
1208 #################################################################################
1215 $statement_handle = $database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1216 $statement_handle->execute();
1218 my $count = $statement_handle->fetchrow_array();
1225 #################################################################################
1226 # getpageinfo: Gets the page information from the filename passed. #
1230 # $dbmodule->getpageinfo(options); #
1232 # options Specifies the following options in any order. #
1234 # PageFilename Specifies the page filename to get the page information from. #
1235 # Reduced Specifies if the reduced version of the page information should #
1237 #################################################################################
1243 my ($passedoptions) = shift;
1244 my (%database_page, $database_page);
1245 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1250 # Get the page from the database.
1252 my $page_filename = $passedoptions->{"PageFilename"};
1253 my $page_reduced = $passedoptions->{"Reduced"};
1255 if (!$page_reduced){
1261 if ($page_reduced eq 1){
1263 $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 );
1265 $statement_handle->execute();
1267 # Check if the page exists in the database.
1269 while (@data_page = $statement_handle->fetchrow_array()){
1271 # Get the values from the array.
1273 $pagefilename = $data_page[0];
1274 $pagename = $data_page[1];
1275 $pagedescription = $data_page[2];
1276 $pagelastmodified = $data_page[3];
1278 # Put the values into the page hash.
1281 "PageFilename" => $pagefilename,
1282 "PageName" => $pagename,
1283 "PageDescription" => $pagedescription,
1284 "PageLastModified" => $class->dateconvert($pagelastmodified),
1294 $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 );
1296 $statement_handle->execute();
1298 # Check if the page exists in the database.
1300 while (@data_page = $statement_handle->fetchrow_array()){
1302 # Get the values from the array.
1304 $pagefilename = $data_page[0];
1305 $pagename = $data_page[1];
1306 $pagedescription = $data_page[2];
1307 $pagesection = $data_page[3];
1308 $pagetemplate = $data_page[4];
1309 $pagedata = $data_page[5];
1310 $pagesettings = $data_page[6];
1311 $pagelastmodified = $data_page[7];
1313 # Put the values into the page hash.
1316 "PageFilename" => $pagefilename,
1317 "PageName" => $pagename,
1318 "PageDescription" => $pagedescription,
1319 "PageSection" => $pagesection,
1320 "PageTemplate" => $pagetemplate,
1321 "PageContent" => $pagedata,
1322 "PageSettings" => $pagesettings,
1323 "PageLastModified" => $class->dateconvert($pagelastmodified),
1332 # Check if the page did exist.
1336 $error = "PageDoesNotExist";
1341 undef $statement_handle;
1342 return %database_page;
1347 #################################################################################
1348 # addpage: Add a page to the selected database. #
1352 # $dbmodule->addpage(options); #
1354 # options Specifies the following options in any order. #
1356 # PageFilename Specifies the page filename to use. #
1357 # PageName Specifies the page name to use. #
1358 # PageDescription Specifies the page description to use. #
1359 # PageSection Specifies the page section to use. #
1360 # PageTemplate Specifies the page template to use. #
1361 # PageContent Specifies the page content to use. #
1362 # PageSettings Specifies the page settings to use. #
1363 #################################################################################
1365 # Get the data that was passed to the subroutine.
1371 my ($passedoptions) = shift;
1376 # Get the values passed to the hash.
1378 my $page_filename = $passedoptions->{"PageFilename"};
1379 my $page_name = $passedoptions->{"PageName"};
1380 my $page_description = $passedoptions->{"PageDescription"};
1381 my $page_section = $passedoptions->{"PageSection"};
1382 my $page_template = $passedoptions->{"PageTemplate"};
1383 my $page_content = $passedoptions->{"PageContent"};
1384 my $page_settings = $passedoptions->{"PageSettings"};
1386 # Check to see if the filename given already exists
1387 # in the page database.
1389 $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 );
1390 $statement_handle->execute();
1392 # Check if a page with the filename given really does
1395 while (@database_page = $statement_handle->fetchrow_array()){
1397 # A page does exist so increment the count to 1.
1403 if ($page_count ne 0){
1405 # The page does exist so set the error value.
1407 $error = "PageExists";
1412 # Check if certain values are undefined.
1420 if (!$page_description){
1422 $page_description = "";
1426 if (!$page_section){
1432 if (!$page_content){
1438 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1439 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1441 # Add the page to the selected database.
1443 $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1444 \'' . $class->convert($page_filename) . '\',
1445 \'' . $class->convert($page_name) . '\',
1446 \'' . $class->convert($page_description) . '\',
1447 \'' . $class->convert($page_section) . '\',
1448 \'' . $class->convert($page_template) . '\',
1449 \'' . $class->convert($page_content) . '\',
1450 \'' . $class->convert($page_settings) . '\',
1451 \'' . $class->convert($page_date) . '\'
1452 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1453 $statement_handle->execute();
1455 undef $statement_handle;
1460 #################################################################################
1461 # deletepage: Delete a page from the selected database. #
1465 # $dbmodule->deletepage(options) #
1467 # options Specifies the following options in any order. #
1469 # PageFilename Specifies the page filename to delete. #
1470 #################################################################################
1475 # Get the data that was passed to the subroutine.
1478 my ($passedoptions) = @_;
1482 # Get the page filename.
1484 my $page_filename = $passedoptions->{"PageFilename"};
1486 # Check if the page exists before deleting it.
1488 $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 );
1489 $statement_handle->execute();
1491 while (@page_info = $statement_handle->fetchrow_array()){
1497 # Check if the page really does exist.
1501 $error = "PageDoesNotExist";
1508 $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1509 $statement_handle->execute();
1514 #################################################################################
1515 # editpage: Edit a page from the selected database. #
1519 # $dbmodule->editpage(options); #
1521 # options Specifies the following options in any order. #
1523 # PageFilename Specifies the filename to edit. #
1524 # PageNewFilename Specifies the new filename to use. #
1525 # PageNewName Specifies the new page name to use. #
1526 # PageNewDescription Specifies the new page description to use. #
1527 # PageNewSection Specifies the new page section to use. #
1528 # PageNewTemplate Specifies the new page template to use. #
1529 # PageNewContent Specifies the new page content to use. #
1530 # PageNewSettings Specifies the new page settings to use. #
1531 #################################################################################
1536 # Get the values passed to the subroutine.
1539 my ($passedoptions) = @_;
1543 # Get the data that was passed to the subroutine.
1545 my $page_filename = $passedoptions->{"PageFilename"};
1546 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1547 my $page_newname = $passedoptions->{"PageNewName"};
1548 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1549 my $page_newsection = $passedoptions->{"PageNewSection"};
1550 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1551 my $page_newcontent = $passedoptions->{"PageNewContent"};
1552 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1554 # Check if the page with the filename given exists.
1556 $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 );
1557 $statement_handle->execute();
1559 # Check if the page really does exist.
1561 while (@page_info = $statement_handle->fetchrow_array()){
1563 # The page information is found.
1569 # Check if the page really does exist.
1573 $error = "PageDoesNotExist";
1578 # Check if there is a page that already exists with the new
1583 if ($page_filename ne $page_newfilename){
1585 $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 );
1586 $statement_handle->execute();
1588 # Check if a page really is using the new filename.
1590 while (@page_info = $statement_handle->fetchrow_array()){
1592 # The page information is found.
1598 if ($page_found eq 1){
1600 $error = "PageExists";
1607 # Get the current date.
1609 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1610 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1612 # Edit the selected page.
1614 $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 );
1615 $statement_handle->execute();
1620 #################################################################################
1621 # movepage: Moves a page from the old database to the new database. #
1625 # $dbmodule->movepage(options); #
1627 # options Specifies the following options in any order. #
1629 # PageFilename Specifies the page with the filename to move. #
1630 #################################################################################
1635 # Get the values passed to the subroutine.
1638 my ($passedoptions) = @_;
1640 my (%database_page, $database_page);
1641 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1645 # Get the data that was passed to the subroutine.
1647 my $page_filename = $passedoptions->{"PageFilename"};
1649 # Check if the page with the filename given exists.
1651 $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 );
1652 $statement_handle->execute();
1654 # Check if the page really does exist.
1656 while (@page_info = $statement_handle->fetchrow_array()){
1658 # Get the values from the array.
1660 $pagefilename = $page_info[0];
1661 $pagename = $page_info[1];
1662 $pagedescription = $page_info[2];
1663 $pagesection = $page_info[3];
1664 $pagetemplate = $page_info[4];
1665 $pagedata = $page_info[5];
1666 $pagesettings = $page_info[6];
1667 $pagelastmodified = $page_info[7];
1669 # Put the values into the page hash.
1672 "PageFilename" => $pagefilename,
1673 "PageName" => $pagename,
1674 "PageDescription" => $pagedescription,
1675 "PageSection" => $pagesection,
1676 "PageTemplate" => $pagetemplate,
1677 "PageContent" => $pagedata,
1678 "PageSettings" => $pagesettings,
1679 "PageLastModified" => $pagelastmodified,
1682 # The page information is found.
1688 # Check if the page really does exist.
1692 $error = "PageDoesNotExist";
1697 # Check if the page with the filename given already exists in
1698 # the database the page is being moved to.
1703 $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 );
1704 $second_statement_handle->execute();
1706 while (@page_info = $second_statement_handle->fetchrow_array()){
1712 # Check if the page really does exist.
1716 $error = "PageAlreadyExists";
1721 # Add the page to the new database.
1723 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1724 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1725 \'' . $class->convert($database_page{"PageName"}) . '\',
1726 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1727 \'' . $class->convert($database_page{"PageSection"}) . '\',
1728 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1729 \'' . $class->convert($database_page{"PageContent"}) . '\',
1730 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1731 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1732 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1733 $second_statement_handle->execute();
1735 # Delete the page from the old database.
1737 $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 );
1738 $statement_handle->execute();
1743 #################################################################################
1744 # copypage: Copies a page from the old database to the new database. #
1748 # $dbmodule->copypage(options); #
1750 # options Specifies the following options in any order. #
1752 # PageFilename Specifies the page with the filename to copy. #
1753 #################################################################################
1758 # Get the values passed to the subroutine.
1761 my ($passedoptions) = @_;
1763 my (%database_page, $database_page);
1764 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1768 # Get the data that was passed to the subroutine.
1770 my $page_filename = $passedoptions->{"PageFilename"};
1772 # Check if the page with the filename given exists.
1774 $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 );
1775 $statement_handle->execute();
1777 # Check if the page really does exist.
1779 while (@page_info = $statement_handle->fetchrow_array()){
1781 # Get the values from the array.
1783 $pagefilename = $page_info[0];
1784 $pagename = $page_info[1];
1785 $pagedescription = $page_info[2];
1786 $pagesection = $page_info[3];
1787 $pagetemplate = $page_info[4];
1788 $pagedata = $page_info[5];
1789 $pagesettings = $page_info[6];
1790 $pagelastmodified = $page_info[7];
1792 # Put the values into the page hash.
1795 "PageFilename" => $pagefilename,
1796 "PageName" => $pagename,
1797 "PageDescription" => $pagedescription,
1798 "PageSection" => $pagesection,
1799 "PageTemplate" => $pagetemplate,
1800 "PageContent" => $pagedata,
1801 "PageSettings" => $pagesettings,
1802 "PageLastModified" => $pagelastmodified,
1805 # The page information is found.
1811 # Check if the page really does exist.
1815 $error = "PageDoesNotExist";
1820 # Check if the page with the filename given already exists in
1821 # the database the page is being moved to.
1826 $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 );
1827 $second_statement_handle->execute();
1829 while (@page_info = $second_statement_handle->fetchrow_array()){
1835 # Check if the page really does exist.
1839 $error = "PageAlreadyExists";
1844 # Add the page to the new database.
1846 $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1847 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1848 \'' . $class->convert($database_page{"PageName"}) . '\',
1849 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1850 \'' . $class->convert($database_page{"PageSection"}) . '\',
1851 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1852 \'' . $class->convert($database_page{"PageContent"}) . '\',
1853 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1854 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1855 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1856 $second_statement_handle->execute();
1860 #################################################################################
1861 # Filter subroutines. #
1862 #################################################################################
1865 #################################################################################
1866 # connectfilter: Connect to the filter database. #
1870 # $dbmodule->connectfilter(missingignore); #
1872 # missingignore Ignore error about database being missing. #
1873 #################################################################################
1879 my $ignoremissing = shift;
1881 # Check if the template database exists.
1883 my $filterdatabase_exists = kiriwrite_fileexists("filters.db.sqlite");
1885 if ($filterdatabase_exists eq 1){
1887 $filterdb_exists = 0;
1889 if (!$ignoremissing){
1891 $error = "FilterDatabaseDoesNotExist";
1898 # Check if the permission settings for the template database are valid.
1900 my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1902 if ($filterdb_permissions eq 1){
1904 # The template database has invalid permissions set
1905 # so return an error value.
1907 if (!$ignoremissing){
1909 $error = "FilterDatabaseInvalidPermissionsSet";
1916 # Connect to the template database.
1918 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1919 $filterdb_database_handle->{unicode} = 1;
1920 $filterdb_loaded = 1;
1924 sub disconnectfilter{
1925 #################################################################################
1926 # disconnectfilter: Disconnect from the filter database. #
1930 # $dbmodule->disconnectfilter(); #
1931 #################################################################################
1933 # Disconnect the template database.
1935 if ($filterdb_loaded eq 1){
1937 undef $filterdb_statement_handle;
1938 $filterdb_database_handle->disconnect();
1945 #################################################################################
1946 # getfiltercount: Gets the count of filters in the filters database. #
1950 # $dbmodule->getfiltercount(); #
1951 #################################################################################
1958 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_filters') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return);
1959 $filterdb_statement_handle->execute();
1961 my $count = $filterdb_statement_handle->fetchrow_array();
1968 #################################################################################
1969 # getfilterlist: Gets the list of filters in the filter database. #
1973 # $dbmodule->getfilterlist(options); #
1975 # StartFrom Specifies where the list of filters should start from. #
1976 # Limit Specifies the amount of the filters to get. #
1977 #################################################################################
1983 my ($passedoptions) = shift;
1988 my $start_from = $passedoptions->{"StartFrom"};
1989 my $limit = $passedoptions->{"Limit"};
1991 if (defined($start_from)){
1999 $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 );
2000 $filterdb_statement_handle->execute();
2004 $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 );
2005 $filterdb_statement_handle->execute();
2009 # Get the list of filters available.
2011 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2013 # Add the filter to the list of available filters.
2015 push(@filter_list, $filter_data[0]);
2019 undef $filterdb_statement_handle;
2020 return @filter_list;
2025 #################################################################################
2026 # getfilterinfo: Gets information about the filter. #
2030 # $dbmodule->getfilterinfo(options); #
2032 # options Specifies the following options in any order. #
2034 # FilterID Specifies the filter ID number to get information from. #
2035 # Reduced Specifies if the reduced version of the filter information #
2036 # should be retrieved. #
2037 #################################################################################
2042 # Get the values passed to the subroutine.
2045 my ($passedoptions) = @_;
2048 my $filter_exists = 0;
2051 # Get the values that are in the hash.
2053 my $filter_id = $passedoptions->{"FilterID"};
2054 my $reduced = $passedoptions->{"Reduced"};
2056 if ($reduced && $reduced eq 1){
2058 $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 );
2059 $filterdb_statement_handle->execute();
2063 $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 );
2064 $filterdb_statement_handle->execute();
2068 # Get the filter information.
2070 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2072 $filter_info{"FilterID"} = $filter_data[0];
2073 $filter_info{"FilterPriority"} = $filter_data[1];
2074 $filter_info{"FilterFind"} = $filter_data[2];
2075 $filter_info{"FilterReplace"} = $filter_data[3];
2076 $filter_info{"FilterEnabled"} = $filter_data[4];
2077 $filter_info{"FilterNotes"} = $filter_data[5];
2083 # Check if the filter exists.
2085 if (!$filter_exists){
2087 # The filter does not exist so return
2090 $error = "FilterDoesNotExist";
2095 # Return the filter information.
2097 undef $filterdb_statement_handle;
2098 return %filter_info;
2103 #################################################################################
2104 # addfilter: Adds a filter to the filter database. #
2108 # $dbmodule->addfilter(options); #
2110 # options Specifies the following options in any order. #
2112 # FindFilter Specifies the find filter to add. #
2113 # ReplaceFilter Specifies the replace filter to add. #
2114 # Priority Specifies the filter priority to use. #
2115 # Notes Specifies the notes to use. #
2116 #################################################################################
2121 # Get the values passed to the subroutine.
2124 my ($passedoptions) = @_;
2126 # Define some variables for later.
2128 my @database_filters;
2131 my $nofiltertable = 0;
2132 my $filter_found = 0;
2133 my $filter_count = 0;
2137 # Get the values from the hash.
2139 my $filter_find = $passedoptions->{"FindFilter"};
2140 my $filter_replace = $passedoptions->{"ReplaceFilter"};
2141 my $filter_priority = $passedoptions->{"Priority"};
2142 my $filter_enabled = $passedoptions->{"Enabled"};
2143 my $filter_notes = $passedoptions->{"Notes"};
2145 # Check if the filter database permissions are valid.
2147 my $filterdb_exists = kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2148 my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2150 if ($filterdb_permissions eq 1){
2152 if ($filterdb_exists eq 0){
2153 $error = "FilterDatabaseInvalidPermissionsSet";
2159 # Check if certain values are undefined and if they
2160 # are then set them blank.
2168 if (!$filter_replace){
2170 $filter_replace = "";
2174 if (!$filter_priority){
2176 $filter_priority = 1;
2180 if (!$filter_notes){
2186 if (!$filter_enabled){
2188 $filter_enabled = "";
2192 my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2194 if ($directory_permissions eq 1 && $filterdb_exists){
2196 # The template database cannot be created because of invalid directory
2197 # permissions so return an error value.
2199 $error = "FilterDatabaseFileUncreateable";
2204 # Check if the filter table exists.
2206 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2208 # Check if there is really no filter table.
2210 if ($nofiltertable){
2212 # Create the filter database table.
2214 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2215 id int(7) primary key,
2217 findsetting varchar(1024),
2218 replacesetting varchar(1024),
2221 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2222 $filterdb_statement_handle->execute();
2226 # Find the lowest filter identification number available.
2228 $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 );
2229 $filterdb_statement_handle->execute();
2231 while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2233 $filter_id = $database_filters[0];
2235 # Add the filter identification to the list of filter IDs.
2237 push(@filterid_list, $filter_id);
2243 # Process each filter looking for a blank available filter.
2245 foreach $filter_id (@filterid_list){
2247 # Check the next filter ID to see if it's blank.
2249 $new_id = $filter_id + 1;
2251 $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 );
2252 $filterdb_statement_handle->execute();
2254 # Get the filter identification number.
2256 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2262 # Check if a filter was found.
2264 if (!$filter_found){
2266 # No filter was found using this ID so exit the loop.
2272 # Increment the filter count and reset the filter found value.
2280 # Check if there were any filters in the filter database.
2282 if (!$filter_count && !$new_id){
2284 # There were no filters in the filter database so set
2285 # the new filter identification value to 1.
2291 # Add the filter to the filter database.
2293 $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, enabled, notes) VALUES (
2294 \'' . $class->convert($new_id) . '\',
2295 \'' . $class->convert($filter_priority) . '\',
2296 \'' . $class->convert($filter_find) . '\',
2297 \'' . $class->convert($filter_replace) .'\',
2298 \'' . $class->convert($filter_enabled) . '\',
2299 \'' . $class->convert($filter_notes) . '\'
2300 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2301 $filterdb_statement_handle->execute();
2306 #################################################################################
2307 # editfilter: Edits a filter in the filter database. #
2311 # $dbmodule->editfilter(options); #
2313 # options Specifies the following options in any order. #
2315 # FilterID Specifies the filter to edit. #
2316 # NewFindFilter Specifies the new find filter setting. #
2317 # NewReplaceFilter Specifies the new replace filter setting. #
2318 # NewFilterPriority Specifies the new filter priority setting. #
2319 # NewEnabled Specifies if the filter should be enabled. #
2320 # NewFilterNotes Specifies the new notes for the filter. #
2321 #################################################################################
2326 # Get the values passed to the subroutine.
2329 my ($passedoptions) = @_;
2332 my $filter_exists = 1;
2335 # Get the values from the hash.
2337 my $filter_id = $passedoptions->{"FilterID"};
2338 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2339 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2340 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2341 my $filter_enabled = $passedoptions->{"NewEnabled"};
2342 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2344 # Check if the filter database permissions are valid.
2346 my $filterdb_exists = kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2347 my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2349 if ($filterdb_permissions eq 1){
2351 if ($filterdb_exists eq 0){
2352 $error = "FilterDatabaseInvalidPermissionsSet";
2358 # Check if the filter exists before editing it.
2360 $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 );
2361 $filterdb_statement_handle->execute();
2363 # Check if the filter exists.
2365 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2371 # Check if the filter really does exist.
2373 if (!$filter_exists){
2375 # The filter does not exist so return
2378 $error = "FilterDoesNotExist";
2383 # Edit the selected filter.
2385 $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2386 findsetting = \'' . $class->convert($filter_newfind) . '\',
2387 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2388 priority = \'' . $class->convert($filter_newpriority) . '\',
2389 enabled = \'' . $class->convert($filter_enabled) . '\',
2390 notes = \'' . $class->convert($filter_newnotes) . '\'
2391 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2392 $filterdb_statement_handle->execute();
2394 undef $filterdb_statement_handle;
2400 #################################################################################
2401 # deletefilter: Deletes a filter from the filter database. #
2405 # $dbmodule->deletefilter(options); #
2407 # options Specifies the following options in any order. #
2409 # FilterID Specifies the filter to delete from the filter database. #
2410 #################################################################################
2415 # Get the values passed to the subroutine.
2418 my ($passedoptions) = @_;
2420 my $filter_exists = 0;
2423 # Get the values from the hash.
2425 my $filter_id = $passedoptions->{"FilterID"};
2427 # Check if the filter exists before deleting.
2429 $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 );
2430 $filterdb_statement_handle->execute();
2432 while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2438 # Check to see if the filter really does exist.
2440 if (!$filter_exists){
2442 $error = "FilterDoesNotExist";
2447 # Delete the filter from the filter database.
2449 $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 );
2450 $filterdb_statement_handle->execute();
2452 undef $filterdb_statement_handle;
2456 #################################################################################
2457 # Template subroutines. #
2458 #################################################################################
2460 sub connecttemplate{
2461 #################################################################################
2462 # connecttemplate: Connect to the template database. #
2466 # $dbmodule->connecttemplate(missingignore); #
2468 # missingignore Ignore errror about database being missing. #
2469 #################################################################################
2475 my $ignoremissing = shift;
2477 # Check if the template database exists.
2479 my $templatedatabase_exists = kiriwrite_fileexists("templates.db.sqlite");
2481 if ($templatedatabase_exists eq 1){
2483 $templatedb_exists = 0;
2485 if (!$ignoremissing){
2487 $error = "TemplateDatabaseDoesNotExist";
2494 # Check if the permission settings for the template database are valid.
2496 my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2498 if ($templatedb_permissions eq 1){
2500 # The template database has invalid permissions set
2501 # so return an error value.
2503 if (!$ignoremissing){
2505 $error = "TemplateDatabaseInvalidPermissionsSet";
2512 # Connect to the template database.
2514 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2515 $template_database_handle->{unicode} = 1;
2516 $templatedb_loaded = 1;
2520 sub disconnecttemplate{
2521 #################################################################################
2522 # disconnecttemplate: Disconnect from the template database. #
2526 # $dbmodule->disconnecttemplate(); #
2527 #################################################################################
2529 # Disconnect the template database.
2531 if ($templatedb_loaded eq 1){
2533 undef $template_statement_handle;
2534 $template_database_handle->disconnect();
2540 sub gettemplatecount{
2541 #################################################################################
2542 # gettemplatecount: Gets the count of templates in the template database. #
2546 # $dbmodule->gettemplatecount(); #
2547 #################################################################################
2554 $template_statement_handle = $template_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_templates') or ( $error = "FilterDatabaseError", $errorext = $template_database_handle->errstr, return);
2555 $template_statement_handle->execute();
2557 my $count = $template_statement_handle->fetchrow_array();
2563 sub gettemplatelist{
2564 #################################################################################
2565 # gettemplatelist: Gets the list of templates. #
2569 # $dbmodule->gettemplatelist(options); #
2571 # options Specifies the following options as a hash (in any order). #
2573 # StartFrom Specifies where the list of templates will start from. #
2574 # Limit Specifies how many templates should be retrieved. #
2575 #################################################################################
2581 my ($passedoptions) = @_;
2583 my $start_from = $passedoptions->{"StartFrom"};
2584 my $limit = $passedoptions->{"Limit"};
2586 if (defined($start_from)){
2594 $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 );
2595 $template_statement_handle->execute();
2599 $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 );
2600 $template_statement_handle->execute();
2604 my @database_template;
2606 my $template_filename;
2608 while (@database_template = $template_statement_handle->fetchrow_array()){
2610 # Get certain values from the array.
2612 $template_filename = $database_template[0];
2614 # Add the template to the list of templates.
2616 push(@templates_list, $template_filename);
2620 return @templates_list;
2624 sub gettemplateinfo{
2625 #################################################################################
2626 # gettemplateinfo: Get information on a template. #
2630 # $dbmodule->gettemplateinfo(options); #
2632 # options Specifies the following options in any order. #
2634 # TemplateFilename Specifies the template filename to use. #
2635 # Reduced Specifies a reduced version of template information to #
2637 #################################################################################
2642 # Get the data passed to the subroutine.
2645 my ($passedoptions) = @_;
2650 my $template_filename;
2652 my $template_description;
2653 my $template_datemodified;
2654 my $template_layout;
2656 my $template_found = 0;
2658 my $filename = $passedoptions->{"TemplateFilename"};
2659 my $reduced = $passedoptions->{"Reduced"};
2661 if ($reduced && $reduced eq 1){
2663 $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 );
2664 $template_statement_handle->execute();
2668 $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 );
2669 $template_statement_handle->execute();
2673 while (@template_data = $template_statement_handle->fetchrow_array()){
2675 # Get certain values from the array.
2677 $template_filename = $template_data[0];
2678 $template_name = $template_data[1];
2679 $template_description = $template_data[2];
2680 $template_layout = $template_data[3];
2681 $template_datemodified = $template_data[4];
2683 # Process them into the hash.
2686 "TemplateFilename" => $template_filename,
2687 "TemplateName" => $template_name,
2688 "TemplateDescription" => $template_description,
2689 "TemplateLayout" => $template_layout,
2690 "TemplateLastModified" => $template_datemodified
2693 $template_found = 1;
2697 if ($template_found eq 0){
2699 # The template was not found in the template database so
2700 # write an error value.
2702 $error = "TemplateDoesNotExist";
2712 #################################################################################
2713 # addtemplate: Adds a template to the template database. #
2717 # $dbmodule->addtemplate(); #
2719 # options Specifies the following options in any order. #
2721 # TemplateFilename Specifies the new template filename. #
2722 # TemplateName Specifies the new template name. #
2723 # TemplateDescription Specifies the new template description. #
2724 # TemplateLayout Specifies the new template layout. #
2725 #################################################################################
2730 # Get the data passed to the subroutine.
2733 my ($passedoptions) = @_;
2736 my $notemplatetable;
2739 my $template_filename = $passedoptions->{"TemplateFilename"};
2740 my $template_name = $passedoptions->{"TemplateName"};
2741 my $template_description = $passedoptions->{"TemplateDescription"};
2742 my $template_layout = $passedoptions->{"TemplateLayout"};
2744 # Check if the template database permissions are valid.
2746 my $templatedb_exists = kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2747 my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2749 if ($templatedb_permissions eq 1){
2751 if ($templatedb_exists eq 0){
2752 $error = "TemplateDatabaseInvalidPermissionsSet";
2758 # Check if the template already exists before adding.
2760 if ($templatedb_exists eq 0){
2762 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2764 if ($blankfile eq 0){
2766 $template_statement_handle->execute();
2768 while (@page_exists = $template_statement_handle->fetchrow_array()){
2770 $error = "TemplatePageExists";
2779 # Get the current date.
2781 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2783 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2785 # Check if certain values are undefined and if they
2786 # are then set them blank.
2788 if (!$template_name){
2790 $template_name = "";
2794 if (!$template_description){
2796 $template_description = "";
2800 if (!$template_layout){
2802 $template_layout = "";
2806 my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2808 if ($directory_permissions eq 1 && $templatedb_exists){
2810 # The template database cannot be created because of invalid directory
2811 # permissions so return an error value.
2813 $error = "TemplateDatabaseUncreateable";
2818 # Check to see if a template can be added.
2820 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2821 \'' . $class->convert($template_filename) . '\',
2822 \'' . $class->convert($template_name) . '\',
2823 \'' . $class->convert($template_description) . '\',
2824 \'' . $class->convert($template_layout) . '\',
2825 \'' . $class->convert($template_date) . '\'
2826 )') or ( $notemplatetable = 1 );
2828 if (!$notemplatetable){
2830 $template_statement_handle->execute();
2834 # Check to see if there is no template table and attempt to create one.
2836 if ($notemplatetable){
2838 # Create a template table.
2840 my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2842 if ($directory_permissions eq 1){
2844 # The template database cannot be created because of invalid directory
2845 # permissions so return an error.
2847 $error = "TemplateDatabaseFileUncreateable";
2852 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2853 filename varchar(256) primary key,
2854 templatename varchar(512),
2855 templatedescription varchar(512),
2856 templatelayout text,
2857 datemodified datetime
2858 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2859 $template_statement_handle->execute();
2861 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2862 \'' . $class->convert($template_filename) . '\',
2863 \'' . $class->convert($template_name) . '\',
2864 \'' . $class->convert($template_description) . '\',
2865 \'' . $class->convert($template_layout) . '\',
2866 \'' . $class->convert($template_date) . '\'
2867 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2868 $template_statement_handle->execute();
2875 #################################################################################
2876 # deletetemplate: Deletes a template from the template database. #
2880 # $dbmodule->deletetemplate(options); #
2882 # options Specifies the following options in any order. #
2884 # TemplateFilename Specifies the template filename to delete. #
2885 #################################################################################
2890 # Get the data passed to the subroutine.
2893 my ($passedoptions) = @_;
2896 my $template_filename = $passedoptions->{"TemplateFilename"};
2897 my $template_count = 0;
2899 # Check if the template exists.
2901 $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 );
2902 $template_statement_handle->execute();
2904 while (@pagedata = $template_statement_handle->fetchrow_array()){
2910 if ($template_count eq 0){
2912 # No pages were returned so return an error value.
2914 $error = "TemplateDoesNotExist";
2919 # Delete the template from the template database.
2921 $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 );
2922 $template_statement_handle->execute();
2927 #################################################################################
2928 # editttemplate: Edits a Kiriwrite template. #
2932 # $dbmodule->edittemplate(options); #
2934 # options Specifies the following options in any order. #
2936 # TemplateFilename Specifies the template filename to edit. #
2937 # NewTemplateFilename Specifies the new template filename. #
2938 # NewTemplateName Specifies the new template name. #
2939 # NewTemplateDescription Specifies the new template description. #
2940 # NewTemplateLayout Specifies the new template layout. #
2941 #################################################################################
2943 # Get the values passed.
2946 my ($passedoptions) = @_;
2947 my $template_found = 0;
2950 # Process the values passed.
2952 my $template_filename = $passedoptions->{"TemplateFilename"};
2953 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2954 my $new_template_name = $passedoptions->{"NewTemplateName"};
2955 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2956 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2958 # Check if the template database permissions are valid.
2960 my $templatedb_exists = kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2961 my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2963 if ($templatedb_permissions eq 1){
2965 if ($templatedb_exists eq 0){
2966 $error = "TemplateDatabaseInvalidPermissionsSet";
2972 # Check if the template exists.
2974 $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 );
2975 $template_statement_handle->execute();
2977 while (@template_info = $template_statement_handle->fetchrow_array()){
2979 $template_found = 1;
2983 # Check to see if the template was found and set an error value if
2986 if ($template_found eq 0){
2988 $error = "TemplateDoesNotExist";
2993 # Get the date and time.
2995 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2996 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2998 # Update the template information.
3000 $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
3001 filename = \'' . $class->convert($new_template_filename) . '\',
3002 templatename = \'' . $class->convert($new_template_name) . '\',
3003 templatedescription = \'' . $class->convert($new_template_description) . '\',
3004 templatelayout = \'' . $class->convert($new_template_layout) . '\',
3005 datemodified = \'' . $class->convert($templatenewdate) . '\'
3006 WHERE filename = \'' . $class->convert($template_filename) . '\'
3007 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
3008 $template_statement_handle->execute();
3012 #################################################################################
3013 # General subroutines. #
3014 #################################################################################
3017 #################################################################################
3018 # connect: Connect to the server. #
3022 # $dbmodule->connect(); #
3023 #################################################################################
3025 # This function is not needed in this database module.
3030 #################################################################################
3031 # connect: Disconnect from the server. #
3035 # $dbmodule->disconnect(); #
3036 #################################################################################
3038 # This function is not needed in this database module.
3040 undef $statement_handle;