Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Commit of recent work in preperation for Kiriwrite 0.5.0
[kiriwrite/.git] / cgi-files / Modules / Database / MySQL5.pm
1 #################################################################################
2 # Kiriwrite Database Module - MySQL 5.x Database Module (MySQL5.pm)             #
3 # Database module for mainipulating data in a MySQL 5.x database.               #
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 Modules::Database::MySQL5;
26 # Enable strict and use warnings.
28 use strict;
29 use warnings;
30 use Encode qw(decode_utf8);
32 # Load the following Perl modules.
34 use DBI qw(:sql_types);
36 # Set the following values.
38 our $VERSION    = "0.5.0";
39 my ($options, %options);
40 my $database_handle;
41 my $statement_handle;
42 my $error;
43 my $errorext;
44 my $database_filename;
45 my $second_database_filename;
47 #################################################################################
48 # Generic Subroutines.                                                          #
49 #################################################################################
51 sub new{
52 #################################################################################
53 # new: Create an instance of Kiriwrite::Database::MySQL                         #
54 #                                                                               #
55 # Usage:                                                                        #
56 #                                                                               #
57 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
58 #################################################################################
59         
60         # Get the perl module name.
62         my $class = shift;
63         my $self = {};
65         return bless($self, $class);
67 }
69 sub loadsettings{
70 #################################################################################
71 # loadsettings: Loads settings into the SQLite database module                  #
72 #                                                                               #
73 # Usage:                                                                        #
74 #                                                                               #
75 # $dbmodule->loadsettings(Directory, options);                                  #
76 #                                                                               #
77 # options       Specifies the following options (in any order).                 #
78 #                                                                               #
79 # Directory     Specifies the directory to use for getting databases.           #
80 # DateTime      Specifies the date and time format to use.                      #
81 # Server        Specifies the server to use.                                    #
82 # Database      Specifies the database to use.                                  #
83 # Username      Specifies the username to use.                                  #
84 # Password      Specifies the password to use.                                  #
85 # Port          Specifies the server port to use.                               #
86 # Protocol      Specifies the protocol to use.                                  #
87 # TablePrefix   Specifies the table prefix to use.                              #
88 #################################################################################
90         # Get the data passed to the subroutine.
92         my $class = shift;
93         my ($passedoptions)     = @_;
95         # Add the directory setting to the list of options (as it's the only
96         # one needed for this database module).
98         %options = (
99                 "Directory"     => $passedoptions->{"Directory"},
100                 "DateTime"      => $passedoptions->{"DateTime"},
101                 "Server"        => $passedoptions->{"Server"},
102                 "Database"      => $passedoptions->{"Database"},
103                 "Username"      => $passedoptions->{"Username"},
104                 "Password"      => $passedoptions->{"Password"},
105                 "Port"          => $passedoptions->{"Port"},
106                 "Protocol"      => $passedoptions->{"Protocol"},
107                 "TablePrefix"   => $passedoptions->{"TablePrefix"}
108         );
112 sub convert{
113 #################################################################################
114 # convert: Converts data into SQL formatted data.                               #
115 #                                                                               #
116 # Usage:                                                                        #
117 #                                                                               #
118 # $dbmodule->convert(data);                                                     #
119 #                                                                               #
120 # data          Specifies the data to convert.                                  #
121 #################################################################################
123         # Get the data passed to the subroutine.
125         my $class       = shift;
126         my $data        = shift;
128         if (!$data){
129                 $data = "";
130         }
132         $data =~ s/'/''/g;
133         $data =~ s/\b//g;
135         return $data;
139 sub dateconvert{
140 #################################################################################
141 # dateconvert: Converts a SQL date into a proper date.                          #
142 #                                                                               #
143 # Usage:                                                                        #
144 #                                                                               #
145 # $dbmodule->dateconvert(date);                                                 #
146 #                                                                               #
147 # date          Specifies the date to convert.                                  #
148 #################################################################################
150         # Get the date passed to the subroutine.
152         my $class       = shift;
153         my $data        = shift;
155         # Convert the date given into the proper date.
157         # Create the following varialbes to be used later.
159         my $date;
160         my $time;
161         my $day;
162         my $day_full;
163         my $month;
164         my $month_check;
165         my $month_full;
166         my $year;
167         my $year_short;
168         my $hour;
169         my $hour_full;
170         my $minute;
171         my $minute_full;
172         my $second;
173         my $second_full;
174         my $seek = 0;
175         my $timelength;
176         my $datelength;
177         my $daylength;
178         my $secondlength;
179         my $startchar = 0;
180         my $char;
181         my $length;
182         my $count = 0;
184         # Split the date and time.
186         $length = length($data);
188         if ($length > 0){
190                 do {
192                         # Get the character and check if it is a space.
194                         $char = substr($data, $seek, 1);
196                         if ($char eq ' '){
198                                 # The character is a space, so get the date and time.
200                                 $date           = substr($data, 0, $seek);
201                                 $timelength     = $length - $seek - 1;
202                                 $time           = substr($data, $seek + 1, $timelength);
204                         }
206                         $seek++;
208                 } until ($seek eq $length);
210                 # Get the year, month and date.
212                 $length = length($date);
213                 $seek = 0;
215                 do {
217                         # Get the character and check if it is a dash.
219                         $char = substr($date, $seek, 1);
221                         if ($char eq '-'){
223                                 # The character is a dash, so get the year, month or day.
225                                 $datelength = $seek - $startchar;
227                                 if ($count eq 0){
229                                         # Get the year from the date.
231                                         $year           = substr($date, 0, $datelength) + 1900;
232                                         $startchar      = $seek;
233                                         $count = 1;
235                                         # Get the last two characters to get the short year
236                                         # version.
238                                         $year_short     = substr($year, 2, 2);
240                                 } elsif ($count eq 1){
242                                         # Get the month and day from the date.
244                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
246                                         # Check if the month is less then 10, if it is
247                                         # add a zero to the value.
249                                         if ($month < 10){
251                                                 $month_full = '0' . $month;
253                                         } else {
255                                                 $month_full = $month;
257                                         }
259                                         $startchar      = $seek;
260                                         $count = 2;
262                                         $daylength      = $length - $seek + 1;
263                                         $day            = substr($date, $startchar + 1, $daylength);
265                                         $day =~ s/^0//;
267                                         # Check if the day is less than 10, if it is
268                                         # add a zero to the value.
270                                         if ($day < 10){
272                                                 $day_full       = '0' . $day;
274                                         } else {
276                                                 $day_full       = $day;
278                                         }
280                                 }
282                         }
284                         $seek++;
286                 } until ($seek eq $length);
288                 # Get the length of the time value and reset certain
289                 # values to 0.
291                 $length = length($time);
292                 $seek = 0;
293                 $count = 0;
294                 $startchar = 0;
296                 do {
298                         # Get the character and check if it is a colon.
300                         $char = substr($time, $seek, 1);
302                         if ($char eq ':'){
304                                 # The character is a colon, so get the hour, minute and day.
306                                 $timelength = $seek - $startchar;
308                                 if ($count eq 0){
310                                         # Get the hour from the time.
312                                         $hour = substr($time, 0, $timelength);
313                                         $hour =~ s/^0//;
314                                         $count = 1;
315                                         $startchar = $seek;
317                                         # If the hour is less than ten then add a
318                                         # zero.
320                                         if ($hour < 10){
322                                                 $hour_full = '0' . $hour;
324                                         } else {
326                                                 $hour_full = $hour;
328                                         }
330                                 } elsif ($count eq 1){
332                                         # Get the minute and second from the time.
334                                         $minute = substr($time, $startchar + 1, $timelength - 1);
335                                         $minute =~ s/^0//;
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                                         $second =~ s/^0//;
356                                         
357                                         # If the second is less than ten then add a
358                                         # zero.
360                                         if ($second < 10){
362                                                 $second_full = '0' . $second;
364                                         } else {
366                                                 $second_full = $second;
368                                         }
370                                 }
372                         }
374                         $seek++;
376                 } until ($seek eq $length);
378                 # Get the setting for displaying the date and time.
380                 $data = $options{"DateTime"};
382                 # Process the setting for displaying the date and time
383                 # using regular expressions
385                 $data =~ s/DD/$day_full/g;
386                 $data =~ s/D/$day/g;
387                 $data =~ s/MM/$month_full/g;
388                 $data =~ s/M/$month/g;
389                 $data =~ s/YY/$year/g;
390                 $data =~ s/Y/$year_short/g;
392                 $data =~ s/hh/$hour_full/g;
393                 $data =~ s/h/$hour/g;
394                 $data =~ s/mm/$minute_full/g;
395                 $data =~ s/m/$minute/g;
396                 $data =~ s/ss/$second_full/g;
397                 $data =~ s/s/$second/g;
399         }
401         return $data;
405 sub geterror{
406 #################################################################################
407 # geterror: Gets the error message (or extended error message).                 #
408 #                                                                               #
409 # Usage:                                                                        #
410 #                                                                               #
411 # $dbmodule->geterror(extended);                                                #
412 #                                                                               #
413 # Extended      Specifies if the extended error should be retrieved.            #
414 #################################################################################
416         # Get the data passed to the subroutine.
418         my $class       = shift;
419         my $extended    = shift;
421         if (!$extended){
422                 $extended = 0;
423         }
425         if (!$errorext){
426                 $errorext = "";
427         }
429         if (!$error){
430                 $error = "";
431         }
433         # Check to see if extended information should be returned.
435         if ($extended eq 1){
437                 # Extended information should be returned.
439                 return $errorext;
441         } else {
443                 # Basic information should be returned.
445                 return $error;
447         }
451 sub dbpermissions{
452 #################################################################################
453 # dbpermissions: Check if the permissions for the database are valid.           #
454 #                                                                               #
455 # Usage:                                                                        #
456 #                                                                               #
457 # $database->dbpermissions(dbname, read, write);                                #
458 #                                                                               #
459 # dbname        Specifies the database name to check.                           #
460 # read          Check to see if the database can be read.                       #
461 # write         Check to see if the database can be written.                    #
462 #################################################################################
464         # This subroutine is not needed for this database module.
466         return 0;
470 sub dbexists{
471 #################################################################################
472 # dbexists: Check if the database exists.                                       #
473 #                                                                               #
474 # Usage:                                                                        #
475 #                                                                               #
476 # $dbmodule->dbexists(dbname);                                                  #
477 #                                                                               #
478 # dbname        Specifies the database name to check.                           #
479 #################################################################################
481         $error = "";
482         $errorext = "";
484         # Get the value that was passed to the subroutine.
486         my $class       = shift;
487         my ($filename)  = @_;
489         my @table_data;
490         my $table_exists = 0;
492         # Check if the table exists.
494         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($filename) . '_database_info\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
495         $statement_handle->execute();
497         while (@table_data = $statement_handle->fetchrow_array()){
499                 $table_exists = 1;
501         }
503         # Check if the table really does exist.
505         if ($table_exists eq 1){
507                 # The table exists so return a value of 0.
509                 return 0;
511         } else {
513                 # The table does not exist so return a value of 1.
515                 return 1;
517         }
522 #################################################################################
523 # General subroutines.                                                          #
524 #################################################################################
526 sub connect{
527 #################################################################################
528 # connect: Connect to the server.                                               #
529 #                                                                               #
530 # Usage:                                                                        #
531 #                                                                               #
532 # $dbmodule->connect();                                                         #
533 #################################################################################
535         $error = "";
536         $errorext = "";
538         # Connect to the server.
540         $database_handle = DBI->connect("DBI:mysql:database=" . $options{"Database"} . ";host=" . $options{"Server"} . ";protocol=" . $options{"Protocol"} . "port=" . $options{"Port"}, $options{"Username"}, $options{"Password"}, { "mysql_enable_utf8" => 1 }) or ( $error = "DatabaseConnectionError", $errorext = DBI->errstr, return );
541         $database_handle->do('SET CHARACTER SET utf8');
542         $database_handle->do('SET NAMES utf8');
546 sub disconnect{
547 #################################################################################
548 # connect: Disconnect from the server.                                          #
549 #                                                                               #
550 # Usage:                                                                        #
551 #                                                                               #
552 # $dbmodule->disconnect();                                                      #
553 #################################################################################
554         
555         # Disconnect from the server.
557         if ($statement_handle){
559                 $statement_handle->finish();
561         }
563         if ($database_handle){
565                 $database_handle->disconnect();
567         }
571 #################################################################################
572 # Database Subroutines.                                                         #
573 #################################################################################
575 sub getdblist{
576 #################################################################################
577 # getdblist: Gets the list of available databases.                              #
578 #                                                                               #
579 # Usage:                                                                        #
580 #                                                                               #
581 # $dbmodule->getdblist();                                                       #
582 #################################################################################
584         $error = "";
585         $errorext = "";
587         # Get the list of databases.
589         $statement_handle = $database_handle->prepare("SHOW TABLES") or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
590         $statement_handle->execute();
592         my @final_table_list;   
593         my @database_table_list;
594         my @table_name;
595         my $table;
597         while (@table_name = $statement_handle->fetchrow_array()){
599                 push(@database_table_list, decode_utf8($table_name[0]));
601         }
603         my $table_prefix = $options{"TablePrefix"};
605         # Find all the database information tables with the correct table prefix.
607         @database_table_list = grep /^$table_prefix/ , @database_table_list;
608         @database_table_list = grep /m*database_info$/ , @database_table_list;
610         foreach $table (@database_table_list){
612                 # Process each table name removing the table prefix name and
613                 # the _database_info part.
615                 $table =~ s/^$table_prefix(_)//g;
616                 $table =~ s/_database_info$//g;
618                 push (@final_table_list, $table);
620         }
622         # Return the final list of databases.
624         return @final_table_list;
628 sub selectdb{
629 #################################################################################
630 # selectdb: Selects the Kiriwrite database.                                     #
631 #                                                                               #
632 # Usage:                                                                        #
633 #                                                                               #
634 # $dbmodule->connect(options);                                                  #
635 #                                                                               #
636 # options       Specifies the following options in any order.                   #
637 #                                                                               #
638 # DatabaseName  Specifies the Kiriwrite database to use.                        #
639 #################################################################################
641         # Get the database name.
643         $error = "";
644         $errorext = "";
646         my $class = shift;
647         my ($passedoptions) = @_;
649         my $dbname = $passedoptions->{"DatabaseName"};
651         my $database_exists = $class->dbexists($dbname);
653         if ($database_exists eq 1){
655                 # The database does not exist so return an error value
656                 # saying that the database does not exist.
658                 $error = "DoesNotExist";
660                 return;
662         }
664         $database_filename = $dbname;
668 sub getdatabaseinfo{
669 #################################################################################
670 # getdatabaseinfo: Get information about the database.                          #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getdatabaseinfo();                                                 #
675 #################################################################################
677         # Get the database information.
679         $error = "";
680         $errorext = "";
682         my $class = shift;
683         my ($databaseinfo, %databaseinfo);
684         my ($sqldata, @sqldata);
686         $statement_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
687         $statement_handle->execute();
689         @sqldata = $statement_handle->fetchrow_array();
691         # Process the database information into a hash.
693         %databaseinfo = (
695                 "DatabaseName"  => decode_utf8($sqldata[0]),
696                 "Description"   => decode_utf8($sqldata[1]),
697                 "Notes"         => decode_utf8($sqldata[2]),
698                 "Categories"    => decode_utf8($sqldata[3]),
699                 "Major"         => decode_utf8($sqldata[4]),
700                 "Minor"         => decode_utf8($sqldata[5]),
701                 "Revision"      => decode_utf8($sqldata[6])
702         );
704         $statement_handle->finish();
706         return %databaseinfo;
710 sub selectseconddb{
711 #################################################################################
712 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
713 # pages to.                                                                     #
714 #                                                                               #
715 # Usage:                                                                        #
716 #                                                                               #
717 # $dbmodule->selectseconddb(options);                                           #
718 #                                                                               #
719 # options       Specifies the following options in any order.                   #
720 #                                                                               #
721 # DatabaseName  Specifies the Kiriwrite database to use.                        #
722 #################################################################################
724         # Get the database name.
726         $error = "";
727         $errorext = "";
729         my $class = shift;
730         my ($passedoptions) = @_;
731         my (%database, $database);
733         my $dbname = $passedoptions->{"DatabaseName"};
735         # Check if the database exists.
737         my $database_exists = $class->dbexists($dbname);
739         if ($database_exists eq 1){
741                 # The database does not exist so return an error value
742                 # saying that the database does not exist.
744                 $error = "DoesNotExist";
746                 return;
748         }
750         # Set the second database filename.
752         $second_database_filename = $dbname;
756 sub getseconddatabaseinfo{
757 #################################################################################
758 # getseconddatabaseinfo: Get information about the database that pages will be  #
759 # moved or copied to.                                                           #
760 #                                                                               #
761 # Usage:                                                                        #
762 #                                                                               #
763 # $dbmodule->getseconddatabaseinfo();                                           #
764 #################################################################################
767         # Get the database information.
769         my $class = shift;
770         my ($databaseinfo, %databaseinfo);
771         my ($sqldata, @sqldata);
773         $error = "";
774         $errorext = "";
776         $statement_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
777         $statement_handle->execute();
779         @sqldata = $statement_handle->fetchrow_array();
781         # Process the database information into a hash.
783         %databaseinfo = (
784                 "DatabaseName"  => decode_utf8($sqldata[0]),
785                 "Description"   => decode_utf8($sqldata[1]),
786                 "Notes"         => decode_utf8($sqldata[2]),
787                 "Categories"    => decode_utf8($sqldata[3]),
788                 "Major"         => decode_utf8($sqldata[4]),
789                 "Minor"         => decode_utf8($sqldata[5]),
790                 "Revision"      => decode_utf8($sqldata[6])
791         );
793         $statement_handle->finish();
795         return %databaseinfo;
799 sub adddatabase{
800 #################################################################################
801 # adddatabase: Adds a Kiriwrite database.                                       #
802 #                                                                               #
803 # Usage:                                                                        #
804 #                                                                               #
805 # $dbmodule->adddatabase(options);                                              #
806 #                                                                               #
807 # options       Specifies the following options in any order.                   #
808 #                                                                               #
809 # DatabaseFilename      Specifies the database file/shortname to use.           #
810 # DatabaseName          Specifies the database name to use.                     #
811 # DatabaseDescription   Specifies the database description to use.              #
812 # DatabaseNotes         Specifies the database notes to use.                    #
813 # DatabaseCategories    Specifies the database categories to use.               #
814 # VersionMajor          Specifies the major version.                            #
815 # VersionMinor          Specifies the minor version.                            #
816 # VersionRevision       Specifies the revision version.                         #
817 #################################################################################
819         # Get the database that was passed to the subroutine.
821         $error  = "";
822         $errorext = "";
824         my $class       = shift;
825         my ($passedoptions) = @_;
827         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
828         my $dbname              = $passedoptions->{"DatabaseName"};
829         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
830         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
831         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
832         my $dbmajorver          = $passedoptions->{"VersionMajor"};
833         my $dbminorver          = $passedoptions->{"VersionMinor"};
834         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
836         # Check if the database with the filename given already exists.
838         my $database_exists     = $class->dbexists($dbfilename);
840         if ($database_exists eq 0){
842                 # The database filename exists so set the error value.
844                 $error = "DatabaseExists";
845                 return;
847         }
849         # Create the database structure (info and page tables);
851         $statement_handle       = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (
852                         name varchar(256) primary key,
853                         description varchar(512), 
854                         notes mediumtext,
855                         categories varchar(512), 
856                         kiriwrite_version_major int(4), 
857                         kiriwrite_version_minor int(4), 
858                         kiriwrite_version_revision int(4)
859         ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
860         $statement_handle->execute();
862         $statement_handle       = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_pages (
863                         filename varchar(256) primary key, 
864                         pagename varchar(512), 
865                         pagedescription varchar(512), 
866                         pagesection varchar(256),
867                         pagetemplate varchar(64),
868                         pagedata mediumtext,
869                         pagesettings int(1),
870                         lastmodified datetime
871         ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
872         $statement_handle->execute();
874         # Convert the values into SQL query formatted values and add an entry
875         # to the kiriwrite_database_info table.
877         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
878                 \'' . $class->convert($dbname) . '\',
879                 \'' . $class->convert($dbdescription) . '\',
880                 \'' . $class->convert($dbnotes) . '\',
881                 \'' . $class->convert($dbcategories) . '\',
882                 \'' . $class->convert($dbmajorver) . '\',
883                 \'' . $class->convert($dbminorver) . '\',
884                 \'' . $class->convert($dbrevisionver) . '\'
885         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
886         $statement_handle->execute();
890 sub editdatabase{
891 #################################################################################
892 # editdatabase: Edits a Kiriwrite Database.                                     #
893 #                                                                               #
894 # Usage:                                                                        #
895 #                                                                               #
896 # $dbmodule->editdatabase(options);                                             #
897 #                                                                               #
898 # options               Specifies the following options in any order.           #
899 #                                                                               #
900 # NewDatabaseFilename   Specifies the new database filename to use.             #
901 # DatabaseName          Specifies the new database name.                        #
902 # DatabaseDescription   Specifies the new database description.                 #
903 # DatabaseNotes         Specifies the new database notes.                       #
904 # DatabaseCategories    Specifies the new database categories.                  #
905 #################################################################################
907         $error          = "";
908         $errorext       = "";
910         my $class       = shift;
911         my ($passedoptions) = @_;
913         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
914         my $dbname              = $passedoptions->{"DatabaseName"};
915         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
916         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
917         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
919         # Check if a new database filename has been specified and if a
920         # new database filename has been specified then change the
921         # database filename.
923         if ($database_filename ne $dbnewfilename){
925                 # Check if a table with the filename already exists before using the
926                 # new filename.
928                 my $database_newexists          = $class->dbexists($dbnewfilename);
930                 if ($database_newexists eq 0){
932                         # The database filename exists so set the error value.
934                         $error = "DatabaseExists";
935                         return;
937                 }
939                 # Rename the tables.
941                 $statement_handle = $database_handle->prepare('RENAME TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info, ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_pages');
942                 $statement_handle->execute();
944         }
946         # Get the current database information.
948         $statement_handle = $database_handle->prepare('SELECT name FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
949         $statement_handle->execute();
951         my @database_oldinfo    = $statement_handle->fetchrow_array();
953         my $dboldname           = decode_utf8($database_oldinfo[0]);
955         # Update the database information.
957         $statement_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info SET name = \'' . $class->convert($dbname) . '\',
958         description = \'' . $class->convert($dbdescription) . '\',
959         notes = \'' . $class->convert($dbnotes) . '\',
960         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
961         $statement_handle->execute();
965 sub deletedatabase{
966 #################################################################################
967 # deletedatabase: Deletes a Kiriwrite database.                                 #
968 #                                                                               #
969 # Usage:                                                                        #
970 #                                                                               #
971 # $dbmodule->deletedatabase(options);                                           #
972 #                                                                               #
973 # options       Specifies the following options in any order.                   #
974 #                                                                               #
975 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
976 #################################################################################
978         $error          = "";
979         $errorext       = "";
981         # Get the database filename.
983         my $class               = shift;
984         my ($passedoptions)     = shift;
986         my $databasename        = $passedoptions->{"DatabaseName"};
988         my @table_data;
989         my $table_exists;
991         # Check if the database with the filename given already exists.
993         my $database_exists     = $class->dbexists($databasename);
995         if ($database_exists eq 1){
997                 # The database does not exist so set the error value.
999                 $error = "DoesNotExist";
1000                 return;
1002         }
1006         # Delete the database tables.
1008         $statement_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_info')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1009         $statement_handle->execute();
1011         # Check if the _database_pages table exists and delete it if it exists.
1013         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages\'')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1014         $statement_handle->execute();
1016         while (@table_data = $statement_handle->fetchrow_array()){
1018                 $table_exists = 1;
1020         }
1022         # Check if the _database_pages table really does exist.
1024         if ($table_exists eq 1){
1026                 # the _database_pages table really does exist so delete it.
1028                 $statement_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1029                 $statement_handle->execute();
1031         }
1035 #################################################################################
1036 # Template subroutines.                                                         #
1037 #################################################################################
1039 sub connecttemplate{
1040 #################################################################################
1041 # connecttemplate: Connect to the template database.                            #
1042 #                                                                               #
1043 # Usage:                                                                        #
1044 #                                                                               #
1045 # $dbmodule->connecttemplate(missingignore);                                    #
1046 #                                                                               #
1047 # missingignore Ignore errror about database being missing.                     #
1048 #################################################################################
1050         $error = "";
1051         $errorext = "";
1053         my $class = shift;
1054         my $ignoremissing = shift;
1055         my $templatedb_exists = 0;
1056         my @templatedb_check;
1058         # Check if the template database exists.
1060         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1061         $statement_handle->execute();
1063         while (@templatedb_check = $statement_handle->fetchrow_array()){
1065                 $templatedb_exists = 1;
1067         }
1069         if (!$templatedb_exists){
1071                 if (!$ignoremissing){
1073                         $error = "TemplateDatabaseDoesNotExist";
1074                         return;
1076                 }
1078         }
1082 sub disconnecttemplate{
1083 #################################################################################
1084 # disconnecttemplate: Disconnect from the template database.                    #
1085 #                                                                               #
1086 # Usage:                                                                        #
1087 #                                                                               #
1088 # $dbmodule->disconnecttemplate();                                              #
1089 #################################################################################
1091         # This subroutine is not used.
1095 sub gettemplatelist{
1096 #################################################################################
1097 # gettemplatelist: Gets the list of templates.                                  #
1098 #                                                                               #
1099 # Usage:                                                                        #
1100 #                                                                               #
1101 # $dbmodule->gettemplatelist(options);                                          #
1102 #                                                                               #
1103 # options       Specifies the following options as a hash (in any order).       #
1104 #                                                                               #
1105 # StartFrom     Specifies where the list of templates will start from.          #
1106 # Limit         Specifies how many templates should be retrieved.               #
1107 #################################################################################
1109         $error = "";
1110         $errorext = "";
1112         my $class               = shift;
1113         my ($passedoptions)     = @_;
1115         my $start_from          = $passedoptions->{"StartFrom"};
1116         my $limit               = $passedoptions->{"Limit"};
1118         if (defined($start_from)){
1120                 if (!$limit){
1121                         
1122                         $limit = 0;
1124                 }
1126                 $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates ORDER BY filename ASC LIMIT ' . $start_from . ',' .  $limit ) or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1127                 $statement_handle->execute();           
1129         } else {
1131                 $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1132                 $statement_handle->execute();
1134         }
1136         my @database_template;
1137         my @templates_list;
1138         my $template_filename;
1140         while (@database_template = $statement_handle->fetchrow_array()){
1142                 # Get certain values from the array.
1144                 $template_filename      = $database_template[0];
1146                 # Add the template to the list of templates.
1148                 push(@templates_list, $template_filename);
1150         }
1152         return @templates_list;
1156 sub gettemplateinfo{
1157 #################################################################################
1158 # gettemplateinfo: Get information on a template.                               #
1159 #                                                                               #
1160 # Usage:                                                                        #
1161 #                                                                               #
1162 # $dbmodule->gettemplateinfo(options);                                          #
1163 #                                                                               #
1164 # options       Specifies the following options in any order.                   #
1165 #                                                                               #
1166 # TemplateFilename      Specifies the template filename to use.                 #
1167 # Reduced               Specifies if the reduced version of the template        #
1168 #                       information should be retrieved.                        #
1169 #################################################################################
1171         $error = "";
1172         $errorext = "";
1174         # Get the data passed to the subroutine.
1176         my $class = shift;
1177         my ($passedoptions) = @_;
1179         my %page_info;
1180         my @template_data;
1182         my $template_filename;
1183         my $template_name;
1184         my $template_description;
1185         my $template_datemodified;
1186         my $template_layout;
1188         my $template_found = 0;
1190         my $filename    = $passedoptions->{"TemplateFilename"};
1191         my $reduced     = $passedoptions->{"Reduced"};
1193         if ($reduced && $reduced eq 1){
1195                 $statement_handle = $database_handle->prepare('SELECT filename, templatename, templatedescription, datemodified FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1196                 $statement_handle->execute();
1198         } else {
1200                 $statement_handle = $database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1201                 $statement_handle->execute();
1203         }
1205         while (@template_data = $statement_handle->fetchrow_array()){
1207                 # Get certain values from the array.
1209                 $template_filename      = decode_utf8($template_data[0]);
1210                 $template_name          = decode_utf8($template_data[1]);
1211                 $template_description   = decode_utf8($template_data[2]);
1212                 $template_layout        = decode_utf8($template_data[3]);
1213                 $template_datemodified  = decode_utf8($template_data[4]);
1215                 # Process them into the hash.
1217                 %page_info = (
1218                         "TemplateFilename" => $template_filename,
1219                         "TemplateName" => $template_name,
1220                         "TemplateDescription" => $template_description,
1221                         "TemplateLayout" => $template_layout,
1222                         "TemplateLastModified" => $template_datemodified
1223                 );
1225                 $template_found = 1;
1227         }
1229         if ($template_found eq 0){
1231                 # The template was not found in the template database so
1232                 # write an error value.
1234                 $error = "TemplateDoesNotExist";
1235                 return;
1237         }
1239         return %page_info;
1243 sub gettemplatecount{
1244 #################################################################################
1245 # gettemplatecount: Gets the count of templates in the template database.       #
1246 #                                                                               #
1247 # Usage:                                                                        #
1248 #                                                                               #
1249 # $dbmodule->gettemplatecount();                                                #
1250 #################################################################################
1252         $error = "";
1253         $errorext = "";
1254  
1255         my $class       = shift;
1256  
1257         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM ' . $class->convert($options{"TablePrefix"}) . '_templates') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return);
1258         $statement_handle->execute();
1259  
1260         my $count = $statement_handle->fetchrow_array();
1261  
1262         return $count;
1266 sub addtemplate{
1267 #################################################################################
1268 # addtemplate: Adds a template to the template database.                        #
1269 #                                                                               #
1270 # Usage:                                                                        #
1271 #                                                                               #
1272 # $dbmodule->addtemplate(options);                                              #
1273 #                                                                               #
1274 # options       Specifies the following options in any order.                   #
1275 #                                                                               #
1276 # TemplateFilename      Specifies the new template filename.                    #
1277 # TemplateName          Specifies the new template name.                        #
1278 # TemplateDescription   Specifies the new template description.                 #
1279 # TemplateLayout        Specifies the new template layout.                      #
1280 #################################################################################
1282         $error = "";
1283         $errorext = "";
1285         # Get the data passed to the subroutine.
1287         my $class = shift;
1288         my ($passedoptions) = @_;
1290         my @page_exists;
1291         my @templatedb_check;
1292         my $templatedb_exists;
1293         my $blankfile = 0;
1295         my $template_filename           = $passedoptions->{"TemplateFilename"};
1296         my $template_name               = $passedoptions->{"TemplateName"};
1297         my $template_description        = $passedoptions->{"TemplateDescription"};
1298         my $template_layout             = $passedoptions->{"TemplateLayout"};
1300         # Check if the template database exists.
1302         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1303         $statement_handle->execute();
1305         while (@templatedb_check = $statement_handle->fetchrow_array()){
1307                 $templatedb_exists = 1;
1309         }
1311         # Check if the template database table exists and if it doesn't
1312         # then create the template database table.
1314         if (!$templatedb_exists){
1316                 $statement_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_templates (
1317                         filename varchar(256) primary key,
1318                         templatename varchar(512),
1319                         templatedescription varchar(512),
1320                         templatelayout mediumtext,
1321                         datemodified datetime
1322                 ) DEFAULT CHARSET=utf8') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1323                 $statement_handle->execute();
1325         }
1327         # Check if the template already exists before adding.
1329         if (!$templatedb_exists){
1331                 $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
1333                 if ($blankfile eq 0){
1335                         $statement_handle->execute();
1337                         while (@page_exists = $statement_handle->fetchrow_array()){
1339                                 $error = "TemplatePageExists";
1340                                 return;
1342                         }
1344                 }
1346         }
1348         # Get the current date.
1349  
1350         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1351  
1352         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1354         # Check if certain values are undefined and if they
1355         # are then set them blank.
1357         if (!$template_name){
1359                 $template_name = "";
1361         }
1363         if (!$template_description){
1365                 $template_description = "";
1367         }
1369         if (!$template_layout){
1371                 $template_layout = "";
1373         }
1375         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
1376                         \'' . $class->convert($template_filename) . '\',
1377                         \'' . $class->convert($template_name) . '\',
1378                         \'' . $class->convert($template_description) . '\',
1379                         \'' . $class->convert($template_layout) . '\',
1380                         \'' . $class->convert($template_date) . '\'
1381         )') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1382         $statement_handle->execute();
1386 sub edittemplate{
1387 #################################################################################
1388 # editttemplate: Edits a Kiriwrite template.                                    #
1389 #                                                                               #
1390 # Usage:                                                                        #
1391 #                                                                               #
1392 # $dbmodule->edittemplate(options);                                             #
1393 #                                                                               #
1394 # options       Specifies the following options in any order.                   #
1395 #                                                                               #
1396 # TemplateFilename              Specifies the template filename to edit.        #
1397 # NewTemplateFilename           Specifies the new template filename.            #
1398 # NewTemplateName               Specifies the new template name.                #
1399 # NewTemplateDescription        Specifies the new template description.         #
1400 # NewTemplateLayout             Specifies the new template layout.              #
1401 #################################################################################
1403         # Get the values passed.
1405         my $class = shift;
1406         my ($passedoptions) = @_;
1407         my $template_found = 0;
1408         my @template_info;
1410         # Process the values passed.
1412         my $template_filename           = $passedoptions->{"TemplateFilename"};
1413         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
1414         my $new_template_name           = $passedoptions->{"NewTemplateName"};
1415         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
1416         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
1418         # Check if the template exists.
1420         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1421         $statement_handle->execute();
1423         while (@template_info = $statement_handle->fetchrow_array()){
1425                 $template_found = 1;
1427         }
1429         # Check to see if the template was found and set an error value if
1430         # it wasn't.
1432         if ($template_found eq 0){
1434                 $error = "TemplateDoesNotExist";
1435                 return;
1437         }
1439         # Get the date and time.
1441         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1442         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1444         # Update the template information.
1446         $statement_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_templates SET
1447                 filename = \'' . $class->convert($new_template_filename) . '\',
1448                 templatename = \'' . $class->convert($new_template_name) . '\',
1449                 templatedescription = \'' . $class->convert($new_template_description) . '\',
1450                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
1451                 datemodified = \'' . $class->convert($templatenewdate) . '\'
1452                 WHERE filename = \'' . $class->convert($template_filename) . '\'
1453         ') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1454         $statement_handle->execute();
1458 sub deletetemplate{
1459 #################################################################################
1460 # deletetemplate: Deletes a template from the template database.                #
1461 #                                                                               #
1462 # Usage:                                                                        #
1463 #                                                                               #
1464 # $dbmodule->deletetemplate(options);                                           #
1465 #                                                                               #
1466 # options       Specifies the following options in any order.                   #
1467 #                                                                               #
1468 # TemplateFilename      Specifies the template filename to delete.              #
1469 #################################################################################
1471         $error = "";
1472         $errorext = "";
1474         # Get the data passed to the subroutine.
1476         my $class = shift;
1477         my ($passedoptions) = @_;
1479         my @pagedata;
1480         my $template_filename = $passedoptions->{"TemplateFilename"};
1481         my $template_count = 0;
1483         # Check if the template exists.
1485         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1486         $statement_handle->execute();
1488         while (@pagedata = $statement_handle->fetchrow_array()){
1490                 $template_count++;
1492         }
1494         if ($template_count eq 0){
1496                 # No pages were returned so return an error value.
1498                 $error = "TemplateDoesNotExist";
1499                 return;
1501         }
1503         # Delete the template from the template database.
1505         $statement_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1506         $statement_handle->execute();
1510 #################################################################################
1511 # Page subroutines.                                                             #
1512 #################################################################################
1514 sub getpagecount{
1515 #################################################################################
1516 # getpagecount: Get the count of pages that are in the database.                #
1517 #                                                                               #
1518 # Usage:                                                                        #
1519 #                                                                               #
1520 # $dbmodule->getpagecount();                                                    #
1521 #################################################################################
1523         $error = "";
1524         $errorext = "";
1526         my $class       = shift;
1528         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1529         $statement_handle->execute();
1531         my $count = $statement_handle->fetchrow_array();
1533         return $count;
1537 sub getpagelist{
1538 #################################################################################
1539 # getpagelist: Gets the list of pages from the database.                        #
1540 #                                                                               #
1541 # Usage:                                                                        #
1542 #                                                                               #
1543 # $dbmodule->getpagelist(options);                                              #
1544 #                                                                               #
1545 # options       Specifies the following options as a hash (in any order).       #
1546 #                                                                               #
1547 # StartFrom     Start from the specified page in the database.                  #
1548 # Limit         Get the amount of pages given.                                  #
1549 #################################################################################
1551         $error = "";
1552         $errorext = "";
1554         my $class       = shift;
1555         my ($passedoptions)     = shift;
1557         my $start_from  = $passedoptions->{"StartFrom"};
1558         my $limit       = $passedoptions->{"Limit"};
1559         
1560         if (defined($start_from)){
1562                 if (!$limit){
1563                         
1564                         $limit = 0;
1566                 }
1568                 $statement_handle       = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1569                 $statement_handle->execute();
1571         } else {
1573                 $statement_handle       = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1574                 $statement_handle->execute();
1576         }
1578         my @database_pagefilenames;
1579         my @database_pagefilenames_final;
1581         # Process the collected pages.
1583         while (@database_pagefilenames = $statement_handle->fetchrow_array){
1585                 # Add each page to the list of pages in the database.
1587                 push(@database_pagefilenames_final, decode_utf8($database_pagefilenames[0]));
1589         }
1591         return @database_pagefilenames_final;   
1595 sub getpageinfo{
1596 #################################################################################
1597 # getpageinfo: Gets the page information from the filename passed.              #
1598 #                                                                               #
1599 # Usage:                                                                        #
1600 #                                                                               #
1601 # $dbmodule->getpageinfo(options);                                              #
1602 #                                                                               #
1603 # options       Specifies the following options in any order.                   #
1604 #                                                                               #
1605 # PageFilename  Specifies the page filename to get the page information from.   #
1606 # Reduced       Specifies if the reduced version of the page information should #
1607 #               be retrieved.                                                   #
1608 #################################################################################
1610         $error = "";
1611         $errorext = "";
1613         my $class               = shift;
1614         my ($passedoptions)     = shift;
1615         my (%database_page, $database_page);
1616         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1618         my @data_page;
1619         my $page_found = 0;
1621         # Get the page from the database.
1623         my $page_filename       = $passedoptions->{"PageFilename"};
1624         my $page_reduced        = $passedoptions->{"Reduced"};
1626         if ($page_reduced eq 1){
1628                 $statement_handle       = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1629                 $statement_handle->execute();
1631                 # Check if the page exists in the database.
1633                 while (@data_page = $statement_handle->fetchrow_array()){
1635                         # Get the values from the array.
1637                         $pagefilename           = decode_utf8($data_page[0]);
1638                         $pagename               = decode_utf8($data_page[1]);
1639                         $pagedescription        = decode_utf8($data_page[2]);
1640                         $pagelastmodified       = decode_utf8($data_page[3]);
1642                         # Put the values into the page hash.
1644                         %database_page = (
1645                                 "PageFilename"          => $pagefilename,
1646                                 "PageName"              => $pagename,
1647                                 "PageDescription"       => $pagedescription,
1648                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1649                         );
1651                         $page_found = 1;
1653                 }
1654         
1655         } else {
1657                 $statement_handle       = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1658                 $statement_handle->execute();
1660                 # Check if the page exists in the database.
1662                 while (@data_page = $statement_handle->fetchrow_array()){
1664                         # Get the values from the array.
1666                         $pagefilename           = decode_utf8($data_page[0]);
1667                         $pagename               = decode_utf8($data_page[1]);
1668                         $pagedescription        = decode_utf8($data_page[2]);
1669                         $pagesection            = decode_utf8($data_page[3]);
1670                         $pagetemplate           = decode_utf8($data_page[4]);
1671                         $pagedata               = decode_utf8($data_page[5]);
1672                         $pagesettings           = decode_utf8($data_page[6]);
1673                         $pagelastmodified       = decode_utf8($data_page[7]);
1675                         # Put the values into the page hash.
1677                         %database_page = (
1678                                 "PageFilename"          => $pagefilename,
1679                                 "PageName"              => $pagename,
1680                                 "PageDescription"       => $pagedescription,
1681                                 "PageSection"           => $pagesection,
1682                                 "PageTemplate"          => $pagetemplate,
1683                                 "PageContent"           => $pagedata,
1684                                 "PageSettings"          => $pagesettings,
1685                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1686                                 # ADD PageLastModifiedInternal
1687                         );
1689                         $page_found = 1;
1691                 }
1693         }
1695         # Check if the page did exist.
1697         if (!$page_found){
1699                 $error = "PageDoesNotExist";
1700                 return;
1702         }
1704         return %database_page;
1709 sub addpage{
1710 #################################################################################
1711 # addpage: Add a page to the selected database.                                 #
1712 #                                                                               #
1713 # Usage:                                                                        #
1714 #                                                                               #
1715 # $dbmodule->addpage(options);                                                  #
1716 #                                                                               #
1717 # options       Specifies the following options in any order.                   #
1718 #                                                                               #
1719 # PageFilename          Specifies the page filename to use.                     #
1720 # PageName              Specifies the page name to use.                         #
1721 # PageDescription       Specifies the page description to use.                  #
1722 # PageSection           Specifies the page section to use.                      #
1723 # PageTemplate          Specifies the page template to use.                     #
1724 # PageContent           Specifies the page content to use.                      #
1725 # PageSettings          Specifies the page settings to use.                     #
1726 #################################################################################
1728         # Get the data that was passed to the subroutine.
1730         $error = "";
1731         $errorext = "";
1733         my $class               = shift;
1734         my ($passedoptions)     = shift;
1736         my @database_page;
1737         my $page_count = 0;
1739         # Get the values passed to the hash.
1741         my $page_filename       = $passedoptions->{"PageFilename"};
1742         my $page_name           = $passedoptions->{"PageName"};
1743         my $page_description    = $passedoptions->{"PageDescription"};
1744         my $page_section        = $passedoptions->{"PageSection"};
1745         my $page_template       = $passedoptions->{"PageTemplate"};
1746         my $page_content        = $passedoptions->{"PageContent"};
1747         my $page_settings       = $passedoptions->{"PageSettings"};
1749         # Check to see if the filename given already exists
1750         # in the page database.
1752         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1753         $statement_handle->execute();
1755         # Check if a page with the filename given really does
1756         # exist.
1758         while (@database_page = $statement_handle->fetchrow_array()){
1760                 # A page does exist so increment the count to 1.
1762                 $page_count++;
1763                 
1764         }
1766         if ($page_count ne 0){
1768                 # The page does exist so set the error value.
1770                 $error = "PageExists";
1771                 return;
1773         }
1775         # Check if certain values are undefined.
1777         if (!$page_name){
1779                 $page_name = "";
1781         }
1783         if (!$page_description){
1785                 $page_description = "";
1787         }
1789         if (!$page_section){
1791                 $page_section = "";
1793         }
1795         if (!$page_content){
1797                 $page_content = "";
1799         }
1801         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1802         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1804         # Add the page to the selected database.
1806         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1807                 \'' . $class->convert($page_filename) . '\',
1808                 \'' . $class->convert($page_name) . '\',
1809                 \'' . $class->convert($page_description) . '\',
1810                 \'' . $class->convert($page_section) . '\',
1811                 \'' . $class->convert($page_template) . '\',
1812                 \'' . $class->convert($page_content) . '\',
1813                 \'' . $class->convert($page_settings) . '\',
1814                 \'' . $class->convert($page_date) . '\'
1815         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1816         $statement_handle->execute();
1820 sub deletepage{
1821 #################################################################################
1822 # deletepage: Delete a page from the selected database.                         #
1823 #                                                                               #
1824 # Usage:                                                                        #
1825 #                                                                               #
1826 # $dbmodule->deletepage(options)                                                #
1827 #                                                                               #
1828 # options       Specifies the following options in any order.                   #
1829 #                                                                               #
1830 # PageFilename  Specifies the page filename to delete.                          #
1831 #################################################################################
1833         $error = "";
1834         $errorext = "";
1836         # Get the data that was passed to the subroutine.
1838         my $class = shift;
1839         my ($passedoptions) = @_;
1840         my @page_info;
1841         my $page_found = 0;
1843         # Get the page filename.
1845         my $page_filename = $passedoptions->{"PageFilename"};
1847         # Check if the page exists before deleting it.
1849         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1850         $statement_handle->execute();
1852         while (@page_info = $statement_handle->fetchrow_array()){
1854                 $page_found = 1;
1856         }
1858         # Check if the page really does exist.
1860         if (!$page_found){
1862                 $error = "PageDoesNotExist";
1863                 return;
1865         }
1867         # Delete the page.
1869         $statement_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1870         $statement_handle->execute();
1874 sub editpage{
1875 #################################################################################
1876 # editpage: Edit a page from the selected database.                             #
1877 #                                                                               #
1878 # Usage:                                                                        #
1879 #                                                                               #
1880 # $dbmodule->editpage(options);                                                 #
1881 #                                                                               #
1882 # options       Specifies the following options in any order.                   #
1883 #                                                                               #
1884 # PageFilename          Specifies the filename to edit.                         #
1885 # PageNewFilename       Specifies the new filename to use.                      #
1886 # PageNewName           Specifies the new page name to use.                     #
1887 # PageNewDescription    Specifies the new page description to use.              #
1888 # PageNewSection        Specifies the new page section to use.                  #
1889 # PageNewTemplate       Specifies the new page template to use.                 #
1890 # PageNewContent        Specifies the new page content to use.                  #
1891 # PageNewSettings       Specifies the new page settings to use.                 #
1892 #################################################################################
1894         $error = "";
1895         $errorext = "";
1897         # Get the values passed to the subroutine.
1899         my $class = shift;
1900         my ($passedoptions) = @_;
1901         my $page_found = 0;
1902         my @page_info;
1904         # Get the data that was passed to the subroutine.
1906         my $page_filename       = $passedoptions->{"PageFilename"};
1907         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1908         my $page_newname        = $passedoptions->{"PageNewName"};
1909         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1910         my $page_newsection     = $passedoptions->{"PageNewSection"};
1911         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1912         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1913         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1915         # Check if the page with the filename given exists.
1917         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1918         $statement_handle->execute();
1920         # Check if the page really does exist.
1922         while (@page_info = $statement_handle->fetchrow_array()){
1924                 # The page information is found.
1926                 $page_found = 1;
1928         }
1930         # Check if the page really does exist.
1932         if (!$page_found){
1934                 $error = "PageDoesNotExist";
1935                 return;
1937         }
1939         # Check if there is a page that already exists with the new
1940         # filename.
1942         $page_found = 0;
1944         if ($page_filename ne $page_newfilename){
1946                 $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1947                 $statement_handle->execute();
1949                 # Check if a page really is using the new filename.
1951                 while (@page_info = $statement_handle->fetchrow_array()){
1953                         # The page information is found.
1955                         $page_found = 1;
1957                 }
1959                 if ($page_found eq 1){
1961                         $error = "PageAlreadyExists";
1962                         return;
1964                 }
1966         }
1968         # Get the current date.
1970         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1971         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1973         # Edit the selected page.
1975         $statement_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_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 );
1976         $statement_handle->execute();
1980 sub movepage{
1981 #################################################################################
1982 # movepage: Moves a page from the old database to the new database.             #
1983 #                                                                               #
1984 # Usage:                                                                        #
1985 #                                                                               #
1986 # $dbmodule->movepage(options);                                                 #
1987 #                                                                               #
1988 # options       Specifies the following options in any order.                   #
1989 #                                                                               #
1990 # PageFilename  Specifies the page with the filename to move.                   #
1991 #################################################################################
1993         $error = "";
1994         $errorext = "";
1996         # Get the values passed to the subroutine.
1998         my $class = shift;
1999         my ($passedoptions) = @_;
2001         my (%database_page, $database_page);
2002         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
2003         my @page_info;
2004         my $page_found = 0;
2006         # Get the data that was passed to the subroutine.
2008         my $page_filename = $passedoptions->{"PageFilename"};
2010         # Check if the page with the filename given exists.
2012         $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
2013         $statement_handle->execute();
2015         # Check if the page really does exist.
2017         while (@page_info = $statement_handle->fetchrow_array()){
2019                 # Get the values from the array.
2021                 $pagefilename           = decode_utf8($page_info[0]);
2022                 $pagename               = decode_utf8($page_info[1]);
2023                 $pagedescription        = decode_utf8($page_info[2]);
2024                 $pagesection            = decode_utf8($page_info[3]);
2025                 $pagetemplate           = decode_utf8($page_info[4]);
2026                 $pagedata               = decode_utf8($page_info[5]);
2027                 $pagesettings           = decode_utf8($page_info[6]);
2028                 $pagelastmodified       = decode_utf8($page_info[7]);
2030                 # Put the values into the page hash.
2032                 %database_page = (
2033                         "PageFilename"          => $pagefilename,
2034                         "PageName"              => $pagename,
2035                         "PageDescription"       => $pagedescription,
2036                         "PageSection"           => $pagesection,
2037                         "PageTemplate"          => $pagetemplate,
2038                         "PageContent"           => $pagedata,
2039                         "PageSettings"          => $pagesettings,
2040                         "PageLastModified"      => $pagelastmodified,
2041                 );
2043                 # The page information is found.
2045                 $page_found = 1;
2047         }
2049         # Check if the page really does exist.
2051         if (!$page_found){
2053                 $error = "PageDoesNotExist";
2054                 return;
2056         }
2058         # Check if the page with the filename given already exists in
2059         # the database the page is being moved to.
2061         $page_found = 0;
2062         @page_info = ();
2064         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2065         $statement_handle->execute();
2067         while (@page_info = $statement_handle->fetchrow_array()){
2069                 $page_found = 1;
2071         }
2073         # Check if the page really does exist.
2075         if ($page_found){
2077                 $error = "PageAlreadyExists";
2078                 return;
2080         }
2082         # Add the page to the new database.
2084         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
2085                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
2086                 \'' . $class->convert($database_page{"PageName"}) . '\',
2087                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
2088                 \'' . $class->convert($database_page{"PageSection"}) . '\',
2089                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
2090                 \'' . $class->convert($database_page{"PageContent"}) . '\',
2091                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
2092                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
2093         )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2094         $statement_handle->execute();
2096         # Delete the page from the old database.
2098         $statement_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
2099         $statement_handle->execute();
2103 sub copypage{
2104 #################################################################################
2105 # copypage: Copies a page from the old database to the new database.            #
2106 #                                                                               #
2107 # Usage:                                                                        #
2108 #                                                                               #
2109 # $dbmodule->copypage(options);                                                 #
2110 #                                                                               #
2111 # options       Specifies the following options in any order.                   #
2112 #                                                                               #
2113 # PageFilename  Specifies the page with the filename to copy.                   #
2114 #################################################################################
2116         $error = "";
2117         $errorext = "";
2119         # Get the values passed to the subroutine.
2121         my $class = shift;
2122         my ($passedoptions) = @_;
2124         my (%database_page, $database_page);
2125         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
2126         my @page_info;
2127         my $page_found = 0;
2129         # Get the data that was passed to the subroutine.
2131         my $page_filename = $passedoptions->{"PageFilename"};
2133         # Check if the page with the filename given exists.
2135         $statement_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
2136         $statement_handle->execute();
2138         # Check if the page really does exist.
2140         while (@page_info = $statement_handle->fetchrow_array()){
2142                 # Get the values from the array.
2144                 $pagefilename           = decode_utf8($page_info[0]);
2145                 $pagename               = decode_utf8($page_info[1]);
2146                 $pagedescription        = decode_utf8($page_info[2]);
2147                 $pagesection            = decode_utf8($page_info[3]);
2148                 $pagetemplate           = decode_utf8($page_info[4]);
2149                 $pagedata               = decode_utf8($page_info[5]);
2150                 $pagesettings           = decode_utf8($page_info[6]);
2151                 $pagelastmodified       = decode_utf8($page_info[7]);
2153                 # Put the values into the page hash.
2155                 %database_page = (
2156                         "PageFilename"          => $pagefilename,
2157                         "PageName"              => $pagename,
2158                         "PageDescription"       => $pagedescription,
2159                         "PageSection"           => $pagesection,
2160                         "PageTemplate"          => $pagetemplate,
2161                         "PageContent"           => $pagedata,
2162                         "PageSettings"          => $pagesettings,
2163                         "PageLastModified"      => $pagelastmodified,
2164                 );
2166                 # The page information is found.
2168                 $page_found = 1;
2170         }
2172         # Check if the page really does exist.
2174         if (!$page_found){
2176                 $error = "PageDoesNotExist";
2177                 return;
2179         }
2181         # Check if the page with the filename given already exists in
2182         # the database the page is being moved to.
2184         $page_found = 0;
2185         @page_info = ();
2187         $statement_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2188         $statement_handle->execute();
2190         while (@page_info = $statement_handle->fetchrow_array()){
2192                 $page_found = 1;
2194         }
2195         
2196         # Check if the page really does exist.
2198         if ($page_found){
2200                 $error = "PageAlreadyExists";
2201                 return;
2203         }
2205         # Add the page to the new database.
2207         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
2208                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
2209                 \'' . $class->convert($database_page{"PageName"}) . '\',
2210                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
2211                 \'' . $class->convert($database_page{"PageSection"}) . '\',
2212                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
2213                 \'' . $class->convert($database_page{"PageContent"}) . '\',
2214                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
2215                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
2216         )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2217         $statement_handle->execute();
2221 #################################################################################
2222 # Filter subroutines.                                                           #
2223 #################################################################################
2225 sub connectfilter{
2226 #################################################################################
2227 # connectfilter: Connect to the filter database.                                #
2228 #                                                                               #
2229 # Usage:                                                                        #
2230 #                                                                               #
2231 # $dbmodule->connectfilter(missingignore);                                      #
2232 #                                                                               #
2233 # missingignore Ignore error about database being missing.                      #
2234 #################################################################################
2236         $error = "";
2237         $errorext = "";
2239         my $class = shift;
2240         my $ignoremissing = shift;
2241         my @filterdb_check;
2242         my $filterdb_exists = 0;
2244         # Check if the template database exists.
2246         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2247         $statement_handle->execute();
2249         while (@filterdb_check = $statement_handle->fetchrow_array()){
2251                 $filterdb_exists = 1;
2253         }
2255         if (!$filterdb_exists){
2257                 if (!$ignoremissing){
2259                         $error = "FilterDatabaseDoesNotExist";
2260                         return;
2262                 }
2264         }
2268 sub disconnectfilter{
2269 #################################################################################
2270 # disconnectfilter: Disconnect from the filter database.                        #
2271 #                                                                               #
2272 # Usage:                                                                        #
2273 #                                                                               #
2274 # $dbmodule->disconnectfilter();                                                #
2275 #################################################################################
2277         # This subroutine is not used.
2281 sub getfilterlist{
2282 #################################################################################
2283 # getfilterlist: Gets the list of filters in the filter database.               #
2284 #                                                                               #
2285 # Usage:                                                                        #
2286 #                                                                               #
2287 # $dbmodule->getfilterlist(options);                                            #
2288 #                                                                               #
2289 # options       Specifies the following options as a hash (in any order).       #
2290 #                                                                               #
2291 # StartFrom     Specifies where the list of filters should start from.          #
2292 # Limit         Specifies the amount of the filters to get.                     #
2293 #################################################################################
2295         $error = "";
2296         $errorext = "";
2298         my $class = shift;
2299         my ($passedoptions)     = shift;
2301         my @filter_list;
2302         my @filter_data;
2304         my $start_from  = $passedoptions->{"StartFrom"};
2305         my $limit       = $passedoptions->{"Limit"};
2307         if (defined($start_from)){
2309                 if (!$limit){
2310                         
2311                         $limit = 0;
2313                 }
2315                 $statement_handle       = $database_handle->prepare('SELECT id, priority FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY priority ASC LIMIT ' . $start_from . ',' . $limit) or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2316                 $statement_handle->execute();
2318         } else {
2319  
2320                 $statement_handle       = $database_handle->prepare('SELECT id, priority FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2321                 $statement_handle->execute();
2323         }
2325         while (@filter_data = $statement_handle->fetchrow_array()){
2327                 # Add the filter to the list of available filters.
2329                 push(@filter_list, decode_utf8($filter_data[0]));
2331         }
2333         return @filter_list;
2337 sub getfiltercount{
2338 #################################################################################
2339 # getfiltercount: Gets the count of filters in the filters database.            #
2340 #                                                                               #
2341 # Usage:                                                                        #
2342 #                                                                               #
2343 # $dbmodule->getfiltercount();                                                  #
2344 #################################################################################
2346         $error = "";
2347         $errorext = "";
2349         my $class       = shift;
2351         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM ' . $class->convert($options{"TablePrefix"}) . '_filters') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return);
2352         $statement_handle->execute();
2354         my $count = $statement_handle->fetchrow_array();
2356         return $count;
2360 sub getfilterinfo{
2361 #################################################################################
2362 # getfilterinfo: Gets information about the filter.                             #
2363 #                                                                               #
2364 # Usage:                                                                        #
2365 #                                                                               #
2366 # $dbmodule->getfilterinfo(options);                                            #
2367 #                                                                               #
2368 # options       Specifies the following options in any order.                   #
2369 #                                                                               #
2370 # FilterID      Specifies the filter ID number to get information from.         #
2371 # Reduced       Specifies to get the reduced version of the filter information. #
2372 #################################################################################
2374         $error = "";
2375         $errorext = "";
2377         # Get the values passed to the subroutine.
2379         my $class               = shift;
2380         my ($passedoptions)     = @_;
2382         my %filter_info;
2383         my $filter_exists       = 0;
2384         my @filter_data;
2386         # Get the values that are in the hash.
2388         my $filter_id           = $passedoptions->{"FilterID"};
2389         my $reduced             = $passedoptions->{"Reduced"};
2391         if ($reduced && $reduced eq 1){
2393                 $statement_handle = $database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2394                 $statement_handle->execute();
2396         } else {
2398                 $statement_handle = $database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled, notes FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2399                 $statement_handle->execute();
2401         }
2403         # Get the filter information.
2405         while (@filter_data = $statement_handle->fetchrow_array()){
2407                 $filter_info{"FilterID"}        = decode_utf8($filter_data[0]);
2408                 $filter_info{"FilterPriority"}  = decode_utf8($filter_data[1]);
2409                 $filter_info{"FilterFind"}      = decode_utf8($filter_data[2]);
2410                 $filter_info{"FilterReplace"}   = decode_utf8($filter_data[3]);
2411                 $filter_info{"FilterEnabled"}   = decode_utf8($filter_data[4]);
2412                 $filter_info{"FilterNotes"}     = decode_utf8($filter_data[5]);
2414                 $filter_exists = 1;
2416         }
2418         # Check if the filter exists.
2420         if (!$filter_exists){
2422                 # The filter does not exist so return
2423                 # an error value.
2425                 $error = "FilterDoesNotExist";
2426                 return;
2428         }
2430         # Return the filter information.
2432         return %filter_info;
2436 sub addfilter{
2437 #################################################################################
2438 # addfilter: Adds a filter to the filter database.                              #
2439 #                                                                               #
2440 # Usage:                                                                        #
2441 #                                                                               #
2442 # $dbmodule->addfilter(options);                                                #
2443 #                                                                               #
2444 # options       Specifies the following options in any order.                   #
2445 #                                                                               #
2446 # FindFilter    Specifies the find filter to add.                               #
2447 # ReplaceFilter Specifies the replace filter to add.                            #
2448 # Priority      Specifies the filter priority to use.                           #
2449 # Enabled       Specifies if the filter should be enabled.                      #
2450 # Notes         Specifies the notes to use.                                     #
2451 #################################################################################
2453         $error = "";
2454         $errorext = "";
2456         # Get the values passed to the subroutine.
2458         my $class = shift;
2459         my ($passedoptions) = @_;
2461         # Define some variables for later.
2463         my @database_filters;
2464         my @filterdb_check;
2465         my @filterid_list;
2466         my @filterid_check;
2467         my $nofiltertable = 0;
2468         my $filter_found = 0;
2469         my $filter_count = 0;
2470         my $filterdb_exists = 0;
2471         my $filter_id;
2472         my $new_id;
2474         # Get the values from the hash.
2476         my $filter_find         = $passedoptions->{"FindFilter"};
2477         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2478         my $filter_priority     = $passedoptions->{"Priority"};
2479         my $filter_enabled      = $passedoptions->{"Enabled"};
2480         my $filter_notes        = $passedoptions->{"Notes"};
2482         # Check if the template database exists.
2484         $statement_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2485         $statement_handle->execute();
2487         while (@filterdb_check = $statement_handle->fetchrow_array()){
2489                 $filterdb_exists = 1;
2491         }
2493         # Check if certain values are undefined and if they
2494         # are then set them blank.
2496         if (!$filter_find){
2498                 $filter_find = "";
2500         }
2502         if (!$filter_replace){
2504                 $filter_replace = "";
2506         }
2508         if (!$filter_priority){
2510                 $filter_priority = 1;
2512         }
2514         if (!$filter_notes){
2516                 $filter_notes = "";
2518         }
2520         if (!$filter_enabled){
2522                 $filter_enabled = "";
2524         }
2526         # Check if there is really no filter table.
2528         if (!$filterdb_exists){
2530                 # Create the filter database table.
2532                 $statement_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_filters (
2533                         id int(7) primary key,
2534                         priority int(5),
2535                         findsetting varchar(1024),
2536                         replacesetting varchar(1024),
2537                         enabled varchar(3),
2538                         notes text
2539                 ) DEFAULT CHARSET=utf8') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2540                 $statement_handle->execute();
2542         }
2544         # Find the lowest filter identification number available.
2546         $statement_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2547         $statement_handle->execute();
2549         while (@database_filters = $statement_handle->fetchrow_array()){
2551                 $filter_id      = decode_utf8($database_filters[0]);
2553                 # Add the filter identification to the list of filter IDs.
2555                 push(@filterid_list, $filter_id);
2557         }
2559         $filter_id = "";
2561         # Process each filter looking for a blank available filter.
2563         foreach $filter_id (@filterid_list){
2565                 # Check the next filter ID to see if it's blank.
2567                 $new_id = $filter_id + 1;
2569                 $statement_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2570                 $statement_handle->execute();
2572                 # Get the filter identification number.
2574                 while (@filterid_check = $statement_handle->fetchrow_array()){
2576                         $filter_found = 1;
2578                 }
2580                 # Check if a filter was found.
2582                 if (!$filter_found){
2584                         # No filter was found using this ID so exit the loop.
2586                         last;
2588                 }
2590                 # Increment the filter count and reset the filter found value.
2592                 $filter_count++;
2593                 $filter_found = 0;
2594                 $new_id = 0;
2596         }
2598         # Check if there were any filters in the filter database.
2600         if (!$filter_count && !$new_id){
2602                 # There were no filters in the filter database so set
2603                 # the new filter identification value to 1.
2605                 $new_id = 1;
2607         }
2609         # Add the filter to the filter database.
2611         $statement_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2612                 \'' . $class->convert($new_id) . '\',
2613                 \'' . $class->convert($filter_priority) . '\',
2614                 \'' . $class->convert($filter_find) . '\',
2615                 \'' . $class->convert($filter_replace) .'\',
2616                 \'' . $class->convert($filter_notes) . '\'
2617         )') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2618         $statement_handle->execute();
2623 sub editfilter{
2624 #################################################################################
2625 # editfilter: Edits a filter in the filter database.                            #
2626 #                                                                               #
2627 # Usage:                                                                        #
2628 #                                                                               #
2629 # $dbmodule->editfilter(options);                                               #
2630 #                                                                               #
2631 # options       Specifies the following options in any order.                   #
2632 #                                                                               #
2633 # FilterID              Specifies the filter to edit.                           #
2634 # NewFindFilter         Specifies the new find filter setting.                  #
2635 # NewReplaceFilter      Specifies the new replace filter setting.               #
2636 # NewFilterPriority     Specifies the new filter priority setting.              #
2637 # NewEnabled            Specifies if the filter is enabled.                     #
2638 # NewFilterNotes        Specifies the new notes for the filter.                 #
2639 #################################################################################
2641         $error = "";
2642         $errorext = "";
2644         # Get the values passed to the subroutine.
2646         my $class = shift;
2647         my ($passedoptions) = @_;
2649         my @filter_data;
2650         my $filter_exists = 1;
2651         my $blankfile = 0;
2653         # Get the values from the hash.
2655         my $filter_id           = $passedoptions->{"FilterID"};
2656         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2657         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2658         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2659         my $filter_enabled      = $passedoptions->{"NewEnabled"};
2660         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2662         # Check if the filter exists before editing it.
2664         $statement_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2665         $statement_handle->execute();
2667         # Check if the filter exists.
2669         while (@filter_data = $statement_handle->fetchrow_array()){
2671                 $filter_exists = 1;
2673         }       
2675         # Check if the filter really does exist.
2677         if (!$filter_exists){
2679                 # The filter does not exist so return
2680                 # an error value.
2682                 $error = "FilterDoesNotExist";
2683                 return;
2685         }
2687         # Edit the selected filter.
2689         $statement_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_filters SET
2690                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2691                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2692                 priority = \'' . $class->convert($filter_newpriority) . '\',
2693                 enabled = \'' . $class->convert($filter_enabled) . '\',
2694                 notes = \'' . $class->convert($filter_newnotes) . '\'
2695         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );       
2696         $statement_handle->execute();
2698         return;
2702 sub deletefilter{
2703 #################################################################################
2704 # deletefilter: Deletes a filter from the filter database.                      #
2705 #                                                                               #
2706 # Usage:                                                                        #
2707 #                                                                               #
2708 # $dbmodule->deletefilter(options);                                             #
2709 #                                                                               #
2710 # options       Specifies the following options in any order.                   #
2711 #                                                                               #
2712 # FilterID      Specifies the filter to delete from the filter database.        #
2713 #################################################################################
2715         $error = "";
2716         $errorext = "";
2718         # Get the values passed to the subroutine.
2720         my $class = shift;
2721         my ($passedoptions) = @_;
2723         my $filter_exists = 0;
2724         my @filter_data;
2726         # Get the values from the hash.
2728         my $filter_id           = $passedoptions->{"FilterID"};
2730         # Check if the filter exists before deleting.
2732         $statement_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2733         $statement_handle->execute();
2735         while (@filter_data = $statement_handle->fetchrow_array()){
2737                 $filter_exists = 1;
2739         }
2741         # Check to see if the filter really does exist.
2743         if (!$filter_exists){
2745                 $error = "FilterDoesNotExist";
2746                 return;
2748         }
2750         # Delete the filter from the filter database.
2752         $statement_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2753         $statement_handle->execute();
2757 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