Plugin Directory

Changeset 1517159


Ignore:
Timestamp:
10/18/2016 08:36:45 AM (9 years ago)
Author:
MrWiblog
Message:

1.4 (2016/10/16)
Added UTF decoding, as per suggestion by @darktek (https://wordpress.org/support/topic/exporting-into-csv-character-error/)
Added unit tests for output methods

Location:
database-browser/trunk
Files:
7 added
3 edited

Legend:

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

    r1139718 r1517159  
    66  Description: Easily browse the data in your database, and download in CSV, XML, SQL and JSON format
    77  Author: Chris Taylor
    8   Version: 1.2
     8  Version: 1.4
    99  Author URI: http://www.stillbreathing.co.uk/
    1010 */
    1111
    1212// include the Plugin_Register class
    13 require_once( plugin_dir_path( __FILE__ ) . "plugin-register.class.php" );
    14 // create a new instance of the Plugin_Register class
    15 $register = new Plugin_Register();
    16 $register->file = __FILE__;
    17 $register->slug = "databasebrowser";
    18 $register->name = "Database Browser";
    19 $register->version = "1.2";
    20 $register->developer = "Chris Taylor";
    21 $register->homepage = "http://www.stillbreathing.co.uk";
    22 $register->Plugin_Register();
     13if ( function_exists( 'plugin_dir_path' ) && class_exists( 'Plugin_Register' ) ) {
     14    require_once( plugin_dir_path( __FILE__ ) . "plugin-register.class.php" );
     15    // create a new instance of the Plugin_Register class
     16    $register = new Plugin_Register();
     17    $register->file = __FILE__;
     18    $register->slug = "databasebrowser";
     19    $register->name = "Database Browser";
     20    $register->version = "1.4";
     21    $register->developer = "Chris Taylor";
     22    $register->homepage = "http://www.stillbreathing.co.uk";
     23    $register->Plugin_Register();
     24}
    2325
    2426if ( ! class_exists( 'DatabaseBrowser' ) ) {
     
    3840         * @since 1.0
    3941         */
    40         var $version = "1.2";
     42        var $version = "1.4";
    4143
    4244        /**
     
    4749        var $tables = array();
    4850
     51        /**
     52         * The name of the WordPress database
     53         *
     54         * @since 1.4
     55         */
     56        var $database_name = null;
     57       
    4958        /**
    5059         * The name of the currently selected table; null if no table selected
     
    145154
    146155        /**
    147          * Initialises the plugin
    148          *
    149          * @since 1.0
    150          */
    151         function DatabaseBrowser() {
    152             require_once( plugin_dir_path( __FILE__ ) . "pagination.class.php" );
    153             $this->formURL = remove_query_arg( "p" );
     156         * Run when an instance of this class is constructed
     157         *
     158         * @since 1.0
     159         */
     160        function __construct() {
     161            if ( function_exists( 'plugin_dir_path' ) ) {
     162                require_once( plugin_dir_path( __FILE__ ) . "pagination.class.php" );
     163                $this->formURL = remove_query_arg( "p" );
     164            }
    154165        }
    155166
     
    177188                header( "Location: tools.php?page=databasebrowser&table=" . $_POST["table"] . "&_wpnonce=" . wp_create_nonce( "query" ) );
    178189            }
     190           
     191            // set the database name
     192            $this->database_name = DB_NAME;
    179193
    180194            // load textdomain
     
    647661                case "xml":
    648662                    $this->forceDownload( "xml" );
    649                     echo $this->toXML();
     663                    echo $this->toXML( $this->database_name, $this->table, $this->columns, $this->rows );
    650664                    die();
    651665                    break;
     
    653667                case "html":
    654668                    $this->forceDownload( "html" );
    655                     echo $this->toHTML();
     669                    echo $this->toHTML( $this->database_name, $this->table, $this->columns, $this->rows );
    656670                    die();
    657671                    break;
     
    659673                case "csv":
    660674                    $this->forceDownload( "csv" );
    661                     echo $this->toCSV();
     675                    echo $this->toCSV( $this->database_name, $this->table, $this->columns, $this->rows );
    662676                    die();
    663677                    break;
     
    665679                case "sql":
    666680                    $this->forceDownload( "sql" );
    667                     echo $this->toSQL();
     681                    echo $this->toSQL( $this->database_name, $this->table, $this->columns, $this->rows );
    668682                    die();
    669683                    break;
     
    671685                case "json":
    672686                    $this->forceDownload( "json" );
    673                     echo $this->toJSON();
     687                    echo $this->toJSON( $this->database_name, $this->table, $this->columns, $this->rows );
    674688                    die();
    675689                    break;
     
    697711
    698712        /**
    699          * Outputs the current data as a JSON document
    700          *
    701          * @since 1.0
    702          */
    703         function toJson() {
     713         * Outputs the given data as a JSON document
     714         *
     715         * @since 1.0
     716         */
     717        function toJson( $database_name, $table, $columns, $rows ) {
    704718            $output = '{
    705     "database": "' . DB_NAME . '",
    706     "table": "' . $this->table . '",
     719    "database": "' . $database_name . '",
     720    "table": "' . $table . '",
    707721    "columns":
    708722    {';
    709             foreach ( $this->columns as $column ) {
     723            foreach ( $columns as $column ) {
    710724                $output .= '
    711725        "column":
     
    730744    "rows":
    731745    {';
    732             foreach ( $this->rows as $row ) {
     746            foreach ( $rows as $row ) {
    733747                $line = '
    734748        "row":
     
    736750        ';
    737751                $col = '';
    738                 foreach ( $this->columns as $column ) {
     752                foreach ( $columns as $column ) {
    739753                    $col .= '
    740             "' . $column->Field . '": "' . $row[$column->Field] . '",';
     754            "' . $column->Field . '": "' . utf8_decode( $row[$column->Field] ) . '",';
    741755                }
    742756                $line .= trim( trim( $col ), "," );
     
    753767
    754768        /**
    755          * Outputs the current data as a CSV document
    756          *
    757          * @since 1.0
    758          */
    759         function toCSV() {
     769         * Outputs the given data as a CSV document
     770         *
     771         * @since 1.0
     772         */
     773        function toCSV( $database_name, $table, $columns, $rows ) {
    760774            $output = "";
    761             foreach ( $this->columns as $column ) {
     775            foreach ( $columns as $column ) {
    762776                $output .= $column->Field . ",";
    763777            }
    764778            $output = trim( $output, "," );
    765779            $output .= "\r\n";
    766             foreach ( $this->rows as $row ) {
     780            foreach ( $rows as $row ) {
    767781                $line = "";
    768                 foreach ( $this->columns as $column ) {
    769                     $line .= '"' . str_replace( '"', '""', $row[$column->Field] ) . '",';
     782                foreach ( $columns as $column ) {
     783                    $line .= '"' . str_replace( '"', '""', utf8_decode( $row[$column->Field] ) ) . '",';
    770784                }
    771785                $line = trim( $line, "," );
     
    778792
    779793        /**
    780          * Outputs the current data as SQL INSERT statements
    781          *
    782          * @since 1.2
    783          */
    784         function toSQL() {
     794         * Outputs the given data as SQL INSERT statements
     795         *
     796         * @since 1.2
     797         */
     798        function toSQL( $database_name, $table, $columns, $rows ) {
    785799            $output = "";
    786             foreach ( $this->rows as $row ) {
    787                 $cols = "INSERT INTO " . $this->table . " (";
    788                 foreach ( $this->columns as $column ) {
     800            foreach ( $rows as $row ) {
     801                $cols = "INSERT INTO " . $table . " (";
     802                foreach ( $columns as $column ) {
    789803                    // for custom queries we don't know the column types
    790804                    if ( property_exists( $column, "Type" ) ) {
     
    799813                $cols .= ") VALUES (";
    800814                $data = "";
    801                 foreach ( $this->columns as $column ) {
     815                foreach ( $columns as $column ) {
    802816                    // for custom queries we don't know the column types
    803817                    if ( property_exists( $column, "Type" ) ) {
     
    857871         * @since 1.0
    858872         */
    859         function toHTML( $class = "", $truncateAtCharacter = -1 ) {
     873        function toHTML( $database_name, $table, $columns, $rows, $class = "", $truncateAtCharacter = -1 ) {
    860874            $output = '
    861875<table class="' . $class . '">
    862876    <thead>
    863877        <tr>';
    864             foreach ( $this->columns as $column ) {
     878            foreach ( $columns as $column ) {
    865879                $output .= "
    866880            <th>" . $column->Field . "</th>";
     
    870884    </thead>
    871885    <tbody>";
    872             foreach ( $this->rows as $row ) {
     886            foreach ( $rows as $row ) {
    873887                $line = "
    874888        <tr>";
    875                 foreach ( $this->columns as $column ) {
     889                foreach ( $columns as $column ) {
    876890
    877891                    // if we are displaying all the content of every field
     
    879893
    880894                        $line .= '
    881             <td>' . $row[$column->Field] . '</td>';
     895            <td>' . utf8_decode( $row[$column->Field] ) . '</td>';
    882896
    883897                    // we need to truncate long field values at the given character
     
    890904                        }
    891905                        $line .= '
    892             <td>' . $value . '</td>';
     906            <td>' . utf8_decode( $value ) . '</td>';
    893907
    894908                    }
     
    910924         * @since 1.0
    911925         */
    912         function toXML() {
     926        function toXML( $database_name, $table, $columns, $rows ) {
    913927            $output = '<?xml version="1.0" encoding="ISO-8859-1" ?>
    914 <database name="' . DB_NAME . '">
    915     <table name="' . $this->table . '">
     928<database name="' . $database_name . '">
     929    <table name="' . $table . '">
    916930        <columns>';
    917             foreach ( $this->columns as $column ) {
     931            foreach ( $columns as $column ) {
    918932                // for custom queries we don't know the column types
    919933                if ( property_exists( $column, "Type" ) ) {
     
    928942        </columns>
    929943        <rows>';
    930             foreach ( $this->rows as $row ) {
     944            foreach ( $rows as $row ) {
    931945                $line = '
    932946            <row>';
    933                 foreach ( $this->columns as $column ) {
     947                foreach ( $columns as $column ) {
    934948                    $line .= '
    935949                <' . $column->Field . '>' . $row[$column->Field] . '</' . $column->Field . '>';
     
    977991 * @since 1.0
    978992 */
    979 if ( class_exists( 'DatabaseBrowser' ) ) {
     993if ( class_exists( 'DatabaseBrowser' ) && function_exists( 'add_action' ) ) {
    980994    $databasebrowser = new DatabaseBrowser();
    981995    add_action( 'admin_menu', array( &$databasebrowser, 'on_admin_menu' ) );
  • database-browser/trunk/readme.txt

    r1139718 r1517159  
    55Requires at least: 3.0.1
    66Tested up to: 4.2
    7 Stable tag: 1.3
     7Stable tag: 1.4
    88
    99Easily query and browse tables in your database, and download in XML, JSON, CSV, SQL and HTML format
     
    3939
    4040== Changelog ==
     41
     42= 1.4 (2016/10/16) =
     43
     44Added UTF decoding, as per suggestion by @darktek (https://wordpress.org/support/topic/exporting-into-csv-character-error/)
     45Added unit tests for output methods
    4146
    4247= 1.3 (2015/04/20) =
  • database-browser/trunk/views/results.php

    r883826 r1517159  
    2626<div class="tablewrapper">
    2727
    28     <?php echo $this->toHTML( "widefat", 250 ); ?>
     28    <?php echo $this->toHTML( $this->database_name, $this->table, $this->columns, $this->rows, "widefat", 250 ); ?>
    2929
    3030</div>
Note: See TracChangeset for help on using the changeset viewer.