PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cli_import.php

https://github.com/shish/shimmie2-utils
PHP | 132 lines | 69 code | 15 blank | 48 comment | 12 complexity | 41a9e43ed9ed54f3781c4b09d86edc37 MD5 | raw file
  1. #!/usr/bin/php
  2. <?php
  3. function add_image($tmpname, $filename, $tags) {
  4. if(file_exists($tmpname)) {
  5. global $user;
  6. $pathinfo = pathinfo($filename);
  7. $metadata['filename'] = $pathinfo['basename'];
  8. $metadata['extension'] = $pathinfo['extension'];
  9. $metadata['tags'] = $tags;
  10. $metadata['source'] = null;
  11. $event = new DataUploadEvent($user, $tmpname, $metadata);
  12. send_event($event);
  13. if($event->vetoed) {
  14. return $event->veto_reason;
  15. }
  16. }
  17. }
  18. function add_dir($base, $subdir="") {
  19. global $page;
  20. if(!is_dir($base)) {
  21. print "Error: $base is not a directory\n";
  22. return;
  23. }
  24. print "Adding $subdir\n";
  25. $dir = opendir("$base/$subdir");
  26. while($filename = readdir($dir)) {
  27. $fullpath = "$base/$subdir/$filename";
  28. if(is_link($fullpath)) {
  29. // ignore
  30. }
  31. else if(is_dir($fullpath)) {
  32. if($filename[0] != ".") {
  33. add_dir($base, "$subdir/$filename");
  34. }
  35. }
  36. else {
  37. $tmpfile = $fullpath;
  38. $tags = $subdir;
  39. $tags = str_replace("/", " ", $tags);
  40. $tags = str_replace("__", " ", $tags);
  41. print "$subdir/$filename (".str_replace(" ", ",", $tags).")... ";
  42. $error = add_image($tmpfile, $filename, $tags);
  43. if(is_null($error)) {
  44. print "ok\n";
  45. }
  46. else {
  47. print "failed: $error\n";
  48. }
  49. }
  50. }
  51. closedir($dir);
  52. }
  53. // ===========================================================================
  54. // set up and purify the environment
  55. define("DEBUG", true);
  56. define("VERSION", 'trunk');
  57. require_once "core/util.inc.php";
  58. version_check();
  59. sanitise_environment();
  60. check_cli();
  61. // load base files
  62. $files = array_merge(glob("core/*.php"), glob("ext/*/main.php"));
  63. foreach($files as $filename) {
  64. require_once $filename;
  65. }
  66. // connect to database
  67. $database = new Database();
  68. $database->db->fnExecute = '_count_execs';
  69. $config = new Config($database);
  70. // load the theme parts
  71. $_theme = $config->get_string("theme", "default");
  72. if(!file_exists("themes/$_theme")) $_theme = "default";
  73. require_once "themes/$_theme/page.class.php";
  74. require_once "themes/$_theme/layout.class.php";
  75. require_once "themes/$_theme/themelet.class.php";
  76. $themelets = glob("ext/*/theme.php");
  77. foreach($themelets as $filename) {
  78. require_once $filename;
  79. }
  80. $custom_themelets = glob("themes/$_theme/*.theme.php");
  81. if($custom_themelets) {
  82. $m = array();
  83. foreach($custom_themelets as $filename) {
  84. if(preg_match("/themes\/$_theme\/(.*)\.theme\.php/",$filename,$m)
  85. && array_contains($themelets, "ext/{$m[1]}/theme.php"))
  86. {
  87. require_once $filename;
  88. }
  89. }
  90. }
  91. // start the page generation waterfall
  92. $page = new Page();
  93. $user = _get_user();
  94. $context = new RequestContext();
  95. $context->page = $page;
  96. $context->user = $user;
  97. $context->database = $database;
  98. $context->config = $config;
  99. send_event(new InitExtEvent($context));
  100. $argv = $_SERVER['argv'];
  101. if(count($argv) == 2) {
  102. add_dir($argv[1]);
  103. }
  104. else {
  105. print "Usage: {$argv[0]} <directory to add>\n";
  106. }
  107. //$page->display();
  108. // for databases which support transactions
  109. $database->db->CommitTrans(true);
  110. ?>