mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-02-28 09:17 pm

[dw-free] Allow importing of your journal from another LiveJournal-based site.

[commit: http://hg.dwscoalition.org/dw-free/rev/1ae4f370f1ed]

http://bugs.dwscoalition.org/show_bug.cgi?id=114

Use the same group name policy as LJ, this is what we should have done to
begin with.

Patch by [staff profile] mark.

Files modified:
  • cgi-bin/DW/User/Edges/WatchTrust.pm
  • cgi-bin/DW/Worker/ContentImporter/Local/TrustGroups.pm
--------------------------------------------------------------------------------
diff -r e044caca6a80 -r 1ae4f370f1ed cgi-bin/DW/User/Edges/WatchTrust.pm
--- a/cgi-bin/DW/User/Edges/WatchTrust.pm	Sat Feb 28 19:51:25 2009 +0000
+++ b/cgi-bin/DW/User/Edges/WatchTrust.pm	Sat Feb 28 21:16:56 2009 +0000
@@ -152,6 +152,7 @@ sub _add_wt_edge {
     return 1;
 }
 
+
 # internal method to delete an edge
 sub _del_wt_edge {
     my ( $from_u, $to_u, $edges ) = @_;
@@ -227,6 +228,21 @@ sub _del_wt_edge {
             if $do_watch && $watch_notify;
     }
 }
+
+
+# returns the valid version of a group name
+sub valid_group_name {
+    my $name = shift;
+
+    # strip off trailing slash(es)
+    $name =~ s!/+$!!;
+
+    # conform to maxes
+    $name = LJ::text_trim( $name, LJ::BMAX_GRPNAME2, LJ::CMAX_GRPNAME2 );
+
+    return $name;
+}
+
 
 ###############################################################################
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -590,14 +606,13 @@ sub create_trust_group {
     # validate other parameters
     confess 'invalid sortorder (not in range 0..255)'
         if exists $opts{sortorder} && $opts{sortorder} !~ /^\d+$/;
-    confess 'invalid group name'
-        if exists $opts{groupname} && $opts{groupname} !~ /^\w[\w\d\_ ]+?\w$/;
     confess 'invalid is_public (not 1/0)'
         if exists $opts{is_public} && $opts{is_public} !~ /^(?:0|1)$/;
 
     # need a name
+    $opts{groupname} = DW::User::Edges::WatchTrust::valid_group_name( $opts{groupname} );
     confess 'name not provided'
-        unless exists $opts{groupname};
+        unless $opts{groupname};
 
     # now perform an edit with our chosen id
     return $id
@@ -640,8 +655,8 @@ sub edit_trust_group {
         );
     $change{sortorder} = $opts{sortorder}
         if exists $opts{sortorder} && $opts{sortorder} =~ /^\d+$/;
-    $change{groupname} = $opts{groupname}
-        if exists $opts{groupname} && $opts{groupname} =~ /^\w[\w\d\_ ]+?\w$/;
+    $change{groupname} = DW::User::Edges::WatchTrust::valid_group_name( $opts{groupname} )
+        if exists $opts{groupname};
     $change{is_public} = $opts{is_public}
         if exists $opts{is_public} && $opts{is_public} =~ /^(?:0|1)$/;
 
diff -r e044caca6a80 -r 1ae4f370f1ed cgi-bin/DW/Worker/ContentImporter/Local/TrustGroups.pm
--- a/cgi-bin/DW/Worker/ContentImporter/Local/TrustGroups.pm	Sat Feb 28 19:51:25 2009 +0000
+++ b/cgi-bin/DW/Worker/ContentImporter/Local/TrustGroups.pm	Sat Feb 28 21:16:56 2009 +0000
@@ -44,44 +44,33 @@ sub merge_trust_groups {
 sub merge_trust_groups {
     my ( $class, $u, $groups ) = @_;
 
+    # map our existing groups to name->id so we can use this list
+    # later to map groups if we've imported before
+    my %name_map;
     my $cur_groups = $u->trust_groups || {};
-    my %name_map;
     foreach my $id ( keys %$cur_groups ) {
-        if ( defined( $cur_groups->{$id} ) ) {
-            my $name = $cur_groups->{$id}->{groupname};
-
-            # remove disallowed characters!
-            $name =~ s/[^\w\d\_ ]//g;
-            $name =~ s/^[^\w]//g;
-            $name =~ s/[^\w]$//g;
-
-            $name_map{$name} = $id;
-        }
+        $name_map{$cur_groups->{$id}->{groupname}} = $id;
     }
 
+    # now map new groups
     my %map;
+    foreach my $group ( @{ $groups || [] } ) {
 
-    foreach my $group ( @$groups ) {
+        # we assume the incoming group is valid
         my $name = $group->{name};
-
-        # remove disallowed characters!
-        $name =~ s/[^\w\d\_ ]//g;
-        $name =~ s/^[^\w]//g;
-        $name =~ s/[^\w]$//g;
-
         my $sort = $group->{sortorder};
         my $public = $group->{public};
-        my $existing = 0;
+
         my $id = 0;
 
-        if ( defined( $name_map{$name} ) ) {
+        if ( defined $name_map{$name} ) {
             $id = $name_map{$name};
             $u->edit_trust_group( id => $id, groupname => $name, sortorder => $sort, is_public => $public );
         } else {
             $id = $u->create_trust_group( groupname => $name, sortorder => $sort, is_public => $public );
         }
 
-        $map{$group->{id}} = $id;
+        $map{$group->{id}} = $id if $id;
     }
 
     return \%map;
--------------------------------------------------------------------------------