PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/cli.php

https://bitbucket.org/nfreear/trackoer-core
PHP | 244 lines | 156 code | 57 blank | 31 comment | 16 complexity | 91318830a91fd8bf42c08c15cd93e866 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Track OER
  4. *
  5. * A web application to facilitate analytics for Open Educational Resources.
  6. *
  7. * @package trackoer-core
  8. * @copyright Copyright 2012 The Open University.
  9. * @author N.D.Freear, 17 August 2012.
  10. * @filesource
  11. */
  12. require_once APPPATH .'/controllers/oembed.php';
  13. /**
  14. * Controller for command-line interface (CLI) - batch processing.
  15. */
  16. class Cli extends Oembed { #MY_Controller {
  17. protected static $ARGS_ALL = array(
  18. 'url' => 'http://labspace.open.ac.uk%2FLearning_to_Learn_1.0', //Course URL.
  19. 'ac' => 'UA-1234578-9',
  20. 'mode' => 'plain-zip', // Was 'fmt'
  21. 'lic' => 'cc:by-nc-sa%2F2.0%2Fuk[/88x31]',
  22. 'dir' => '/input/directory',
  23. 'out' => '"C:/output directory"',
  24. 'log' => '%2Flogs%2Ffile.log',
  25. 'jspath' => '../Shared',
  26. 'css' => 'font-size:small',
  27. 'e' => '(extended debug)',
  28. 'v' => '(version)',
  29. 'h' => '(help)',
  30. );
  31. const ARGS = 'url ac fmt lic dir out log jspath css e v h'; //All.
  32. const ARGS_REQ = 'dir out';
  33. public function __construct() {
  34. parent::__construct();
  35. $this->load->tracker('Google', NULL);
  36. $this->load->library('Creative_Commons');
  37. $this->load->helper('directory');
  38. $this->load->config('batch_config');
  39. }
  40. protected function _echo_batch_version() {
  41. $version = <<<EOF
  42. Track OER batch processor CLI (trackoer-core).
  43. Copyright 2012-08-18 The Open University.
  44. EOF;
  45. echo $version;
  46. exit (1);
  47. }
  48. protected function _echo_batch_help() {
  49. $usage = <<<EOF
  50. Usage:
  51. \$ php index.php cli/batch
  52. EOF;
  53. foreach (self::$ARGS_ALL as $arg => $val) {
  54. if (strlen($arg)==1) {
  55. $usage .= "\t-$arg $val" .PHP_EOL;
  56. } else {
  57. $usage .= "\t--$arg=$val" .PHP_EOL;
  58. }
  59. }
  60. echo $usage;
  61. exit (1);
  62. }
  63. protected function _parse_batch_args($args) {
  64. if (! $this->input->is_cli_request()) {
  65. $this->_error('command-line only');
  66. }
  67. if (! $args) {
  68. $args = array();
  69. }
  70. // Process '--X=Y' into key-value pairs -- $params['X'] = 'Y';
  71. $params = array();
  72. foreach ($args as $arg) {
  73. $out;
  74. parse_str(ltrim($arg, '-'), $out);
  75. $params += $out;
  76. }
  77. // Merge array from configuration file.
  78. $config = $this->config->item('cli_batch');
  79. if ($config) {
  80. echo "Found 'cli_batch' configuration. Merging.. (command line overrides config.)". PHP_EOL;
  81. $params += $config;
  82. }
  83. if (! $params) {
  84. $this->_cli_error('missing arguments');
  85. }
  86. // Parameter validation..
  87. foreach ($params as $key => $value) {
  88. if (! isset(self::$ARGS_ALL[$key])) { #in_array($key, $tests)) {
  89. $this->_cli_error("unrecognised CLI argument '--$key' (--name=value)", 400);
  90. }
  91. }
  92. $params = (object) $params;
  93. // Output help or version info.
  94. if (isset($param->h)) {
  95. $this->_echo_batch_help();
  96. }
  97. if (isset($params->v)) {
  98. $this->_echo_batch_version();
  99. }
  100. // Check for required arguments.
  101. if (! isset($params->dir)) {
  102. $this->_cli_error("parameter '--dir=%2Finput%2Fdir' is required");
  103. }
  104. return $params;
  105. }
  106. protected function _cli_error($message) {
  107. echo 'Error, '. $message . PHP_EOL;
  108. $this->_echo_batch_help();
  109. }
  110. /**
  111. * THE cli/batch public method.
  112. */
  113. public function batch($args = NULL) {
  114. $params = $this->_parse_batch_args(func_get_args());
  115. // Make an oEmbed call..
  116. $result = parent::index($params);
  117. $dir_map = directory_map($params->dir);
  118. var_dump($params); #, $dir_map);
  119. if (! $dir_map) {
  120. $this->_cli_error("directory_map failed, $params->dir");
  121. }
  122. $batch_template = $this->load->view('cc_code/batch_template', $view_data = NULL, $return = TRUE);
  123. $source_host = parse_url($params->url, PHP_URL_HOST);
  124. $license_url = $this->cc->getLicenseUrl($params->lic);
  125. // We'll record some 'counts'
  126. $cn_proc = $cn_dir = $cn_nohtml = 0;
  127. foreach ($dir_map as $key => $filename) {
  128. // Filter directories!
  129. if (is_array($filename)) {
  130. echo "Skipping directory, $key" .PHP_EOL;
  131. $cn_dir++;
  132. continue;
  133. }
  134. if (! preg_match('/.+\.html?$/', $filename)) {
  135. echo "Skipping non-HTML file, $filename" .PHP_EOL;
  136. $cn_nohtml++;
  137. continue;
  138. }
  139. // Filter - only HTML/ XML?
  140. $input = file_get_contents($params->dir .'/'. $filename);
  141. $title = $result->title;
  142. // Get <title>, <h1>..
  143. if (preg_match('@<h1[^>]*>(.+)</h1@i', $input, $matches)) {
  144. $title .= '/ '. $matches[1];
  145. }
  146. $embed_code = strtr(
  147. $batch_template,
  148. array(
  149. '__GA_ID__' => $params->ac,
  150. '__CC_TEXT_URL__' => $this->ga->campaignUrl($license_url, $params->mode, TRACKER_RDF_LIC_LINK, $source_host, $result->identifier),
  151. '__CC_ICON_URL__' => $this->ga->campaignUrl($license_url, $params->mode, TRACKER_RDF_LIC_ICON, $source_host, $result->identifier),
  152. '__CC_ICON_SRC__' => $this->cc->getImageUrl($params->lic),
  153. #'__CC_TERMS__' => str_replace('cc:', '', $params->lic), # License terms, eg. 'by', 'by-nc-sa'
  154. #'__CC_VJ__' => '3.0', # License version[/jurisdiction], eg. '2.0/uk' or '3.0'
  155. '__CC_LABEL__' => 'Creative Commons Attribution 3.0 Unported License',
  156. '_ATTR_NAME_' => 'OpenLearn-LabSpace - Bridge to Success B2S', #'OpenLearn/ Andrew Studnicky',
  157. '_ATTR_URL_' => $this->ga->campaignUrl('http://labspace.open.ac.uk/b2s', $params->mode, TRACKER_RDF_ATTR_LINK, $source_host, $result->identifier),
  158. '_TITLE_' => $title,
  159. '_SOURCE_URL_' => $this->ga->campaignUrl($params->url, $params->mode, TRACKER_RDF_SRC_LINK, $source_host, $result->identifier),
  160. '_SOURCE_TEXT_'=> $params->url,
  161. '__COURSE_HOST__'=> $source_host,
  162. '__COURSE_ID__' => $result->identifier,
  163. '__WORK_ID__' => $filename,
  164. '__MODE__' => $params->mode, # 'scorm', 'ims' etc.
  165. '__SCRIPT_PATH__'=> $params->jspath, # Relative path.
  166. '__SCRIPT_ARG__' => 'type="text/javascript"', # HTML5 ''.
  167. '__STYLE__' => $params->css,
  168. )
  169. );
  170. // Initially try to inject code before the last closing </div>..
  171. $cn_inject = 0;
  172. $output = preg_replace('@(</div>\s+</body>)@ms', $embed_code .PHP_EOL. '$1', $input, 1, $cn_inject);
  173. //..Fallback to injecting before </body>.
  174. if (! $cn_inject) {
  175. $output = preg_replace('@(</(body|html)>)@i', $embed_code .PHP_EOL. '$1', $input, 1, $cn_inject);
  176. }
  177. $out_file = $params->out .'/'. $filename;
  178. $bytes = file_put_contents($out_file, $output);
  179. echo "> File out, $bytes : $out_file" .PHP_EOL;
  180. $cn_proc++;
  181. //break;
  182. }
  183. echo "$cn_proc files processed, OK" .PHP_EOL;
  184. echo "$cn_dir/$cn_nohtml directories/no-HTML files skipped." .PHP_EOL;
  185. exit (0);
  186. }
  187. }