Plugin Directory

Changeset 513959


Ignore:
Timestamp:
03/03/2012 03:55:11 PM (14 years ago)
Author:
Albert Bertilsson
Message:

Version 1.1

Location:
database-peek/trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • database-peek/trunk/database-peek.php

    r504375 r513959  
    2727*/
    2828
     29require_once( dirname( __FILE__) . '/integrate.php');
     30require_once( dirname( __FILE__) . '/settings.php');
     31
     32
    2933if (!class_exists("ABDatabasePeek")) {
    3034    class ABDatabasePeek {
    3135
    32         private $footer_msg = 'Database Peek version 1.0 Developed by Albert Bertilsson in 2012';
    33         private $no_access_msg = 'You do not have sufficient permissions to access this page.';
     36        public static $footer_msg = 'Database Peek version 1.1 Developed by Albert Bertilsson in 2012';
     37        public static $no_access_msg = 'You do not have sufficient permissions to access this page.';
    3438           
    3539        private $wp_ms_tables = array('blogs', 'blog_versions', 'registration_log', 'signups',
     
    7983            add_management_page('Database Peek', 'Database Peek', 'manage_options', 'ab-database-peek',
    8084                             array($this,'main_page'));
    81             add_options_page('Database Peek', 'Database Peek', 'manage_options', 'ab-database-peek',
    82                              array($this,'settings_page'));
    83         }
    84 
    85 
    86         function register_mysettings() { // whitelist options
    87             register_setting( $this->option_group, 'ABDatabasePeek-pagesize' );
    8885        }
    8986
     
    161158
    162159       
    163         function show_table_link($table, $row = -1) {
     160        public static function show_table_link($table, $row = -1) {
    164161            $url = admin_url('admin.php?page=ab-database-peek');
    165162
     
    167164           
    168165            if ($row == -1)
    169                 remove_query_arg('row');
     166                $url = remove_query_arg('row', $url);
    170167            else
    171                 add_query_arg('row', $row);
     168                $url = add_query_arg('row', $row, $url);
    172169           
    173170            return $url;
     
    226223       
    227224               
     225        function check_table_parameter() {
     226            if (!isset($_GET['table'])) return false;
     227           
     228            global $wpdb;
     229           
     230            $rows = $wpdb->get_results( "show tables" );
     231            $col = $wpdb->get_col_info('name', 0);
     232            foreach ($rows as $row) {
     233                if ($_GET['table'] == $row->$col) return true;
     234            }
     235           
     236            return false;
     237        }
     238       
     239       
     240        function check_column_parameter($param) {
     241            if (!isset($_GET['table'])) return false;
     242            if (!isset($_GET[$param])) return false;
     243           
     244            global $wpdb;
     245           
     246            $table = $_GET['table'];
     247            $rows = $wpdb->get_results( "select * from $table where 1 = 0" );
     248            $col_names = $wpdb->get_col_info('name');
     249            foreach ($col_names as $n) {
     250                if ($_GET[$param] == $n) return true;
     251            }
     252           
     253            return false;
     254        }
     255       
     256       
    228257        function main_page() {
    229258           
    230259            //must check that the user has the required capability
    231260            if (!current_user_can('manage_options')) {
    232                 wp_die( __($this->no_access_msg) );
    233             }
     261                wp_die( __(ABDatabasePeek::$no_access_msg) );
     262            }
     263           
     264            $table = false;
     265            if ($this->check_table_parameter()) $table = true;
    234266           
    235267            global $wpdb;
     
    240272            <br>
    241273            <?php
    242             if (isset($_GET['table']) && isset($_GET['row'])) {
     274            if ($table && isset($_GET['row'])) {
    243275                $this->show_row($_GET['table'], $_GET['row']);
    244276            }
    245277            else {
    246                 if (isset($_GET['table'])) {
     278                if ($table) {
    247279                    $this->display_table($_GET['table']);
    248280                }
     
    254286            <br>
    255287            <div class="info">
    256             <?php _e($this->footer_msg); ?>
     288            <?php _e(ABDatabasePeek::$footer_msg); ?>
    257289            </div>
    258290           
     
    260292            <?php
    261293        }
    262        
    263        
    264        
    265         function settings_page() {
    266 
    267             //must check that the user has the required capability
    268             if (!current_user_can('manage_options')) {
    269                 wp_die( __($this->no_access_msg) );
    270             }
    271 
    272             if (isset($_GET['settings-updated'])) {
    273                 ?>
    274                 <div class="updated"><p><strong>
    275                 <?php _e('Settings saved.' ); ?>
    276                 </strong></p></div><?php
    277             }
    278            
    279             ?>
    280             <div class="wrap">
    281             <div id="icon-options-general" class="icon32"></div>
    282             <h2>Database Peek Settings</h2>
    283             <form method="POST" action="options.php">
    284                 <?php
    285                 settings_fields( $this->option_group );
    286                 do_settings_sections( $this->option_group );
    287                 ?>
    288                 <table class="form-table"><tbody>
    289                     <tr valign="top">
    290                         <th scope="row">
    291                             <label for="pagesize">Rows per page</label>
    292                         </th>
    293                         <td>
    294                             <input type="text" id="ABDatabasePeek-pagesize" maxlength="5" size="10" name="ABDatabasePeek-pagesize"
    295                                 value="<?php echo $this->get_numeric_option('ABDatabasePeek-pagesize', 1, 100); ?>" class="regular-text" />
    296                             <span class="description">For table display (default = 100)</span>
    297                         </td>
    298                     </tr>
    299                 </tbody></table>
    300                 <p class="submit">
    301                     <input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes">
    302                 </p>
    303             </form>
    304             <div class="info">
    305             <?php _e($this->footer_msg); ?>
    306             </div>
    307             </div>
    308             <?php
    309         }
    310        
     294   
    311295       
    312296       
     
    472456       
    473457       
    474         function get_numeric_option($name, $min, $default) {
    475             $value = get_option($name);
    476             if (empty($value)) $value = $default;
    477 
    478             if (is_numeric($value))
    479                 $value = (int)$value;
    480            
    481             if ($value < $min) $value = $min;
    482            
    483             return $value;
    484         }
    485        
    486        
    487458        function display_table($table) {
    488459
     
    493464            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
    494465
    495             $limit = $this->get_numeric_option('ABDatabasePeek-pagesize', 1, 100);
     466            $limit = ABDatabasePeekSettings::option_pagesize();
    496467           
    497468            $offset = ( $pagenum - 1 ) * $limit;
     
    504475            </h3>
    505476
    506             <?php
    507             $total = $wpdb->get_var( $wpdb->prepare( "select count(*) from $table" ) );
     477            <form method="get" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
     478                <?php
     479                foreach ($_GET as $k => $v) {
     480                    if ($k != 'filter' && $k != 'match')
     481                        echo "<input type=\"hidden\" name=\"$k\" value=\"$v\">";
     482                }
     483                ?>
     484
     485                <select name="filter">
     486                <?php
     487                $rows = $wpdb->get_results( "select * from $table where 1 = 0" );
     488                $col_names = $wpdb->get_col_info('name');
     489                foreach ($col_names as $n) {
     490                    $selected = '';
     491                    if (isset($_GET['filter']) && $_GET['filter'] == $n) $selected = 'selected';
     492                    echo "<option value=\"$n\" $selected>$n</option>";
     493                }
     494                ?>
     495                </select>
     496                <input type="text" name="match" maxlength="20" size="10" class="regular-text" value="<?php if( isset($_GET['match']) ) echo $_GET['match']; ?>">
     497                <input type="submit" class="button-primary" value="Filter">
     498            </form>
     499
     500            <?php
     501            $filter = '';
     502            $match = '';
     503            if (isset($_GET['filter']) && $this->check_column_parameter('filter') && isset($_GET['match'])) {
     504                //$filter = 'where ' . $_GET['filter'] . ' like "%%' . $_GET['match'] . '%%"';
     505                $filter = 'where ' . $_GET['filter'] . ' like %s';
     506                $match = '%' . $_GET['match'] . '%';
     507            }
     508           
     509            $total = $wpdb->get_var( $wpdb->prepare( "select count(*) from $table $filter", $match ) );
     510            $tabletotal = $wpdb->get_var( "select count(*) from $table" );
    508511            $num_of_pages = ceil( $total / $limit );
    509512            $page_links = paginate_links( array(
     
    517520             
    518521            if ( $page_links ) {
    519                 echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
    520             }
    521 
    522             $rows = $wpdb->get_results( "select * from $table limit $offset, $limit" );
     522                echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . ' ' . $total . ' / ' . $tabletotal . '</div></div>';
     523            } else {
     524                echo '<br>';
     525            }
     526
     527            $orderby = '';
     528            if (isset( $_GET['sort'] ) && $this->check_column_parameter('sort')) $orderby = 'order by ' . $_GET['sort'];
     529            if (isset( $_GET['desc'] ) ) $orderby .= ' desc';
     530           
     531            $rows = $wpdb->get_results( $wpdb->prepare( "select * from $table $filter $orderby limit $offset, $limit", $match ) );
    523532   
    524533            ?>
     
    531540            foreach ($col_names as $n) {
    532541                echo '<th>';
    533                
     542
     543                $sort = add_query_arg('sort', $n, $_SERVER["REQUEST_URI"]);
     544                if (isset( $_GET['sort'] ) && $_GET['sort'] == $n) {
     545                    if (isset($_GET['desc']))
     546                        $sort = remove_query_arg('desc', $sort);
     547                    else
     548                        $sort = add_query_arg('desc', '', $sort);
     549                }
     550                else
     551                    $sort = remove_query_arg('desc', $sort);
     552
     553                echo "<a href=\"$sort\">";
     554                if (isset( $_GET['sort'] ) && $_GET['sort'] == $n) echo '<strong>';
    534555                if (count($col_names) > 5)
    535556                    for ($i = 0 ; $i < strlen($n) ; $i++)
     
    538559                    echo $n;
    539560                   
     561                if (isset( $_GET['sort'] ) && $_GET['sort'] == $n) echo '</strong>';
     562                echo '</a>';
    540563                echo '</th>';
    541564            }
     
    566589           
    567590            if ( $page_links ) {
    568                 echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
    569             }
    570 
    571         }
    572 
    573        
    574        
     591                echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . ' ' . $total . ' / ' . $tabletotal . '</div></div>';
     592            }
     593
     594        }
     595
     596
    575597        function list_tables() {
    576598            global $wpdb;
     
    578600            $rows = $wpdb->get_results( "show tables" );
    579601            $col = $wpdb->get_col_info('name', 0);
     602            $trow = 0;
    580603   
    581604            ?>
     
    611634                   
    612635                        $count++;
     636                        $trow += $rowcount;
    613637                    }
    614638                }
     
    635659               
    636660                    $count++;
     661                    $trow += $rowcount;
    637662                }
    638663            }
     
    657682                   
    658683                        $count++;
     684                        $trow += $rowcount;
    659685                    }
    660686                }
    661687            }
    662688            ?>
    663             </tbody></table>
     689            </tbody>
     690            <tfoot><tr>
     691            <th><?php echo $count; ?></th><th></th><th><?php echo $trow; ?></th>
     692            </tr></tfoot>
     693            </table>
    664694            <?php
    665695        }
     
    669699
    670700
     701
    671702$ab_database_peek = new ABDatabasePeek();
    672703
    673704if (isset($ab_database_peek)) {
    674     // create the menu
    675705    if (is_admin()) {
    676706        add_action('admin_menu', array($ab_database_peek,'create_menus'));
    677        
    678         add_action( 'admin_init', array($ab_database_peek,'register_mysettings') );
    679707    }
    680708}
    681 ?>
     709
     710
     711
     712$ab_database_peek_settings = new ABDatabasePeekSettings();
     713
     714if (isset($ab_database_peek_settings)) {
     715    if (is_admin()) {
     716        add_action('admin_menu', array($ab_database_peek_settings,'create_menus'));
     717        add_action( 'admin_init', array($ab_database_peek_settings,'register_mysettings') );
     718    }
     719}
     720
     721
     722
     723$ab_database_peek_integrate = new ABDatabasePeekIntegrate();
     724
     725if (isset($ab_database_peek_integrate)) {
     726    if (is_admin()) {
     727        $ab_database_peek_integrate->integrate();
     728    }
     729}
  • database-peek/trunk/readme.txt

    r504351 r513959  
    44Requires at least: 3.0.1
    55Tested up to: 3.3.1
    6 Stable tag: 1.0
     6Stable tag: 1.1
    77
    88Database Peek allows admins to view the database content in a user friendly and safe way.
     
    1010== Description ==
    1111
    12 For those with a need to look at the pure contents of the WordPress database. The plugin gives admins a user friendly and safe way to display the tables of the database as well as show the detailed information of the tables and view each row of data including rows in other tables referring to the displayed row.
     12For those with a need to look at the pure contents of the WordPress database. The plugin gives admins a user friendly and safe way to display the tables of the database as well as show the detailed information of the tables and view each row of data including rows in other tables referring to the displayed row. Functionallity for filtering and sorting makes it very easy to get a good overview and find specific data. Integration with other parts of the administration tool makes it quick and easy to examine various entities in the database.
    1313
    1414This is not a replacement to having access to the WordPress database but it provides several benefits:
     
    3232== Changelog ==
    3333
     34= 1.1 =
     35* Fixed bug in the function that created links between tables.
     36* Code clean up, separate file for options.
     37* Added settings option to add shortcut links on edit page for pages and posts.
     38* Added shortcut links on listing pages for pages, posts, tags, comments and links.
     39* Added sums of total number of tables and rows in the footer of the table list.
     40* Added clickable column names to sort the display order.
     41* Added option to filter results by matching a value in a selected column.
     42
    3443= 1.0 =
    3544* Initial Release
Note: See TracChangeset for help on using the changeset viewer.