Revision [10116]

This is an old revision of DbInfoAction made by JavaWoman on 2005-07-18 14:23:38.

 

DbInfo Action

Now implemented as a WikkaBetaFeatures beta feature on this server.

See also:
Documentation:
  • DbInfoActionInfo

Supporting code:
This is the development page for the DbInfo action.
 

What


Just a little action that provides Admins an easy way to inspect the structure of the tables Wikka is using. (It's not a data browser!)


Why


Admins, and especially admins that like to tweak Wikka and create their own extensions, frequently need to check what exactly the database structure is that Wikka is using. Column names and sizes, indexes, and such are importantthings to know when creating extensions.

Of course it's possible to use an external client like PhpMyAdmin or a desktop client but for a quick lookup it's handy to have that information available right inside Wikka.


How


Although the PHP implementation of MySQL provides a few shortcuts with special functions, these don't support all information that we need. However, the MySQL SHOW command can reveal all we need to know, and it can be used with the PHP PHP:mysql_query function.

Limitations


Since revealing database structure is a potential security risk, only adminstrators will be able to see what this little action has on offer.

Apart from that, it makes use of the configured Wikka database user; what this user can see, really depends on what permissions it has been given prior to installation of Wikka, so your mileage may vary.

The Code


Save as actions/dbinfo.php:
  1. <?php
  2. /**
  3.  * Displays definition data (DDL) for the database and tables Wikka uses.
  4.  *
  5.  * Features:
  6.  *  By default shows creation DDL for the database Wikka uses, and a drill-down form to show
  7.  *  creation DDL for each of the wikka tables.
  8.  *  By specifying all='1' possibly more databases become visible (depending on the permissions of the Wikka database user);
  9.  *  if multiple databases are visible, a selection form is shown to pick a database.
  10.  *  By specifying prefix='0' the prefix configured for Wikka is ignored, allowing other tables in the same database (if any)
  11.  *  to be inspected.
  12.  *
  13.  * Syntax:
  14.  *  {{dbinfo [all="0|1"] [prefix="0|1"]}}
  15.  *
  16.  * @package     Actions
  17.  * @subpackage  DatabaseAdmin
  18.  * @name        DBinfo
  19.  *
  20.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
  21.  * @copyright   Copyright © 2005, Marjolein Katsma
  22.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  23.  * @since       Wikka 1.1.6.x
  24.  * @version     0.3
  25.  *
  26.  * @input       string  $all        optional: 0|1; default: 0
  27.  *                                  - 0: show only the database Wikka's tables are in (if visible)
  28.  *                                  - 1: show all (visible) databases
  29.  * @input       integer $prefix     optional: 0|1; default: 1
  30.  *                                  - 0: all tables regardless of prefix
  31.  *                                  - 1: show only tables with Wikka-configured name prefix
  32.  *
  33.  * @output      string  drill-down forms to show databases, tables and creation DDL for them
  34.  *
  35.  * @uses    IsAdmin()
  36.  * @uses    FormOpen()
  37.  * @uses    FormClose()
  38.  * @uses    Format()
  39.  * @uses    MakeId()
  40.  */
  41.  
  42. // escape & placeholder: action allowed only once per page
  43. if (defined('HD_DBINFO'))
  44. {
  45.     echo '{{dbinfo}}';
  46.     return;
  47. }
  48.  
  49. // ----------------- constants and variables ------------------
  50.  
  51. // constants
  52.  
  53. // set defaults
  54. $bAll       = FALSE;        # one column for columnar layout
  55. $bPrefix    = TRUE;         # default display type
  56.  
  57. // UI strings
  58. define('HD_DBINFO','Database Information');
  59. define('HD_DBINFO_DB','Database');
  60. define('HD_DBINFO_TABLES','Tables');
  61. define('HD_DB_CREATE_DDL','DDL to create database %s:');                # %s will hold database name
  62. define('HD_TABLE_CREATE_DDL','DDL to create table %s:');                # %s will hold table name
  63. define('TXT_INFO_1','This utility provides some information about the database(s) and tables in your system.');
  64. define('TXT_INFO_2',' Depending on permissions for the Wikka database user, not all databases or tables may be visible.');
  65. define('TXT_INFO_3',' Where creation DDL is given, this reflects everything that would be needed to exactly recreate the same database and table definitions,');
  66. define('TXT_INFO_4',' including defaults that may not have been specified explicitly.');
  67. define('FORM_SELDB_LEGEND','Databases');
  68. define('FORM_SELTABLE_LEGEND','Tables');
  69. define('FORM_SELDB_OPT_LABEL','Select a database:');
  70. define('FORM_SELTABLE_OPT_LABEL','Select a table:');
  71. define('FORM_SUBMIT_SELDB','Select');
  72. define('FORM_SUBMIT_SELTABLE','Select');
  73. define('MSG_ONLY_ADMIN','Sorry, only administrators can view database information');
  74. define('MSG_SINGLE_DB',"Information for the '%s' database.");           # %s will hold database name
  75. define('MSG_NO_TABLES',"No tables found in the '%s' database.");        # %s will hold database name
  76. define('MSG_NO_DB_DDL','Creation DDL for %s could not be retrieved.');  # %s will hold database name
  77. define('MSG_NO_TABLE_DDL','Creation DDL for %s could not be retrieved.');# %s will hold table name
  78.  
  79. $hdDbInfo       = HD_DBINFO;
  80. $hdDatabase     = HD_DBINFO_DB;
  81. $hdTables       = HD_DBINFO_TABLES;
  82. $txtActionInfo  = TXT_INFO_1.TXT_INFO_2.TXT_INFO_3.TXT_INFO_4;
  83. $msgOnlyAdmin   = MSG_ONLY_ADMIN;
  84.  
  85. // variables
  86.  
  87. $isAdmin    = $this->IsAdmin();
  88. $database   = $this->config['mysql_database'];
  89. $prefix     = $this->config['table_prefix'];
  90.  
  91. // ---------------------- processsing --------------------------
  92.  
  93. // --------------- get parameters ----------------
  94.  
  95. if ($isAdmin)
  96. {
  97.     if (is_array($vars))
  98.     {
  99.         foreach ($vars as $param => $value)
  100.         {
  101.             switch ($param)
  102.             {
  103.                 case 'all':
  104.                     if ($value == 1) $bAll = TRUE;
  105.                     break;
  106.                 case 'prefix':
  107.                     if ($value == 0) $bPrefix = FALSE;
  108.                     break;
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114. // ------------------ get data -------------------
  115.  
  116. if ($isAdmin)
  117. {
  118.     // list of databases to choose from
  119.     $aDbList = array();
  120.     if ($bAll)
  121.     {
  122.         $query = 'SHOW DATABASES';
  123.         $tableresult = mysql_query($query);
  124.         if ($tableresult)
  125.         {
  126.             while ($row = mysql_fetch_assoc($tableresult))
  127.             {
  128.                 $aDbList[] = $row['Database'];
  129.             }
  130.         }
  131.         else                                            # catch-all if no databases are / can be shown
  132.         {
  133.             $aDbList[] = $database;
  134.         }
  135.     }
  136.     else
  137.     {
  138.         $aDbList[] = $database;
  139.     }
  140.  
  141.     // data for selected database
  142.     if ($bAll)
  143.     {
  144.         if (isset($_POST['dbselect']) || isset($_POST['tableselect']))              # form submitted
  145.         {
  146.             if (isset($_POST['seldb']) && in_array($_POST['seldb'],$aDbList))       # valid choice
  147.             {
  148.                 $seldb = $_POST['seldb'];
  149.             }
  150.             else                                        # ignore invalid choice
  151.             {
  152.                 $seldb = $database;
  153.             }
  154.         }
  155.     }
  156.     else
  157.     {
  158.         $seldb = $database;                             # no choice: wikka database
  159.     }
  160.     if (isset($seldb))
  161.     {
  162.         $query = 'SHOW CREATE DATABASE '.$seldb;
  163.         $dbcreateresult = mysql_query($query);
  164.         if ($dbcreateresult)
  165.         {
  166.             $row = mysql_fetch_assoc($dbcreateresult);
  167.             $dbcreate = $row['Create Database'];
  168.         }
  169.     }
  170.  
  171.     // table list
  172.     $aTableList = array();
  173.     if (isset($seldb))
  174.     {
  175.         $query = 'SHOW TABLES FROM '.$seldb;
  176.         if ($bPrefix)
  177.         {
  178.             $pattern = $prefix.'%';
  179.             $query .= " LIKE '".$pattern."'";
  180.         }
  181.         $tablelistresult = mysql_query($query);
  182.         if ($tablelistresult)
  183.         {
  184.             $colname = 'Tables_in_'.$seldb;
  185.             if ($bPrefix)
  186.             {
  187.                 $colname .= ' ('.$pattern.')';
  188.             }
  189.             while ($row = mysql_fetch_assoc($tablelistresult))
  190.             {
  191.                 $aTableList[] = $row[$colname];
  192.             }
  193.         }
  194.     }
  195.  
  196.     // data for selected table
  197.     if (isset($_POST['tableselect']))                   # form submitted
  198.     {
  199.         if (isset($_POST['seltable']) && in_array($_POST['seltable'],$aTableList))  # valid choice
  200.         {
  201.             $seltable = $_POST['seltable'];
  202.             $query = 'SHOW CREATE TABLE '.$seltable;
  203.             $tablecreateresult = mysql_query($query);
  204.             if ($tablecreateresult)
  205.             {
  206.                 $row = mysql_fetch_assoc($tablecreateresult);
  207.                 $tablecreate = $row['Create Table'];
  208.             }
  209.         }
  210.     }
  211. }
  212.  
  213. // ---------------- build forms ------------------
  214.  
  215. if ($isAdmin)
  216. {
  217.     // build datatabase selection form if more than one database to show
  218.     if (count($aDbList) > 1)
  219.     {
  220.         $dbselform  = $this->FormOpen('','','POST','dbsel');
  221.         $dbselform .= '<fieldset>'."\n";
  222.         $dbselform .= ' <legend>'.FORM_SELDB_LEGEND.'</legend>'."\n";
  223.         $dbselform .= ' <label for="seldb" class="mainlabel">'.FORM_SELDB_OPT_LABEL.'</label> '."\n";
  224.         $dbselform .= ' <select name="seldb" id="seldb">'."\n";
  225.         foreach ($aDbList as $db)
  226.         {
  227.             if (isset($seldb))
  228.             {
  229.                 $dbselform .= '     <option value="'.$db.'"'.(($seldb == $db)? ' selected="selected"' : '').'>'.$db.'</option>'."\n";
  230.             }
  231.             else
  232.             {
  233.                 $dbselform .= '     <option value="'.$db.'">'.$db.'</option>'."\n";
  234.             }
  235.         }
  236.         $dbselform .= " </select>\n";
  237.         $dbselform .= ' <input type="submit" name="dbselect" "value="'.FORM_SUBMIT_SELDB.'">'."\n";
  238.         $dbselform .= "</fieldset>\n";
  239.         $dbselform .= $this->FormClose();
  240.     }
  241.     else
  242.     {
  243.         $dbselmsg = '<p>'.sprintf(MSG_SINGLE_DB,$aDbList[0])."</p>\n";
  244.     }
  245.  
  246.     // build table selection form
  247.     if (isset($seldb))
  248.     {
  249.         if (count($aTableList) > 0)
  250.         {
  251.             $tableselform  = $this->FormOpen('','','POST','tablesel');
  252.             $tableselform .= '<fieldset class="hidden">'."\n";
  253.             $tableselform .= '  <input type="hidden" name="seldb" "value="'.$seldb.'">'."\n";
  254.             $tableselform .= '</fieldset>'."\n";
  255.             $tableselform .= '<fieldset>'."\n";
  256.             $tableselform .= '  <legend>'.FORM_SELTABLE_LEGEND.'</legend>'."\n";
  257.             $tableselform .= '  <label for="seltable" class="mainlabel">'.FORM_SELTABLE_OPT_LABEL.'</label> '."\n";
  258.             $tableselform .= '  <select name="seltable" id="seltable">'."\n";
  259.             foreach ($aTableList as $table)
  260.             {
  261.                 if (isset($seltable))
  262.                 {
  263.                     $tableselform .= '      <option value="'.$table.'"'.(($seltable == $table)? ' selected="selected"' : '').'>'.$table.'</option>'."\n";
  264.                 }
  265.                 else
  266.                 {
  267.                     $tableselform .= '      <option value="'.$table.'">'.$table.'</option>'."\n";
  268.                 }
  269.             }
  270.             $tableselform .= "  </select>\n";
  271.             $tableselform .= '  <input type="submit" name="tableselect" "value="'.FORM_SUBMIT_SELTABLE.'">'."\n";
  272.             $tableselform .= "</fieldset>\n";
  273.             $tableselform .= $this->FormClose();
  274.         }
  275.         else
  276.         {
  277.             $tableselmsg = '<p>'.sprintf(MSG_NO_TABLES,$seldb)."</p>\n";
  278.         }
  279.     }
  280.  
  281.     // build results
  282.     if (isset($seldb))
  283.     {
  284.         $hdDbDdl = sprintf(HD_DB_CREATE_DDL,$seldb);
  285.         if (isset($dbcreate))
  286.         {
  287.             $dbresult = $this->Format('% %(sql)'.$dbcreate.'% %');
  288.         }
  289.         else
  290.         {
  291.             $dbresult = '<p>'.sprintf(MSG_NO_DB_DDL,$seldb).'</p>';
  292.         }
  293.         if (isset($seltable))
  294.         {
  295.             $hdTableDdl = sprintf(HD_TABLE_CREATE_DDL,$seltable);
  296.             if (isset($tablecreate))
  297.             {
  298.                 $tableresult = $this->Format('% %(sql)'.$tablecreate.'% %');
  299.             }
  300.             else
  301.             {
  302.                 $tableresult = '<p>'.sprintf(MSG_NO_TABLE_DDL,$seltable).'</p>';
  303.             }
  304.         }
  305.     }
  306.     // ids - use constant for variable-content heading
  307.     $idDbInfo   = $this->makeId('div','dbinfo');
  308.     $idDbDdl    = $this->makeId('hn','ddl_for_database');
  309.     $idTableDdl = $this->makeId('hn','ddl_for_table');
  310. }
  311.  
  312.  
  313. // ------------ show data and forms --------------
  314.  
  315. echo '<div id="'.$idDbInfo.'">'."\n";
  316. echo '<h3>'.$hdDbInfo.'</h3>'."\n";
  317. if ($isAdmin)
  318. {
  319.     echo '<p>'.$txtActionInfo."</p>\n";
  320.     echo '<h4>'.$hdDatabase.'</h4>'."\n";
  321.     if (isset($dbselform))
  322.     {
  323.         echo $dbselform;
  324.     }
  325.     elseif (isset($dbselmsg))
  326.     {
  327.         echo $dbselmsg;
  328.     }
  329.     if (isset($seldb))
  330.     {
  331.         echo "<br />\n";
  332.         echo '<h5 id="'.$idDbDdl.'">'.$hdDbDdl.'</h5>'."\n";
  333.         echo $dbresult;
  334.  
  335.         echo "<br />\n";
  336.         echo '<h4>'.$hdTables.'</h4>'."\n";
  337.         if (isset($tableselform))
  338.         {
  339.             echo $tableselform;
  340.         }
  341.         elseif (isset($tableselmsg))
  342.         {
  343.             echo $tableselmsg;
  344.         }
  345.         if (isset($seltable))
  346.         {
  347.             echo "<br />\n";
  348.             echo '<h5 id="'.$idTableDdl.'">'.$hdTableDdl.'</h5>'."\n";
  349.             echo $tableresult;
  350.         }
  351.     }
  352.     echo "</div>\n";
  353. }
  354. else
  355. {
  356.     echo '<p>'.$msgOnlyAdmin."</p>\n";
  357. }
  358. ?>


Make sure to replace all occurrences of '% %' with '%%'!


Supporting methods

As can be seen (and is documented in the docblock) the new DbInfo() action uses two methods that are themselves beta features - both of them documented already: AdvancedFormOpen FormOpen() (in its beta version!) and GenerateUniqueId makeId(). See the documentation pages for for each for necessary supporting code for the DbInfo action!


Styling


The forms the action display are accessible. Like those for the AdvancedReferrersHandler, they need corresponding styling. In fact the styling is the same, but since these are stillbeta features they have not been turned into a general "forms" styling yet (as will eventually have to happen to give all forms a constent look and feel). For now, the solution was to extend the extra stylesheet created for the AdvancedReferrersHandler; it looks as follows now:
 

css/refmenu.css
  1. /*
  2.     This stylesheet is for the referrers and blacklist handlers.
  3.     It will need to be integrated  with the main stylesheet.
  4.  
  5.     JW 2005-07-08 - extended for the dbinfo action and forms.
  6. */
  7.  
  8. h4 {
  9.     margin-top: 0.3em !important;   /* remove !important when integrating into main stylesheet or including it after that */
  10. }
  11.  
  12. .refmenu {
  13.     margin: 0;
  14.     padding: 0;
  15.     margin-top: 1em;
  16. }
  17. .refmenu .menu {
  18.     margin: 0;
  19.     padding: 0;
  20. }
  21. .refmenu .menu li {
  22.     list-style: none;
  23.     float: left;
  24.     margin-right: 3px;              /* margin-right goes together with float left (or vice versa) */
  25.     padding: 1px 2px;
  26.     font-size: 85%;
  27.     line-height: 1.2em;
  28.     color: #000000;
  29.     background-color: #DDDDDD;
  30. }
  31. br.clear {
  32.     clear: both;
  33. }
  34.  
  35. form fieldset.hidden {              /* for all forms! not just referrers/dbinfo */
  36.     display: none;
  37. }
  38.  
  39. #refform, #dbinfo {
  40.     color: inherit;
  41.     background-color: inherit;
  42.     margin-top: 1em;
  43.     margin-bottom: 1em;
  44. }
  45. #refform {
  46.     width: 32em;
  47. }
  48.  
  49. #form_dbsel, #form_tablesel {
  50.     width: 40em;
  51. }
  52.  
  53. #refform fieldset, #form_dbsel fieldset, #form_tablesel fieldset {
  54.     padding: 1em;
  55.     margin-bottom: 0.3em;
  56.     border: 1px solid #666666;
  57. }
  58.  
  59. #refform legend, #form_dbsel legend, #form_tablesel legend  {
  60.     padding: 0 2px;
  61.     color: #000000;
  62.     background-color: #DDDDDD;
  63.     border: 1px solid #666666;
  64.     margin-bottom: 0.3em;
  65. }
  66.  
  67. #refform .mainlabel {
  68.     float: left;
  69.     width: 4.6em;   /* width will work on _floated_ element, even if not a block! */
  70.     padding-right: 0.5em;
  71. }
  72. #form_dbsel .mainlabel, #form_tablesel .mainlabel {
  73.     float: left;
  74.     width: 9.8em;   /* width will work on _floated_ element, even if not a block! */
  75.     padding-right: 0.5em;
  76. }
  77.  
  78. #q, #qo, #ho {
  79.     width: 10em;
  80. }
  81. #h {
  82.     width: 3em;
  83.     text-align: right;
  84. }
  85.  
  86. #reflist {
  87.     margin-top: 1em;
  88.     margin-bottom: 1em;
  89.     border: none;
  90. }
  91. #reflist .hits {
  92.     width: 3em;
  93.     padding-right: 5px;
  94.     text-align: right;
  95.     vertical-align: middle;
  96. }
  97. #reflist .action {
  98.     width: 5em;
  99.     padding-left: 5px;
  100.     padding-right: 5px;
  101.     text-align: center;
  102.     vertical-align: middle;
  103. }
  104. #reflist .refs {
  105.     padding-left: 5px;
  106.     text-align: left;
  107.     vertical-align: middle;
  108. }


The little stylesheet file to import into your own "skin" has been extended accordingly:

css/refmenu_col.css
  1. /*
  2.     For custom stylesheets: copy this into your stylesheet; the
  3.     adapt the colors here (made to match the default Wikka skin)
  4.     to match your own.
  5.  
  6.     JW 2005-07-08 - extended for the dbinfo action and forms.
  7. */
  8.  
  9. .refmenu .menu li {
  10.     color: #000000;
  11.     background-color: #DDDDDD;
  12. }
  13.  
  14. form fieldset.hidden {              /* for all forms! not just referrers/dbinfo */
  15.     display: none;
  16. }
  17. #refform fieldset, #form_dbsel fieldset, #form_tablesel fieldset {
  18.     border: 1px solid #666666;
  19. }
  20. #refform legend, #form_dbsel legend, #form_tablesel legend  {
  21.     color: #000000;
  22.     background-color: #DDDDDD;
  23.     border: 1px solid #666666;
  24. }



Todo


Not a real todo, but it could potentially be extended to prompt for a user/password combination for another database user; that might be handy for Admins taking care of several Wikis installed on the same server. Let us know if you'd be interested in such a feature.


Comments?


As always, comments and suggestions very welcome.



CategoryDevelopmentActions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki