I ran into this problem when Google went down and so did Zawodny (Google API Offline Aagain, MT pissed. Once Google is down (if you have used the MTGoogleSearch tags), MT cannot rebuild pages.

Here's the fix.

Using the following command:

diff -C 3 Template/Context.pm Template/Context.pm.old
The diff output looks like:
*** Template/Context.pm Tue Jul  1 14:54:20 2003
--- Template/Context.pm.old     Tue Jul  1 14:50:45 2003
***************
*** 1748,1780 ****
          \&SOAP::XMLSchema2001::Deserializer::as_boolean;
      }

!       my $result;
!
!       eval {
!               $result = SOAP::Lite->service('file:' . $wsdl)
                             ->doGoogleSearch($key, $query, 0, $max,
                               0, '', 0, '', 'latin1', 'latin1'
                               );
-       };

      my $tokens = $ctx->stash('tokens');
      my $builder = $ctx->stash('builder');
      my $res = '';
!
!       if( ($@) || (!(@{ $result->{resultElements} })) )
!       {
!               $ctx->stash('google_result', '');
          my $out = $builder->build($ctx, $tokens, $cond);
!       }
!       else
!       {
!       for my $rec (@{ $result->{resultElements} }) {
!               $ctx->stash('google_result', $rec);
!               my $out = $builder->build($ctx, $tokens, $cond);
!               return $ctx->error( $builder->errstr ) unless defined $out;
!               $res .= $out;
!       }
!       }
      $res;
  }

--- 1748,1767 ----
          \&SOAP::XMLSchema2001::Deserializer::as_boolean;
      }

!       my $result = SOAP::Lite->service('file:' . $wsdl)
                             ->doGoogleSearch($key, $query, 0, $max,
                               0, '', 0, '', 'latin1', 'latin1'
                               );

      my $tokens = $ctx->stash('tokens');
      my $builder = $ctx->stash('builder');
      my $res = '';
!     for my $rec (@{ $result->{resultElements} }) {
!         $ctx->stash('google_result', $rec);
          my $out = $builder->build($ctx, $tokens, $cond);
!         return $ctx->error( $builder->errstr ) unless defined $out;
!         $res .= $out;
!     }
      $res;
  }

In English, go into your movabletype/lib/MT/Template' directory and open up 'Context.pm'. Around line 1745 or so, you will see the Google search stuff using SOAP::Lite. Take the line that looks like:

my $result = SOAP::Lite->service('file:' . $wsdl)
                       ->doGoogleSearch($key, $query, 0, $max, 0, '', 0, '', 'latin1', 'latin1');
and wrap that in eval tags like so:
my $result;

eval {
    $result = SOAP::Lite->service('file:' . $wsdl)
                        ->doGoogleSearch($key, $query, 0, $max, 0, '', 0, '', 'latin1', 'latin1');
};
Then, you are going to need to wrap the processing of the Google results in an if-else. Find the line that looks like:
for my $rec (@{ $result->{resultElements} }) {
    $ctx->stash('google_result', $rec);
    my $out = $builder->build($ctx, $tokens, $cond);
    return $ctx->error( $builder->errstr ) unless defined $out;
    $res .= $out;
}
And, wrap that in an if-else with error checking making sure $@ was not set and $result->{resultElements} has some elements in it like so:
if( ($@) || (!(@{ $result->{resultElements} })) )
{
    # Google and/or SOAP::Lite freaked out, do some stuff here
}
else
{
    for my $rec (@{ $result->{resultElements} }) {
        $ctx->stash('google_result', $rec);
        my $out = $builder->build($ctx, $tokens, $cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
}
Finally, add in the following code to the error-trapped portion of the if statement, and you are done.
if( ($@) || (!(@{ $result->{resultElements} })) )
{
    $ctx->stash('google_result', '');
    my $out = $builder->build($ctx, $tokens, $cond);
}
else
{
    for my $rec (@{ $result->{resultElements} }) {
        $ctx->stash('google_result', $rec);
        my $out = $builder->build($ctx, $tokens, $cond);
        return $ctx->error( $builder->errstr ) unless defined $out;
        $res .= $out;
    }
}

And, there you have at. Now, your MT will still work even if Google dies.

J$
#!/usr/bin/perl
J=>money
;$_=ord$"<<s>>$J>,s-.-
$&*$'+$&-e&&y[%_(8)]]J]
&&print chop;print chr

Trackbacks

Once In a Blue Moon
Excerpt: It doesn't happen often but every once in a while Google goes down. Or more specifically the Google APIs no longer work as expected. This causes a problem for those of us using Movable Type's MTGoogleSearch because when Google is having this problem we...
Weblog: John's Jottings
Tracked: February 8, 2004 10:56 PM

Comments

I’ve been using MTGoogleSearch to return the "titles" of Related Entries on my VoIP Blog and unfortunately some of the related entries have UTF-8 characters in the titles returned by MTGoogleSearch, which changes my webpage’s default iso-8859-1 encoding to UTF-8.

If at least 1 of the Related Entries titles has a UTF-8 character, this causes funky characters to display in the entire blog body. That is, all of my em-dashes, quotes, apostrophes, etc. in the blog body are messed up. Even though I explicitly specify the encoding (using MTPublishCharset) in the template to be iso-8859-1, I guess MT actually encodes and saves the file in UTF-8 format, which overrides this code:

In fact, when I click View Source (opens in Notepad) and then do a Save As, it displays UTF-8 in the filetype instead of the usual ANSI when I View Source on a blog entry without the funky characters.

Compare this page:
http://blog.tmcnet.com/blog/testblog/main-test.asp
(has MTGoogleSearch/Related Entries)

with
http://blog.tmcnet.com/blog/testblog/main-test2.asp
(deleted MTGoogleSearch/Related Entries from template)

Same page – I just deleted MTGoogleSearch code from the template.

Notice the funky characters in the first one?

I could probably re-encode all my posts (ISO-8859-1) to UTF-8, but that’s a huge hassle. At least, I think it is. I tried changing MT’s default encoding to UTF-8 and rebuilt my site, but then my posts had even more funky characters, which would require me to go through and Edit all my blog posts and fix them.

There should be a way of forcing MTGoogleSearch to strip UTF-8 characters or just ignore them without changing my blog's default iso-8859-1 encoding, no? Maybe I can "force" MTGoogleSearch to iso-8859-1?

Sorry for the long post.
Any suggestions?

Posted by Tom Keating on February 4, 2005 02:36 PM