transcript.php
182 lines
| 6.4 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; | ||||
| r4 | ||||
| r1 | if(substr_count($line, "<") != substr_count($line, ">")) | |||
| { | ||||
| return false; | ||||
| } | ||||
| r4 | ||||
| r1 | return true; | |||
| } | ||||
| // Retrieve transcript for this strip from the database, modifying the strip object. | ||||
| function gettranscript(&$strip) | ||||
| { | ||||
| global $mtdb; | ||||
| r4 | ||||
| r1 | $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') | ||||
| r4 | or mtdie("There was an error fetching the panel count in the transcript for $strip->id, panel $i. " . mysqli_error(), 'SQL Error'); | |||
| r1 | if(!$result) continue; | |||
| r4 | ||||
| r1 | $output.= "\nnewpanel\n"; | |||
| r4 | while($row = mysqli_fetch_row($result)) { | |||
| r1 | if(strlen($row[0]) < 1) continue; | |||
| r4 | ||||
| r1 | $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 = ''; | ||||
| r4 | ||||
| r1 | $mtdb->query('START TRANSACTION'); | |||
| r4 | ||||
| r1 | //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 | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, '#', mysqli_real_escape_string($mtdb->link, $lines[$j]), ''); | |||
| r1 | } 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)); | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysqli_real_escape_string($mtdb->link, $speaker), '', ''); | |||
| r1 | } 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) | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysqli_real_escape_string($mtdb->link, $speaker), '', ''); | |||
| r1 | ||||
| $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]) ); | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysqli_real_escape_string($mtdb->link, $speaker), | |||
| mysqli_real_escape_string($mtdb->link, $lines[$j]), mysqli_real_escape_string($mtdb->link, $search)); | ||||
| r1 | $has_spoken = true; | |||
| } else { | ||||
| # Line is unrecognized, add it as a comment | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, '#', mysqli_real_escape_string($mtdb->link, $lines[$j]), ''); | |||
| r1 | } | |||
| if( $insert_sql && false === $mtdb->query( $insert_sql ) ) { | ||||
| $mtdb->query('ROLLBACK'); | ||||
| r7 | mtdie (mysqli_error($mtdb->link), 'Error inserting transcript.'); | |||
| r1 | } | |||
| } | ||||
| if(null !== $speaker && !$has_spoken) { | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, $i, $j, mysqli_real_escape_string($mtdb->link, $speaker), '', ''); | |||
| r1 | if( false === $mtdb->query( $insert_sql ) ) { | |||
| $mtdb->query('ROLLBACK'); | ||||
| r7 | mtdie (mysqli_error($mtdb->link), 'Error inserting transcript.'); | |||
| r1 | } | |||
| } | ||||
| } | ||||
| r4 | ||||
| r1 | } 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); | ||||
| r4 | ||||
| r1 | for($i = 1; $i < $numPanels; $i++) { | |||
| $lines = explode("\n", $panels[$i]); | ||||
| $numLines = count($lines); | ||||
| foreach($lines as $currLine) | ||||
| $currLine = trim($currLine); | ||||
| r4 | ||||
| r1 | for($j = 1; $j < $numLines; $j++) { | |||
| $spoken = explode("::", $lines[$j]); // Distinguish between speaker and speech | ||||
| r4 | ||||
| r1 | $spoken[0] = trim($spoken[0]); // Strip excess whitespace | |||
| $spoken[1] = trim($spoken[1]); | ||||
| r4 | ||||
| r1 | if(strlen($spoken[0]) < 1) continue; // Disregard null | |||
| $spoken[2] = preg_replace('/[[:punct:]]|(?<=\s)\s+/', ' ', strtolower($spoken[1]) ); // Make searchable text | ||||
| r4 | ||||
| r1 | if(!bracketbalance($spoken[1])) | |||
| $info .= "<p>Warning: Open brackets do not match close brackets in panel $i for speaker ".htmlentities($spoken[0]).'</p>'; | ||||
| r4 | $insert_sql = sprintf($inserter, (int)$strip->id, (int)$i, (int)$j, mysqli_real_escape_string($mtdb->link, $spoken[0]), | |||
| mysqli_real_escape_string($mtdb->link, $spoken[1]), mysqli_real_escape_string($mtdb->link, $spoken[2]) ); | ||||
| r1 | if( false === $mtdb->query( $insert_sql ) ) { | |||
| $mtdb->query('ROLLBACK'); | ||||
| r7 | mtdie (mysqli_error($mtdb->link), 'Error inserting transcript.'); | |||
| r1 | } | |||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| $mtdb->query('COMMIT'); | ||||
| return $info; | ||||
| } | ||||
| function _nospeaker($name) | ||||
| { | ||||
| return "nospeaking: $name"; | ||||
| } | ||||
| ?> | ||||
