From a25032a400f4942575882113faf7258bbc3d8f3e Mon Sep 17 00:00:00 2001 From: Nick Freear Date: Tue, 7 Jul 2015 10:51:45 +0100 Subject: [PATCH] Bug #3/#4, Adopt `MY_Loader::send_oembed_response()` function [iet:3729593] * Re-instate `POParser.php` (removed in: https://github.com/IET-OU/open-media-player-core/commit/b2cb0cb960) --- README.md | 16 ++- composer.json | 2 +- src/third_party/POParser.php | 204 +++++++++++++++++++++++++++ src/views/oembed/_generic_iframe.php | 2 +- src/views/oembed/oupodcast.php | 4 +- 5 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 src/third_party/POParser.php diff --git a/README.md b/README.md index 74d04d1..5cc3c41 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,23 @@ # IET-OU / open-media-player-core -Core components of Open Media Player — a unified, accessible online audio and video player solution for The Open University. _Part of the Open Media Player framework._ +Core components of Open Media Player — a unified, accessible online audio and video player solution for The Open University. _Part of the Open Media Player framework — more soon!_ * https://mediaplayer.open.ac.uk +## Installation + +Install and test using Git and [Composer][], + +```sh + git clone https://github.com/IET-OU/open-media-player-core + composer install + composer test +``` + + --- -License: `to be confirmed.`kkx +License: `to be confirmed.` © 2011-2015 The Open University. ([Institute of Educational Technology][]) @@ -14,3 +25,4 @@ License: `to be confirmed.`kkx [gpl]: http://gnu.org/licenses/gpl-2.0.html "GPL-2.0+" [parent]: https://github.com/IET-OU/ouplayer [Institute of Educational Technology]: http://iet.open.ac.uk/ +[Composer]: https://getcomposer.org/ diff --git a/composer.json b/composer.json index 94a8f2e..6977864 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "IET_OU\\Open_Media_Player\\": [ "src", "src/providers", "src/Players" ], "IET_OU\\SubClasses\\": "src/SubClasses" }, - "classmap": [ "src/third_party/class.pdf2text.php" ] + "classmap": [ "src/third_party/" ] }, "autoload-dev": { "psr-4": { diff --git a/src/third_party/POParser.php b/src/third_party/POParser.php new file mode 100644 index 0000000..8c54f92 --- /dev/null +++ b/src/third_party/POParser.php @@ -0,0 +1,204 @@ +. + */ + +class POParser +{ + private $_filename; + + /** + * Format of a msgid entry: + * array( + * 'references' => array(), // each file on a new line + * 'translator-comments' => '', + * 'extracted-comments' => '', + * 'flags' => array( + * 'fuzzy' + * ... + * ), + * 'previous-msgctxt' => '', + * 'previous-msgid' => '', + * 'msgctxt' => '', + * 'msgid' => '', + * + * // when no plural forms + * 'msgstr' => '', + * + * // when plural forms + * 'msgid_pural' => '', + * 'msgstr' => array( + * 0 => '', // singular + * 1 => '', // 1st plural form + * 2 => '', // 2nd plural form + * ... + * n => '' // nth plural form + * ) + * ) + * + * @see http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files + */ + + protected function _dequote($str) + { + return substr($str, 1, -1); + } + + public function parse($filename) + { + // basic file verification + if (!is_file($filename)) { + throw new Exception('The specified file does not exist.'); + } + if (substr($filename, strrpos($filename, '.')) !== '.po') { + throw new Exception('The specified file is not a PO file.'); + } + + $lines = file($filename, FILE_IGNORE_NEW_LINES); + + // on the first two lines I'm expecting msgid respectively msgstr, + // both containing empty strings + $entries = array( +// array( +// 'msgid' => '', +// 'msgstr' => array('') +// ) + ); + + // parsing headers; stop at the first empty line + $headers = array( + 'Project-Id-Version' => '', + 'Report-Msgid-Bugs-To' => '', + 'POT-Creation-Date' => '', + 'PO-Revision-Date' => '', + 'Last-Translator' => '', + 'Language-Team' => '', + 'Content-Type' => '', + 'Content-Transfer-Encoding' => '', + 'Plural-Forms' => '', + ); + $i = 2; + while ($line = $lines[$i++]) { + $line = $this->_dequote($line); + $colonIndex = strpos($line, ':'); + if ($colonIndex === false) { + continue; + } + $headerName = substr($line, 0, $colonIndex); + if (!isset($headers[$headerName])) { + continue; + } + // skip the white space after the colon and remove the \n at the end + $headers[$headerName] = substr($line, $colonIndex + 1, -2); + } + + $entry = array(); + for ($n = count($lines); $i < $n; $i++) { + $line = $lines[$i]; + if ($line === '') { + $entries[] = $entry; + $entry = array(); + continue; + } + if ($line[0] == '#') { + $comment = substr($line, 3); + switch ($line[1]) { + // translator comments + case ' ': { + if (!isset($entry['translator-comments'])) { + $entry['translator-comments'] = $comment; + } + else { + $entry['translator-comments'] .= "\n" . $comment; + } + break; + } + // extracted comments + case '.': { + if (!isset($entry['extracted-comments'])) { + $entry['extracted-comments'] = $comment; + } + else { + $entry['extracted-comments'] .= "\n" . $comment; + } + break; + } + // reference + case ':': { + if (!isset($entry['references'])) { + $entry['references'] = array(); + } + $entry['references'][] = $comment; + break; + } + // flag + case ',': { + if (!isset($entry['flags'])) { + $entry['flags'] = array(); + } + $entry['flags'][] = $comment; + break; + } + // previous msgid, msgctxt + case '|': { + // msgi[d] + if ($comment[4] == 'd') { + $entry['previous-msgid'] = $this->_dequote(substr($comment, 6)); + } + // msgc[t]xt + else { + $entry['previous-msgctxt'] = $this->_dequote(substr($comment, 8)); + } + break; + } + } + } + else if (strpos($line, 'msgid') === 0) { + if ($line[5] === ' ') { + $entry['msgid'] = $this->_dequote(substr($line, 6)); + } + // msgid[_]plural + else { + $entry['msgid_plural'] = $this->_dequote(substr($line, 13)); + } + } + else if (strpos($line, 'msgstr') === 0) { + // no plural forms + if ($line[6] === ' ') { + $entry['msgstr'] = $this->_dequote(substr($line, 7)); + } + // plural forms + else { + if (!isset($entry['msgstr'])) { + $entry['msgstr'] = array(); + } + $entry['msgstr'][] = $this->_dequote(substr($line, strpos($line, ' ') + 1)); + } + } + else if ($line[0] === '"' && isset($entry['msgstr'])) { + $line = "\n" . preg_replace('/([^\\\\])\\\\n$/', "\$1\n", $this->_dequote($line)); + if (!is_array($entry['msgstr'])) { + $entry['msgstr'] .= $line; + } + else { + $entry['msgstr'][count($entry['msgstr']) - 1] .= $line; + } + } + } + return array($headers, $entries); + } +} diff --git a/src/views/oembed/_generic_iframe.php b/src/views/oembed/_generic_iframe.php index 6daf487..59d7a1d 100644 --- a/src/views/oembed/_generic_iframe.php +++ b/src/views/oembed/_generic_iframe.php @@ -36,4 +36,4 @@ 'callback'=>$callback, 'oembed' =>$oembed, ); - $this->oembed_response($view_data); + $this->send_oembed_response($view_data); diff --git a/src/views/oembed/oupodcast.php b/src/views/oembed/oupodcast.php index d244a5a..f013b55 100644 --- a/src/views/oembed/oupodcast.php +++ b/src/views/oembed/oupodcast.php @@ -96,5 +96,5 @@ 'callback'=>$callback, 'oembed' =>$oembed, ); - - $this->load->view('oembed/render', $view_data); + + $this->send_oembed_response($view_data);