PageRenderTime 1104ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/WikiTrust/sql/create_db.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 154 lines | 99 code | 21 blank | 34 comment | 23 complexity | 833d9795823e4594a4972f00dedda107 MD5 | raw file
  1. #!/usr/bin/php
  2. <?php
  3. # Copyright (c) 2007,2008 Luca de Alfaro
  4. # Copyright (c) 2007,2008 Ian Pye
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of the
  9. # License, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. # USA
  18. # This script created the wikitrust tables needed for an online analysis.
  19. # Usage: ./create_db.php "path to the target MediaWiki installation"
  20. # database_root_user
  21. # [remove]
  22. $mw_root = $argv[1];
  23. $dba = $argv[2];
  24. $dba_pass = "";
  25. // If true, we remove tables. If false, we create them.
  26. $do_remove = ($argc > 2 && $argv[3] == "remove")? true: false;
  27. $db_indexes = array(); // Current indexes of interest.
  28. $db_tables = array(); // Store all of the tables currently present.
  29. $create_scripts = array(); // The actual SQL to create tables. Defined below.
  30. $remove_scripts = array(); // The actual SQL to remove tables. Defined below.
  31. $create_index_scripts = array();
  32. $remove_index_scripts = array();
  33. if(!$mw_root || !is_dir($mw_root) || !isset($dba)
  34. || !is_file($mw_root."/LocalSettings.php")){
  35. print "Usage: ./create_db.php 'path to the target MediaWiki installation' database_root_user [remove]\n";
  36. exit(-1);
  37. }
  38. print ($do_remove)? "Removing tables\n": "Creating tables\n";
  39. print "Do you really want to do this? [Y/n]: ";
  40. $continue = strtoupper(fread(STDIN, 1));
  41. if ($continue != "Y" && $continue != "\n"){
  42. print "Aborting script\n";
  43. exit(0);
  44. }
  45. // Reads the root password from std in, or a file if given
  46. if (array_key_exists(4, $argv) && is_file($argv[4])){
  47. $dba_pass = trim(file_get_contents($argv[4]));
  48. } else {
  49. print "Enter the root mysql password:\n";
  50. $dba_pass = rtrim(shell_exec('
  51. bash -c \'
  52. stty_orig=`stty -g`
  53. trap "stty ${stty_orig}; exit" 1 2 3 15
  54. stty -echo <&- 2<&-
  55. read -s PASS
  56. stty ${stty_orig} <&- 2<&-
  57. trap 1 2 3 15
  58. echo $PASS
  59. \'
  60. '));
  61. }
  62. // Load all of the MW files.
  63. include($mw_root."/maintenance/commandLine.inc");
  64. global $wgDBserver, $wgDBname, $wgDBtype, $wgDBuser, $wgDBprefix, $wgCreateRevisionIndex;
  65. // Source the update scripts
  66. require($mw_root."/extensions/WikiTrust/includes/TrustUpdateScripts.inc");
  67. // If this is set, create an index on the revision table.
  68. if ($wgCreateRevisionIndex){
  69. $db_indexes[$wgDBprefix . 'revision'] = array();
  70. } else {
  71. $db_indexes[$wgDBprefix . 'revision']['revision_id_timestamp_idx'] = True;
  72. }
  73. // Create the needed tables, if neccesary.
  74. $dbr = wfGetDB( DB_SLAVE );
  75. // First check to see what tables have already been created.
  76. $res = $dbr->query("show tables");
  77. while ($row = $dbr->fetchRow($res)){
  78. $db_tables[$row[0]] = True;
  79. }
  80. // And check to see what indexes have already been created.
  81. foreach ($db_indexes as $table => $idx){
  82. $res = $dbr->query("show index from " . $table);
  83. while ($row = $dbr->fetchRow($res)){
  84. $db_indexes[$table][$row[2]] = True;
  85. }
  86. }
  87. // We need root priveledges to do this.
  88. $db_root = DatabaseBase::newFromType( $wgDBtype,
  89. array(
  90. 'host' => $wgDBserver,
  91. 'user' => $dba,
  92. 'password' => $dba_pass,
  93. 'dbname' => $wgDBname
  94. )
  95. );
  96. if (!$do_remove){
  97. // Now do the actual creating of tables.
  98. foreach ($create_scripts as $table => $scripts) {
  99. if (!array_key_exists($table, $db_tables)){
  100. foreach ($scripts as $script){
  101. $db_root->query($script);
  102. }
  103. }
  104. }
  105. // Now do the actual creating of indexes.
  106. foreach ($create_index_scripts as $table => $idxs){
  107. foreach ($idxs as $name => $idx){
  108. if(!array_key_exists($name, $db_indexes[$table])){
  109. $db_root->query($idx);
  110. }
  111. }
  112. }
  113. } else {
  114. // Or removing.
  115. foreach ($remove_scripts as $table => $scripts) {
  116. if (array_key_exists($table, $db_tables)){
  117. foreach ($scripts as $script){
  118. $db_root->query($script);
  119. }
  120. }
  121. }
  122. // ...Of indexes.
  123. foreach ($remove_index_scripts as $table => $idxs){
  124. foreach ($idxs as $name => $idx){
  125. if(array_key_exists($name, $db_indexes[$table])){
  126. $db_root->query($idx);
  127. }
  128. }
  129. }
  130. }
  131. // Finally, we commit any leftovers.
  132. $db_root->query("COMMIT");
  133. print ($do_remove)? "Removed tables\n": "Created tables\n";