Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Version 0.2.0 Import. Speed of Kiriwrite optimised and other alterations
[kiriwrite/.git] / cgi-files / Modules / Database / SQLite.pm
1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm)                #
3 # Database module for mainipulating SQLite databases in the database directory. #
4 #                                                                               #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk>              #
6 #                                                                               #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
8 #                                                                               #
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.                             #
12 #                                                                               #
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.#
16 #                                                                               #
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.
28 use strict;
29 use warnings;
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION    = "0.2.0";
38 my ($options, %options);
39 my $database_handle;
40 my $statement_handle;
41 my $error = "";
42 my $errorext = "";
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_statement_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_statement_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_statement_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines.                                                          #
59 #################################################################################
61 sub new{
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite                        #
64 #                                                                               #
65 # Usage:                                                                        #
66 #                                                                               #
67 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
68 #################################################################################
69         
70         # Get the perl module name.
72         my $class = shift;
73         my $self = {};
75         return bless($self, $class);
77 }
79 sub loadsettings{
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module                  #
82 #                                                                               #
83 # Usage:                                                                        #
84 #                                                                               #
85 # $dbmodule->loadsettings(Directory, options);                                  #
86 #                                                                               #
87 # options       Specifies the following options (in any order).                 #
88 #                                                                               #
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.
103         my $class = shift;
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).
109         %options = (
110                 "Directory"     => $passedoptions->{"Directory"},
111                 "DateTime"      => $passedoptions->{"DateTime"},
112         );
116 sub convert{
117 #################################################################################
118 # convert: Converts data into SQL formatted data.                               #
119 #                                                                               #
120 # Usage:                                                                        #
121 #                                                                               #
122 # $dbmodule->convert(data);                                                     #
123 #                                                                               #
124 # data          Specifies the data to convert.                                  #
125 #################################################################################
127         # Get the data passed to the subroutine.
129         my $class       = shift;
130         my $data        = shift;
132         if (!$data){
133                 $data = "";
134         }
136         $data =~ s/'/''/g;
137         $data =~ s/\b//g;
139         return $data;
143 sub dateconvert{
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date.                          #
146 #                                                                               #
147 # Usage:                                                                        #
148 #                                                                               #
149 # $dbmodule->dateconvert(date);                                                 #
150 #                                                                               #
151 # date          Specifies the date to convert.                                  #
152 #################################################################################
154         # Get the date passed to the subroutine.
156         my $class       = shift;
157         my $data        = shift;
159         # Convert the date given into the proper date.
161         # Create the following varialbes to be used later.
163         my $date;
164         my $time;
165         my $day;
166         my $day_full;
167         my $month;
168         my $month_check;
169         my $month_full;
170         my $year;
171         my $year_short;
172         my $hour;
173         my $hour_full;
174         my $minute;
175         my $minute_full;
176         my $second;
177         my $second_full;
178         my $seek = 0;
179         my $timelength;
180         my $datelength;
181         my $daylength;
182         my $secondlength;
183         my $startchar = 0;
184         my $char;
185         my $length;
186         my $count = 0;
188         # Split the date and time.
190         $length = length($data);
192         if ($length > 0){
194                 do {
196                         # Get the character and check if it is a space.
198                         $char = substr($data, $seek, 1);
200                         if ($char eq ' '){
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);
208                         }
210                         $seek++;
212                 } until ($seek eq $length);
214                 # Get the year, month and date.
216                 $length = length($date);
217                 $seek = 0;
219                 do {
221                         # Get the character and check if it is a dash.
223                         $char = substr($date, $seek, 1);
225                         if ($char eq '-'){
227                                 # The character is a dash, so get the year, month or day.
229                                 $datelength = $seek - $startchar;
231                                 if ($count eq 0){
233                                         # Get the year from the date.
235                                         $year           = substr($date, 0, $datelength) + 1900;
236                                         $startchar      = $seek;
237                                         $count = 1;
239                                         # Get the last two characters to get the short year
240                                         # version.
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.
253                                         if ($month < 10){
255                                                 $month_full = '0' . $month;
257                                         } else {
259                                                 $month_full = $month;
261                                         }
263                                         $startchar      = $seek;
264                                         $count = 2;
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.
272                                         if ($day < 10){
274                                                 $day_full       = '0' . $day;
276                                         } else {
278                                                 $day_full       = $day;
280                                         }
282                                 }
284                         }
286                         $seek++;
288                 } until ($seek eq $length);
290                 # Get the length of the time value and reset certain
291                 # values to 0.
293                 $length = length($time);
294                 $seek = 0;
295                 $count = 0;
296                 $startchar = 0;
298                 do {
300                         # Get the character and check if it is a colon.
302                         $char = substr($time, $seek, 1);
304                         if ($char eq ':'){
306                                 # The character is a colon, so get the hour, minute and day.
308                                 $timelength = $seek - $startchar;
310                                 if ($count eq 0){
312                                         # Get the hour from the time.
314                                         $hour = substr($time, 0, $timelength);
315                                         $count = 1;
316                                         $startchar = $seek;
318                                         # If the hour is less than ten then add a
319                                         # zero.
321                                         if ($hour < 10){
323                                                 $hour_full = '0' . $hour;
325                                         } else {
327                                                 $hour_full = $hour;
329                                         }
331                                 } elsif ($count eq 1){
333                                         # Get the minute and second from the time.
335                                         $minute = substr($time, $startchar + 1, $timelength - 1);
336                                         $count = 2;
337                                                 
338                                         # If the minute is less than ten then add a
339                                         # zero.
341                                         if ($minute < 10){
343                                                 $minute_full = '0' . $minute;
345                                         } else {
347                                                 $minute_full = $minute;
349                                         }
351                                         $startchar = $seek;
353                                         $secondlength = $length - $seek + 1;
354                                         $second = substr($time, $startchar + 1, $secondlength);
355                                         
356                                         # If the second is less than ten then add a
357                                         # zero.
359                                         if ($second < 10){
361                                                 $second_full = '0' . $second;
363                                         } else {
365                                                 $second_full = $second;
367                                         }
369                                 }
371                         }
373                         $seek++;
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;
385                 $data =~ s/D/$day/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;
398         }
400         return $data;
404 sub geterror{
405 #################################################################################
406 # geterror: Gets the error message (or extended error message).                 #
407 #                                                                               #
408 # Usage:                                                                        #
409 #                                                                               #
410 # $dbmodule->geterror(extended);                                                #
411 #                                                                               #
412 # Extended      Specifies if the extended error should be retrieved.            #
413 #################################################################################
415         # Get the data passed to the subroutine.
417         my $class       = shift;
418         my $extended    = shift;
420         if (!$extended){
421                 $extended = 0;
422         }
424         if (!$errorext){
425                 $errorext = "";
426         }
428         if (!$error){
429                 $error = "";
430         }
432         # Check to see if extended information should be returned.
434         if ($extended eq 1){
436                 # Extended information should be returned.
438                 return $errorext;
440         } else {
442                 # Basic information should be returned.
444                 return $error;
446         }
450 sub dbpermissions{
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid.           #
453 #                                                                               #
454 # Usage:                                                                        #
455 #                                                                               #
456 # $database->dbpermissions(dbname, read, write);                                #
457 #                                                                               #
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.
469         if ($readper){
471                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473                         # The database can be read.
475                 } else {
477                         # The database cannot be read, so return a value
478                         # of 1.
480                         return 1;
482                 }
484         }
486         # Check if the database can be written.
488         if ($writeper){
490                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492                         # The database can be read.
494                 } else {
496                         # The database cannot be read, so return a value
497                         # of 1.
499                         return 1;
501                 }
503         }
505         # No errors have occured while checking so return a value
506         # of 0.
508         return 0;
512 sub dbexists{
513 #################################################################################
514 # dbexists: Check if the database exists.                                       #
515 #                                                                               #
516 # Usage:                                                                        #
517 #                                                                               #
518 # $dbmodule->dbexists(dbname);                                                  #
519 #                                                                               #
520 # dbname        Specifies the database name to check.                           #
521 #################################################################################
523         # Get the value that was passed to the subroutine.
525         my $class       = shift;
526         my ($filename)  = @_;
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.
535                 return 0;
537         } else {
539                 # Specified file does not exist so return a value of 1.
541                 return 1;
543         }
547 #################################################################################
548 # Database Subroutines.                                                         #
549 #################################################################################
551 sub getdblist{
552 #################################################################################
553 # getdblist: Gets the list of available databases.                              #
554 #                                                                               #
555 # Usage:                                                                        #
556 #                                                                               #
557 # $dbmodule->getdblist();                                                       #
558 #################################################################################
560         # Get the list of databases.
562         my @data_directory;
563         my @data_directory_final;
564         my $database;
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
578                         # are valid.
580                 } else {
582                         # The permission settings for reading the directory
583                         # are invalid so return an error value.
585                         $error = "DataDirInvalidPermissions";
586                         return;
588                 }
590         } else {
592                 # The database directory does not exist, so return an
593                 # error value.
595                 $error = "DataDirMissing";
596                 return;
598         }
600         opendir(DATADIR, $options{"Directory"});
601         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
602         closedir(DATADIR);
604         # Process the list of databases.
606         foreach $database (@data_directory){
608                 $database =~ s/.db.sqlite$//og;
609                 $database_filename_friendly = $database;
611                 #$database_filename_length = length($database);
612                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
613                 push(@data_directory_final, $database_filename_friendly);
615         }
617         # Return the list of databases.
619         return @data_directory_final;
623 sub getdatabaseinfo{
624 #################################################################################
625 # getdatabaseinfo: Get information about the database.                          #
626 #                                                                               #
627 # Usage:                                                                        #
628 #                                                                               #
629 # $dbmodule->getdatabaseinfo();                                                 #
630 #################################################################################
632         # Get the database information.
634         my $class = shift;
635         my ($databaseinfo, %databaseinfo);
636         my ($sqldata, @sqldata);
638         $error = "";
639         $errorext = "";
641         $statement_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
642                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
643         );
644         $statement_handle->execute();
646         @sqldata = $statement_handle->fetchrow_array();
648         # Process the database information into a hash.
650         %databaseinfo = (
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]
658         );
660         $statement_handle->finish();
661         undef $statement_handle;
663         return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be  #
670 # moved or copied to.                                                           #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getseconddatabaseinfo();                                           #
675 #################################################################################
677         # Get the database information.
679         my $class = shift;
680         my ($databaseinfo, %databaseinfo);
681         my ($sqldata, @sqldata);
683         $error = "";
684         $errorext = "";
686         $second_statement_handle = $second_database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
687                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
688         );
689         $second_statement_handle->execute();
691         @sqldata = $second_statement_handle->fetchrow_array();
693         # Process the database information into a hash.
695         %databaseinfo = (
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]
703         );
705         $second_statement_handle->finish();
706         undef $second_statement_handle;
708         return %databaseinfo;
712 sub adddatabase{
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database.                                       #
715 #                                                                               #
716 # Usage:                                                                        #
717 #                                                                               #
718 # $dbmodule->adddatabase(options);                                              #
719 #                                                                               #
720 # options       Specifies the following options in any order.                   #
721 #                                                                               #
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.
734         $error  = "";
735         $errorext = "";
737         my $class       = shift;
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";
758                 return;
760         }
762         # Create the database structure.
764         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765         $database_handle->{unicode} = 1;
766         $statement_handle               = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767                         name varchar(256) primary key, 
768                         description varchar(512), 
769                         notes text, 
770                         categories varchar(512), 
771                         kiriwrite_version_major int(4), 
772                         kiriwrite_version_minor int(4), 
773                         kiriwrite_version_revision int(4)
774         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775         $statement_handle->execute();
777         $statement_handle       = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778                         filename varchar(256) primary key, 
779                         pagename varchar(512), 
780                         pagedescription varchar(512), 
781                         pagesection varchar(256),
782                         pagetemplate varchar(64),
783                         pagedata text,
784                         pagesettings int(1),
785                         lastmodified datetime
786         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787         $statement_handle->execute();
789         # Convert the values into SQL query formatted values and add an entry
790         # to the kiriwrite_database_info table.
792         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793                 \'' . $class->convert($dbname) . '\',
794                 \'' . $class->convert($dbdescription) . '\',
795                 \'' . $class->convert($dbnotes) . '\',
796                 \'' . $class->convert($dbcategories) . '\',
797                 \'' . $class->convert($dbmajorver) . '\',
798                 \'' . $class->convert($dbminorver) . '\',
799                 \'' . $class->convert($dbrevisionver) . '\'
800         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801         $statement_handle->execute();
805 sub editdatabase{
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database.                                     #
808 #                                                                               #
809 # Usage:                                                                        #
810 #                                                                               #
811 # $dbmodule->editdatabase(options);                                             #
812 #                                                                               #
813 # options               Specifies the following options in any order.           #
814 #                                                                               #
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 #################################################################################
822         $error          = "";
823         $errorext       = "";
825         my $class       = shift;
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
836         # database filename.
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"}){
844                         
845                         # The directory is readable.
847                 } else {
849                         # The directory is not readable so set the error value.
851                         $error = "DataDirInvalidPermissions";
853                         return;
855                 }
857                 if (-w $options{"Directory"}){
859                         # The directory is writeable.
861                 } else {
863                         # The directory is not writeable so set the error value.
865                         $error = "DataDirInvalidPermissions";
867                         return;
869                 }
871                 # Check if a database filename already exists before using the
872                 # new filename.
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";
881                         return;
883                 }
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";
894                         return;
896                 }
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;
915         }
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";
926                 return;
928         }
930         # Get the current database information.
932         $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933         $statement_handle->execute();
935         my @database_oldinfo    = $statement_handle->fetchrow_array();
937         my $dboldname           = $database_oldinfo[0];
939         # Update the database information.
941         $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942         description = \'' . $class->convert($dbdescription) . '\',
943         notes = \'' . $class->convert($dbnotes) . '\',
944         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945         $statement_handle->execute();
947         undef $statement_handle;
948         return;
952 sub deletedatabase{
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database.                                 #
955 #                                                                               #
956 # Usage:                                                                        #
957 #                                                                               #
958 # $dbmodule->deletedatabase(options);                                           #
959 #                                                                               #
960 # options       Specifies the following options in any order.                   #
961 #                                                                               #
962 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
963 #################################################################################
965         $error          = "";
966         $errorext       = "";
968         # Get the database filename.
970         my $class               = shift;
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";
984                 return;
986         }
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
995                 # value.
997                 $error = "InvalidPermissionsSet";
998                 return;
1000         }
1002         # Delete the database.
1004         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1010 sub selectdb{
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database.                                     #
1013 #                                                                               #
1014 # Usage:                                                                        #
1015 #                                                                               #
1016 # $dbmodule->connect(options);                                                  #
1017 #                                                                               #
1018 # options       Specifies the following options in any order.                   #
1019 #                                                                               #
1020 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1021 #################################################################################
1023         # Get the database name.
1025         $error = "";
1026         $errorext = "";
1028         my $class = shift;
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";
1045                 return;
1047         }
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
1057                 # permissions set.
1059                 $error = "InvalidPermissionsSet";
1060                 
1061                 return;
1063         }
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;
1073 sub selectseconddb{
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1076 # pages to.                                                                     #
1077 #                                                                               #
1078 # Usage:                                                                        #
1079 #                                                                               #
1080 # $dbmodule->selectseconddb(options);                                           #
1081 #                                                                               #
1082 # options       Specifies the following options in any order.                   #
1083 #                                                                               #
1084 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1085 #################################################################################
1087         # Get the database name.
1089         $error = "";
1090         $errorext = "";
1092         my $class = shift;
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";
1109                 return;
1111         }
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
1121                 # permissions set.
1123                 $error = "InvalidPermissionsSet";
1124                 
1125                 return;
1127         }
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 #################################################################################
1141 sub getpagelist{
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database.                        #
1144 #                                                                               #
1145 # Usage:                                                                        #
1146 #                                                                               #
1147 # $dbmodule->getpagelist(options);                                              #
1148 #                                                                               #
1149 # options       Specifies the following options in any order.                   #
1150 #                                                                               #
1151 # StartFrom     Start from the specified page in the database.                  #
1152 # Limit         Get the amount of pages given.                                  #
1153 #################################################################################
1155         $error = "";
1156         $errorext = "";
1158         my $class               = shift;
1159         my ($passedoptions)     = shift;
1161         my $start_from  = $passedoptions->{"StartFrom"};
1162         my $limit       = $passedoptions->{"Limit"};
1164         if (defined($start_from)){
1166                 if (!$limit){
1167                         
1168                         $limit = 0;
1170                 }
1172                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1173                 $statement_handle->execute();
1175         } else {
1177                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1178                 $statement_handle->execute();
1180         }
1182         my @database_pagefilenames;
1183         my @database_pagefilenames_final;
1185         # Process the collected pages.
1187         while (@database_pagefilenames = $statement_handle->fetchrow_array){
1189                 # Add each page to the list of pages in the database.
1191                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1193         }
1195         undef $statement_handle;
1196         return @database_pagefilenames_final;
1200 sub getpagecount{
1201 #################################################################################
1202 # getpagecount: Get the count of pages that are in the database.                #
1203 #                                                                               #
1204 # Usage:                                                                        #
1205 #                                                                               #
1206 # $dbmodule->getpagecount();                                                    #
1207 #################################################################################
1209         $error = "";
1210         $errorext = "";
1212         my $class       = shift;
1214         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1215         $statement_handle->execute();
1217         my $count = $statement_handle->fetchrow_array();
1219         return $count;
1223 sub getpageinfo{
1224 #################################################################################
1225 # getpageinfo: Gets the page information from the filename passed.              #
1226 #                                                                               #
1227 # Usage:                                                                        #
1228 #                                                                               #
1229 # $dbmodule->getpageinfo(options);                                              #
1230 #                                                                               #
1231 # options       Specifies the following options in any order.                   #
1232 #                                                                               #
1233 # PageFilename  Specifies the page filename to get the page information from.   #
1234 # Reduced       Specifies if the reduced version of the page information should #
1235 #               be retrieved.                                                   #
1236 #################################################################################
1238         $error = "";
1239         $errorext = "";
1241         my $class               = shift;
1242         my ($passedoptions)     = shift;
1243         my (%database_page, $database_page);
1244         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1246         my @data_page;
1247         my $page_found = 0;
1249         # Get the page from the database.
1251         my $page_filename       = $passedoptions->{"PageFilename"};
1252         my $page_reduced        = $passedoptions->{"Reduced"};
1254         if (!$page_reduced){
1256                 $page_reduced = 0;
1258         }
1260         if ($page_reduced eq 1){
1262                 $statement_handle       = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1264                 $statement_handle->execute();
1266                 # Check if the page exists in the database.
1268                 while (@data_page = $statement_handle->fetchrow_array()){
1269         
1270                         # Get the values from the array.
1271         
1272                         $pagefilename           = $data_page[0];
1273                         $pagename               = $data_page[1];
1274                         $pagedescription        = $data_page[2];
1275                         $pagelastmodified       = $data_page[3];
1276         
1277                         # Put the values into the page hash.
1278         
1279                         %database_page = (
1280                                 "PageFilename"          => $pagefilename,
1281                                 "PageName"              => $pagename,
1282                                 "PageDescription"       => $pagedescription,
1283                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1284                         );
1285         
1286                         $page_found = 1;
1287         
1288                 }
1291         } else {
1292                 
1293                 $statement_handle       = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1295                 $statement_handle->execute();
1297                 # Check if the page exists in the database.
1299                 while (@data_page = $statement_handle->fetchrow_array()){
1300         
1301                         # Get the values from the array.
1302         
1303                         $pagefilename           = $data_page[0];
1304                         $pagename               = $data_page[1];
1305                         $pagedescription        = $data_page[2];
1306                         $pagesection            = $data_page[3];
1307                         $pagetemplate           = $data_page[4];
1308                         $pagedata               = $data_page[5];
1309                         $pagesettings           = $data_page[6];
1310                         $pagelastmodified       = $data_page[7];
1311         
1312                         # Put the values into the page hash.
1313         
1314                         %database_page = (
1315                                 "PageFilename"          => $pagefilename,
1316                                 "PageName"              => $pagename,
1317                                 "PageDescription"       => $pagedescription,
1318                                 "PageSection"           => $pagesection,
1319                                 "PageTemplate"          => $pagetemplate,
1320                                 "PageContent"           => $pagedata,
1321                                 "PageSettings"          => $pagesettings,
1322                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1323                         );
1324         
1325                         $page_found = 1;
1326         
1327                 }
1329         }
1330         
1331         # Check if the page did exist.
1333         if (!$page_found){
1335                 $error = "PageDoesNotExist";
1336                 return;
1338         }
1340         undef $statement_handle;
1341         return %database_page;
1345 sub addpage{
1346 #################################################################################
1347 # addpage: Add a page to the selected database.                                 #
1348 #                                                                               #
1349 # Usage:                                                                        #
1350 #                                                                               #
1351 # $dbmodule->addpage(options);                                                  #
1352 #                                                                               #
1353 # options       Specifies the following options in any order.                   #
1354 #                                                                               #
1355 # PageFilename          Specifies the page filename to use.                     #
1356 # PageName              Specifies the page name to use.                         #
1357 # PageDescription       Specifies the page description to use.                  #
1358 # PageSection           Specifies the page section to use.                      #
1359 # PageTemplate          Specifies the page template to use.                     #
1360 # PageContent           Specifies the page content to use.                      #
1361 # PageSettings          Specifies the page settings to use.                     #
1362 #################################################################################
1364         # Get the data that was passed to the subroutine.
1366         $error = "";
1367         $errorext = "";
1369         my $class               = shift;
1370         my ($passedoptions)     = shift;
1372         my @database_page;
1373         my $page_count = 0;
1375         # Get the values passed to the hash.
1377         my $page_filename       = $passedoptions->{"PageFilename"};
1378         my $page_name           = $passedoptions->{"PageName"};
1379         my $page_description    = $passedoptions->{"PageDescription"};
1380         my $page_section        = $passedoptions->{"PageSection"};
1381         my $page_template       = $passedoptions->{"PageTemplate"};
1382         my $page_content        = $passedoptions->{"PageContent"};
1383         my $page_settings       = $passedoptions->{"PageSettings"};
1385         # Check to see if the filename given already exists
1386         # in the page database.
1388         $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1389         $statement_handle->execute();
1391         # Check if a page with the filename given really does
1392         # exist.
1394         while (@database_page = $statement_handle->fetchrow_array()){
1396                 # A page does exist so increment the count to 1.
1398                 $page_count++;
1399                 
1400         }
1402         if ($page_count ne 0){
1404                 # The page does exist so set the error value.
1406                 $error = "PageExists";
1407                 return;
1409         }
1411         # Check if certain values are undefined.
1413         if (!$page_name){
1415                 $page_name = "";
1417         }
1419         if (!$page_description){
1421                 $page_description = "";
1423         }
1425         if (!$page_section){
1427                 $page_section = "";
1429         }
1431         if (!$page_content){
1433                 $page_content = "";
1435         }
1437         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1438         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1440         # Add the page to the selected database.
1442         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1443                 \'' . $class->convert($page_filename) . '\',
1444                 \'' . $class->convert($page_name) . '\',
1445                 \'' . $class->convert($page_description) . '\',
1446                 \'' . $class->convert($page_section) . '\',
1447                 \'' . $class->convert($page_template) . '\',
1448                 \'' . $class->convert($page_content) . '\',
1449                 \'' . $class->convert($page_settings) . '\',
1450                 \'' . $class->convert($page_date) . '\'
1451         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1452         $statement_handle->execute();
1454         undef $statement_handle;
1458 sub deletepage{
1459 #################################################################################
1460 # deletepage: Delete a page from the selected database.                         #
1461 #                                                                               #
1462 # Usage:                                                                        #
1463 #                                                                               #
1464 # $dbmodule->deletepage(options)                                                #
1465 #                                                                               #
1466 # options       Specifies the following options in any order.                   #
1467 #                                                                               #
1468 # PageFilename  Specifies the page filename to delete.                          #
1469 #################################################################################
1471         $error = "";
1472         $errorext = "";
1474         # Get the data that was passed to the subroutine.
1476         my $class = shift;
1477         my ($passedoptions) = @_;
1478         my @page_info;
1479         my $page_found = 0;
1481         # Get the page filename.
1483         my $page_filename = $passedoptions->{"PageFilename"};
1485         # Check if the page exists before deleting it.
1487         $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1488         $statement_handle->execute();
1490         while (@page_info = $statement_handle->fetchrow_array()){
1492                 $page_found = 1;
1494         }
1496         # Check if the page really does exist.
1498         if (!$page_found){
1500                 $error = "PageDoesNotExist";
1501                 return;
1503         }
1505         # Delete the page.
1507         $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1508         $statement_handle->execute();
1512 sub editpage{
1513 #################################################################################
1514 # editpage: Edit a page from the selected database.                             #
1515 #                                                                               #
1516 # Usage:                                                                        #
1517 #                                                                               #
1518 # $dbmodule->editpage(options);                                                 #
1519 #                                                                               #
1520 # options       Specifies the following options in any order.                   #
1521 #                                                                               #
1522 # PageFilename          Specifies the filename to edit.                         #
1523 # PageNewFilename       Specifies the new filename to use.                      #
1524 # PageNewName           Specifies the new page name to use.                     #
1525 # PageNewDescription    Specifies the new page description to use.              #
1526 # PageNewSection        Specifies the new page section to use.                  #
1527 # PageNewTemplate       Specifies the new page template to use.                 #
1528 # PageNewContent        Specifies the new page content to use.                  #
1529 # PageNewSettings       Specifies the new page settings to use.                 #
1530 #################################################################################
1532         $error = "";
1533         $errorext = "";
1535         # Get the values passed to the subroutine.
1537         my $class = shift;
1538         my ($passedoptions) = @_;
1539         my $page_found = 0;
1540         my @page_info;
1542         # Get the data that was passed to the subroutine.
1544         my $page_filename       = $passedoptions->{"PageFilename"};
1545         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1546         my $page_newname        = $passedoptions->{"PageNewName"};
1547         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1548         my $page_newsection     = $passedoptions->{"PageNewSection"};
1549         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1550         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1551         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1553         # Check if the page with the filename given exists.
1555         $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1556         $statement_handle->execute();
1558         # Check if the page really does exist.
1560         while (@page_info = $statement_handle->fetchrow_array()){
1562                 # The page information is found.
1564                 $page_found = 1;
1566         }
1568         # Check if the page really does exist.
1570         if (!$page_found){
1572                 $error = "PageDoesNotExist";
1573                 return;
1575         }
1577         # Check if there is a page that already exists with the new
1578         # filename.
1580         $page_found = 0;
1582         if ($page_filename ne $page_newfilename){
1584                 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1585                 $statement_handle->execute();
1587                 # Check if a page really is using the new filename.
1589                 while (@page_info = $statement_handle->fetchrow_array()){
1591                         # The page information is found.
1593                         $page_found = 1;
1595                 }
1597                 if ($page_found eq 1){
1599                         $error = "PageExists";
1600                         return;
1602                 }
1604         }
1606         # Get the current date.
1608         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1609         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1611         # Edit the selected page.
1613         $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_pages SET filename = \'' . $class->convert($page_newfilename) . '\', pagename = \'' . $class->convert($page_newname) . '\', pagedescription = \'' . $class->convert($page_newdescription) . '\', pagesection = \'' . $class->convert($page_newsection) . '\', pagetemplate = \'' . $class->convert($page_newtemplate) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagesettings = \'' . $class->convert($page_newsettings) . '\', lastmodified = \'' . $page_date . '\' WHERE filename = \'' . $class->convert($page_filename) . '\'')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1614         $statement_handle->execute();
1618 sub movepage{
1619 #################################################################################
1620 # movepage: Moves a page from the old database to the new database.             #
1621 #                                                                               #
1622 # Usage:                                                                        #
1623 #                                                                               #
1624 # $dbmodule->movepage(options);                                                 #
1625 #                                                                               #
1626 # options       Specifies the following options in any order.                   #
1627 #                                                                               #
1628 # PageFilename  Specifies the page with the filename to move.                   #
1629 #################################################################################
1631         $error = "";
1632         $errorext = "";
1634         # Get the values passed to the subroutine.
1636         my $class = shift;
1637         my ($passedoptions) = @_;
1639         my (%database_page, $database_page);
1640         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1641         my @page_info;
1642         my $page_found = 0;
1644         # Get the data that was passed to the subroutine.
1646         my $page_filename = $passedoptions->{"PageFilename"};
1648         # Check if the page with the filename given exists.
1650         $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1651         $statement_handle->execute();
1653         # Check if the page really does exist.
1655         while (@page_info = $statement_handle->fetchrow_array()){
1657                 # Get the values from the array.
1659                 $pagefilename           = $page_info[0];
1660                 $pagename               = $page_info[1];
1661                 $pagedescription        = $page_info[2];
1662                 $pagesection            = $page_info[3];
1663                 $pagetemplate           = $page_info[4];
1664                 $pagedata               = $page_info[5];
1665                 $pagesettings           = $page_info[6];
1666                 $pagelastmodified       = $page_info[7];
1668                 # Put the values into the page hash.
1670                 %database_page = (
1671                         "PageFilename"          => $pagefilename,
1672                         "PageName"              => $pagename,
1673                         "PageDescription"       => $pagedescription,
1674                         "PageSection"           => $pagesection,
1675                         "PageTemplate"          => $pagetemplate,
1676                         "PageContent"           => $pagedata,
1677                         "PageSettings"          => $pagesettings,
1678                         "PageLastModified"      => $pagelastmodified,
1679                 );
1681                 # The page information is found.
1683                 $page_found = 1;
1685         }
1687         # Check if the page really does exist.
1689         if (!$page_found){
1691                 $error = "PageDoesNotExist";
1692                 return;
1694         }
1696         # Check if the page with the filename given already exists in
1697         # the database the page is being moved to.
1699         $page_found = 0;
1700         @page_info = ();
1702         $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1703         $second_statement_handle->execute();
1705         while (@page_info = $second_statement_handle->fetchrow_array()){
1707                 $page_found = 1;
1709         }
1710         
1711         # Check if the page really does exist.
1713         if ($page_found){
1715                 $error = "PageAlreadyExists";
1716                 return;
1718         }
1720         # Add the page to the new database.
1722         $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1723                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1724                 \'' . $class->convert($database_page{"PageName"}) . '\',
1725                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1726                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1727                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1728                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1729                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1730                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1731         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1732         $second_statement_handle->execute();
1734         # Delete the page from the old database.
1736         $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1737         $statement_handle->execute();
1741 sub copypage{
1742 #################################################################################
1743 # copypage: Copies a page from the old database to the new database.            #
1744 #                                                                               #
1745 # Usage:                                                                        #
1746 #                                                                               #
1747 # $dbmodule->copypage(options);                                                 #
1748 #                                                                               #
1749 # options       Specifies the following options in any order.                   #
1750 #                                                                               #
1751 # PageFilename  Specifies the page with the filename to copy.                   #
1752 #################################################################################
1754         $error = "";
1755         $errorext = "";
1757         # Get the values passed to the subroutine.
1759         my $class = shift;
1760         my ($passedoptions) = @_;
1762         my (%database_page, $database_page);
1763         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1764         my @page_info;
1765         my $page_found = 0;
1767         # Get the data that was passed to the subroutine.
1769         my $page_filename = $passedoptions->{"PageFilename"};
1771         # Check if the page with the filename given exists.
1773         $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1774         $statement_handle->execute();
1776         # Check if the page really does exist.
1778         while (@page_info = $statement_handle->fetchrow_array()){
1780                 # Get the values from the array.
1782                 $pagefilename           = $page_info[0];
1783                 $pagename               = $page_info[1];
1784                 $pagedescription        = $page_info[2];
1785                 $pagesection            = $page_info[3];
1786                 $pagetemplate           = $page_info[4];
1787                 $pagedata               = $page_info[5];
1788                 $pagesettings           = $page_info[6];
1789                 $pagelastmodified       = $page_info[7];
1791                 # Put the values into the page hash.
1793                 %database_page = (
1794                         "PageFilename"          => $pagefilename,
1795                         "PageName"              => $pagename,
1796                         "PageDescription"       => $pagedescription,
1797                         "PageSection"           => $pagesection,
1798                         "PageTemplate"          => $pagetemplate,
1799                         "PageContent"           => $pagedata,
1800                         "PageSettings"          => $pagesettings,
1801                         "PageLastModified"      => $pagelastmodified,
1802                 );
1804                 # The page information is found.
1806                 $page_found = 1;
1808         }
1810         # Check if the page really does exist.
1812         if (!$page_found){
1814                 $error = "PageDoesNotExist";
1815                 return;
1817         }
1819         # Check if the page with the filename given already exists in
1820         # the database the page is being moved to.
1822         $page_found = 0;
1823         @page_info = ();
1825         $second_statement_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1826         $second_statement_handle->execute();
1828         while (@page_info = $second_statement_handle->fetchrow_array()){
1830                 $page_found = 1;
1832         }
1833         
1834         # Check if the page really does exist.
1836         if ($page_found){
1838                 $error = "PageAlreadyExists";
1839                 return;
1841         }
1843         # Add the page to the new database.
1845         $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1846                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1847                 \'' . $class->convert($database_page{"PageName"}) . '\',
1848                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1849                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1850                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1851                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1852                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1853                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1854         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1855         $second_statement_handle->execute();
1859 #################################################################################
1860 # Filter subroutines.                                                           #
1861 #################################################################################
1863 sub connectfilter{
1864 #################################################################################
1865 # connectfilter: Connect to the filter database.                                #
1866 #                                                                               #
1867 # Usage:                                                                        #
1868 #                                                                               #
1869 # $dbmodule->connectfilter(missingignore);                                      #
1870 #                                                                               #
1871 # missingignore Ignore error about database being missing.                      #
1872 #################################################################################
1874         $error = "";
1875         $errorext = "";
1877         my $class = shift;
1878         my $ignoremissing = shift;
1880         # Check if the template database exists.
1882         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1883         
1884         if ($filterdatabase_exists eq 1){
1886                 $filterdb_exists = 0;
1888                 if (!$ignoremissing){
1890                         $error = "FilterDatabaseDoesNotExist";
1891                         return;
1893                 }
1895         }
1897         # Check if the permission settings for the template database are valid.
1899         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1901         if ($filterdb_permissions eq 1){
1903                 # The template database has invalid permissions set
1904                 # so return an error value.
1906                 if (!$ignoremissing){
1908                         $error = "FilterDatabaseInvalidPermissionsSet";
1909                         return;
1911                 }
1913         }
1915         # Connect to the template database.
1917         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1918         $filterdb_database_handle->{unicode} = 1;
1919         $filterdb_loaded = 1;
1923 sub disconnectfilter{
1924 #################################################################################
1925 # disconnectfilter: Disconnect from the filter database.                        #
1926 #                                                                               #
1927 # Usage:                                                                        #
1928 #                                                                               #
1929 # $dbmodule->disconnectfilter();                                                #
1930 #################################################################################
1932         # Disconnect the template database.
1934         if ($filterdb_loaded eq 1){
1936                 undef $filterdb_statement_handle;
1937                 $filterdb_database_handle->disconnect();
1939         }
1943 sub getfiltercount{
1944 #################################################################################
1945 # getfiltercount: Gets the count of filters in the filters database.            #
1946 #                                                                               #
1947 # Usage:                                                                        #
1948 #                                                                               #
1949 # $dbmodule->getfiltercount();                                                  #
1950 #################################################################################
1952         $error = "";
1953         $errorext = "";
1955         my $class       = shift;
1957         $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_filters') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return);
1958         $filterdb_statement_handle->execute();
1960         my $count = $filterdb_statement_handle->fetchrow_array();
1962         return $count;  
1966 sub getfilterlist{
1967 #################################################################################
1968 # getfilterlist: Gets the list of filters in the filter database.               #
1969 #                                                                               #
1970 # Usage:                                                                        #
1971 #                                                                               #
1972 # $dbmodule->getfilterlist(options);                                            #
1973 #                                                                               #
1974 # StartFrom     Specifies where the list of filters should start from.          #
1975 # Limit         Specifies the amount of the filters to get.                     #
1976 #################################################################################
1978         $error = "";
1979         $errorext = "";
1981         my $class               = shift;
1982         my ($passedoptions)     = shift;
1983         
1984         my @filter_list;
1985         my @filter_data;
1987         my $start_from  = $passedoptions->{"StartFrom"};
1988         my $limit       = $passedoptions->{"Limit"};
1990         if (defined($start_from)){
1992                 if (!$limit){
1993                         
1994                         $limit = 0;
1996                 }
1998                 $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC LIMIT ' . $start_from . ',' . $limit) or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1999                 $filterdb_statement_handle->execute();
2001         } else {
2003                 $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2004                 $filterdb_statement_handle->execute();
2006         }
2008         # Get the list of filters available.
2010         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2012                 # Add the filter to the list of available filters.
2014                 push(@filter_list, $filter_data[0]);
2016         }
2018         undef $filterdb_statement_handle;
2019         return @filter_list;
2023 sub getfilterinfo{
2024 #################################################################################
2025 # getfilterinfo: Gets information about the filter.                             #
2026 #                                                                               #
2027 # Usage:                                                                        #
2028 #                                                                               #
2029 # $dbmodule->getfilterinfo(options);                                            #
2030 #                                                                               #
2031 # options       Specifies the following options in any order.                   #
2032 #                                                                               #
2033 # FilterID      Specifies the filter ID number to get information from.         #
2034 # Reduced       Specifies if the reduced version of the filter information      #
2035 #               should be retrieved.                                            #
2036 #################################################################################
2038         $error = "";
2039         $errorext = "";
2041         # Get the values passed to the subroutine.
2043         my $class               = shift;
2044         my ($passedoptions)     = @_;
2046         my %filter_info;
2047         my $filter_exists       = 0;
2048         my @filter_data;
2050         # Get the values that are in the hash.
2052         my $filter_id           = $passedoptions->{"FilterID"};
2053         my $reduced             = $passedoptions->{"Reduced"};
2054         
2055         if ($reduced && $reduced eq 1){
2057                 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2058                 $filterdb_statement_handle->execute();
2060         } else {
2062                 $filterdb_statement_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 );
2063                 $filterdb_statement_handle->execute();
2065         }
2067         # Get the filter information.
2069         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2071                 $filter_info{"FilterID"}        = $filter_data[0];
2072                 $filter_info{"FilterPriority"}  = $filter_data[1];
2073                 $filter_info{"FilterFind"}      = $filter_data[2];
2074                 $filter_info{"FilterReplace"}   = $filter_data[3];
2075                 $filter_info{"FilterNotes"}     = $filter_data[4];
2077                 $filter_exists = 1;
2079         }
2081         # Check if the filter exists.
2083         if (!$filter_exists){
2085                 # The filter does not exist so return
2086                 # an error value.
2088                 $error = "FilterDoesNotExist";
2089                 return;
2091         }
2093         # Return the filter information.
2095         undef $filterdb_statement_handle;
2096         return %filter_info;
2100 sub addfilter{
2101 #################################################################################
2102 # addfilter: Adds a filter to the filter database.                              #
2103 #                                                                               #
2104 # Usage:                                                                        #
2105 #                                                                               #
2106 # $dbmodule->addfilter(options);                                                #
2107 #                                                                               #
2108 # options       Specifies the following options in any order.                   #
2109 #                                                                               #
2110 # FindFilter    Specifies the find filter to add.                               #
2111 # ReplaceFilter Specifies the replace filter to add.                            #
2112 # Priority      Specifies the filter priority to use.                           #
2113 # Notes         Specifies the notes to use.                                     #
2114 #################################################################################
2116         $error = "";
2117         $errorext = "";
2119         # Get the values passed to the subroutine.
2121         my $class = shift;
2122         my ($passedoptions) = @_;
2124         # Define some variables for later.
2126         my @database_filters;
2127         my @filterid_list;
2128         my @filterid_check;
2129         my $nofiltertable = 0;
2130         my $filter_found = 0;
2131         my $filter_count = 0;
2132         my $filter_id;
2133         my $new_id;
2135         # Get the values from the hash.
2137         my $filter_find         = $passedoptions->{"FindFilter"};
2138         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2139         my $filter_priority     = $passedoptions->{"Priority"};
2140         my $filter_notes        = $passedoptions->{"Notes"};
2142         # Check if the filter database permissions are valid.
2144         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2145         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2147         if ($filterdb_permissions eq 1){
2149                 if ($filterdb_exists eq 0){
2150                         $error = "FilterDatabaseInvalidPermissionsSet";
2151                         return;
2152                 }
2154         }
2156         # Check if certain values are undefined and if they
2157         # are then set them blank.
2159         if (!$filter_find){
2161                 $filter_find = "";
2163         }
2165         if (!$filter_replace){
2167                 $filter_replace = "";
2169         }
2171         if (!$filter_priority){
2173                 $filter_priority = 1;
2175         }
2177         if (!$filter_notes){
2179                 $filter_notes = "";
2181         }
2183         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2185         if ($directory_permissions eq 1 && $filterdb_exists){
2187                 # The template database cannot be created because of invalid directory
2188                 # permissions so return an error value.
2190                 $error = "FilterDatabaseFileUncreateable";
2191                 return; 
2193         }
2195         # Check if the filter table exists.
2197         $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2199         # Check if there is really no filter table.
2201         if ($nofiltertable){
2203                 # Create the filter database table.
2205                 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2206                         id int(7) primary key,
2207                         priority int(5),
2208                         findsetting varchar(1024),
2209                         replacesetting varchar(1024),
2210                         notes text
2211                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2212                 $filterdb_statement_handle->execute();
2214         }
2216         # Find the lowest filter identification number available.
2218         $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 );
2219         $filterdb_statement_handle->execute();
2221         while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2223                 $filter_id      = $database_filters[0];
2225                 # Add the filter identification to the list of filter IDs.
2227                 push(@filterid_list, $filter_id);
2229         }
2231         $filter_id = "";
2233         # Process each filter looking for a blank available filter.
2235         foreach $filter_id (@filterid_list){
2237                 # Check the next filter ID to see if it's blank.
2239                 $new_id = $filter_id + 1;
2241                 $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 );
2242                 $filterdb_statement_handle->execute();
2244                 # Get the filter identification number.
2246                 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2248                         $filter_found = 1;
2250                 }
2252                 # Check if a filter was found.
2254                 if (!$filter_found){
2256                         # No filter was found using this ID so exit the loop.
2258                         last;
2260                 }
2262                 # Increment the filter count and reset the filter found value.
2264                 $filter_count++;
2265                 $filter_found = 0;
2266                 $new_id = 0;
2268         }
2270         # Check if there were any filters in the filter database.
2272         if (!$filter_count && !$new_id){
2274                 # There were no filters in the filter database so set
2275                 # the new filter identification value to 1.
2277                 $new_id = 1;
2279         }
2281         # Add the filter to the filter database.
2283         $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2284                 \'' . $class->convert($new_id) . '\',
2285                 \'' . $class->convert($filter_priority) . '\',
2286                 \'' . $class->convert($filter_find) . '\',
2287                 \'' . $class->convert($filter_replace) .'\',
2288                 \'' . $class->convert($filter_notes) . '\'
2289         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2290         $filterdb_statement_handle->execute();
2294 sub editfilter{
2295 #################################################################################
2296 # editfilter: Edits a filter in the filter database.                            #
2297 #                                                                               #
2298 # Usage:                                                                        #
2299 #                                                                               #
2300 # $dbmodule->editfilter(options);                                               #
2301 #                                                                               #
2302 # options       Specifies the following options in any order.                   #
2303 #                                                                               #
2304 # FilterID              Specifies the filter to edit.                           #
2305 # NewFindFilter         Specifies the new find filter setting.                  #
2306 # NewReplaceFilter      Specifies the new replace filter setting.               #
2307 # NewFilterPriority     Specifies the new filter priority setting.              #
2308 # NewFilterNotes        Specifies the new notes for the filter.                 #
2309 #################################################################################
2311         $error = "";
2312         $errorext = "";
2314         # Get the values passed to the subroutine.
2316         my $class = shift;
2317         my ($passedoptions) = @_;
2319         my @filter_data;
2320         my $filter_exists = 1;
2321         my $blankfile = 0;
2323         # Get the values from the hash.
2325         my $filter_id           = $passedoptions->{"FilterID"};
2326         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2327         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2328         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2329         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2331         # Check if the filter database permissions are valid.
2333         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2334         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2336         if ($filterdb_permissions eq 1){
2338                 if ($filterdb_exists eq 0){
2339                         $error = "FilterDatabaseInvalidPermissionsSet";
2340                         return;
2341                 }
2343         }
2345         # Check if the filter exists before editing it.
2347         $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 );
2348         $filterdb_statement_handle->execute();
2350         # Check if the filter exists.
2352         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2354                 $filter_exists = 1;
2356         }       
2358         # Check if the filter really does exist.
2360         if (!$filter_exists){
2362                 # The filter does not exist so return
2363                 # an error value.
2365                 $error = "FilterDoesNotExist";
2366                 return;
2368         }
2370         # Edit the selected filter.
2372         $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2373                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2374                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2375                 priority = \'' . $class->convert($filter_newpriority) . '\',
2376                 notes = \'' . $class->convert($filter_newnotes) . '\'
2377         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2378         $filterdb_statement_handle->execute();
2380         undef $filterdb_statement_handle;
2381         return;
2385 sub deletefilter{
2386 #################################################################################
2387 # deletefilter: Deletes a filter from the filter database.                      #
2388 #                                                                               #
2389 # Usage:                                                                        #
2390 #                                                                               #
2391 # $dbmodule->deletefilter(options);                                             #
2392 #                                                                               #
2393 # options       Specifies the following options in any order.                   #
2394 #                                                                               #
2395 # FilterID      Specifies the filter to delete from the filter database.        #
2396 #################################################################################
2398         $error = "";
2399         $errorext = "";
2401         # Get the values passed to the subroutine.
2403         my $class = shift;
2404         my ($passedoptions) = @_;
2406         my $filter_exists = 0;
2407         my @filter_data;
2409         # Get the values from the hash.
2411         my $filter_id           = $passedoptions->{"FilterID"};
2413         # Check if the filter exists before deleting.
2415         $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 );
2416         $filterdb_statement_handle->execute();
2418         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2420                 $filter_exists = 1;
2422         }
2424         # Check to see if the filter really does exist.
2426         if (!$filter_exists){
2428                 $error = "FilterDoesNotExist";
2429                 return;
2431         }
2433         # Delete the filter from the filter database.
2435         $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 );
2436         $filterdb_statement_handle->execute();
2438         undef $filterdb_statement_handle;
2442 #################################################################################
2443 # Template subroutines.                                                         #
2444 #################################################################################
2446 sub connecttemplate{
2447 #################################################################################
2448 # connecttemplate: Connect to the template database.                            #
2449 #                                                                               #
2450 # Usage:                                                                        #
2451 #                                                                               #
2452 # $dbmodule->connecttemplate(missingignore);                                    #
2453 #                                                                               #
2454 # missingignore Ignore errror about database being missing.                     #
2455 #################################################################################
2457         $error = "";
2458         $errorext = "";
2460         my $class = shift;
2461         my $ignoremissing = shift;
2463         # Check if the template database exists.
2465         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2466         
2467         if ($templatedatabase_exists eq 1){
2469                 $templatedb_exists = 0;
2471                 if (!$ignoremissing){
2473                         $error = "TemplateDatabaseDoesNotExist";
2474                         return;
2476                 }
2478         }
2480         # Check if the permission settings for the template database are valid.
2482         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2484         if ($templatedb_permissions eq 1){
2486                 # The template database has invalid permissions set
2487                 # so return an error value.
2489                 if (!$ignoremissing){
2491                         $error = "TemplateDatabaseInvalidPermissionsSet";
2492                         return;
2494                 }
2496         }
2498         # Connect to the template database.
2500         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2501         $template_database_handle->{unicode} = 1;
2502         $templatedb_loaded = 1;
2506 sub disconnecttemplate{
2507 #################################################################################
2508 # disconnecttemplate: Disconnect from the template database.                    #
2509 #                                                                               #
2510 # Usage:                                                                        #
2511 #                                                                               #
2512 # $dbmodule->disconnecttemplate();                                              #
2513 #################################################################################
2515         # Disconnect the template database.
2517         if ($templatedb_loaded eq 1){
2519                 undef $template_statement_handle;
2520                 $template_database_handle->disconnect();
2522         }
2526 sub gettemplatecount{
2527 #################################################################################
2528 # gettemplatecount: Gets the count of templates in the template database.       #
2529 #                                                                               #
2530 # Usage:                                                                        #
2531 #                                                                               #
2532 # $dbmodule->gettemplatecount();                                                #
2533 #################################################################################
2535         $error = "";
2536         $errorext = "";
2537  
2538         my $class       = shift;
2539  
2540         $template_statement_handle      = $template_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_templates') or ( $error = "FilterDatabaseError", $errorext = $template_database_handle->errstr, return);
2541         $template_statement_handle->execute();
2542  
2543         my $count = $template_statement_handle->fetchrow_array();
2544  
2545         return $count;
2549 sub gettemplatelist{
2550 #################################################################################
2551 # gettemplatelist: Gets the list of templates.                                  #
2552 #                                                                               #
2553 # Usage:                                                                        #
2554 #                                                                               #
2555 # $dbmodule->gettemplatelist(options);                                          #
2556 #                                                                               #
2557 # options       Specifies the following options as a hash (in any order).       #
2558 #                                                                               #
2559 # StartFrom     Specifies where the list of templates will start from.          #
2560 # Limit         Specifies how many templates should be retrieved.               #
2561 #################################################################################
2563         $error = "";
2564         $errorext = "";
2566         my $class               = shift;
2567         my ($passedoptions)     = @_;
2569         my $start_from          = $passedoptions->{"StartFrom"};
2570         my $limit               = $passedoptions->{"Limit"};
2572         if (defined($start_from)){
2574                 if (!$limit){
2575                         
2576                         $limit = 0;
2578                 }
2580                 $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 );
2581                 $template_statement_handle->execute();          
2583         } else {
2585                 $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 );
2586                 $template_statement_handle->execute();
2588         }
2590         my @database_template;
2591         my @templates_list;
2592         my $template_filename;
2594         while (@database_template = $template_statement_handle->fetchrow_array()){
2596                 # Get certain values from the array.
2598                 $template_filename      = $database_template[0];
2600                 # Add the template to the list of templates.
2602                 push(@templates_list, $template_filename);
2604         }
2606         return @templates_list;
2610 sub gettemplateinfo{
2611 #################################################################################
2612 # gettemplateinfo: Get information on a template.                               #
2613 #                                                                               #
2614 # Usage:                                                                        #
2615 #                                                                               #
2616 # $dbmodule->gettemplateinfo(options);                                          #
2617 #                                                                               #
2618 # options       Specifies the following options in any order.                   #
2619 #                                                                               #
2620 # TemplateFilename      Specifies the template filename to use.                 #
2621 # Reduced               Specifies a reduced version of template information to  #
2622 #                       get.                                                    #
2623 #################################################################################
2625         $error = "";
2626         $errorext = "";
2628         # Get the data passed to the subroutine.
2630         my $class = shift;
2631         my ($passedoptions) = @_;
2633         my %page_info;
2634         my @template_data;
2636         my $template_filename;
2637         my $template_name;
2638         my $template_description;
2639         my $template_datemodified;
2640         my $template_layout;
2642         my $template_found = 0;
2644         my $filename    = $passedoptions->{"TemplateFilename"};
2645         my $reduced     = $passedoptions->{"Reduced"};
2647         if ($reduced && $reduced eq 1){
2649                 $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 );
2650                 $template_statement_handle->execute();
2652         } else {
2654                 $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 );
2655                 $template_statement_handle->execute();
2657         }
2659         while (@template_data = $template_statement_handle->fetchrow_array()){
2661                 # Get certain values from the array.
2663                 $template_filename      = $template_data[0];
2664                 $template_name          = $template_data[1];
2665                 $template_description   = $template_data[2];
2666                 $template_layout        = $template_data[3];
2667                 $template_datemodified  = $template_data[4];
2669                 # Process them into the hash.
2671                 %page_info = (
2672                         "TemplateFilename" => $template_filename,
2673                         "TemplateName" => $template_name,
2674                         "TemplateDescription" => $template_description,
2675                         "TemplateLayout" => $template_layout,
2676                         "TemplateLastModified" => $template_datemodified
2677                 );
2679                 $template_found = 1;
2681         }
2683         if ($template_found eq 0){
2685                 # The template was not found in the template database so
2686                 # write an error value.
2688                 $error = "TemplateDoesNotExist";
2689                 return;
2691         }
2693         return %page_info;
2697 sub addtemplate{
2698 #################################################################################
2699 # addtemplate: Adds a template to the template database.                        #
2700 #                                                                               #
2701 # Usage:                                                                        #
2702 #                                                                               #
2703 # $dbmodule->addtemplate();                                                     #
2704 #                                                                               #
2705 # options       Specifies the following options in any order.                   #
2706 #                                                                               #
2707 # TemplateFilename      Specifies the new template filename.                    #
2708 # TemplateName          Specifies the new template name.                        #
2709 # TemplateDescription   Specifies the new template description.                 #
2710 # TemplateLayout        Specifies the new template layout.                      #
2711 #################################################################################
2713         $error = "";
2714         $errorext = "";
2716         # Get the data passed to the subroutine.
2718         my $class = shift;
2719         my ($passedoptions) = @_;
2721         my @page_exists;
2722         my $notemplatetable;
2723         my $blankfile = 0;
2725         my $template_filename           = $passedoptions->{"TemplateFilename"};
2726         my $template_name               = $passedoptions->{"TemplateName"};
2727         my $template_description        = $passedoptions->{"TemplateDescription"};
2728         my $template_layout             = $passedoptions->{"TemplateLayout"};
2730         # Check if the template database permissions are valid.
2732         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2733         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2735         if ($templatedb_permissions eq 1){
2737                 if ($templatedb_exists eq 0){
2738                         $error = "TemplateDatabaseInvalidPermissionsSet";
2739                         return;
2740                 }
2742         }
2744         # Check if the template already exists before adding.
2746         if ($templatedb_exists eq 0){
2748                 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2750                 if ($blankfile eq 0){
2752                         $template_statement_handle->execute();
2754                         while (@page_exists = $template_statement_handle->fetchrow_array()){
2756                                 $error = "TemplatePageExists";
2757                                 return;
2759                         }
2761                 }
2763         }
2765         # Get the current date.
2766  
2767         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2768  
2769         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2771         # Check if certain values are undefined and if they
2772         # are then set them blank.
2774         if (!$template_name){
2776                 $template_name = "";
2778         }
2780         if (!$template_description){
2782                 $template_description = "";
2784         }
2786         if (!$template_layout){
2788                 $template_layout = "";
2790         }
2792         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2794         if ($directory_permissions eq 1 && $templatedb_exists){
2796                 # The template database cannot be created because of invalid directory
2797                 # permissions so return an error value.
2799                 $error = "TemplateDatabaseUncreateable";
2800                 return; 
2802         }
2804         # Check to see if a template can be added.
2806         $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2807                                 \'' . $class->convert($template_filename) . '\',
2808                                 \'' . $class->convert($template_name) . '\',
2809                                 \'' . $class->convert($template_description) . '\',
2810                                 \'' . $class->convert($template_layout) . '\',
2811                                 \'' . $class->convert($template_date) . '\'
2812         )') or ( $notemplatetable = 1 );
2814         if (!$notemplatetable){
2816                 $template_statement_handle->execute();
2818         }
2820         # Check to see if there is no template table and attempt to create one.
2822         if ($notemplatetable){
2824                 # Create a template table.
2826                 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2828                 if ($directory_permissions eq 1){
2830                         # The template database cannot be created because of invalid directory
2831                         # permissions so return an error.
2833                         $error = "TemplateDatabaseFileUncreateable";
2834                         return;
2836                 }
2838                 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2839                         filename varchar(256) primary key,
2840                         templatename varchar(512),
2841                         templatedescription varchar(512),
2842                         templatelayout text,
2843                         datemodified datetime
2844                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2845                 $template_statement_handle->execute();
2847                 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2848                                 \'' . $class->convert($template_filename) . '\',
2849                                 \'' . $class->convert($template_name) . '\',
2850                                 \'' . $class->convert($template_description) . '\',
2851                                 \'' . $class->convert($template_layout) . '\',
2852                                 \'' . $class->convert($template_date) . '\'
2853                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2854                 $template_statement_handle->execute();
2856         }
2860 sub deletetemplate{
2861 #################################################################################
2862 # deletetemplate: Deletes a template from the template database.                #
2863 #                                                                               #
2864 # Usage:                                                                        #
2865 #                                                                               #
2866 # $dbmodule->deletetemplate(options);                                           #
2867 #                                                                               #
2868 # options       Specifies the following options in any order.                   #
2869 #                                                                               #
2870 # TemplateFilename      Specifies the template filename to delete.              #
2871 #################################################################################
2873         $error = "";
2874         $errorext = "";
2876         # Get the data passed to the subroutine.
2878         my $class = shift;
2879         my ($passedoptions) = @_;
2881         my @pagedata;
2882         my $template_filename = $passedoptions->{"TemplateFilename"};
2883         my $template_count = 0;
2885         # Check if the template exists.
2887         $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 );
2888         $template_statement_handle->execute();
2890         while (@pagedata = $template_statement_handle->fetchrow_array()){
2892                 $template_count++;
2894         }
2896         if ($template_count eq 0){
2898                 # No pages were returned so return an error value.
2900                 $error = "TemplateDoesNotExist";
2901                 return;
2903         }
2905         # Delete the template from the template database.
2907         $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 );
2908         $template_statement_handle->execute();
2912 sub edittemplate{
2913 #################################################################################
2914 # editttemplate: Edits a Kiriwrite template.                                    #
2915 #                                                                               #
2916 # Usage:                                                                        #
2917 #                                                                               #
2918 # $dbmodule->edittemplate(options);                                             #
2919 #                                                                               #
2920 # options       Specifies the following options in any order.                   #
2921 #                                                                               #
2922 # TemplateFilename              Specifies the template filename to edit.        #
2923 # NewTemplateFilename           Specifies the new template filename.            #
2924 # NewTemplateName               Specifies the new template name.                #
2925 # NewTemplateDescription        Specifies the new template description.         #
2926 # NewTemplateLayout             Specifies the new template layout.              #
2927 #################################################################################
2929         # Get the values passed.
2931         my $class = shift;
2932         my ($passedoptions) = @_;
2933         my $template_found = 0;
2934         my @template_info;
2936         # Process the values passed.
2938         my $template_filename           = $passedoptions->{"TemplateFilename"};
2939         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2940         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2941         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2942         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2944         # Check if the template database permissions are valid.
2946         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2947         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2949         if ($templatedb_permissions eq 1){
2951                 if ($templatedb_exists eq 0){
2952                         $error = "TemplateDatabaseInvalidPermissionsSet";
2953                         return;
2954                 }
2956         }
2958         # Check if the template exists.
2960         $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 );
2961         $template_statement_handle->execute();
2963         while (@template_info = $template_statement_handle->fetchrow_array()){
2965                 $template_found = 1;
2967         }
2969         # Check to see if the template was found and set an error value if
2970         # it wasn't.
2972         if ($template_found eq 0){
2974                 $error = "TemplateDoesNotExist";
2975                 return;
2977         }
2979         # Get the date and time.
2981         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2982         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2984         # Update the template information.
2986         $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2987                 filename = \'' . $class->convert($new_template_filename) . '\',
2988                 templatename = \'' . $class->convert($new_template_name) . '\',
2989                 templatedescription = \'' . $class->convert($new_template_description) . '\',
2990                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2991                 datemodified = \'' . $class->convert($templatenewdate) . '\'
2992                 WHERE filename = \'' . $class->convert($template_filename) . '\'
2993         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2994         $template_statement_handle->execute();
2998 #################################################################################
2999 # General subroutines.                                                          #
3000 #################################################################################
3002 sub connect{
3003 #################################################################################
3004 # connect: Connect to the server.                                               #
3005 #                                                                               #
3006 # Usage:                                                                        #
3007 #                                                                               #
3008 # $dbmodule->connect();                                                         #
3009 #################################################################################
3011         # This function is not needed in this database module.
3015 sub disconnect{
3016 #################################################################################
3017 # connect: Disconnect from the server.                                          #
3018 #                                                                               #
3019 # Usage:                                                                        #
3020 #                                                                               #
3021 # $dbmodule->disconnect();                                                      #
3022 #################################################################################
3024         # This function is not needed in this database module.
3026         undef $statement_handle;
3030 1;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy