1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Fixed Suggestion for reply-email text handling

Discussion in 'Resolved Bug Reports' started by ccb, Mar 1, 2012.

  1. ccb

    ccb New Member

    I have been working with the watched_thread_reply_messagetext email template for ign.com/boards. We are running the snippet helper to shorten and clean the text sent out in email as follows:
    {xen:helper snippet, $reply.message, 30, {xen:array 'fromStart=1', 'stripQuote=1'}}

    After compilation, this runs through XenForo_Helper_String::bbCodeStrip(), where handling of the stripQuote option is "extremely primitive". This was giving us messy output when the message begins with nested quotes. We would see portions of quoted text and unbalanced [/quote] tags. Here is a suggestion for "if($stripQuote)..." in bbCodeStrip(), tested against a few hundred of our board posts.

    // The following block reimplements XenForo's $stripQuote behavior. The original did
    // an ungreedy regex on spans of
    tags when quotes were nested. Here we attempt to deal
    // hierarchically with nested quotes and multiple quote sections in the text.
    if ( $stripQuote ) {
    // Break on every start- or end-quote tag and work from there.
    $chunks = preg_split( '{(\[quote[^\]]*\]|\[/quote\])}si', $string, NULL, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
    if ( sizeof( $chunks ) > 1 ) {
    $string = '';
    $quoteLevel = 0;
    $hadQuotes = false;
    foreach ( $chunks as $chunk ) {
    if ( preg_match( '{^\[quote}i', $chunk ) ) {
    $quoteLevel += 1;
    $string .= "\n";
    $hadQuotes = true;
    } elseif ( preg_match( '{^\[/quote}i', $chunk ) ) {
    if ( $quoteLevel > 0 ) {
    $quoteLevel -= 1;
    }
    $string .= "\n";
    $hadQuotes = true;
    } else {
    if ( $quoteLevel == 0 ) {
    $string .= $chunk;
    }
    }
    }
    // This part is optional, but it tends to improve the text.
    if ( $hadQuotes ) {
    // Quotes are typically found at the start of the message.
    $string = preg_replace( '/^\n+/', '', $string );
    // We inserted some newlines, so let's clean up any long runs.
    $string = preg_replace( '/\n\n\n+/', "\n\n", $string );
    }
    }
    }
     
  2. ccb

    ccb New Member

    ...and of course I had literal bbcode "quote" tags in my commented PHP. You get the idea.
     
  3. ccb

    ccb New Member

    OK, this will come out better. Great code deserves great formatting.

    PHP:
    // The following block reimplements XenForo's $stripQuote behavior. The original did
    // an ungreedy regex on spans of [quote] tags, resulting in orphaned quotation fragments
    // and unbalanced [/quote] tags when quotes were nested. Here we attempt to deal
    // hierarchically with nested quotes and multiple quote sections in the text.
    if ( $stripQuote ) {
        
    // Break on every start- or end-quote tag and work from there.
        
    $chunks preg_split'{(\[quote[^\]]*\]|\[/quote\])}si'$stringNULLPREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
        if ( 
    sizeof$chunks ) > ) {
            
    $string '';
            
    $quoteLevel 0;
            
    $hadQuotes false;
            foreach ( 
    $chunks as $chunk ) {
                if ( 
    preg_match'{^\[quote}i'$chunk ) ) {
                    
    $quoteLevel += 1;
                    
    $string .= "\n";
                    
    $hadQuotes true;
                } elseif ( 
    preg_match'{^\[/quote}i'$chunk ) ) {
                    if ( 
    $quoteLevel ) {
                        
    $quoteLevel -= 1;
                    }
                    
    $string .= "\n";
                    
    $hadQuotes true;
                } else {
                    if ( 
    $quoteLevel == ) {
                        
    $string .= $chunk;
                    }
                }
            }
            
    // This part is optional, but it tends to improve the text.
            
    if ( $hadQuotes ) {
                
    // Quotes are typically found at the start of the message.
                
    $string preg_replace'/^\n+/'''$string );
                
    // We inserted some newlines, so let's clean up any long runs.
                
    $string preg_replace'/\n\n\n+/'"\n\n"$string );
            }
        }
    }
     
    wmtech and (deleted member) like this.
  4. Mike

    Mike XenForo Developer Staff Member

    I've implemented a similar concept to this in 1.2 now. :)
     

Share This Page