Well, first I should have told you to replace $this->registry with $vbulletin, because there was one that didn't have a ->db on it. $this is used when inside an object method. I guess you copied the code from a class definition, so those had to be changed.
Also, these two lines had extra '(' characters (in red):
Code:
$vbulletin->db->query_read_slave(("SET CHARACTER SET utf8");
$stream = $vbulletin->db->query_read_slave(("