Changeset 2081

Show
Ignore:
Timestamp:
04/25/08 00:22:49 (7 months ago)
Author:
bchoate
Message:

Limit replace operation to checked items. BugId:69487

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-36/lib/MT/CMS/Search.pm

    r2038 r2081  
    441441                } 
    442442            }; 
    443         } 
    444         if ( $blog_id || ($type eq 'blog') ) { 
    445             $iter = $class->load_iter( \%terms, \%args ) or die $class->errstr; 
    446         } 
    447         else { 
    448  
    449             my @streams; 
    450             if ( $author->is_superuser ) { 
    451                 @streams = ( { iter => $class->load_iter( \%terms, \%args ) } ); 
    452             }  
     443        } else { 
     444            if ( $blog_id || ($type eq 'blog') ) { 
     445                $iter = $class->load_iter( \%terms, \%args ) or die $class->errstr; 
     446            } 
    453447            else { 
    454                 # Get an iter for each accessible blog 
    455                 my @perms = $app->model('permission')->load( 
    456                     { blog_id => '0', author_id => $author->id }, 
    457                     { not => { blog_id => 1 } }, 
    458                 ); 
    459                 if (@perms) { 
    460                     @streams = map { 
    461                         { 
    462                             iter => $class->load_iter( 
    463                                 { 
    464                                     blog_id => $_->blog_id, 
    465                                     %terms 
    466                                 }, 
    467                                 \%args 
    468                             ) 
     448 
     449                my @streams; 
     450                if ( $author->is_superuser ) { 
     451                    @streams = ( { iter => $class->load_iter( \%terms, \%args ) } ); 
     452                }  
     453                else { 
     454                    # Get an iter for each accessible blog 
     455                    my @perms = $app->model('permission')->load( 
     456                        { blog_id => '0', author_id => $author->id }, 
     457                        { not => { blog_id => 1 } }, 
     458                    ); 
     459                    if (@perms) { 
     460                        @streams = map { 
     461                            { 
     462                                iter => $class->load_iter( 
     463                                    { 
     464                                        blog_id => $_->blog_id, 
     465                                        %terms 
     466                                    }, 
     467                                    \%args 
     468                                ) 
     469                            } 
     470                        } @perms; 
     471                    } 
     472                } 
     473 
     474                # Pull out the head of each iterator 
     475                # Next: effectively mergesort the various iterators 
     476                # To call the iterator n times takes time in O(bn) 
     477                #   with 'b' the number of blogs 
     478                # we expect to hit the iterator l/p times where 'p' is the 
     479                #   prob. of the search term appearing and 'l' is $limit 
     480                $_->{head} = $_->{iter}->() foreach @streams; 
     481                if ( $type ne 'template' ) { 
     482                    $iter = sub { 
     483 
     484                        # find the head with greatest created_on 
     485                        my $which = \$streams[0]; 
     486                        foreach my $iter (@streams) { 
     487                            next 
     488                              if !exists $iter->{head} 
     489                              || !$which 
     490                              || !${$which}->{head} 
     491                              || !defined( $iter->{head} ); 
     492                            if ( $iter->{head}->created_on > 
     493                                ${$which}->{head}->created_on ) 
     494                            { 
     495                                $which = \$iter; 
     496                            } 
    469497                        } 
    470                     } @perms; 
    471                 } 
    472             } 
    473  
    474             # Pull out the head of each iterator 
    475             # Next: effectively mergesort the various iterators 
    476             # To call the iterator n times takes time in O(bn) 
    477             #   with 'b' the number of blogs 
    478             # we expect to hit the iterator l/p times where 'p' is the 
    479             #   prob. of the search term appearing and 'l' is $limit 
    480             $_->{head} = $_->{iter}->() foreach @streams; 
    481             if ( $type ne 'template' ) { 
    482                 $iter = sub { 
    483  
    484                     # find the head with greatest created_on 
    485                     my $which = \$streams[0]; 
    486                     foreach my $iter (@streams) { 
    487                         next 
    488                           if !exists $iter->{head} 
    489                           || !$which 
    490                           || !${$which}->{head} 
    491                           || !defined( $iter->{head} ); 
    492                         if ( $iter->{head}->created_on > 
    493                             ${$which}->{head}->created_on ) 
    494                         { 
    495                             $which = \$iter; 
     498 
     499                        # Advance the chosen one 
     500                        my $result = ${$which}->{head}; 
     501                        ${$which}->{head} = ${$which}->{iter}->() if $result; 
     502                        $result; 
     503                    }; 
     504                } 
     505                else { 
     506                    $iter = sub { 
     507                        return undef unless @streams; 
     508 
     509                        # find the head with greatest created_on 
     510                        my $which = \$streams[0]; 
     511                        while ( @streams && ( !defined ${$which}->{head} ) ) { 
     512                            shift @streams; 
     513                            last unless @streams; 
     514                            $which = \$streams[0]; 
    496515                        } 
    497                     } 
    498  
    499                     # Advance the chosen one 
    500                     my $result = ${$which}->{head}; 
    501                     ${$which}->{head} = ${$which}->{iter}->() if $result; 
    502                     $result; 
    503                 }; 
    504             } 
    505             else { 
    506                 $iter = sub { 
    507                     return undef unless @streams; 
    508  
    509                     # find the head with greatest created_on 
    510                     my $which = \$streams[0]; 
    511                     while ( @streams && ( !defined ${$which}->{head} ) ) { 
    512                         shift @streams; 
    513                         last unless @streams; 
    514                         $which = \$streams[0]; 
    515                     } 
    516                     my $result = ${$which}->{head}; 
    517                     ${$which}->{head} = ${$which}->{iter}->() if $result; 
    518                     $result; 
    519                 }; 
     516                        my $result = ${$which}->{head}; 
     517                        ${$which}->{head} = ${$which}->{iter}->() if $result; 
     518                        $result; 
     519                    }; 
     520                } 
    520521            } 
    521522        }