PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/og/scripts/generate-og-d6-content.php

https://github.com/mattpclark/n-peace
PHP | 324 lines | 201 code | 42 blank | 81 comment | 0 complexity | 620813bb67fdef5819378e7a1bd334b5 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Generate content for a Drupal 6 database to test the upgrade process.
  5. *
  6. * @todo: Currently we use Drush for producing the script, find out why I wasn't
  7. * able to use just php-cli.
  8. *
  9. * Run this script at the root of an existing Drupal 6 installation.
  10. * Steps to use this generation script:
  11. * - Install Drupal 6.
  12. * - Install and enable Organic groups module.
  13. * - Copy from Drupal 7 includes/utility.inc to Drupal 6 includes folder (the
  14. * include file is version agnostic).
  15. * - Execute script to create the content by running
  16. * drush php-script generate-og-d6-content.php
  17. * - Execute script to dump the database by running
  18. * drush php-script dump-database-d6.sh > drupal-6.og.database.php
  19. * from the command line of the Drupal 6 ROOT directory.
  20. * - Since Organic groups module is a contrib module, it needs to be disabled
  21. * for the upgrade path, thus open the result file with a text editor and
  22. * under the {system} table, change the "status" value of the 'og' insertion
  23. * to 0.
  24. *
  25. * This scripts produces the following scenario:
  26. * - Nid 1: Group without posts.
  27. * - Nid 2: Group with 3 posts (Nid 3 - 5).
  28. * - Nid 6: Orphan group content (i.e. not attached to a group).
  29. * - Nid 7, 8: Groups that share a group content (Nid 9).
  30. * - Nid 10: Group with members:
  31. * - Uid 3: Group manager.
  32. * - Uid 4: Pending member.
  33. * - Uid 5: Active member.
  34. * - Uid 6: Pending admin member.
  35. * - Uid 7: Active admin member.
  36. * - Nid 11: Group with "Open" selective state.
  37. * - Nid 12: Group with "Moderated" selective state.
  38. * - Nid 13: Group with "Invite only" selective state.
  39. * - Nid 14: Group with "Closed" selective state.
  40. */
  41. // Define settings.
  42. $cmd = 'index.php';
  43. $_SERVER['HTTP_HOST'] = 'default';
  44. $_SERVER['PHP_SELF'] = '/index.php';
  45. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  46. $_SERVER['SERVER_SOFTWARE'] = NULL;
  47. $_SERVER['REQUEST_METHOD'] = 'GET';
  48. $_SERVER['QUERY_STRING'] = '';
  49. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
  50. $_SERVER['HTTP_USER_AGENT'] = 'console';
  51. $modules_to_enable = array('og', 'user', 'node');
  52. // Bootstrap Drupal.
  53. include_once './includes/bootstrap.inc';
  54. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  55. // Enable requested modules
  56. include_once './modules/system/system.admin.inc';
  57. $form = system_modules();
  58. foreach ($modules_to_enable as $module) {
  59. $form_state['values']['status'][$module] = TRUE;
  60. }
  61. $form_state['values']['disabled_modules'] = $form['disabled_modules'];
  62. system_modules_submit(NULL, $form_state);
  63. unset($form_state);
  64. // Run cron after installing
  65. drupal_cron_run();
  66. // Create new content types.
  67. module_load_include('inc','node','content_types');
  68. $form_state = array( 'values' => array() );
  69. $form_state['values']['name'] = 'test-group';
  70. $form_state['values']['type'] = 'test_group';
  71. $form_state['values']['og_content_type_usage'] = 'group';
  72. drupal_execute('node_type_form',$form_state);
  73. $form_state = array( 'values'=>array() );
  74. $form_state['values']['name'] = 'test-post-group';
  75. $form_state['values']['type'] = 'test_post_group';
  76. $form_state['values']['og_content_type_usage'] = 'group_post_standard';
  77. drupal_execute('node_type_form',$form_state);
  78. // Create users with ID 3 to 7.
  79. $user_ids=array();
  80. foreach (range(3, 7) as $i) {
  81. $user_values = array();
  82. $user_values['name'] = 'og_test_user' . $i;
  83. $user_values['mail'] = 'og_test_user' . $i . '@example.com';
  84. $user_values['pass'] = user_password(5);
  85. $user_values['status'] = 1;
  86. $user = user_save(NULL,$user_values);
  87. $user_ids[$i] = $user->uid;
  88. }
  89. /**
  90. * Organic groups content generation interface.
  91. */
  92. interface ogContent {
  93. //Returns a list of groups configuration arrays.
  94. public function groupList($user_ids);
  95. // Returns a list of posts configuation arrays
  96. // Groups nids generated in groupList are provided in groups parameter.
  97. public function postList($user_ids, $groups);
  98. // Performs actions of the generated groups and posts.
  99. public function groupActions($user_ids, $groups, $posts);
  100. }
  101. /**
  102. * A group without posts.
  103. */
  104. class ogGroupNoPosts implements ogContent {
  105. public function groupList($user_ids) {
  106. $list = array();
  107. $list[] = array(
  108. 'title' => 'group-without-posts',
  109. 'uid' => $user_ids[3],
  110. 'body' => 'group without posts',
  111. 'og_description' => 'description group without posts.',
  112. );
  113. return $list;
  114. }
  115. public function postList($user_ids, $groups) {
  116. return array();
  117. }
  118. public function groupActions($user_ids, $groups, $posts) {}
  119. }
  120. /**
  121. * A group with three posts.
  122. */
  123. class ogGroupThreePosts implements ogContent {
  124. public function groupList($user_ids) {
  125. $list = array();
  126. $list[] = array(
  127. 'title' => 'group-with-3-posts',
  128. 'uid' => $user_ids[3],
  129. 'body' => 'group with 3 posts',
  130. 'og_description' => 'description group with 3 posts.',
  131. );
  132. return $list;
  133. }
  134. public function postList($user_ids, $groups) {
  135. $gid = $groups[0];
  136. $list = array();
  137. foreach ( array(1,2,3) as $itr){
  138. $list[] = array(
  139. 'title' => 'group-posts-' . $itr,
  140. 'uid' => $user_ids[3],
  141. 'body' => 'group posts ' . $itr,
  142. 'og_groups' => array($gid),
  143. );
  144. }
  145. return $list;
  146. }
  147. public function groupActions($user_ids, $groups, $posts){}
  148. }
  149. /**
  150. * A group post not associated to any other group.
  151. */
  152. class ogGroupOrphanPost implements ogContent {
  153. public function groupList($user_ids) {
  154. return array();
  155. }
  156. public function postList($user_ids, $groups) {
  157. $list = array();
  158. $list[] = array(
  159. 'title' => 'group-posts-orphan',
  160. 'uid' => $user_ids[3],
  161. 'body' => 'group posts orphan',
  162. 'og_groups' => array(),
  163. );
  164. return $list;
  165. }
  166. public function groupActions($user_ids, $groups, $posts){}
  167. }
  168. /**
  169. * A group post associated with two groups.
  170. */
  171. class ogGroupPostMultipleGroups implements ogContent {
  172. public function groupList($user_ids) {
  173. $list = array();
  174. $list['alpha'] = array(
  175. 'title' => 'group-alpha',
  176. 'uid' => $user_ids[3],
  177. 'body' => 'group alpha',
  178. 'og_description' => 'description group alpha.',
  179. );
  180. $list['beta'] =array(
  181. 'title' => 'group-beta',
  182. 'uid' => $user_ids[3],
  183. 'body' => 'group beta',
  184. 'og_description' => 'description group beta.',
  185. );
  186. return $list;
  187. }
  188. public function postList($user_ids, $groups) {
  189. $list = array();
  190. $gid_b = $groups['beta'];
  191. $gid_a = $groups['alpha'];
  192. $list[] = array(
  193. 'title' => 'group-posts-ab',
  194. 'uid' => $user_ids[3],
  195. 'body' => 'group posts ab',
  196. 'og_groups' => array($gid_a,$gid_b)
  197. );
  198. return $list;
  199. }
  200. public function groupActions($user_ids, $groups, $posts) {}
  201. }
  202. /**
  203. * A group with multiple members.
  204. */
  205. class ogGroupUserAction implements ogContent {
  206. public function groupList($user_ids) {
  207. $list = array();
  208. $list[] = array(
  209. 'title' => 'group-with-user-action',
  210. 'uid' => $user_ids[3],
  211. 'body' => 'group with user action',
  212. 'og_description' => 'description with user action.',
  213. );
  214. return $list;
  215. }
  216. public function postList($user_ids, $groups) {
  217. return array();
  218. }
  219. public function groupActions($user_ids, $groups, $posts) {
  220. $gid = $groups[0];
  221. // - user ID 4 as pending member.
  222. og_save_subscription( $gid , $user_ids[4] , array('is_active' => 0));
  223. // - user ID 5 as active member.
  224. og_save_subscription( $gid , $user_ids[5] , array('is_active' => 1));
  225. // - user ID 6 as pending admin member.
  226. og_save_subscription( $gid , $user_ids[6] , array('is_active' => 0 , 'is_admin' => 1));
  227. // - user ID 7 as active admin member.
  228. og_save_subscription( $gid , $user_ids[7] , array('is_active' => 1 , 'is_admin' => 1));
  229. }
  230. }
  231. /**
  232. * Groups with different selective state (e.g. open, moderated, etc'.).
  233. */
  234. class ogGroupSelectiveState implements ogContent {
  235. public function groupList($user_ids) {
  236. $list = array();
  237. foreach (og_selective_map() as $key => $value) {
  238. $list[] = array(
  239. 'title' => 'group-selective-state-' . $value,
  240. 'uid' => $user_ids[3],
  241. 'body' => 'Group with selective state set to ' . $value,
  242. 'og_description' => 'Group with selective state set.',
  243. );
  244. }
  245. return $list;
  246. }
  247. public function postList($user_ids, $groups) {
  248. return array();
  249. }
  250. public function groupActions($user_ids, $groups, $posts) {}
  251. }
  252. // Start content generation.
  253. $og_content_config = array();
  254. $og_content_config[] = new ogGroupNoPosts();
  255. $og_content_config[] = new ogGroupThreePosts();
  256. $og_content_config[] = new ogGroupOrphanPost();
  257. $og_content_config[] = new ogGroupPostMultipleGroups();
  258. $og_content_config[] = new ogGroupUserAction();
  259. $og_content_config[] = new ogGroupSelectiveState();
  260. foreach ($og_content_config as $content_config){
  261. $groups = array_map('og_group_node' , $content_config->groupList($user_ids) );
  262. $posts = array_map('og_post_node', $content_config->postList($user_ids, $groups));
  263. $content_config->groupActions($user_ids, $groups, $posts);
  264. }
  265. /**
  266. * Create a group node.
  267. */
  268. function og_group_node($values = array()) {
  269. $node=(object)$values;
  270. $node->type = 'test_group';
  271. node_save($node);
  272. return $node->nid;
  273. }
  274. /**
  275. * Create a group post.
  276. */
  277. function og_post_node($values = array()){
  278. $node=(object)$values;
  279. $node->type = 'test_post_group';
  280. node_save($node);
  281. return $node->nid;
  282. }