PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/developer/helpers/developer_task.php

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