Ticket #81: ticket_81.patch

File ticket_81.patch, 15.0 kB (added by grimoire, 3 years ago)

Patch against [735] for customisable locale / category prefixes

  • trunk/templates/node.tt

     
    2626[% END %] 
    2727 
    2828[% IF current %] 
    29   [% IF backlinks_in_title AND is_indexable_node %] 
    30     <h2 class="node_name"><a href="[% cgi_url %]?action=index;index_type=[% CGI.escape(index_type) %];index_value=[% CGI.escape(index_value) %]">[% node_name %]</a></h2> 
    31   [% ELSIF backlinks_in_title %] 
     29  [% IF backlinks_in_title %] 
    3230    <h2 class="node_name"><a href="[% cgi_url %]?action=show_backlinks&id=[% CGI.escape(node_name) %]">[% node_name %]</a></h2> 
    3331  [% ELSE %] 
    3432    <h2 class="node_name">[% node_name %]</h2> 
  • trunk/INSTALL

     
    249249National Grid, Geography::NationalGrid::IE to use the Irish National 
    250250Grid, and Geo::Coordinates::UTM to use a UTM ellipsoid. 
    251251 
     252 
     253  "Prefix to add to automatically generated category names?" 
     254  "Prefix to add to automatically generated locale names?" 
     255 
     256If a user specifies a category or locale for a node which does not exist, a 
     257node for that category or locale will be automatically generated. By default, a 
     258category of "Foo" will lead to a node titled "Category Foo", and a locale of 
     259"Bar" will lead to a node titled "Category Bar". These options allow this 
     260behaviour to be changed. 
     261 
    252262* Dependency errors 
    253263 
    254264If, after you have answered these questions, the build script complains 
  • trunk/lib/OpenGuides/Config.pm

     
    1212   formatting_rules_node formatting_rules_link backlinks_in_title template_path 
    1313   custom_template_path geo_handler ellipsoid gmaps_api_key centre_long 
    1414   centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84 
     15   category_prefix locale_prefix 
    1516); 
    1617my @questions = map { $_ . "__qu" } @variables; 
    1718OpenGuides::Config->mk_accessors( @variables ); 
     
    8283                     centre_lat => 0, 
    8384                     default_gmaps_zoom => 5, 
    8485                     default_gmaps_search_zoom => 3, 
    85                      force_wgs84 => 0 
     86                     force_wgs84 => 0, 
     87                     category_prefix => "Category ", 
     88                     locale_prefix => "Locale ", 
    8689                   ); 
    8790 
    8891    # See if we already have some config variables set. 
     
    145148        centre_lat => "What is the latitude of the centre point of a map to draw for your guide? (This question can be ignored if you aren't using Google Maps)", 
    146149        default_gmaps_zoom => "What default zoom level shall we use for Google Maps? (This question can be ignored if you aren't using Google Maps)", 
    147150        default_gmaps_search_zoom => "What default zoom level shall we use for Google Maps in the search results? (This question can be ignored if you aren't using Google Maps)", 
    148         force_wgs84 => "Forcibly treat stored lat/long data as if they used the WGS84 ellipsoid?" 
     151        force_wgs84 => "Forcibly treat stored lat/long data as if they used the WGS84 ellipsoid?", 
     152        category_prefix => "Prefix to add to automatically generated category names?", 
     153        locale_prefix => "Prefix to add to automatically generated locale names?" 
    149154    ); 
    150155 
    151156    foreach my $var ( keys %questions ) { 
     
    246251 
    247252=item * force_wgs84 
    248253 
     254=item * category_prefix 
     255 
     256=item * locale_prefix 
     257 
    249258=back 
    250259 
    251260=head1 AUTHOR 
  • trunk/lib/OpenGuides/Template.pm

     
    268268                        split("\r\n", $locales_text); 
    269269    } 
    270270 
     271    my $category_prefix = ( $config->category_prefix ne '' ? $config->category_prefix . ' ' : ''); 
     272    my $locale_prefix = ( $config->locale_prefix ne '' ? $config->locale_prefix . ' ' : ''); 
     273 
    271274    my @categories = map { { name => $_, 
    272                              url  => "$script_name?Category_" 
    273             . uri_escape($formatter->node_name_to_node_param($_)) } } @catlist; 
     275                             url  => "$script_name?" 
     276            . uri_escape($formatter->node_name_to_node_param($category_prefix . $_)) } } @catlist; 
    274277 
    275278    my @locales    = map { { name => $_, 
    276                              url  => "$script_name?Locale_" 
    277             . uri_escape($formatter->node_name_to_node_param($_)) } } @loclist; 
     279                             url  => "$script_name?" 
     280            . uri_escape($formatter->node_name_to_node_param($locale_prefix . $_)) } } @loclist; 
    278281 
    279282    # The 'website' attribute might contain a URL so we wiki-format it here 
    280283    # rather than just CGI::escapeHTMLing it all in the template. 
  • trunk/lib/OpenGuides/Utils.pm

     
    113113    my $script_name = $config->script_name; 
    114114    my $search_url = $config->script_url . "search.cgi"; 
    115115 
     116    my $locale_prefix = $config->locale_prefix; 
     117    my $category_prefix = $config->category_prefix; 
     118 
    116119    my %macros = ( 
    117120        '@SEARCHBOX' => 
    118121            qq(<form action="$search_url" method="get"><input type="text" size="20" name="search"><input type="submit" name="Go" value="Search"></form>), 
     
    127130                  my $link_title = $_[2] || "View all pages in $_[0] $_[1]"; 
    128131                  return qq(<a href="$script_name?action=index;index_type=) . uri_escape(lc($_[0])) . qq(;index_value=) . uri_escape($_[1]) . qq(">$link_title</a>); 
    129132                }, 
     133        qr/\@LOCALE_LINK\s+\[\[($locale_prefix)\s*([^\]|]+)\|?([^\]]+)?\]\]/ => 
     134            sub { 
     135                  # We may be being called by CGI::Wiki::Plugin::Diff, 
     136                  # which doesn't know it has to pass us $wiki - and 
     137                  # we don't use it anyway. 
     138                  if ( UNIVERSAL::isa( $_[0], "CGI::Wiki" ) ) { 
     139                      shift; # just throw it away 
     140                  } 
     141                  my $localename = $_[0] || "Locale"; 
     142                  my $link_title = $_[2] || "View all pages in $localename $_[1]"; 
     143                  return qq(<a href="$script_name?action=index;index_type=category;index_value=) . uri_escape($_[1]) . qq(">$link_title</a>); 
     144                }, 
     145        qr/\@CATEGORY_LINK\s+\[\[($category_prefix)\s*([^\]|]+)\|?([^\]]+)?\]\]/ => 
     146            sub { 
     147                  # We may be being called by CGI::Wiki::Plugin::Diff, 
     148                  # which doesn't know it has to pass us $wiki - and 
     149                  # we don't use it anyway. 
     150                  if ( UNIVERSAL::isa( $_[0], "CGI::Wiki" ) ) { 
     151                      shift; # just throw it away 
     152                  } 
     153                  my $categoryname = $_[0] || "Category"; 
     154                  my $link_title = $_[2] || "View all pages in $categoryname $_[1]"; 
     155                  return qq(<a href="$script_name?action=index;index_type=category;index_value=) . uri_escape($_[1]) . qq(">$link_title</a>); 
     156                }, 
    130157        qr/\@INDEX_LIST\s+\[\[(Category|Locale)\s+([^\]]+)]]/ => 
    131158             sub { 
    132159                   my ($wiki, $type, $value) = @_; 
     
    135162                   # which doesn't know it has to pass us $wiki 
    136163                   unless ( UNIVERSAL::isa( $wiki, "CGI::Wiki" ) ) { 
    137164                       return "(unprocessed INDEX_LIST macro)"; 
    138                    } 
     165                           } 
    139166 
    140167                   my @nodes = sort $wiki->list_nodes_by_metadata( 
    141168                       metadata_type  => $type, 
     
    154181                                                                link => $node, 
    155182                                                              ) 
    156183                                . "\n"; 
    157                    } 
     184                       } 
    158185                   return $return; 
    159186                 }, 
    160187        qr/\@RSS\s+(.+)/ => sub { 
  • trunk/lib/OpenGuides.pm

     
    147147 
    148148    my %tt_vars; 
    149149 
    150     if ( $id =~ /^(Category|Locale) (.*)$/ ) { 
    151         my $type = $1; 
    152         $tt_vars{is_indexable_node} = 1; 
    153         $tt_vars{index_type} = lc($type); 
    154         $tt_vars{index_value} = $2; 
    155         $tt_vars{"rss_".lc($type)."_url"} = 
    156                            $config->script_name . "?action=rc;format=rss;" 
    157                            . lc($type) . "=" . lc(CGI->escape($2)); 
    158     } 
    159  
    160150    my %current_data = $wiki->retrieve_node( $id ); 
    161151    my $current_version = $current_data{version}; 
    162152    undef $version if ($version && $version == $current_version); 
     
    492482sub show_index { 
    493483    my ($self, %args) = @_; 
    494484    my $wiki = $self->wiki; 
     485    my $config = $self->config; 
    495486    my $formatter = $wiki->formatter; 
    496487    my %tt_vars; 
    497488    my @selnodes; 
     
    512503                metadata_value => $args{value}, 
    513504                ignore_case    => 1 
    514505            ); 
    515             my $name = ucfirst($args{type}) . " $args{value}"; 
     506            my $name_prefix; 
     507            my $type_prefix; 
     508            if ( $args{type} eq 'category' or $args{type} eq 'locale' ) { 
     509                $type_prefix = $config->category_prefix; 
     510                $type_prefix .= ' ' if ( $type_prefix ne '' ); 
     511                $name_prefix = $type_prefix || $args{type}; 
     512            } else { 
     513                # Not sure this should ever be reached, actually 
     514                $type_prefix = ucfirst($args{type}) . ' '; 
     515                $name_prefix = $type_prefix; 
     516            } 
    516517            my $url = $self->config->script_name 
    517518                      . "?" 
    518                       . ucfirst( $args{type} ) 
    519                       . "_" 
    520519                      . uri_escape( 
    521                                       $formatter->node_name_to_node_param($args{value}) 
     520                                      $formatter->node_name_to_node_param($type_prefix . $args{value}) 
    522521                                  ); 
    523522            $tt_vars{criterion} = { 
    524523                type  => $args{type}, 
    525524                value => $args{value}, # for RDF version 
    526                 name  => CGI->escapeHTML( $name ), 
     525                name  => CGI->escapeHTML($name_prefix . ' ' . $args{value}), 
    527526                url   => $url 
    528527            }; 
    529528            $tt_vars{not_editable} = 1; 
     
    759758    $metadata{longitude} = delete $metadata{longitude_unmunged} 
    760759        if $metadata{longitude_unmunged}; 
    761760 
    762     # Check to make sure all the indexable nodes are created 
    763     foreach my $type (qw(Category Locale)) { 
    764         my $lctype = lc($type); 
    765         foreach my $index (@{$metadata{$lctype}}) { 
    766             $index =~ s/(.*)/\u$1/; 
    767             my $node = $type . " " . $index; 
    768             # Uppercase the node name before checking for existence 
    769             $node =~ s/ (\S+)/ \u$1/g; 
    770             unless ( $wiki->node_exists($node) ) { 
    771                 my $category = $type eq "Category" ? "Category" : "Locales"; 
    772                 $wiki->write_node( 
    773                                      $node, 
    774                                      "\@INDEX_LINK [[$node]]", 
    775                                      undef, 
    776                                      { 
    777                                          username => "Auto Create", 
    778                                          comment  => "Auto created $lctype stub page", 
    779                                          category => $category 
    780                                      } 
    781                                  ); 
     761    # We must look at the categories and locales for our new nodes, and make 
     762    # sure that the nodes referenced are in category "Category" or "Locale" 
     763    # respectively. 
     764 
     765    foreach my $type (qw/category locale/) { 
     766        my $prefix = $type eq 'category' ? $config->category_prefix : $config->locale_prefix; 
     767        $prefix .= ' ' if ( $prefix ne '' ); 
     768        # Look at all entries for "Categories" or "Locales" for our new node 
     769        foreach my $index (@{$metadata{$type}}) { 
     770            my $nodename = $prefix . $index; 
     771            my $category = $type eq 'category' ? 'Category' : 'Locales'; 
     772            if ( $wiki->node_exists($nodename) ) { 
     773                # We need to make sure that this node is in the relevant category 
     774                my %referencednode = $wiki->retrieve_node($nodename); 
     775                if ( not grep { $_ eq $category } @{$referencednode{metadata}{category}} ) { 
     776                    # Add the required Category to the node, and write it 
     777                    my $uctype = uc($type); 
     778                    $referencednode{content} .= "\n\@${uctype}_LINK [[$nodename]]"; 
     779                    push @{$referencednode{metadata}{category}}, $category; 
     780                    $referencednode{metadata}{username} = "Auto Create"; 
     781                    $referencednode{metadata}{comment} = "Added to $type $category"; 
     782                    $referencednode{metadata}{major_change} = 0; 
     783                    my $conflict = $wiki->write_node ( 
     784                                        $nodename, 
     785                                        $referencednode{content}, 
     786                                        $referencednode{checksum}, 
     787                                        $referencednode{metadata} 
     788                                      ); 
     789                    if ( $conflict == 0 ) { 
     790                        warn "Conflict while adding category $category to node $nodename, failing silently"; 
     791                    } 
     792                } 
     793            } else { 
     794                # There is no node for our new node's category or locale, we must create it 
     795                my $uctype = uc($type); 
     796                $wiki->write_node ( 
     797                                        $nodename, 
     798                                        "\@${uctype}_LINK [[$nodename]]", 
     799                                        undef, 
     800                                        { 
     801                                            username => "Auto Create", 
     802                                            comment => "Auto created $type stub page", 
     803                                            category => $category 
     804                                        } 
     805                                  ); 
    782806            } 
    783807        } 
    784808    } 
    785      
     809 
    786810    foreach my $var ( qw( summary username comment edit_type ) ) { 
    787811        $metadata{$var} = $q->param($var) || ""; 
    788812    } 
  • trunk/Changes

     
    110.52     
     2        Added configuration options "category_prefix" and "locale_prefix", used 
     3          for automatically generated categories and locales, and to generate 
     4          links within nodes. 
     5        Added macros @CATEGORY_LINK and @LOCALE_LINK to replace @INDEX_LINK 
    26        IMPORTANT CHANGE: "supersearch.cgi" is now simply "search.cgi". If you 
    37          have customisations to your templates, you may need to make changes 
    48          to reflect this.