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.1.0";
38 my ($options, %options);
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_string_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_string_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_string_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$//g;
609 $database_filename_friendly = $database;
611 #$database_filename_length = length($database);
612 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
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 $string_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 $string_handle->execute();
646 @sqldata = $string_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 $string_handle->finish();
661 undef $string_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_string_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_string_handle->execute();
691 @sqldata = $second_string_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_string_handle->finish();
706 undef $second_string_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 $string_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 $string_handle->execute();
777 $string_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 $string_handle->execute();
789 # Convert the values into SQL query formatted values and add an entry
790 # to the kiriwrite_database_info table.
792 $string_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 $string_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 $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933 $string_handle->execute();
935 my @database_oldinfo = $string_handle->fetchrow_array();
937 my $dboldname = $database_oldinfo[0];
939 # Update the database information.
941 $string_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 $string_handle->execute();
947 undef $string_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(); #
1148 #################################################################################
1155 $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1156 $string_handle->execute();
1158 my @database_pagefilenames;
1159 my @database_pagefilenames_final;
1161 # Process the collected pages.
1163 while (@database_pagefilenames = $string_handle->fetchrow_array){
1165 # Add each page to the list of pages in the database.
1167 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1171 undef $string_handle;
1172 return @database_pagefilenames_final;
1177 #################################################################################
1178 # getpageinfo: Gets the page information from the filename passed. #
1182 # $dbmodule->getpageinfo(options); #
1184 # options Specifies the following options in any order. #
1186 # PageFilename Specifies the page filename to get the page information from. #
1187 # Reduced Specifies if the reduced version of the page information should #
1189 #################################################################################
1195 my ($passedoptions) = shift;
1196 my (%database_page, $database_page);
1197 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1202 # Get the page from the database.
1204 my $page_filename = $passedoptions->{"PageFilename"};
1205 my $page_reduced = $passedoptions->{"Reduced"};
1207 if ($page_reduced eq 1){
1209 $string_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 );
1211 $string_handle->execute();
1213 # Check if the page exists in the database.
1215 while (@data_page = $string_handle->fetchrow_array()){
1217 # Get the values from the array.
1219 $pagefilename = $data_page[0];
1220 $pagename = $data_page[1];
1221 $pagedescription = $data_page[2];
1222 $pagelastmodified = $data_page[3];
1224 # Put the values into the page hash.
1227 "PageFilename" => $pagefilename,
1228 "PageName" => $pagename,
1229 "PageDescription" => $pagedescription,
1230 "PageLastModified" => $class->dateconvert($pagelastmodified),
1240 $string_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 );
1242 $string_handle->execute();
1244 # Check if the page exists in the database.
1246 while (@data_page = $string_handle->fetchrow_array()){
1248 # Get the values from the array.
1250 $pagefilename = $data_page[0];
1251 $pagename = $data_page[1];
1252 $pagedescription = $data_page[2];
1253 $pagesection = $data_page[3];
1254 $pagetemplate = $data_page[4];
1255 $pagedata = $data_page[5];
1256 $pagesettings = $data_page[6];
1257 $pagelastmodified = $data_page[7];
1259 # Put the values into the page hash.
1262 "PageFilename" => $pagefilename,
1263 "PageName" => $pagename,
1264 "PageDescription" => $pagedescription,
1265 "PageSection" => $pagesection,
1266 "PageTemplate" => $pagetemplate,
1267 "PageContent" => $pagedata,
1268 "PageSettings" => $pagesettings,
1269 "PageLastModified" => $class->dateconvert($pagelastmodified),
1278 # Check if the page did exist.
1282 $error = "PageDoesNotExist";
1287 undef $string_handle;
1288 return %database_page;
1293 #################################################################################
1294 # addpage: Add a page to the selected database. #
1298 # $dbmodule->addpage(options); #
1300 # options Specifies the following options in any order. #
1302 # PageFilename Specifies the page filename to use. #
1303 # PageName Specifies the page name to use. #
1304 # PageDescription Specifies the page description to use. #
1305 # PageSection Specifies the page section to use. #
1306 # PageTemplate Specifies the page template to use. #
1307 # PageContent Specifies the page content to use. #
1308 # PageSettings Specifies the page settings to use. #
1309 #################################################################################
1311 # Get the data that was passed to the subroutine.
1317 my ($passedoptions) = shift;
1322 # Get the values passed to the hash.
1324 my $page_filename = $passedoptions->{"PageFilename"};
1325 my $page_name = $passedoptions->{"PageName"};
1326 my $page_description = $passedoptions->{"PageDescription"};
1327 my $page_section = $passedoptions->{"PageSection"};
1328 my $page_template = $passedoptions->{"PageTemplate"};
1329 my $page_content = $passedoptions->{"PageContent"};
1330 my $page_settings = $passedoptions->{"PageSettings"};
1332 # Check to see if the filename given already exists
1333 # in the page database.
1335 $string_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 );
1336 $string_handle->execute();
1338 # Check if a page with the filename given really does
1341 while (@database_page = $string_handle->fetchrow_array()){
1343 # A page does exist so increment the count to 1.
1349 if ($page_count ne 0){
1351 # The page does exist so set the error value.
1353 $error = "PageExists";
1358 # Check if certain values are undefined.
1366 if (!$page_description){
1368 $page_description = "";
1372 if (!$page_section){
1378 if (!$page_content){
1384 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1385 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1387 # Add the page to the selected database.
1389 $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1390 \'' . $class->convert($page_filename) . '\',
1391 \'' . $class->convert($page_name) . '\',
1392 \'' . $class->convert($page_description) . '\',
1393 \'' . $class->convert($page_section) . '\',
1394 \'' . $class->convert($page_template) . '\',
1395 \'' . $class->convert($page_content) . '\',
1396 \'' . $class->convert($page_settings) . '\',
1397 \'' . $class->convert($page_date) . '\'
1398 )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1399 $string_handle->execute();
1401 undef $string_handle;
1406 #################################################################################
1407 # deletepage: Delete a page from the selected database. #
1411 # $dbmodule->deletepage(options) #
1413 # options Specifies the following options in any order. #
1415 # PageFilename Specifies the page filename to delete. #
1416 #################################################################################
1421 # Get the data that was passed to the subroutine.
1424 my ($passedoptions) = @_;
1428 # Get the page filename.
1430 my $page_filename = $passedoptions->{"PageFilename"};
1432 # Check if the page exists before deleting it.
1434 $string_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 );
1435 $string_handle->execute();
1437 while (@page_info = $string_handle->fetchrow_array()){
1443 # Check if the page really does exist.
1447 $error = "PageDoesNotExist";
1454 $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1455 $string_handle->execute();
1460 #################################################################################
1461 # editpage: Edit a page from the selected database. #
1465 # $dbmodule->editpage(options); #
1467 # options Specifies the following options in any order. #
1469 # PageFilename Specifies the filename to edit. #
1470 # PageNewFilename Specifies the new filename to use. #
1471 # PageNewName Specifies the new page name to use. #
1472 # PageNewDescription Specifies the new page description to use. #
1473 # PageNewSection Specifies the new page section to use. #
1474 # PageNewTemplate Specifies the new page template to use. #
1475 # PageNewContent Specifies the new page content to use. #
1476 # PageNewSettings Specifies the new page settings to use. #
1477 #################################################################################
1482 # Get the values passed to the subroutine.
1485 my ($passedoptions) = @_;
1489 # Get the data that was passed to the subroutine.
1491 my $page_filename = $passedoptions->{"PageFilename"};
1492 my $page_newfilename = $passedoptions->{"PageNewFilename"};
1493 my $page_newname = $passedoptions->{"PageNewName"};
1494 my $page_newdescription = $passedoptions->{"PageNewDescription"};
1495 my $page_newsection = $passedoptions->{"PageNewSection"};
1496 my $page_newtemplate = $passedoptions->{"PageNewTemplate"};
1497 my $page_newcontent = $passedoptions->{"PageNewContent"};
1498 my $page_newsettings = $passedoptions->{"PageNewSettings"};
1500 # Check if the page with the filename given exists.
1502 $string_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 );
1503 $string_handle->execute();
1505 # Check if the page really does exist.
1507 while (@page_info = $string_handle->fetchrow_array()){
1509 # The page information is found.
1515 # Check if the page really does exist.
1519 $error = "PageDoesNotExist";
1524 # Check if there is a page that already exists with the new
1527 if ($page_filename ne $page_newfilename){
1529 $string_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 );
1530 $string_handle->execute();
1532 # Check if a page really is using the new filename.
1534 while (@page_info = $string_handle->fetchrow_array()){
1536 # The page information is found.
1542 if ($page_found eq 1){
1544 $error = "PageExists";
1551 # Get the current date.
1553 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1554 my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1556 # Edit the selected page.
1558 $string_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 );
1559 $string_handle->execute();
1564 #################################################################################
1565 # movepage: Moves a page from the old database to the new database. #
1569 # $dbmodule->movepage(options); #
1571 # options Specifies the following options in any order. #
1573 # PageFilename Specifies the page with the filename to move. #
1574 #################################################################################
1579 # Get the values passed to the subroutine.
1582 my ($passedoptions) = @_;
1584 my (%database_page, $database_page);
1585 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1589 # Get the data that was passed to the subroutine.
1591 my $page_filename = $passedoptions->{"PageFilename"};
1593 # Check if the page with the filename given exists.
1595 $string_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 );
1596 $string_handle->execute();
1598 # Check if the page really does exist.
1600 while (@page_info = $string_handle->fetchrow_array()){
1602 # Get the values from the array.
1604 $pagefilename = $page_info[0];
1605 $pagename = $page_info[1];
1606 $pagedescription = $page_info[2];
1607 $pagesection = $page_info[3];
1608 $pagetemplate = $page_info[4];
1609 $pagedata = $page_info[5];
1610 $pagesettings = $page_info[6];
1611 $pagelastmodified = $page_info[7];
1613 # Put the values into the page hash.
1616 "PageFilename" => $pagefilename,
1617 "PageName" => $pagename,
1618 "PageDescription" => $pagedescription,
1619 "PageSection" => $pagesection,
1620 "PageTemplate" => $pagetemplate,
1621 "PageContent" => $pagedata,
1622 "PageSettings" => $pagesettings,
1623 "PageLastModified" => $pagelastmodified,
1626 # The page information is found.
1632 # Check if the page really does exist.
1636 $error = "PageDoesNotExist";
1641 # Check if the page with the filename given already exists in
1642 # the database the page is being moved to.
1647 $second_string_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 );
1648 $second_string_handle->execute();
1650 while (@page_info = $second_string_handle->fetchrow_array()){
1656 # Check if the page really does exist.
1660 $error = "PageAlreadyExists";
1665 # Add the page to the new database.
1667 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1668 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1669 \'' . $class->convert($database_page{"PageName"}) . '\',
1670 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1671 \'' . $class->convert($database_page{"PageSection"}) . '\',
1672 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1673 \'' . $class->convert($database_page{"PageContent"}) . '\',
1674 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1675 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1676 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1677 $second_string_handle->execute();
1679 # Delete the page from the old database.
1681 $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1682 $string_handle->execute();
1687 #################################################################################
1688 # copypage: Copies a page from the old database to the new database. #
1692 # $dbmodule->copypage(options); #
1694 # options Specifies the following options in any order. #
1696 # PageFilename Specifies the page with the filename to copy. #
1697 #################################################################################
1702 # Get the values passed to the subroutine.
1705 my ($passedoptions) = @_;
1707 my (%database_page, $database_page);
1708 my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1712 # Get the data that was passed to the subroutine.
1714 my $page_filename = $passedoptions->{"PageFilename"};
1716 # Check if the page with the filename given exists.
1718 $string_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 );
1719 $string_handle->execute();
1721 # Check if the page really does exist.
1723 while (@page_info = $string_handle->fetchrow_array()){
1725 # Get the values from the array.
1727 $pagefilename = $page_info[0];
1728 $pagename = $page_info[1];
1729 $pagedescription = $page_info[2];
1730 $pagesection = $page_info[3];
1731 $pagetemplate = $page_info[4];
1732 $pagedata = $page_info[5];
1733 $pagesettings = $page_info[6];
1734 $pagelastmodified = $page_info[7];
1736 # Put the values into the page hash.
1739 "PageFilename" => $pagefilename,
1740 "PageName" => $pagename,
1741 "PageDescription" => $pagedescription,
1742 "PageSection" => $pagesection,
1743 "PageTemplate" => $pagetemplate,
1744 "PageContent" => $pagedata,
1745 "PageSettings" => $pagesettings,
1746 "PageLastModified" => $pagelastmodified,
1749 # The page information is found.
1755 # Check if the page really does exist.
1759 $error = "PageDoesNotExist";
1764 # Check if the page with the filename given already exists in
1765 # the database the page is being moved to.
1770 $second_string_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 );
1771 $second_string_handle->execute();
1773 while (@page_info = $second_string_handle->fetchrow_array()){
1779 # Check if the page really does exist.
1783 $error = "PageAlreadyExists";
1788 # Add the page to the new database.
1790 $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1791 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1792 \'' . $class->convert($database_page{"PageName"}) . '\',
1793 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1794 \'' . $class->convert($database_page{"PageSection"}) . '\',
1795 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1796 \'' . $class->convert($database_page{"PageContent"}) . '\',
1797 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1798 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1799 )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1800 $second_string_handle->execute();
1804 #################################################################################
1805 # Filter subroutines. #
1806 #################################################################################
1809 #################################################################################
1810 # connectfilter: Connect to the filter database. #
1814 # $dbmodule->connectfilter(missingignore); #
1816 # missingignore Ignore error about database being missing. #
1817 #################################################################################
1823 my $ignoremissing = shift;
1825 # Check if the template database exists.
1827 my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1829 if ($filterdatabase_exists eq 1){
1831 $filterdb_exists = 0;
1833 if (!$ignoremissing){
1835 $error = "FilterDatabaseDoesNotExist";
1842 # Check if the permission settings for the template database are valid.
1844 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1846 if ($filterdb_permissions eq 1){
1848 # The template database has invalid permissions set
1849 # so return an error value.
1851 if (!$ignoremissing){
1853 $error = "FilterDatabaseInvalidPermissionsSet";
1860 # Connect to the template database.
1862 $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1863 $filterdb_database_handle->{unicode} = 1;
1864 $filterdb_loaded = 1;
1868 sub disconnectfilter{
1869 #################################################################################
1870 # disconnectfilter: Disconnect from the filter database. #
1874 # $dbmodule->disconnectfilter(); #
1875 #################################################################################
1877 # Disconnect the template database.
1879 if ($filterdb_loaded eq 1){
1881 undef $filterdb_string_handle;
1882 $filterdb_database_handle->disconnect();
1889 #################################################################################
1890 # getfilterlist: Gets the list of filters in the filter database. #
1894 # $dbmodule->getfilterlist(); #
1895 #################################################################################
1903 # Get the list of filters available.
1905 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1906 $filterdb_string_handle->execute();
1908 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1910 # Add the filter to the list of available filters.
1912 push(@filter_list, $filter_data[0]);
1916 undef $filterdb_string_handle;
1917 return @filter_list;
1922 #################################################################################
1923 # getfilterinfo: Gets information about the filter. #
1927 # $dbmodule->getfilterinfo(options); #
1929 # options Specifies the following options in any order. #
1931 # FilterID Specifies the filter ID number to get information from. #
1932 #################################################################################
1937 # Get the values passed to the subroutine.
1940 my ($passedoptions) = @_;
1943 my $filter_exists = 0;
1946 # Get the values that are in the hash.
1948 my $filter_id = $passedoptions->{"FilterID"};
1950 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1951 $filterdb_string_handle->execute();
1953 # Get the filter information.
1955 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1957 $filter_info{"FilterID"} = $filter_data[0];
1958 $filter_info{"FilterPriority"} = $filter_data[1];
1959 $filter_info{"FilterFind"} = $filter_data[2];
1960 $filter_info{"FilterReplace"} = $filter_data[3];
1961 $filter_info{"FilterNotes"} = $filter_data[4];
1967 # Check if the filter exists.
1969 if (!$filter_exists){
1971 # The filter does not exist so return
1974 $error = "FilterDoesNotExist";
1979 # Return the filter information.
1981 undef $filterdb_string_handle;
1982 return %filter_info;
1987 #################################################################################
1988 # addfilter: Adds a filter to the filter database. #
1992 # $dbmodule->addfilter(options); #
1994 # options Specifies the following options in any order. #
1996 # FindFilter Specifies the find filter to add. #
1997 # ReplaceFilter Specifies the replace filter to add. #
1998 # Priority Specifies the filter priority to use. #
1999 # Notes Specifies the notes to use. #
2000 #################################################################################
2005 # Get the values passed to the subroutine.
2008 my ($passedoptions) = @_;
2010 # Define some variables for later.
2012 my @database_filters;
2015 my $nofiltertable = 0;
2016 my $filter_found = 0;
2017 my $filter_count = 0;
2021 # Get the values from the hash.
2023 my $filter_find = $passedoptions->{"FindFilter"};
2024 my $filter_replace = $passedoptions->{"ReplaceFilter"};
2025 my $filter_priority = $passedoptions->{"Priority"};
2026 my $filter_notes = $passedoptions->{"Notes"};
2028 # Check if the filter database permissions are valid.
2030 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2031 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2033 if ($filterdb_permissions eq 1){
2035 if ($filterdb_exists eq 0){
2036 $error = "FilterDatabaseInvalidPermissionsSet";
2042 # Check if certain values are undefined and if they
2043 # are then set them blank.
2051 if (!$filter_replace){
2053 $filter_replace = "";
2057 if (!$filter_priority){
2059 $filter_priority = 1;
2063 if (!$filter_notes){
2069 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2071 if ($directory_permissions eq 1 && $filterdb_exists){
2073 # The template database cannot be created because of invalid directory
2074 # permissions so return an error value.
2076 $error = "FilterDatabaseFileUncreateable";
2081 # Check if the filter table exists.
2083 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2085 # Check if there is really no filter table.
2087 if ($nofiltertable){
2089 # Create the filter database table.
2091 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2092 id int(7) primary key,
2094 findsetting varchar(1024),
2095 replacesetting varchar(1024),
2097 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2098 $filterdb_string_handle->execute();
2102 # Find the lowest filter identification number available.
2104 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2105 $filterdb_string_handle->execute();
2107 while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2109 $filter_id = $database_filters[0];
2111 # Add the filter identification to the list of filter IDs.
2113 push(@filterid_list, $filter_id);
2119 # Process each filter looking for a blank available filter.
2121 foreach $filter_id (@filterid_list){
2123 # Check the next filter ID to see if it's blank.
2125 $new_id = $filter_id + 1;
2127 $filterdb_string_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 );
2128 $filterdb_string_handle->execute();
2130 # Get the filter identification number.
2132 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2138 # Check if a filter was found.
2140 if (!$filter_found){
2142 # No filter was found using this ID so exit the loop.
2148 # Increment the filter count and reset the filter found value.
2156 # Check if there were any filters in the filters database.
2158 if (!$filter_count && !$new_id){
2160 # There were no filters in the filters database so set
2161 # the new filter identification value to 1.
2167 # Add the filter to the filter database.
2169 $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2170 \'' . $class->convert($new_id) . '\',
2171 \'' . $class->convert($filter_priority) . '\',
2172 \'' . $class->convert($filter_find) . '\',
2173 \'' . $class->convert($filter_replace) .'\',
2174 \'' . $class->convert($filter_notes) . '\'
2175 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2176 $filterdb_string_handle->execute();
2181 #################################################################################
2182 # editfilter: Edits a filter in the filter database. #
2186 # $dbmodule->editfilter(options); #
2188 # options Specifies the following options in any order. #
2190 # FilterID Specifies the filter to edit. #
2191 # NewFindFilter Specifies the new find filter setting. #
2192 # NewReplaceFilter Specifies the new replace filter setting. #
2193 # NewFilterPriority Specifies the new filter priority setting. #
2194 # NewFilterNotes Specifies the new notes for the filter. #
2195 #################################################################################
2200 # Get the values passed to the subroutine.
2203 my ($passedoptions) = @_;
2206 my $filter_exists = 1;
2209 # Get the values from the hash.
2211 my $filter_id = $passedoptions->{"FilterID"};
2212 my $filter_newfind = $passedoptions->{"NewFindFilter"};
2213 my $filter_newreplace = $passedoptions->{"NewReplaceFilter"};
2214 my $filter_newpriority = $passedoptions->{"NewFilterPriority"};
2215 my $filter_newnotes = $passedoptions->{"NewFilterNotes"};
2217 # Check if the filter database permissions are valid.
2219 my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2220 my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2222 if ($filterdb_permissions eq 1){
2224 if ($filterdb_exists eq 0){
2225 $error = "FilterDatabaseInvalidPermissionsSet";
2231 # Check if the filter exists before editing it.
2233 $filterdb_string_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 );
2234 $filterdb_string_handle->execute();
2236 # Check if the filter exists.
2238 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2244 # Check if the filter really does exist.
2246 if (!$filter_exists){
2248 # The filter does not exist so return
2251 $error = "FilterDoesNotExist";
2256 # Edit the selected filter.
2258 $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2259 findsetting = \'' . $class->convert($filter_newfind) . '\',
2260 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2261 priority = \'' . $class->convert($filter_newpriority) . '\',
2262 notes = \'' . $class->convert($filter_newnotes) . '\'
2263 WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2264 $filterdb_string_handle->execute();
2266 undef $filterdb_string_handle;
2272 #################################################################################
2273 # deletefilter: Deletes a filter from the filter database. #
2277 # $dbmodule->deletefilter(options); #
2279 # options Specifies the following options in any order. #
2281 # FilterID Specifies the filter to delete from the filter database. #
2282 #################################################################################
2287 # Get the values passed to the subroutine.
2290 my ($passedoptions) = @_;
2292 my $filter_exists = 0;
2295 # Get the values from the hash.
2297 my $filter_id = $passedoptions->{"FilterID"};
2299 # Check if the filter exists before deleting.
2301 $filterdb_string_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 );
2302 $filterdb_string_handle->execute();
2304 while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2310 # Check to see if the filter really does exist.
2312 if (!$filter_exists){
2314 $error = "FilterDoesNotExist";
2319 # Delete the filter from the filter database.
2321 $filterdb_string_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2322 $filterdb_string_handle->execute();
2324 undef $filterdb_string_handle;
2328 #################################################################################
2329 # Template subroutines. #
2330 #################################################################################
2332 sub connecttemplate{
2333 #################################################################################
2334 # connecttemplate: Connect to the template database. #
2338 # $dbmodule->connecttemplate(missingignore); #
2340 # missingignore Ignore errror about database being missing. #
2341 #################################################################################
2347 my $ignoremissing = shift;
2349 # Check if the template database exists.
2351 my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2353 if ($templatedatabase_exists eq 1){
2355 $templatedb_exists = 0;
2357 if (!$ignoremissing){
2359 $error = "TemplateDatabaseDoesNotExist";
2366 # Check if the permission settings for the template database are valid.
2368 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2370 if ($templatedb_permissions eq 1){
2372 # The template database has invalid permissions set
2373 # so return an error value.
2375 if (!$ignoremissing){
2377 $error = "TemplateDatabaseInvalidPermissionsSet";
2384 # Connect to the template database.
2386 $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2387 $template_database_handle->{unicode} = 1;
2388 $templatedb_loaded = 1;
2392 sub disconnecttemplate{
2393 #################################################################################
2394 # disconnecttemplate: Disconnect from the template database. #
2398 # $dbmodule->disconnecttemplate(); #
2399 #################################################################################
2401 # Disconnect the template database.
2403 if ($templatedb_loaded eq 1){
2405 undef $template_string_handle;
2406 $template_database_handle->disconnect();
2412 sub gettemplatelist{
2413 #################################################################################
2414 # gettemplatelist: Gets the list of templates. #
2418 # $dbmodule->gettemplatelist(); #
2419 #################################################################################
2424 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2425 $template_string_handle->execute();
2427 my @database_template;
2429 my $template_filename;
2431 while (@database_template = $template_string_handle->fetchrow_array()){
2433 # Get certain values from the array.
2435 $template_filename = $database_template[0];
2437 # Add the template to the list of templates.
2439 push(@templates_list, $template_filename);
2443 return @templates_list;
2447 sub gettemplateinfo{
2448 #################################################################################
2449 # gettemplateinfo: Get information on a template. #
2453 # $dbmodule->gettemplateinfo(options); #
2455 # options Specifies the following options in any order. #
2457 # TemplateFilename Specifies the template filename to use. #
2458 #################################################################################
2463 # Get the data passed to the subroutine.
2466 my ($passedoptions) = @_;
2471 my $template_filename;
2473 my $template_description;
2474 my $template_datemodified;
2475 my $template_layout;
2477 my $template_found = 0;
2479 my $filename = $passedoptions->{"TemplateFilename"};
2481 $template_string_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 );
2482 $template_string_handle->execute();
2484 while (@template_data = $template_string_handle->fetchrow_array()){
2486 # Get certain values from the array.
2488 $template_filename = $template_data[0];
2489 $template_name = $template_data[1];
2490 $template_description = $template_data[2];
2491 $template_layout = $template_data[3];
2492 $template_datemodified = $template_data[4];
2494 # Process them into the hash.
2497 "TemplateFilename" => $template_filename,
2498 "TemplateName" => $template_name,
2499 "TemplateDescription" => $template_description,
2500 "TemplateLayout" => $template_layout,
2501 "TemplateLastModified" => $template_datemodified
2504 $template_found = 1;
2508 if ($template_found eq 0){
2510 # The template was not found in the template database so
2511 # write an error value.
2513 $error = "TemplateDoesNotExist";
2523 #################################################################################
2524 # addtemplate: Adds a template to the template database. #
2528 # $dbmodule->addtemplate(); #
2530 # options Specifies the following options in any order. #
2532 # TemplateFilename Specifies the new template filename. #
2533 # TemplateName Specifies the new template name. #
2534 # TemplateDescription Specifies the new template description. #
2535 # TemplateLayout Specifies the new template layout. #
2536 #################################################################################
2541 # Get the data passed to the subroutine.
2544 my ($passedoptions) = @_;
2547 my $notemplatetable;
2550 my $template_filename = $passedoptions->{"TemplateFilename"};
2551 my $template_name = $passedoptions->{"TemplateName"};
2552 my $template_description = $passedoptions->{"TemplateDescription"};
2553 my $template_layout = $passedoptions->{"TemplateLayout"};
2555 # Check if the template database permissions are valid.
2557 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2558 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2560 if ($templatedb_permissions eq 1){
2562 if ($templatedb_exists eq 0){
2563 $error = "TemplateDatabaseInvalidPermissionsSet";
2569 # Check if the template already exists before adding.
2571 if ($templatedb_exists eq 0){
2573 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2575 if ($blankfile eq 0){
2577 $template_string_handle->execute();
2579 while (@page_exists = $template_string_handle->fetchrow_array()){
2581 $error = "TemplatePageExists";
2590 # Get the current date.
2592 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2594 my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2596 # Check if certain values are undefined and if they
2597 # are then set them blank.
2599 if (!$template_name){
2601 $template_name = "";
2605 if (!$template_description){
2607 $template_description = "";
2611 if (!$template_layout){
2613 $template_layout = "";
2617 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2619 if ($directory_permissions eq 1 && $templatedb_exists){
2621 # The template database cannot be created because of invalid directory
2622 # permissions so return an error value.
2624 $error = "TemplateDatabaseUncreateable";
2629 # Check to see if a template can be added.
2631 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2632 \'' . $class->convert($template_filename) . '\',
2633 \'' . $class->convert($template_name) . '\',
2634 \'' . $class->convert($template_description) . '\',
2635 \'' . $class->convert($template_layout) . '\',
2636 \'' . $class->convert($template_date) . '\'
2637 )') or ( $notemplatetable = 1 );
2639 if (!$notemplatetable){
2641 $template_string_handle->execute();
2645 # Check to see if there is no template table and attempt to create one.
2647 if ($notemplatetable){
2649 # Create a template table.
2651 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2653 if ($directory_permissions eq 1){
2655 # The template database cannot be created because of invalid directory
2656 # permissions so return an error.
2658 $error = "TemplateDatabaseFileUncreateable";
2663 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2664 filename varchar(256) primary key,
2665 templatename varchar(512),
2666 templatedescription varchar(512),
2667 templatelayout text,
2668 datemodified datetime
2669 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2670 $template_string_handle->execute();
2672 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2673 \'' . $class->convert($template_filename) . '\',
2674 \'' . $class->convert($template_name) . '\',
2675 \'' . $class->convert($template_description) . '\',
2676 \'' . $class->convert($template_layout) . '\',
2677 \'' . $class->convert($template_date) . '\'
2678 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2679 $template_string_handle->execute();
2686 #################################################################################
2687 # deletetemplate: Deletes a template from the template database. #
2691 # $dbmodule->deletetemplate(options); #
2693 # options Specifies the following options in any order. #
2695 # TemplateFilename Specifies the template filename to delete. #
2696 #################################################################################
2701 # Get the data passed to the subroutine.
2704 my ($passedoptions) = @_;
2707 my $template_filename = $passedoptions->{"TemplateFilename"};
2708 my $template_count = 0;
2710 # Check if the template exists.
2712 $template_string_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 );
2713 $template_string_handle->execute();
2715 while (@pagedata = $template_string_handle->fetchrow_array()){
2721 if ($template_count eq 0){
2723 # No pages were returned so return an error value.
2725 $error = "TemplateDoesNotExist";
2730 # Delete the template from the template database.
2732 $template_string_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2733 $template_string_handle->execute();
2738 #################################################################################
2739 # editttemplate: Edits a Kiriwrite template. #
2743 # $dbmodule->edittemplate(options); #
2745 # options Specifies the following options in any order. #
2747 # TemplateFilename Specifies the template filename to edit. #
2748 # NewTemplateFilename Specifies the new template filename. #
2749 # NewTemplateName Specifies the new template name. #
2750 # NewTemplateDescription Specifies the new template description. #
2751 # NewTemplateLayout Specifies the new template layout. #
2752 #################################################################################
2754 # Get the values passed.
2757 my ($passedoptions) = @_;
2758 my $template_found = 0;
2761 # Process the values passed.
2763 my $template_filename = $passedoptions->{"TemplateFilename"};
2764 my $new_template_filename = $passedoptions->{"NewTemplateFilename"};
2765 my $new_template_name = $passedoptions->{"NewTemplateName"};
2766 my $new_template_description = $passedoptions->{"NewTemplateDescription"};
2767 my $new_template_layout = $passedoptions->{"NewTemplateLayout"};
2769 # Check if the template database permissions are valid.
2771 my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2772 my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2774 if ($templatedb_permissions eq 1){
2776 if ($templatedb_exists eq 0){
2777 $error = "TemplateDatabaseInvalidPermissionsSet";
2783 # Check if the template exists.
2785 $template_string_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 );
2786 $template_string_handle->execute();
2788 while (@template_info = $template_string_handle->fetchrow_array()){
2790 $template_found = 1;
2794 # Check to see if the template was found and set an error value if
2797 if ($template_found eq 0){
2799 $error = "TemplateDoesNotExist";
2804 # Get the date and time.
2806 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2807 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2809 # Update the template information.
2811 $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2812 filename = \'' . $class->convert($new_template_filename) . '\',
2813 templatename = \'' . $class->convert($new_template_name) . '\',
2814 templatedescription = \'' . $class->convert($new_template_description) . '\',
2815 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2816 datemodified = \'' . $class->convert($templatenewdate) . '\'
2817 WHERE filename = \'' . $class->convert($template_filename) . '\'
2818 ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2819 $template_string_handle->execute();
2823 #################################################################################
2824 # General subroutines. #
2825 #################################################################################
2828 #################################################################################
2829 # connect: Connect to the server. #
2833 # $dbmodule->connect(); #
2834 #################################################################################
2836 # This function is not needed in this database module.
2841 #################################################################################
2842 # connect: Disconnect from the server. #
2846 # $dbmodule->disconnect(); #
2847 #################################################################################
2849 # This function is not needed in this database module.
2851 undef $string_handle;