PageRenderTime 87ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/modules/developer/helpers/developer_task.php

https://github.com/kuranoglu/gallery3-contrib
PHP | 309 lines | 165 code | 9 blank | 135 comment | 13 complexity | d323953497bb38aeb06a0c32d68b8405 MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2009 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. class developer_task_Core {
  21. static function available_tasks() {
  22. // Return empty array so nothing appears in the maintenance screen
  23. return array();
  24. }
  25. static function create_module($task) {
  26. $context = unserialize($task->context);
  27. if (empty($context["module"])) {
  28. $context["class_name"] = strtr($context["name"], " ", "_");
  29. $context["module"] = strtolower($context["class_name"]);
  30. $context["module_path"] = (MODPATH . $context["module"]);
  31. }
  32. switch ($context["step"]) {
  33. case 0: // Create directory tree
  34. foreach (array("", "controllers", "helpers", "js", "views") as $dir) {
  35. $path = "{$context['module_path']}/$dir";
  36. if (!file_exists($path)) {
  37. mkdir($path);
  38. chmod($path, 0777);
  39. }
  40. }
  41. break;
  42. case 1: // Generate installer
  43. $context["installer"] = array();
  44. self::_render_helper_file($context, "installer");
  45. break;
  46. case 2: // Generate theme helper
  47. $context["theme"] = !isset($context["theme"]) ? array() : $context["theme"];
  48. self::_render_helper_file($context, "theme");
  49. break;
  50. case 3: // Generate block helper
  51. $context["block"] = array();
  52. self::_render_helper_file($context, "block");
  53. break;
  54. case 4: // Generate menu helper
  55. $context["menu"] = !isset($context["menu"]) ? array() : $context["menu"];
  56. self::_render_helper_file($context, "menu");
  57. break;
  58. case 5: // Generate event helper
  59. self::_render_helper_file($context, "event");
  60. break;
  61. case 6: // Generate admin controller
  62. $file = "{$context['module_path']}/controllers/admin_{$context['module']}.php";
  63. ob_start();
  64. $v = new View("admin_controller.txt");
  65. $v->name = $context["name"];
  66. $v->module = $context["module"];
  67. $v->class_name = $context["class_name"];
  68. print $v->render();
  69. file_put_contents($file, ob_get_contents());
  70. ob_end_clean();
  71. break;
  72. case 7: // Generate admin form
  73. $file = "{$context['module_path']}/views/admin_{$context['module']}.html.php";
  74. ob_start();
  75. $v = new View("admin_html.txt");
  76. $v->name = $context["name"];
  77. $v->module = $context["module"];
  78. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  79. print $v->render();
  80. file_put_contents($file, ob_get_contents());
  81. ob_end_clean();
  82. break;
  83. case 8: // Generate controller
  84. $file = "{$context['module_path']}/controllers/{$context['module']}.php";
  85. ob_start();
  86. $v = new View("controller.txt");
  87. $v->name = $context["name"];
  88. $v->module = $context["module"];
  89. $v->class_name = $context["class_name"];
  90. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  91. print $v->render();
  92. file_put_contents($file, ob_get_contents());
  93. ob_end_clean();
  94. break;
  95. case 9: // Generate sidebar block view
  96. $file = "{$context['module_path']}/views/{$context['module']}_block.html.php";
  97. ob_start();
  98. $v = new View("block_html.txt");
  99. $v->name = $context["name"];
  100. $v->module = $context["module"];
  101. $v->class_name = $context["class_name"];
  102. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  103. print $v->render();
  104. file_put_contents($file, ob_get_contents());
  105. ob_end_clean();
  106. break;
  107. case 10: // Generate module.info (do last)
  108. $file = "{$context["module_path"]}/module.info";
  109. ob_start();
  110. $v = new View("module_info.txt");
  111. $v->module_name = $context["name"];
  112. $v->module_description = $context["description"];
  113. print $v->render();
  114. file_put_contents($file, ob_get_contents());
  115. ob_end_clean();
  116. break;
  117. }
  118. if (isset($file)) {
  119. chmod($file, 0666);
  120. }
  121. $task->done = (++$context["step"]) >= 11;
  122. $task->context = serialize($context);
  123. $task->state = "success";
  124. $task->percent_complete = ($context["step"] / 11.0) * 100;
  125. }
  126. private static function _render_helper_file($context, $helper) {
  127. if (isset($context[$helper])) {
  128. $config = Kohana::config("developer.methods");
  129. $file = "{$context["module_path"]}/helpers/{$context["module"]}_{$helper}.php";
  130. touch($file);
  131. chmod($file, 0666);
  132. ob_start();
  133. $v = new View("$helper.txt");
  134. $v->helper = $helper;
  135. $v->name = $context["name"];
  136. $v->module = $context["module"];
  137. $v->module_name = $context["name"];
  138. $v->css_id = strtr($context["name"], " ", "");
  139. $v->css_id = preg_replace("#\s#", "", $context["name"]);
  140. $v->callbacks = empty($context[$helper]) ? array() : array_fill_keys($context[$helper], 1);
  141. print $v->render();
  142. file_put_contents($file, ob_get_contents());
  143. ob_end_clean();
  144. }
  145. }
  146. static function create_content($task) {
  147. $context = unserialize($task->context);
  148. $batch_cnt = $context["batch"];
  149. while ($context["albums"] > 0 && $batch_cnt > 0) {
  150. set_time_limit(30);
  151. self::_add_album_or_photo("album");
  152. $context["current"]++;
  153. $context["albums"]--;
  154. $batch_cnt--;
  155. }
  156. while ($context["photos"] > 0 && $batch_cnt > 0) {
  157. set_time_limit(30);
  158. self::_add_album_or_photo();
  159. $context["current"]++;
  160. $context["photos"]--;
  161. $batch_cnt--;
  162. }
  163. while ($context["comments"] > 0 && $batch_cnt > 0) {
  164. self::_add_comment();
  165. $context["current"]++;
  166. $context["comments"]--;
  167. $batch_cnt--;
  168. }
  169. while ($context["tags"] > 0 && $batch_cnt > 0) {
  170. self::_add_tag();
  171. $context["current"]++;
  172. $context["tags"]--;
  173. $batch_cnt--;
  174. }
  175. $task->done = $context["current"] >= $context["total"];
  176. $task->context = serialize($context);
  177. $task->state = "success";
  178. $task->percent_complete = $context["current"] / $context["total"] * 100;
  179. }
  180. private static function _add_album_or_photo($desired_type=null) {
  181. srand(time());
  182. $parents = ORM::factory("item")->where("type", "album")->find_all()->as_array();
  183. $owner_id = user::active()->id;
  184. $test_images = glob(dirname(dirname(__FILE__)) . "/data/*.[Jj][Pp][Gg]");
  185. $parent = $parents[array_rand($parents)];
  186. $parent->reload();
  187. $type = $desired_type;
  188. if (!$type) {
  189. $type = rand(0, 10) ? "photo" : "album";
  190. }
  191. if ($type == "album") {
  192. $thumb_size = module::get_var("core", "thumb_size");
  193. $rand = rand();
  194. $parents[] = album::create(
  195. $parent, "rnd_$rand", "Rnd $rand", "random album $rand", $owner_id)
  196. ->save();
  197. } else {
  198. $photo_index = rand(0, count($test_images) - 1);
  199. photo::create($parent, $test_images[$photo_index], basename($test_images[$photo_index]),
  200. "rnd_" . rand(), "sample thumb", $owner_id);
  201. }
  202. }
  203. private static function _add_comment() {
  204. srand(time());
  205. $photos = ORM::factory("item")->where("type", "photo")->find_all()->as_array();
  206. $users = ORM::factory("user")->find_all()->as_array();
  207. if (empty($photos)) {
  208. return;
  209. }
  210. if (module::is_active("akismet")) {
  211. akismet::$test_mode = 1;
  212. }
  213. $photo = $photos[array_rand($photos)];
  214. $author = $users[array_rand($users)];
  215. $guest_name = ucfirst(self::_random_phrase(rand(1, 3)));
  216. $guest_email = sprintf("%s@%s.com", self::_random_phrase(1), self::_random_phrase(1));
  217. $guest_url = sprintf("http://www.%s.com", self::_random_phrase(1));
  218. comment::create($photo, $author, self::_random_phrase(rand(8, 500)),
  219. $guest_name, $guest_email, $guest_url);
  220. }
  221. private static function _add_tag() {
  222. $items = ORM::factory("item")->find_all()->as_array();
  223. if (!empty($items)) {
  224. $tags = self::_generateTags();
  225. $tag_name = $tags[array_rand($tags)];
  226. $item = $items[array_rand($items)];
  227. tag::add($item, $tag_name);
  228. }
  229. }
  230. private static function _random_phrase($count) {
  231. static $words;
  232. if (empty($words)) {
  233. $sample_text = "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium
  234. laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi
  235. architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas
  236. sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione
  237. voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit,
  238. amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut
  239. labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis
  240. nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi
  241. consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam
  242. nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla
  243. pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis
  244. praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi
  245. sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt
  246. mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et
  247. expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque
  248. nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas
  249. assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis
  250. debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et
  251. molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut
  252. reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores
  253. repellat.";
  254. $words = preg_split('/\s+/', $sample_text);
  255. }
  256. $chosen = array();
  257. for ($i = 0; $i < $count; $i++) {
  258. $chosen[] = $words[array_rand($words)];
  259. }
  260. return implode(' ', $chosen);
  261. }
  262. private static function _generateTags($number=10){
  263. // Words from lorem2.com
  264. $words = explode(
  265. " ",
  266. "Lorem ipsum dolor sit amet consectetuer adipiscing elit Donec odio Quisque volutpat " .
  267. "mattis eros Nullam malesuada erat ut turpis Suspendisse urna nibh viverra non " .
  268. "semper suscipit posuere a pede Donec nec justo eget felis facilisis " .
  269. "fermentum Aliquam porttitor mauris sit amet orci Aenean dignissim pellentesque " .
  270. "felis Morbi in sem quis dui placerat ornare Pellentesque odio nisi euismod in " .
  271. "pharetra a ultricies in diam Sed arcu Cras consequat Praesent dapibus neque " .
  272. "id cursus faucibus tortor neque egestas augue eu vulputate magna eros eu " .
  273. "erat Aliquam erat volutpat Nam dui mi tincidunt quis accumsan porttitor " .
  274. "facilisis luctus metus Phasellus ultrices nulla quis nibh Quisque a " .
  275. "lectus Donec consectetuer ligula vulputate sem tristique cursus Nam nulla quam " .
  276. "gravida non commodo a sodales sit amet nisi Pellentesque fermentum " .
  277. "dolor Aliquam quam lectus facilisis auctor ultrices ut elementum vulputate " .
  278. "nunc Sed adipiscing ornare risus Morbi est est blandit sit amet sagittis vel " .
  279. "euismod vel velit Pellentesque egestas sem Suspendisse commodo ullamcorper " .
  280. "magna");
  281. while ($number--) {
  282. $results[] = $words[array_rand($words, 1)];
  283. }
  284. return $results;
  285. }
  286. }