function do_remove_orphaned_streams()
{
    //get all stream ids present in mv_stream_files and mv_stream_images
    $dbr = wfGetDB(DB_READ);
    $orphaned_streams = array();
    $all_valid_streams = get_all_mv_streams();
    //could be done with a join ..oh well
    $result = $dbr->select('mv_stream_files', 'stream_id', '', __METHOD__, array('GROUP BY' => 'stream_id'));
    while ($stream = $dbr->fetchObject($result)) {
        if (!isset($all_valid_streams[$stream->stream_id])) {
            $orphaned_streams[$stream->stream_id] = 1;
        }
    }
    $result = $dbr->select('mv_stream_images', 'stream_id', '', __METHOD__, array('GROUP BY' => 'stream_id'));
    while ($stream = $dbr->fetchObject($result)) {
        if (!isset($all_valid_streams[$stream->stream_id])) {
            $orphaned_streams[$stream->stream_id] = 1;
        }
    }
    foreach ($orphaned_streams as $stream_id => $na) {
        $mvStream = new MV_Stream(array('id' => $stream_id));
        //double check stream does not exist:
        if (!$mvStream->doesStreamExist()) {
            print "stream id: {$stream_id} does not exist in stream table (remove)\n";
            //remove files in the stream directory:
            $filedir = '/video/metavid/mvprime_stream_images/' . MV_StreamImage::getRelativeImagePath($stream_id);
            //print "dir is: $filedir \n";
            if (is_dir($filedir)) {
                $cmd = 'rm -rf ' . $filedir;
                print "removing image run#: {$cmd} \n";
                shell_exec($cmd);
            }
            //print "removing DB entires for $stream_id\n";
            $mvStream->deleteDB();
        }
    }
    /*$streams = get_all_mv_streams();
    	foreach( $streams as $stream){
    		//check if stream page exists:
    		$mvStreamTitle = Title::newFromText( $stream->name, MV_NS_STREAM );
    		if( !$mvStreamTitle->exists() ){
    			print "stream: {$stream->name} does not exist as a wiki page\n";
    			//should remove here
    			$mvStream = new MV_Stream( $stream );
    		}
    	}*/
}
 function getLocalImageDir($stream_id)
 {
     global $mvLocalImgLoc;
     $img_dir = $mvLocalImgLoc . '/' . MV_StreamImage::getRelativeImagePath($stream_id);
     if (!is_dir($img_dir)) {
         if (!mkdir($img_dir, 0777, true)) {
             echo "error in making dir: {$img_dir}";
         }
     }
     return $img_dir;
 }
$mvTitle = new MV_Title('Stream:' . $stream_name);
if (!$mvTitle->doesStreamExist()) {
    print $stream_name . " does not exist ... creating\n";
    // print 'do stream desc'."\n";
    if (!isset($MVStreams)) {
        $MVStreams = array();
    }
    // init the stream (legacy from old stream insert system)
    $MVStreams[$stream->name] = new MV_Stream($stream);
    do_add_stream($mvTitle, $stream);
}
$stream_id = $mvTitle->getStreamId();
print 'got stream id: ' . $stream_id . "\n";
//check for & update stream files:
do_stream_file_check($stream);
$filedir = '/video/metavid/mvprime_stream_images/' . MV_StreamImage::getRelativeImagePath($stream_id);
if (!is_dir($filedir)) {
    shell_exec("mkdir {$filedir}");
    shell_exec("chmod -R 777 {$filedir}");
}
echo "working on: {$filename} \n";
$ocroutput = "";
//@@TODO we should do sequential output and parse the OCR file if it already exists.
//make sure we can write to the ocr file:
$ocrfileloc = "{$workingdir}/{$stream_name}.ocr";
$fh = @fopen($ocrfileloc, 'a') or die("\nError: can't write to ocr file\n");
fclose($fh);
//gets duration from ffmpeg
$dbw = $dbr = wfGetDB(DB_MASTER);
for ($i = 0; $i < $duration; $i += $interval) {
    //only run the ffmpeg cmd if we have to:
require_once 'maintenance_util.inc.php';
if (count($args) == 0 || isset($options['help'])) {
    print '
USAGE
 php ogg_thumb_insert.php stream_name filename interval duration

EXAMPLE where stream.ogg is 10min 20 seconds long and we get a frame every 5 seconds:
 ogg_thumb_insert.php stream_name stream.ogg 5 620

Notes:
  if possible you want to use the source footage rather than the ogg to generate the thumbnails (ie the mpeg2 or dv)

';
    exit;
}
$stream_name = $args[0];
$filename = $args[1];
$interval = $args[2];
$duration = $args[3];
$MV_Stream = MV_Stream::newStreamByName($stream_name);
$stream_id = $MV_Stream->getStreamId();
$filedir = '../stream_images/' . MV_StreamImage::getRelativeImagePath($stream_id);
$dbw = $dbr = wfGetDB(DB_MASTER);
for ($i = 0; $i < $duration; $i += $interval) {
    shell_exec("ffmpeg -ss {$i} -i {$filename} -vcodec mjpeg -vframes 1 -an -f rawvideo -y {$filedir}/{$i}.jpg");
    if (is_file("{$filedir}/{$i}.jpg")) {
        $dbw->query("INSERT INTO `mv_stream_images` (`stream_id`, `time`) VALUES ({$stream_id}, {$i})");
    } else {
        print "failed to create file: {$filedir}/{$i}.jpg \n";
    }
}