PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/docroot/pgadmin/indexes.php

https://github.com/mterenzio/FollowThis
PHP | 383 lines | 299 code | 57 blank | 27 comment | 55 complexity | cdc1b6e3edb797cb5935293eff466454 MD5 | raw file
  1. <?php
  2. /**
  3. * List indexes on a table
  4. *
  5. * $Id: indexes.php,v 1.46 2008/01/08 22:50:29 xzilla Exp $
  6. */
  7. // Include application functions
  8. include_once('./libraries/lib.inc.php');
  9. include_once('./classes/class.select.php');
  10. $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
  11. /**
  12. * Show confirmation of cluster index and perform actual cluster
  13. */
  14. function doClusterIndex($confirm) {
  15. global $data, $misc, $action;
  16. global $lang;
  17. if ($confirm) {
  18. // Default analyze to on
  19. $_REQUEST['analyze'] = true;
  20. $misc->printTrail('index');
  21. $misc->printTitle($lang['strclusterindex'],'pg.index.cluster');
  22. echo "<p>", sprintf($lang['strconfcluster'], $misc->printVal($_REQUEST['index'])), "</p>\n";
  23. echo "<form action=\"indexes.php\" method=\"post\">\n";
  24. echo "<p><input type=\"checkbox\" id=\"analyze\" name=\"analyze\"", (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), " /><label for=\"analyze\">{$lang['stranalyze']}</label></p>\n";
  25. echo "<input type=\"hidden\" name=\"action\" value=\"cluster_index\" />\n";
  26. echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
  27. echo "<input type=\"hidden\" name=\"index\" value=\"", htmlspecialchars($_REQUEST['index']), "\" />\n";
  28. echo $misc->form;
  29. echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />\n";
  30. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
  31. echo "</form>\n";
  32. }
  33. else {
  34. $status = $data->clusterIndex($_POST['table'], $_POST['index']);
  35. if ($status == 0)
  36. if (isset($_POST['analyze'])){
  37. $status = $data->analyzeDB($_POST['table']);
  38. if ($status == 0)
  39. doDefault($lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']);
  40. else
  41. doDefault($lang['stranalyzebad']);
  42. } else
  43. doDefault($lang['strclusteredgood']);
  44. else
  45. doDefault($lang['strclusteredbad']);
  46. }
  47. }
  48. function doReindex() {
  49. global $data, $lang;
  50. $status = $data->reindex('INDEX', $_REQUEST['index']);
  51. if ($status == 0)
  52. doDefault($lang['strreindexgood']);
  53. else
  54. doDefault($lang['strreindexbad']);
  55. }
  56. /**
  57. * Displays a screen where they can enter a new index
  58. */
  59. function doCreateIndex($msg = '') {
  60. global $data, $misc;
  61. global $lang;
  62. if (!isset($_POST['formIndexName'])) $_POST['formIndexName'] = '';
  63. if (!isset($_POST['formIndexType'])) $_POST['formIndexType'] = null;
  64. if (!isset($_POST['formCols'])) $_POST['formCols'] = '';
  65. if (!isset($_POST['formWhere'])) $_POST['formWhere'] = '';
  66. if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
  67. $attrs = $data->getTableAttributes($_REQUEST['table']);
  68. // Fetch all tablespaces from the database
  69. if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
  70. $misc->printTrail('table');
  71. $misc->printTitle($lang['strcreateindex'],'pg.index.create');
  72. $misc->printMsg($msg);
  73. $selColumns = new XHTML_select("TableColumnList",true,10);
  74. $selColumns->set_style("width: 10em;");
  75. if ($attrs->recordCount() > 0) {
  76. while (!$attrs->EOF) {
  77. $selColumns->add(new XHTML_Option($attrs->fields['attname']));
  78. $attrs->moveNext();
  79. }
  80. }
  81. $selIndex = new XHTML_select("IndexColumnList[]", true, 10);
  82. $selIndex->set_style("width: 10em;");
  83. $selIndex->set_attribute("id", "IndexColumnList");
  84. $buttonAdd = new XHTML_Button("add", ">>");
  85. $buttonAdd->set_attribute("onclick", "buttonPressed(this);");
  86. $buttonAdd->set_attribute("type", "button");
  87. $buttonRemove = new XHTML_Button("remove", "<<");
  88. $buttonRemove->set_attribute("onclick", "buttonPressed(this);");
  89. $buttonRemove->set_attribute("type", "button");
  90. echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"indexes.php\" method=\"post\">\n";
  91. echo "<table>\n";
  92. echo "<tr><th class=\"data required\" colspan=\"3\">{$lang['strindexname']}</th></tr>";
  93. echo "<tr>";
  94. echo "<td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"formIndexName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
  95. htmlspecialchars($_POST['formIndexName']), "\" /></td></tr>";
  96. echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\">&nbsp;</th>";
  97. echo "<th class=\"data required\">{$lang['strindexcolumnlist']}</th></tr>\n";
  98. echo "<tr><td class=\"data1\">" . $selColumns->fetch() . "</td>\n";
  99. echo "<td class=\"data1\">" . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>";
  100. echo "<td class=\"data1\">" . $selIndex->fetch() . "</td></tr>\n";
  101. echo "</table>\n";
  102. echo "<table> \n";
  103. echo "<tr>";
  104. echo "<th class=\"data left required\" scope=\"row\">{$lang['strindextype']}</th>";
  105. echo "<td class=\"data1\"><select name=\"formIndexType\">";
  106. foreach ($data->typIndexes as $v) {
  107. echo "<option value=\"", htmlspecialchars($v), "\"",
  108. ($v == $_POST['formIndexType']) ? ' selected="selected"' : '', ">", htmlspecialchars($v), "</option>\n";
  109. }
  110. echo "</select></td></tr>\n";
  111. echo "<tr>";
  112. echo "<th class=\"data left\" scope=\"row\"><label for=\"formUnique\">{$lang['strunique']}</label></th>";
  113. echo "<td class=\"data1\"><input type=\"checkbox\" id=\"formUnique\" name=\"formUnique\"", (isset($_POST['formUnique']) ? 'checked="checked"' : ''), " /></td>";
  114. echo "</tr>";
  115. echo "<tr>";
  116. echo "<th class=\"data left\" scope=\"row\">{$lang['strwhere']}</th>";
  117. echo "<td class=\"data1\">(<input name=\"formWhere\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
  118. htmlspecialchars($_POST['formWhere']), "\" />)</td>";
  119. echo "</tr>";
  120. // Tablespace (if there are any)
  121. if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
  122. echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
  123. echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n";
  124. // Always offer the default (empty) option
  125. echo "\t\t\t\t<option value=\"\"",
  126. ($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n";
  127. // Display all other tablespaces
  128. while (!$tablespaces->EOF) {
  129. $spcname = htmlspecialchars($tablespaces->fields['spcname']);
  130. echo "\t\t\t\t<option value=\"{$spcname}\"",
  131. ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
  132. $tablespaces->moveNext();
  133. }
  134. echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
  135. }
  136. if ($data->hasConcurrentIndexBuild()) {
  137. echo "<tr>";
  138. echo "<th class=\"data left\" scope=\"row\"><label for=\"formConcur\">{$lang['strconcurrently']}</label></th>";
  139. echo "<td class=\"data1\"><input type=\"checkbox\" id=\"formConcur\" name=\"formConcur\"", (isset($_POST['formConcur']) ? 'checked="checked"' : ''), " /></td>";
  140. echo "</tr>";
  141. }
  142. echo "</table>";
  143. echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create_index\" />\n";
  144. echo $misc->form;
  145. echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
  146. echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
  147. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
  148. echo "</form>\n";
  149. }
  150. /**
  151. * Actually creates the new index in the database
  152. * @@ Note: this function can't handle columns with commas in them
  153. */
  154. function doSaveCreateIndex() {
  155. global $data;
  156. global $lang;
  157. // Handle databases that don't have partial indexes
  158. if (!isset($_POST['formWhere'])) $_POST['formWhere'] = '';
  159. // Default tablespace to null if it isn't set
  160. if (!isset($_POST['formSpc'])) $_POST['formSpc'] = null;
  161. // Check that they've given a name and at least one column
  162. if ($_POST['formIndexName'] == '') doCreateIndex($lang['strindexneedsname']);
  163. elseif (!isset($_POST['IndexColumnList']) || $_POST['IndexColumnList'] == '') doCreateIndex($lang['strindexneedscols']);
  164. else {
  165. $status = $data->createIndex($_POST['formIndexName'], $_POST['table'], $_POST['IndexColumnList'],
  166. $_POST['formIndexType'], isset($_POST['formUnique']), $_POST['formWhere'], $_POST['formSpc'],
  167. isset($_POST['formConcur']));
  168. if ($status == 0)
  169. doDefault($lang['strindexcreated']);
  170. else
  171. doCreateIndex($lang['strindexcreatedbad']);
  172. }
  173. }
  174. /**
  175. * Show confirmation of drop index and perform actual drop
  176. */
  177. function doDropIndex($confirm) {
  178. global $data, $misc;
  179. global $lang;
  180. if ($confirm) {
  181. $misc->printTrail('index');
  182. $misc->printTitle($lang['strdrop'],'pg.index.drop');
  183. echo "<p>", sprintf($lang['strconfdropindex'], $misc->printVal($_REQUEST['index'])), "</p>\n";
  184. echo "<form action=\"indexes.php\" method=\"post\">\n";
  185. echo "<input type=\"hidden\" name=\"action\" value=\"drop_index\" />\n";
  186. echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
  187. echo "<input type=\"hidden\" name=\"index\" value=\"", htmlspecialchars($_REQUEST['index']), "\" />\n";
  188. echo $misc->form;
  189. echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
  190. echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
  191. echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
  192. echo "</form>\n";
  193. }
  194. else {
  195. $status = $data->dropIndex($_POST['index'], isset($_POST['cascade']));
  196. if ($status == 0)
  197. doDefault($lang['strindexdropped']);
  198. else
  199. doDefault($lang['strindexdroppedbad']);
  200. }
  201. }
  202. function doDefault($msg = '') {
  203. global $data, $misc;
  204. global $lang;
  205. function indPre(&$rowdata, $actions) {
  206. global $data, $lang;
  207. if ($data->phpBool($rowdata->fields['indisprimary'])) {
  208. $rowdata->fields['+constraints'] = $lang['strprimarykey'];
  209. $actions['drop']['disable'] = true;
  210. }
  211. elseif ($data->phpBool($rowdata->fields['indisunique'])) {
  212. $rowdata->fields['+constraints'] = $lang['struniquekey'];
  213. $actions['drop']['disable'] = true;
  214. }
  215. else
  216. $rowdata->fields['+constraints'] = '';
  217. return $actions;
  218. }
  219. $misc->printTrail('table');
  220. $misc->printTabs('table','indexes');
  221. $misc->printMsg($msg);
  222. $indexes = $data->getIndexes($_REQUEST['table']);
  223. $columns = array(
  224. 'index' => array(
  225. 'title' => $lang['strname'],
  226. 'field' => field('indname'),
  227. ),
  228. 'definition' => array(
  229. 'title' => $lang['strdefinition'],
  230. 'field' => field('inddef'),
  231. ),
  232. 'constraints' => array(
  233. 'title' => $lang['strconstraints'],
  234. 'field' => field('+constraints'),
  235. 'type' => 'verbatim',
  236. 'params'=> array('align' => 'center'),
  237. ),
  238. 'clustered' => array(
  239. 'title' => $lang['strclustered'],
  240. 'field' => field('indisclustered'),
  241. 'type' => 'yesno',
  242. ),
  243. 'actions' => array(
  244. 'title' => $lang['stractions'],
  245. ),
  246. 'comment' => array(
  247. 'title' => $lang['strcomment'],
  248. 'field' => field('idxcomment'),
  249. ),
  250. );
  251. $actions = array(
  252. 'cluster' => array(
  253. 'title' => $lang['strclusterindex'],
  254. 'url' => "indexes.php?action=confirm_cluster_index&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
  255. 'vars' => array('index' => 'indname'),
  256. ),
  257. 'reindex' => array(
  258. 'title' => $lang['strreindex'],
  259. 'url' => "indexes.php?action=reindex&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
  260. 'vars' => array('index' => 'indname'),
  261. ),
  262. 'drop' => array(
  263. 'title' => $lang['strdrop'],
  264. 'url' => "indexes.php?action=confirm_drop_index&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
  265. 'vars' => array('index' => 'indname'),
  266. ),
  267. );
  268. $misc->printTable($indexes, $columns, $actions, $lang['strnoindexes'], 'indPre');
  269. echo "<p><a class=\"navlink\" href=\"indexes.php?action=create_index&amp;{$misc->href}&amp;table=",
  270. urlencode($_REQUEST['table']), "\">{$lang['strcreateindex']}</a></p>\n";
  271. }
  272. function doTree() {
  273. global $misc, $data;
  274. $indexes = $data->getIndexes($_REQUEST['table']);
  275. $reqvars = $misc->getRequestVars('table');
  276. function getIcon($f) {
  277. if ($f['indisprimary'] == 't')
  278. return 'PrimaryKey';
  279. if ($f['indisunique'] == 't')
  280. return 'UniqueConstraint';
  281. return 'Index';
  282. }
  283. $attrs = array(
  284. 'text' => field('indname'),
  285. 'icon' => callback('getIcon'),
  286. );
  287. $misc->printTreeXML($indexes, $attrs);
  288. exit;
  289. }
  290. if ($action == 'tree') doTree();
  291. $misc->printHeader($lang['strindexes'], "<script src=\"indexes.js\" type=\"text/javascript\"></script>");
  292. if ($action == 'create_index' || $action == 'save_create_index')
  293. echo "<body onload=\"init();\">";
  294. else
  295. $misc->printBody();
  296. switch ($action) {
  297. case 'cluster_index':
  298. if (isset($_POST['cluster'])) doClusterIndex(false);
  299. else doDefault();
  300. break;
  301. case 'confirm_cluster_index':
  302. doClusterIndex(true);
  303. break;
  304. case 'reindex':
  305. doReindex();
  306. break;
  307. case 'save_create_index':
  308. if (isset($_POST['cancel'])) doDefault();
  309. else doSaveCreateIndex();
  310. break;
  311. case 'create_index':
  312. doCreateIndex();
  313. break;
  314. case 'drop_index':
  315. if (isset($_POST['drop'])) doDropIndex(false);
  316. else doDefault();
  317. break;
  318. case 'confirm_drop_index':
  319. doDropIndex(true);
  320. break;
  321. default:
  322. doDefault();
  323. break;
  324. }
  325. $misc->printFooter();
  326. ?>