Ignore:
Timestamp:
2013-05-06T15:23:45+12:00 (11 years ago)
Author:
jmt12
Message:

Replacing hardcoded additions to INC and PATH environment variables with conditional ones - this allows us to use the order of values in these variables for precedence, thus allows better support for extensions that override classifiers, plugins etc. ENV and PATH functions already exists in util, but augmentINC() is a new function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/classify.pm

    r23118 r27303  
    2929
    3030require util;
     31use FileUtils;
    3132require AllList;
    3233
     
    4647
    4748    # find the classifier
    48     my $customclassname;
     49    # - used to have hardcoded list of places to load classifier from. We
     50    # should, instead, try loading from all of the perllib places on the
     51    # library path, as that improves support for extensions. Special cases
     52    # needed for collection specific and custom classifier. [jmt12]
     53    my @possible_class_paths;
    4954    if (defined($ENV{'GSDLCOLLECTION'}))
    5055    {
    51     $customclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
    52                                               "perllib", "classify", "${classifier}.pm");
    53     }
    54     my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "classify", "${classifier}.pm");
    55     my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'}, "perllib", "classify", "${classifier}.pm");
    56 
    57     if (defined($customclassname) && -e $customclassname) { require $customclassname; }
    58     elsif (-e $colclassname) { require $colclassname; }
    59     elsif (-e $mainclassname) { require $mainclassname; }
    60     else {
    61     &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classifier) && die "\n";
    62     }
     56      push(@possible_class_paths, &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, 'custom', $ENV{'GSDLCOLLECTION'}, 'perllib', 'classify', $classifier . '.pm'));
     57    }
     58    # (why does GSDLCOLLECTDIR get set to GSDLHOME for classinfo calls?)
     59    if ($ENV{'GSDLCOLLECTDIR'} ne $ENV{'GSDLHOME'})
     60    {
     61      push(@possible_class_paths, &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'classify', $classifier . '.pm'));
     62    }
     63    foreach my $library_path (@INC)
     64    {
     65      # only interested in classify paths found in the library paths
     66      if ($library_path =~ /classify$/)
     67      {
     68       push(@possible_class_paths, &FileUtils::filenameConcatenate($library_path, $classifier . '.pm'));
     69      }
     70    }
     71    my $found_class = 0;
     72    foreach my $possible_class_path (@possible_class_paths)
     73    {
     74      if (-e $possible_class_path)
     75      {
     76        require $possible_class_path;
     77        $found_class = 1;
     78        last;
     79      }
     80    }
     81    if (!$found_class)
     82    {
     83      &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classifier) && die "\n";
     84    }
     85
    6386    my ($classobj);
    6487    my $options = "-gsdlinfo";
     
    7497    my $classify_number  = 1;
    7598
    76      my $colclassdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify");
    77      unshift (@INC, $colclassdir);
    78    
     99    my $colclassdir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"perllib/classify");
     100    # - ensure colclassdir doesn't already exist in INC before adding, other-
     101    # wise we risk clobbering classifier inheritence implied by order of paths
     102    # in INC [jmt12]
     103    my $inc_paths = join(':',@INC);
     104    if ($inc_paths !~ /$colclassdir/)
     105    {
     106      unshift (@INC, $colclassdir);
     107    }
     108
    79109    foreach my $classifyoption (@$classify_list) {
    80110
     
    84114
    85115    # find the classifier
    86         my $customclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
    87                                                   "perllib", "classify", "${classname}.pm");
    88         my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "classify", "${classname}.pm");
    89         my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'}, "perllib", "classify", "${classname}.pm");
    90 
    91     if (-e $customclassname) { require $customclassname; }
    92     elsif (-e $colclassname) { require $colclassname; }
    93     elsif (-e $mainclassname) { require $mainclassname; }
    94     else { &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
    95            # die "ERROR - couldn't find classifier \"$classname\"\n";
     116        # - replaced as explained in load_classifier_for_info() [jmt12]
     117        my @possible_class_paths;
     118        if (defined($ENV{'GSDLCOLLECTION'}))
     119        {
     120          push(@possible_class_paths, &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, 'custom', $ENV{'GSDLCOLLECTION'}, 'perllib', 'classify', $classname . '.pm'));
     121        }
     122        # (why does GSDLCOLLECTDIR get set to GSDLHOME for classinfo calls?)
     123        if ($ENV{'GSDLCOLLECTDIR'} ne $ENV{'GSDLHOME'})
     124        {
     125          push(@possible_class_paths,&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'classify', $classname . '.pm'));
     126        }
     127        foreach my $library_path (@INC)
     128        {
     129          # only interested in classify paths found in the library paths
     130          if ($library_path =~ /classify$/)
     131          {
     132            push(@possible_class_paths, &FileUtils::filenameConcatenate($library_path, $classname . '.pm'));
     133          }
     134        }
     135        my $found_class = 0;
     136        foreach my $possible_class_path (@possible_class_paths)
     137        {
     138          if (-e $possible_class_path)
     139          {
     140            require $possible_class_path;
     141            $found_class = 1;
     142            last;
     143          }
     144        }
     145        if (!$found_class)
     146        {
     147          &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
    96148        }
    97149
Note: See TracChangeset for help on using the changeset viewer.