Commit 5b999ec1 authored by lpsolit%gmail.com's avatar lpsolit%gmail.com
Browse files

Bug 323590: Make it possible to have multiple WEB_BASE_URI -> WEB_BASE_PATH...

Bug 323590: Make it possible to have multiple WEB_BASE_URI -> WEB_BASE_PATH mappings - Patch by Fr�d�ric Buclin <LpSolit@gmail.com> r=myk
parent 04dd9b62
Loading
Loading
Loading
Loading
+20 −16
Original line number Diff line number Diff line
@@ -3,27 +3,14 @@
package Doctor;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw($template $vars %CONFIG);  # symbols to export on request
@EXPORT_OK = qw(%CONFIG);  # symbols to export on request

use strict;

use File::Temp qw(tempfile tempdir);
use Template;
use AppConfig qw(:expand :argcount);

# Create the global template object that processes templates and specify
# configuration parameters that apply to templates processed in this script.
our $template = Template->new({
    # Colon-separated list of directories containing templates.
    INCLUDE_PATH => "templates",
    PRE_CHOMP    => 1,
    POST_CHOMP   => 1
});

# Define the global variables and functions that will be passed to the UI
# template.  Individual functions add their own values to this hash before
# sending them to the templates they process.
our $vars = {};
use CGI;

# Create an AppConfig object and populate it with parameters defined
# in the configuration file.
@@ -36,7 +23,24 @@ our $config = AppConfig->new({
$config->file("doctor.conf");
our %CONFIG = $config->varlist(".*");

$vars->{'config'} = \%CONFIG;
# Create the global template object that processes templates and specify
# configuration parameters that apply to templates processed in this script.
my $_template;
sub template {
    my $class = shift;
    # INCLUDE_PATH is a colon-separated list of directories containing templates.
    $_template ||= Template->new({INCLUDE_PATH => "templates",
                                  PRE_CHOMP    => 1,
                                  POST_CHOMP   => 1});
    return $_template;
}

my $_cgi;
sub cgi {
    my $class = shift;
    $_cgi ||= new CGI();
    return $_cgi;
}

sub system_capture {
    # Runs a command and captures its output and errors.  This should be using
+89 −0
Original line number Diff line number Diff line
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is Doctor.
#
# The Initial Developer of the Original Code is Netscape 
# Communications Corporation. Portions created by Netscape 
# are Copyright (C) 2002 Netscape Communications Corporation. 
# All Rights Reserved.
#
# Contributor(s): Myk Melez <myk@mozilla.org>
#                 Frédéric Buclin <LpSolit@gmail.com>

package Doctor::Error;

use strict;

use Doctor qw(%CONFIG);

use base qw(Exporter);
@Doctor::Error::EXPORT = qw(ThrowCodeError ThrowUserError);

sub ThrowUserError {
    # Throw an error about a problem with the user's request.  This function
    # should avoid mentioning system problems displaying the error message, since
    # the user isn't going to care about them and probably doesn't need to deal
    # with them after fixing their own mistake.  Errors should be gentle on 
    # the user, since many "user" errors are caused by bad UI that trip them up.

    # !!! Mail code errors to the system administrator!
    my $cgi = Doctor->cgi;
    my $template = Doctor->template;
    my $vars = {};

    ($vars->{'message'},
     $vars->{'title'},
     $vars->{'cvs_command'},
     $vars->{'cvs_error_code'},
     $vars->{'cvs_error_message'}) = @_;

    print $cgi->header;
    $template->process("user-error.tmpl", $vars)
      || print( ($vars->{'title'} ? "<h1>$vars->{'title'}</h1>" : "") . 
                "<p>$vars->{'message'}</p><p>Please go back and try again.</p>" );
    exit;
}

sub ThrowCodeError {
    # Throw error about a problem with the code.  This function should be
    # apologetic and deferent to the user, since it isn't the user's fault
    # the code didn't work.

    # !!! Mail code errors to the system administrator!
    my $cgi = Doctor->cgi;
    my $template = Doctor->template;
    my $vars = {};

    ($vars->{'message'}, $vars->{'title'}) = @_;
    $vars->{'config'} = \%CONFIG;

    print $cgi->header;
    $template->process("code-error.tmpl", $vars)
      || print("
            <p>
            Unfortunately Doctor has experienced an internal error from which
            it was unable to recover.  More information about the error is
            provided below. Please forward this information along with any
            other information that would help diagnose and fix this problem
            to the system administrator at
            <a href=\"mailto:$CONFIG{ADMIN_EMAIL}\">$CONFIG{ADMIN_EMAIL}</a>.
            </p>
            <p>
            couldn't process error.tmpl template: " . $template->error() . 
            "; error occurred while trying to display error message: " . 
            ($vars->{'title'} ? "$vars->{'title'}: ": "") . $vars->{'message'} .
            "</p>");
    exit;
}

1;
+42 −7
Original line number Diff line number Diff line
#!/usr/bin/perl -I.. -w
#!/usr/bin/perl -w

package Doctor::File;

@@ -6,7 +6,8 @@ use strict;

use Cwd qw(getcwd chdir);

use Doctor qw(:DEFAULT %CONFIG $template);
use Doctor qw(:DEFAULT %CONFIG);
use Doctor::Error;

use File::Temp qw(tempfile);

@@ -27,7 +28,10 @@ sub new {

    bless($self, $class);

    if (defined $spec) { $self->spec($spec) }
    if (defined $spec) {
        $self->spec($spec)
          || ThrowUserError($self->{_error});
    }

    return $self;
}
@@ -77,11 +81,42 @@ sub spec {

        # Remove the absolute URI for files on the web site (if any)
        # from the beginning of the path.
        if ($CONFIG{WEB_BASE_URI_PATTERN}) {
        my $i = 0;
        my $match_found = 0;

        while (defined $CONFIG{'WEB_BASE_URI_' . ++$i}) {
            $CONFIG{'WEB_BASE_URI'} = $CONFIG{'WEB_BASE_URI_' . $i};
            $CONFIG{'WEB_BASE_URI_PATTERN'} = $CONFIG{'WEB_BASE_URI_PATTERN_' . $i};
            $CONFIG{'WEB_BASE_PATH'} = $CONFIG{'WEB_BASE_PATH_' . $i};

            if ($CONFIG{'WEB_BASE_URI_PATTERN'}) {
                if ($self->{_spec} =~ /^$CONFIG{WEB_BASE_URI_PATTERN}/i) {
                    $self->{_spec} =~ s/^$CONFIG{WEB_BASE_URI_PATTERN}//i;
                    $match_found = 1;
                    last;
                }
            }
            else {
                if ($self->{_spec} =~ /^\Q$CONFIG{WEB_BASE_URI}\E/i) {
                    $self->{_spec} =~ s/^\Q$CONFIG{WEB_BASE_URI}\E//i;
                    $match_found = 1;
                    last;
                }
            }
            # If we come here, then the URI doesn't match a known URL.
            # Maybe it's a URI relative to the CVS repository.
            if ($self->{_spec} =~ /^\Q$CONFIG{WEB_BASE_PATH}\E/) {
                $match_found = 1;
                last;
            }
        }

        # If the given URI doesn't match anything and there are more than
        # one website managed by this installation, we cannot go further
        # as we have no idea which website the user is talking about.
        if (!$match_found && $i > 2) {
            $self->{_error} = "Invalid URI: " . $self->{_spec};
            return;
        }

        # Entire Spec Issues
+9 −69
Original line number Diff line number Diff line
@@ -19,27 +19,24 @@
# All Rights Reserved.
#
# Contributor(s): Myk Melez <myk@mozilla.org>
#                 Frédéric Buclin <LpSolit@gmail.com>

################################################################################
# Script Initialization
################################################################################

# Make it harder to do dangerous things in Perl.
use diagnostics;
use strict;
use lib ".";

# Make it easier to access Perl's built-in variables by turning on the option 
# of referencing them by sensible name instead of punctuation mark.
use English;

# Include the standard Perl CGI library and create a new request object
# to handle this CGI request.
use CGI;
my $request = new CGI;

use Doctor qw($template $vars %CONFIG);

use Doctor qw(%CONFIG);
use Doctor::File;
use Doctor::Error;

my $request = Doctor->cgi;
my $template = Doctor->template;
my $vars = {};
$vars->{'config'} = \%CONFIG;

################################################################################
# Script Configuration
@@ -325,63 +322,6 @@ sub ValidateVersions() {
    }
}


################################################################################
# Error Handling
################################################################################

sub ThrowUserError {
    # Throw an error about a problem with the user's request.  This function
    # should avoid mentioning system problems displaying the error message, since
    # the user isn't going to care about them and probably doesn't need to deal
    # with them after fixing their own mistake.  Errors should be gentle on 
    # the user, since many "user" errors are caused by bad UI that trip them up.
    
    # !!! Mail code errors to the system administrator!

    ($vars->{'message'}, 
     $vars->{'title'}, 
     $vars->{'cvs_command'}, 
     $vars->{'cvs_error_code'}, 
     $vars->{'cvs_error_message'}) = @_;
    
    chdir($HOME);
    print $request->header;
    $template->process("user-error.tmpl", $vars)
      || print( ($vars->{'title'} ? "<h1>$vars->{'title'}</h1>" : "") . 
                "<p>$vars->{'message'}</p><p>Please go back and try again.</p>" );
    exit;
}

sub ThrowCodeError {
    # Throw error about a problem with the code.  This function should be
    # apologetic and deferent to the user, since it isn't the user's fault
    # the code didn't work.
    
    # !!! Mail code errors to the system administrator!
    
    ($vars->{'message'}, $vars->{'title'}) = @_;
    
    chdir($HOME);
    print $request->header;
    $template->process("code-error.tmpl", $vars)
      || print("
            <p>
            Unfortunately Doctor has experienced an internal error from which
            it was unable to recover.  More information about the error is
            provided below. Please forward this information along with any
            other information that would help diagnose and fix this problem
            to the system administrator at
            <a href=\"mailto:$CONFIG{ADMIN_EMAIL}\">$CONFIG{ADMIN_EMAIL}</a>.
            </p>
            <p>
            couldn't process error.tmpl template: " . $template->error() . 
            "; error occurred while trying to display error message: " . 
            ($vars->{'title'} ? "$vars->{'title'}: ": "") . $vars->{'message'} .
            "</p>");
    exit;
}

################################################################################
# Misc
################################################################################
+5 −3
Original line number Diff line number Diff line
@@ -21,7 +21,9 @@ TEMP_DIR = /tmp/doctor
# For files accessible via the web, the absolute URI at which 
# editable files are found.  Used to convert file names to URLs
# and vice-versa.  Add a trailing slash.
WEB_BASE_URI = http://www.mozilla.org/
# Several separate installations can be added by incrementing _n
# in WEB_BASE_URI_n, WEB_BASE_URI_PATTERN_n and WEB_BASE_PATH_n.
WEB_BASE_URI_1 = http://www.mozilla.org/

# A regular expression matching the base URI, useful when
# files can exist at multiple locations on the web (i.e.
@@ -30,14 +32,14 @@ WEB_BASE_URI = http://www.mozilla.org/
# Note that only the base URI above will be used to reconstruct 
# URLs from paths, since there is no way to derive a single
# base URI from this pattern.
WEB_BASE_URI_PATTERN = http:\/\/(www\.)?mozilla\.org\/
WEB_BASE_URI_PATTERN_1 = http:\/\/(www\.)?mozilla\.org\/

# For files accessible via the web, the path to those files
# in the CVS repository.  Used to convert file names to URLs
# and vice-versa.  Don't add a preceding slash, since this path
# is relative to the CVS root directory, but do add a trailing
# slash.
WEB_BASE_PATH = mozilla-org/html/
WEB_BASE_PATH_1 = mozilla-org/html/

# The email address of the administrator for this installation.
# Change this to your email address.
Loading