Changeset 1517159
- Timestamp:
- 10/18/2016 08:36:45 AM (9 years ago)
- Location:
- database-browser/trunk
- Files:
-
- 7 added
- 3 edited
-
database-browser.php (modified) (23 diffs)
-
readme.txt (modified) (2 diffs)
-
tests (added)
-
tests/Output_Base_Test.php (added)
-
tests/toCSV_Test.php (added)
-
tests/toHTML_Test.php (added)
-
tests/toJSON_Test.php (added)
-
tests/toSQL_Test.php (added)
-
tests/toXML_Test.php (added)
-
views/results.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
database-browser/trunk/database-browser.php
r1139718 r1517159 6 6 Description: Easily browse the data in your database, and download in CSV, XML, SQL and JSON format 7 7 Author: Chris Taylor 8 Version: 1. 28 Version: 1.4 9 9 Author URI: http://www.stillbreathing.co.uk/ 10 10 */ 11 11 12 12 // 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(); 13 if ( 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 } 23 25 24 26 if ( ! class_exists( 'DatabaseBrowser' ) ) { … … 38 40 * @since 1.0 39 41 */ 40 var $version = "1. 2";42 var $version = "1.4"; 41 43 42 44 /** … … 47 49 var $tables = array(); 48 50 51 /** 52 * The name of the WordPress database 53 * 54 * @since 1.4 55 */ 56 var $database_name = null; 57 49 58 /** 50 59 * The name of the currently selected table; null if no table selected … … 145 154 146 155 /** 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 } 154 165 } 155 166 … … 177 188 header( "Location: tools.php?page=databasebrowser&table=" . $_POST["table"] . "&_wpnonce=" . wp_create_nonce( "query" ) ); 178 189 } 190 191 // set the database name 192 $this->database_name = DB_NAME; 179 193 180 194 // load textdomain … … 647 661 case "xml": 648 662 $this->forceDownload( "xml" ); 649 echo $this->toXML( );663 echo $this->toXML( $this->database_name, $this->table, $this->columns, $this->rows ); 650 664 die(); 651 665 break; … … 653 667 case "html": 654 668 $this->forceDownload( "html" ); 655 echo $this->toHTML( );669 echo $this->toHTML( $this->database_name, $this->table, $this->columns, $this->rows ); 656 670 die(); 657 671 break; … … 659 673 case "csv": 660 674 $this->forceDownload( "csv" ); 661 echo $this->toCSV( );675 echo $this->toCSV( $this->database_name, $this->table, $this->columns, $this->rows ); 662 676 die(); 663 677 break; … … 665 679 case "sql": 666 680 $this->forceDownload( "sql" ); 667 echo $this->toSQL( );681 echo $this->toSQL( $this->database_name, $this->table, $this->columns, $this->rows ); 668 682 die(); 669 683 break; … … 671 685 case "json": 672 686 $this->forceDownload( "json" ); 673 echo $this->toJSON( );687 echo $this->toJSON( $this->database_name, $this->table, $this->columns, $this->rows ); 674 688 die(); 675 689 break; … … 697 711 698 712 /** 699 * Outputs the currentdata as a JSON document700 * 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 ) { 704 718 $output = '{ 705 "database": "' . DB_NAME. '",706 "table": "' . $t his->table . '",719 "database": "' . $database_name . '", 720 "table": "' . $table . '", 707 721 "columns": 708 722 {'; 709 foreach ( $ this->columns as $column ) {723 foreach ( $columns as $column ) { 710 724 $output .= ' 711 725 "column": … … 730 744 "rows": 731 745 {'; 732 foreach ( $ this->rows as $row ) {746 foreach ( $rows as $row ) { 733 747 $line = ' 734 748 "row": … … 736 750 '; 737 751 $col = ''; 738 foreach ( $ this->columns as $column ) {752 foreach ( $columns as $column ) { 739 753 $col .= ' 740 "' . $column->Field . '": "' . $row[$column->Field]. '",';754 "' . $column->Field . '": "' . utf8_decode( $row[$column->Field] ) . '",'; 741 755 } 742 756 $line .= trim( trim( $col ), "," ); … … 753 767 754 768 /** 755 * Outputs the currentdata as a CSV document756 * 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 ) { 760 774 $output = ""; 761 foreach ( $ this->columns as $column ) {775 foreach ( $columns as $column ) { 762 776 $output .= $column->Field . ","; 763 777 } 764 778 $output = trim( $output, "," ); 765 779 $output .= "\r\n"; 766 foreach ( $ this->rows as $row ) {780 foreach ( $rows as $row ) { 767 781 $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] ) ) . '",'; 770 784 } 771 785 $line = trim( $line, "," ); … … 778 792 779 793 /** 780 * Outputs the currentdata as SQL INSERT statements781 * 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 ) { 785 799 $output = ""; 786 foreach ( $ this->rows as $row ) {787 $cols = "INSERT INTO " . $t his->table . " (";788 foreach ( $ this->columns as $column ) {800 foreach ( $rows as $row ) { 801 $cols = "INSERT INTO " . $table . " ("; 802 foreach ( $columns as $column ) { 789 803 // for custom queries we don't know the column types 790 804 if ( property_exists( $column, "Type" ) ) { … … 799 813 $cols .= ") VALUES ("; 800 814 $data = ""; 801 foreach ( $ this->columns as $column ) {815 foreach ( $columns as $column ) { 802 816 // for custom queries we don't know the column types 803 817 if ( property_exists( $column, "Type" ) ) { … … 857 871 * @since 1.0 858 872 */ 859 function toHTML( $ class = "", $truncateAtCharacter = -1 ) {873 function toHTML( $database_name, $table, $columns, $rows, $class = "", $truncateAtCharacter = -1 ) { 860 874 $output = ' 861 875 <table class="' . $class . '"> 862 876 <thead> 863 877 <tr>'; 864 foreach ( $ this->columns as $column ) {878 foreach ( $columns as $column ) { 865 879 $output .= " 866 880 <th>" . $column->Field . "</th>"; … … 870 884 </thead> 871 885 <tbody>"; 872 foreach ( $ this->rows as $row ) {886 foreach ( $rows as $row ) { 873 887 $line = " 874 888 <tr>"; 875 foreach ( $ this->columns as $column ) {889 foreach ( $columns as $column ) { 876 890 877 891 // if we are displaying all the content of every field … … 879 893 880 894 $line .= ' 881 <td>' . $row[$column->Field]. '</td>';895 <td>' . utf8_decode( $row[$column->Field] ) . '</td>'; 882 896 883 897 // we need to truncate long field values at the given character … … 890 904 } 891 905 $line .= ' 892 <td>' . $value. '</td>';906 <td>' . utf8_decode( $value ) . '</td>'; 893 907 894 908 } … … 910 924 * @since 1.0 911 925 */ 912 function toXML( ) {926 function toXML( $database_name, $table, $columns, $rows ) { 913 927 $output = '<?xml version="1.0" encoding="ISO-8859-1" ?> 914 <database name="' . DB_NAME. '">915 <table name="' . $t his->table . '">928 <database name="' . $database_name . '"> 929 <table name="' . $table . '"> 916 930 <columns>'; 917 foreach ( $ this->columns as $column ) {931 foreach ( $columns as $column ) { 918 932 // for custom queries we don't know the column types 919 933 if ( property_exists( $column, "Type" ) ) { … … 928 942 </columns> 929 943 <rows>'; 930 foreach ( $ this->rows as $row ) {944 foreach ( $rows as $row ) { 931 945 $line = ' 932 946 <row>'; 933 foreach ( $ this->columns as $column ) {947 foreach ( $columns as $column ) { 934 948 $line .= ' 935 949 <' . $column->Field . '>' . $row[$column->Field] . '</' . $column->Field . '>'; … … 977 991 * @since 1.0 978 992 */ 979 if ( class_exists( 'DatabaseBrowser' ) ) {993 if ( class_exists( 'DatabaseBrowser' ) && function_exists( 'add_action' ) ) { 980 994 $databasebrowser = new DatabaseBrowser(); 981 995 add_action( 'admin_menu', array( &$databasebrowser, 'on_admin_menu' ) ); -
database-browser/trunk/readme.txt
r1139718 r1517159 5 5 Requires at least: 3.0.1 6 6 Tested up to: 4.2 7 Stable tag: 1. 37 Stable tag: 1.4 8 8 9 9 Easily query and browse tables in your database, and download in XML, JSON, CSV, SQL and HTML format … … 39 39 40 40 == Changelog == 41 42 = 1.4 (2016/10/16) = 43 44 Added UTF decoding, as per suggestion by @darktek (https://wordpress.org/support/topic/exporting-into-csv-character-error/) 45 Added unit tests for output methods 41 46 42 47 = 1.3 (2015/04/20) = -
database-browser/trunk/views/results.php
r883826 r1517159 26 26 <div class="tablewrapper"> 27 27 28 <?php echo $this->toHTML( "widefat", 250 ); ?>28 <?php echo $this->toHTML( $this->database_name, $this->table, $this->columns, $this->rows, "widefat", 250 ); ?> 29 29 30 30 </div>
Note: See TracChangeset
for help on using the changeset viewer.