PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/developer/helpers/developer_task.php

https://github.com/mp2300/gallery3-contrib
PHP | 323 lines | 161 code | 9 blank | 153 comment | 13 complexity | ca8ad72cf11b85188fb4ebc631b75bb7 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 event helper
  55. self::_render_helper_file($context, "event");
  56. break;
  57. case 5: // Generate admin controller
  58. $file = "{$context['module_path']}/controllers/admin_{$context['module']}.php";
  59. ob_start();
  60. $v = new View("admin_controller.txt");
  61. $v->name = $context["name"];
  62. $v->module = $context["module"];
  63. $v->class_name = $context["class_name"];
  64. print $v->render();
  65. file_put_contents($file, ob_get_contents());
  66. ob_end_clean();
  67. break;
  68. case 6: // Generate admin form
  69. $file = "{$context['module_path']}/views/admin_{$context['module']}.html.php";
  70. ob_start();
  71. $v = new View("admin_html.txt");
  72. $v->name = $context["name"];
  73. $v->module = $context["module"];
  74. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  75. print $v->render();
  76. file_put_contents($file, ob_get_contents());
  77. ob_end_clean();
  78. break;
  79. case 7: // Generate controller
  80. $file = "{$context['module_path']}/controllers/{$context['module']}.php";
  81. ob_start();
  82. $v = new View("controller.txt");
  83. $v->name = $context["name"];
  84. $v->module = $context["module"];
  85. $v->class_name = $context["class_name"];
  86. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  87. print $v->render();
  88. file_put_contents($file, ob_get_contents());
  89. ob_end_clean();
  90. break;
  91. case 8: // Generate sidebar block view
  92. $file = "{$context['module_path']}/views/{$context['module']}_block.html.php";
  93. ob_start();
  94. $v = new View("block_html.txt");
  95. $v->name = $context["name"];
  96. $v->module = $context["module"];
  97. $v->class_name = $context["class_name"];
  98. $v->css_id = preg_replace("#\s+#", "", $context["name"]);
  99. print $v->render();
  100. file_put_contents($file, ob_get_contents());
  101. ob_end_clean();
  102. break;
  103. case 9: // Generate module.info (do last)
  104. $file = "{$context["module_path"]}/module.info";
  105. ob_start();
  106. $v = new View("module_info.txt");
  107. $v->module_name = $context["display_name"];
  108. $v->module_description = $context["description"];
  109. print $v->render();
  110. file_put_contents($file, ob_get_contents());
  111. ob_end_clean();
  112. break;
  113. }
  114. if (isset($file)) {
  115. chmod($file, 0666);
  116. }
  117. $task->done = (++$context["step"]) >= 11;
  118. $task->context = serialize($context);
  119. $task->state = "success";
  120. $task->percent_complete = ($context["step"] / 11.0) * 100;
  121. }
  122. private static function _render_helper_file($context, $helper) {
  123. if (isset($context[$helper])) {
  124. $config = Kohana::config("developer.methods");
  125. $file = "{$context["module_path"]}/helpers/{$context["module"]}_{$helper}.php";
  126. touch($file);
  127. chmod($file, 0666);
  128. ob_start();
  129. $v = new View("$helper.txt");
  130. $v->helper = $helper;
  131. $v->name = $context["name"];
  132. $v->module = $context["module"];
  133. $v->module_name = $context["name"];
  134. $v->css_id = strtr($context["name"], " ", "");
  135. $v->css_id = preg_replace("#\s#", "", $context["name"]);
  136. $v->callbacks = empty($context[$helper]) ? array() : array_fill_keys($context[$helper], 1);
  137. print $v->render();
  138. file_put_contents($file, ob_get_contents());
  139. ob_end_clean();
  140. }
  141. }
  142. static function create_content($task) {
  143. $context = unserialize($task->context);
  144. $batch_cnt = $context["batch"];
  145. while ($context["albums"] > 0 && $batch_cnt > 0) {
  146. set_time_limit(30);
  147. self::_add_album_or_photo("album");
  148. $context["current"]++;
  149. $context["albums"]--;
  150. $batch_cnt--;
  151. }
  152. while ($context["photos"] > 0 && $batch_cnt > 0) {
  153. set_time_limit(30);
  154. self::_add_album_or_photo();
  155. $context["current"]++;
  156. $context["photos"]--;
  157. $batch_cnt--;
  158. }
  159. while ($context["comments"] > 0 && $batch_cnt > 0) {
  160. self::_add_comment();
  161. $context["current"]++;
  162. $context["comments"]--;
  163. $batch_cnt--;
  164. }
  165. while ($context["tags"] > 0 && $batch_cnt > 0) {
  166. self::_add_tag();
  167. $context["current"]++;
  168. $context["tags"]--;
  169. $batch_cnt--;
  170. }
  171. $task->done = $context["current"] >= $context["total"];
  172. $task->context = serialize($context);
  173. $task->state = "success";
  174. $task->percent_complete = $context["current"] / $context["total"] * 100;
  175. }
  176. private static function _add_album_or_photo($desired_type=null) {
  177. srand(time());
  178. $parents = ORM::factory("item")->where("type", "=", "album")->find_all()->as_array();
  179. $owner_id = user::active()->id;
  180. $test_images = glob(dirname(dirname(__FILE__)) . "/data/*.[Jj][Pp][Gg]");
  181. $parent = $parents[array_rand($parents)];
  182. $parent->reload();
  183. $type = $desired_type;
  184. if (!$type) {
  185. $type = rand(0, 10) ? "photo" : "album";
  186. }
  187. if ($type == "album") {
  188. $thumb_size = module::get_var("core", "thumb_size");
  189. $rand = rand();
  190. $item = ORM::factory("item");
  191. $item->type = "album";
  192. $item->parent_id = $parent->id;
  193. $item->name = "rnd_$rand";
  194. $item->title = "Rnd $rand";
  195. $item->description = "random album $rand";
  196. $item->owner_id = $owner_id;
  197. $parents[] = $item->save();
  198. } else {
  199. $photo_index = rand(0, count($test_images) - 1);
  200. $item = ORM:factory("item");
  201. $item->type = "photo";
  202. $item->parent_id = $parent->id;
  203. $item->set_data_file($test_images[$photo_index]);
  204. $item->name = basename($test_images[$photo_index]);
  205. $item->title = "rnd_" . rand();
  206. $item->description = "sample thumb";
  207. $item->owner_id = $owner_id;
  208. $item->save();
  209. }
  210. }
  211. private static function _add_comment() {
  212. srand(time());
  213. $photos = ORM::factory("item")->where("type", "=", "photo")->find_all()->as_array();
  214. $users = ORM::factory("user")->find_all()->as_array();
  215. if (empty($photos)) {
  216. return;
  217. }
  218. if (module::is_active("akismet")) {
  219. akismet::$test_mode = 1;
  220. }
  221. $photo = $photos[array_rand($photos)];
  222. $author = $users[array_rand($users)];
  223. $guest_name = ucfirst(self::_random_phrase(rand(1, 3)));
  224. $guest_email = sprintf("%s@%s.com", self::_random_phrase(1), self::_random_phrase(1));
  225. $guest_url = sprintf("http://www.%s.com", self::_random_phrase(1));
  226. $comment = ORM::factory("comment");
  227. $comment->author_id = $author->id;
  228. $comment->text = self::_random_phrase(rand(8, 500));
  229. $comment->guest_name = $guest_name;
  230. $comment->guest_email = $guest_email;
  231. $comment->guest_url = $guest_url;
  232. $comment->save();
  233. }
  234. private static function _add_tag() {
  235. $items = ORM::factory("item")->find_all()->as_array();
  236. if (!empty($items)) {
  237. $tags = self::_generateTags();
  238. $tag_name = $tags[array_rand($tags)];
  239. $item = $items[array_rand($items)];
  240. tag::add($item, $tag_name);
  241. }
  242. }
  243. private static function _random_phrase($count) {
  244. static $words;
  245. if (empty($words)) {
  246. $sample_text = "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium
  247. laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi
  248. architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas
  249. sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione
  250. voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit,
  251. amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut
  252. labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis
  253. nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi
  254. consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam
  255. nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla
  256. pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis
  257. praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi
  258. sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt
  259. mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et
  260. expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque
  261. nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas
  262. assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis
  263. debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et
  264. molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut
  265. reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores
  266. repellat.";
  267. $words = preg_split('/\s+/', $sample_text);
  268. }
  269. $chosen = array();
  270. for ($i = 0; $i < $count; $i++) {
  271. $chosen[] = $words[array_rand($words)];
  272. }
  273. return implode(' ', $chosen);
  274. }
  275. private static function _generateTags($number=10){
  276. // Words from lorem2.com
  277. $words = explode(
  278. " ",
  279. "Lorem ipsum dolor sit amet consectetuer adipiscing elit Donec odio Quisque volutpat " .
  280. "mattis eros Nullam malesuada erat ut turpis Suspendisse urna nibh viverra non " .
  281. "semper suscipit posuere a pede Donec nec justo eget felis facilisis " .
  282. "fermentum Aliquam porttitor mauris sit amet orci Aenean dignissim pellentesque " .
  283. "felis Morbi in sem quis dui placerat ornare Pellentesque odio nisi euismod in " .
  284. "pharetra a ultricies in diam Sed arcu Cras consequat Praesent dapibus neque " .
  285. "id cursus faucibus tortor neque egestas augue eu vulputate magna eros eu " .
  286. "erat Aliquam erat volutpat Nam dui mi tincidunt quis accumsan porttitor " .
  287. "facilisis luctus metus Phasellus ultrices nulla quis nibh Quisque a " .
  288. "lectus Donec consectetuer ligula vulputate sem tristique cursus Nam nulla quam " .
  289. "gravida non commodo a sodales sit amet nisi Pellentesque fermentum " .
  290. "dolor Aliquam quam lectus facilisis auctor ultrices ut elementum vulputate " .
  291. "nunc Sed adipiscing ornare risus Morbi est est blandit sit amet sagittis vel " .
  292. "euismod vel velit Pellentesque egestas sem Suspendisse commodo ullamcorper " .
  293. "magna");
  294. while ($number--) {
  295. $results[] = $words[array_rand($words, 1)];
  296. }
  297. return $results;
  298. }
  299. }