transcript.php
182 lines
| 6.2 KiB
| text/x-php
|
PhpLexer
/ include / transcript.php
| r1 | <?php | |||
| //check that angle brackets are balanced | ||||
| function bracketbalance($line) | ||||
| { | ||||
| #first, if no angle brackets, we're OK | ||||
| if(substr_count($line, "<") == 0 && substr_count($line, ">") == 0) | ||||
| return true; | ||||
| if(substr_count($line, "<") != substr_count($line, ">")) | ||||
| { | ||||
| return false; | ||||
| } | ||||
| return true; | ||||
| } | ||||
| // Retrieve transcript for this strip from the database, modifying the strip object. | ||||
| function gettranscript(&$strip) | ||||
| { | ||||
| global $mtdb; | ||||
| $result = $mtdb->query('SELECT strip FROM transcript WHERE strip=' . (int)$strip->id ); | ||||
| if($result) | ||||
| { | ||||
| $output = ''; | ||||
| /* | ||||
| #this gets me the highest panel number that has a transcript | ||||
| # panels beyond that either lack speech or don't exist | ||||
| # either way, I care not | ||||
| Might be able to exchange this loop of getOne()s for a getAll() call. | ||||
| */ | ||||
| $numPanels = $mtdb->getOne( 'SELECT MAX(panel) FROM transcript WHERE transcript.strip=' . (int)$strip->id ); | ||||
| if( $numPanels ) { | ||||
| for($i = 1; $i <= $numPanels; $i++) { | ||||
| $result = $mtdb->query( 'SELECT speaker, speech FROM transcript WHERE transcript.strip=' . (int)$strip->id . ' AND panel=' .$i.' ORDER BY line') | ||||
| or mtdie("There was an error fetching the panel count in the transcript for $strip->id, panel $i. " . mysql_error(), 'SQL Error'); | ||||
| if(!$result) continue; | ||||
| $output.= "\nnewpanel\n"; | ||||
| while($row = mysql_fetch_row($result)) { | ||||
| if(strlen($row[0]) < 1) continue; | ||||
| $output.= $row[0]; | ||||
| if($row[1] !== '') $output.= ":: ".$row[1]; | ||||
| $output.= "\n"; | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| $strip->transcript = $output; | ||||
| return $output; | ||||
| } | ||||
| // Parse submitted transcript from strip object, and insert it into the database. | ||||
| function savetranscript( &$strip ) { | ||||
| global $mtdb; | ||||
| $info = ''; | ||||
| $mtdb->query('START TRANSACTION'); | ||||
| //remove any old transcript data - it's being replaced | ||||
| $mtdb->query( 'DELETE FROM transcript WHERE transcript.strip=' . (int)$strip->id ); | ||||
| if( $strip->transcript_posted ) { | ||||
| $inserter = 'INSERT INTO transcript (strip,panel,line,speaker,speech,search) VALUES (%d,%d,%d,"%s","%s","%s")'; | ||||
| if(strpos($strip->transcript_posted, 'Panel <$n>') !== FALSE) { | ||||
| # This is probably a scrivener script | ||||
| $panels = explode('Panel <$n>', $strip->transcript_posted); | ||||
| $panels = array_map('trim', $panels); | ||||
| $numPanels = count($panels); | ||||
| for($i = 0; $i < $numPanels; $i++) { | ||||
| $lines = explode("\n", $panels[$i]); | ||||
| $lines = array_map('trim', $lines); | ||||
| # Initialize speaker controls | ||||
| $speaker = null; | ||||
| $has_spoken = true; | ||||
| for($j = 0; $j < count($lines); $j++) { | ||||
| $insert_sql = ''; | ||||
| if(strpos($lines[$j], '(') === 0) { | ||||
| # Line is a note, add it as a comment | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, '#', mysql_real_escape_string($lines[$j]), ''); | ||||
| } elseif(strpos($lines[$j], '[') === 0 || strlen($lines[$j]) == 0) { | ||||
| # Line is an annotation or blank, do nothing | ||||
| continue; | ||||
| } elseif(strpos($lines[$j], 'nospeaking:') === 0) { | ||||
| # Line contains a list of nonspeaking characters | ||||
| array_splice($lines, $j, 1, array_map('_nospeaker', explode(',', substr($lines[$j], 11)))); | ||||
| $speaker = trim(substr($lines[$j], 11)); | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysql_real_escape_string($speaker), '', ''); | ||||
| } elseif($i > 0 && $lines[$j] == strtoupper($lines[$j])) { | ||||
| # Line designates a new speaker, note speaker | ||||
| # Handle speakers who did not say anything | ||||
| if(null !== $speaker && !$has_spoken) | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysql_real_escape_string($speaker), '', ''); | ||||
| $speaker = ucfirst(strtolower($lines[$j])); | ||||
| $has_spoken = false; | ||||
| } elseif(null !== $speaker) { | ||||
| # Line is speech, match with speaker and add | ||||
| if(!bracketbalance($lines[$j])) | ||||
| $info .= "<p>Warning: Open brackets do not match close brackets in panel $i for speaker ".htmlentities($speaker).'</p>'; | ||||
| $search = preg_replace( '/[[:punct:]]|(?<=\s)\s+/', ' ', strtolower($lines[$j]) ); | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysql_real_escape_string($speaker), | ||||
| mysql_real_escape_string($lines[$j]), mysql_real_escape_string($search)); | ||||
| $has_spoken = true; | ||||
| } else { | ||||
| # Line is unrecognized, add it as a comment | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, '#', mysql_real_escape_string($lines[$j]), ''); | ||||
| } | ||||
| if( $insert_sql && false === $mtdb->query( $insert_sql ) ) { | ||||
| $mtdb->query('ROLLBACK'); | ||||
| mtdie (mysql_error(), 'Error inserting transcript.'); | ||||
| } | ||||
| } | ||||
| if(null !== $speaker && !$has_spoken) { | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysql_real_escape_string($speaker), '', ''); | ||||
| if( false === $mtdb->query( $insert_sql ) ) { | ||||
| $mtdb->query('ROLLBACK'); | ||||
| mtdie (mysql_error(), 'Error inserting transcript.'); | ||||
| } | ||||
| } | ||||
| } | ||||
| } else { | ||||
| # Assume that this is a Kalium style transcript | ||||
| $panels = explode('newpanel', $strip->transcript_posted); | ||||
| $numPanels = count($panels); | ||||
| foreach($panels as $currPanel) | ||||
| $currPanel = trim($currPanel); | ||||
| for($i = 1; $i < $numPanels; $i++) { | ||||
| $lines = explode("\n", $panels[$i]); | ||||
| $numLines = count($lines); | ||||
| foreach($lines as $currLine) | ||||
| $currLine = trim($currLine); | ||||
| for($j = 1; $j < $numLines; $j++) { | ||||
| $spoken = explode("::", $lines[$j]); // Distinguish between speaker and speech | ||||
| $spoken[0] = trim($spoken[0]); // Strip excess whitespace | ||||
| $spoken[1] = trim($spoken[1]); | ||||
| if(strlen($spoken[0]) < 1) continue; // Disregard null | ||||
| $spoken[2] = preg_replace('/[[:punct:]]|(?<=\s)\s+/', ' ', strtolower($spoken[1]) ); // Make searchable text | ||||
| if(!bracketbalance($spoken[1])) | ||||
| $info .= "<p>Warning: Open brackets do not match close brackets in panel $i for speaker ".htmlentities($spoken[0]).'</p>'; | ||||
| $insert_sql = sprintf($inserter, (int)$strip->id, (int)$i, (int)$j, mysql_real_escape_string($spoken[0]), | ||||
| mysql_real_escape_string($spoken[1]), mysql_real_escape_string($spoken[2]) ); | ||||
| if( false === $mtdb->query( $insert_sql ) ) { | ||||
| $mtdb->query('ROLLBACK'); | ||||
| mtdie (mysql_error(), 'Error inserting transcript.'); | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| $mtdb->query('COMMIT'); | ||||
| return $info; | ||||
| } | ||||
| function _nospeaker($name) | ||||
| { | ||||
| return "nospeaking: $name"; | ||||
| } | ||||
| ?> | ||||
