Index: trunk/extensions/ApiSandbox/SpecialApiSandbox.php |
— | — | @@ -54,10 +54,17 @@ |
55 | 55 | |
56 | 56 | $apiMain = new ApiMain( new FauxRequest( array() ), $wgEnableWriteAPI ); |
57 | 57 | $apiQuery = new ApiQuery( $apiMain, 'query' ); |
| 58 | + $formats = array_flip( array_filter( array_keys( $apiMain->getFormats() ), |
| 59 | + 'SpecialApiSandbox::filterFormats' ) ); |
58 | 60 | |
59 | 61 | $s = '<table class="api-sandbox-options"> |
60 | 62 | <tbody> |
61 | | -<tr><td class="api-sandbox-label"><label for="api-sandbox-action">action=</label></td><td class="api-sandbox-value">' |
| 63 | +'; |
| 64 | + $s .= '<tr><td class="api-sandbox-label"><label for="api-sandbox-format">format=</label></td><td class="api-sandbox-value">' |
| 65 | + . $this->getSelect( 'format', $formats, 'json' ) |
| 66 | + . '</td><td></td></tr> |
| 67 | +'; |
| 68 | + $s .= '<tr><td class="api-sandbox-label"><label for="api-sandbox-action">action=</label></td><td class="api-sandbox-value">' |
62 | 69 | . $this->getSelect( 'action', $apiMain->getModules() ) |
63 | 70 | . '</td><td id="api-sandbox-help" rowspan="2"></td></tr> |
64 | 71 | '; |
— | — | @@ -79,7 +86,7 @@ |
80 | 87 | return $s; |
81 | 88 | } |
82 | 89 | |
83 | | - private function getSelect( $name, $items ) { |
| 90 | + private function getSelect( $name, $items, $default = false ) { |
84 | 91 | $items = array_keys( $items ); |
85 | 92 | sort( $items ); |
86 | 93 | $s = Html::openElement( 'select', array( |
— | — | @@ -87,12 +94,18 @@ |
88 | 95 | 'name' => $name, |
89 | 96 | 'id' => "api-sandbox-$name" ) |
90 | 97 | ); |
91 | | - $s .= "\n\t" . Html::element( 'option', |
92 | | - array( 'value' => '-', 'selected' => 'selected' ), |
93 | | - wfMessage( "apisb-select-$name" )->text() |
94 | | - ); |
| 98 | + if ( $default === false ) { |
| 99 | + $s .= "\n\t" . Html::element( 'option', |
| 100 | + array( 'value' => '-', 'selected' => 'selected' ), |
| 101 | + wfMessage( "apisb-select-$name" )->text() |
| 102 | + ); |
| 103 | + } |
95 | 104 | foreach ( $items as $item ) { |
96 | | - $s .= "\n\t" . Html::element( 'option', array( 'value' => $item ), $item ); |
| 105 | + $attributes = array( 'value' => $item ); |
| 106 | + if ( $item === $default ) { |
| 107 | + $attributes['selected'] = 'selected'; |
| 108 | + } |
| 109 | + $s .= "\n\t" . Html::element( 'option', $attributes, $item ); |
97 | 110 | } |
98 | 111 | $s .= "\n" . Html::closeElement( 'select' ) . "\n"; |
99 | 112 | return $s; |
— | — | @@ -103,4 +116,12 @@ |
104 | 117 | . "\n\t" . Html::rawElement( 'legend', array(), wfMessage( "apisb-$name" )->parse() ) |
105 | 118 | . "\n"; |
106 | 119 | } |
| 120 | + |
| 121 | + /** |
| 122 | + * Callback that returns false if its argument (format name) ends with 'fm' |
| 123 | + * @return boolean |
| 124 | + */ |
| 125 | + private static function filterFormats( $value ) { |
| 126 | + return !preg_match( '/fm$/', $value ); |
| 127 | + } |
107 | 128 | } |
\ No newline at end of file |
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js |
— | — | @@ -6,7 +6,8 @@ |
7 | 7 | $content.show(); |
8 | 8 | |
9 | 9 | // page elements |
10 | | - var $action = $( '#api-sandbox-action' ), |
| 10 | + var $format = $( '#api-sandbox-format' ), |
| 11 | + $action = $( '#api-sandbox-action' ), |
11 | 12 | $prop = $( '#api-sandbox-prop' ), |
12 | 13 | $propRow = $( '#api-sandbox-prop-row' ), |
13 | 14 | $help = $( '#api-sandbox-help' ), |
— | — | @@ -54,7 +55,7 @@ |
55 | 56 | if ( $action.val() == 'query' ) { |
56 | 57 | url += '&prop=' + $prop.val(); |
57 | 58 | } |
58 | | - url += '&format=json'; // @todo: |
| 59 | + url += '&format=' + $format.val(); |
59 | 60 | var params = ''; |
60 | 61 | for ( var i = 0; i < info.parameters.length; i++ ) { |
61 | 62 | var param = info.parameters[i], |
— | — | @@ -91,7 +92,13 @@ |
92 | 93 | dataType: 'text', |
93 | 94 | type: isset( info.mustbeposted ) ? 'POST' : 'GET', |
94 | 95 | success: function( data ) { |
95 | | - data = data.match( /<pre>[\s\S]*<\/pre>/ )[0]; |
| 96 | + var match = data.match( /<pre>[\s\S]*<\/pre>/ ); |
| 97 | + if ( $.isArray( match ) ) { |
| 98 | + data = match[0]; |
| 99 | + } else { |
| 100 | + // some actions don't honor user-specified format |
| 101 | + data = '<pre>' + mw.html.escape( data ) + '</pre>'; |
| 102 | + } |
96 | 103 | $output.html( data ); |
97 | 104 | }, |
98 | 105 | error: function( jqXHR, textStatus, errorThrown ) { |