r87343 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87342‎ | r87343 | r87344 >
Date:18:12, 3 May 2011
Author:maxsem
Status:ok
Tags:
Comment:
Support for different formats
Modified paths:
  • /trunk/extensions/ApiSandbox/SpecialApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ApiSandbox/SpecialApiSandbox.php
@@ -54,10 +54,17 @@
5555
5656 $apiMain = new ApiMain( new FauxRequest( array() ), $wgEnableWriteAPI );
5757 $apiQuery = new ApiQuery( $apiMain, 'query' );
 58+ $formats = array_flip( array_filter( array_keys( $apiMain->getFormats() ),
 59+ 'SpecialApiSandbox::filterFormats' ) );
5860
5961 $s = '<table class="api-sandbox-options">
6062 <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">'
6269 . $this->getSelect( 'action', $apiMain->getModules() )
6370 . '</td><td id="api-sandbox-help" rowspan="2"></td></tr>
6471 ';
@@ -79,7 +86,7 @@
8087 return $s;
8188 }
8289
83 - private function getSelect( $name, $items ) {
 90+ private function getSelect( $name, $items, $default = false ) {
8491 $items = array_keys( $items );
8592 sort( $items );
8693 $s = Html::openElement( 'select', array(
@@ -87,12 +94,18 @@
8895 'name' => $name,
8996 'id' => "api-sandbox-$name" )
9097 );
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+ }
95104 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 );
97110 }
98111 $s .= "\n" . Html::closeElement( 'select' ) . "\n";
99112 return $s;
@@ -103,4 +116,12 @@
104117 . "\n\t" . Html::rawElement( 'legend', array(), wfMessage( "apisb-$name" )->parse() )
105118 . "\n";
106119 }
 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+ }
107128 }
\ No newline at end of file
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js
@@ -6,7 +6,8 @@
77 $content.show();
88
99 // page elements
10 - var $action = $( '#api-sandbox-action' ),
 10+ var $format = $( '#api-sandbox-format' ),
 11+ $action = $( '#api-sandbox-action' ),
1112 $prop = $( '#api-sandbox-prop' ),
1213 $propRow = $( '#api-sandbox-prop-row' ),
1314 $help = $( '#api-sandbox-help' ),
@@ -54,7 +55,7 @@
5556 if ( $action.val() == 'query' ) {
5657 url += '&prop=' + $prop.val();
5758 }
58 - url += '&format=json'; // @todo:
 59+ url += '&format=' + $format.val();
5960 var params = '';
6061 for ( var i = 0; i < info.parameters.length; i++ ) {
6162 var param = info.parameters[i],
@@ -91,7 +92,13 @@
9293 dataType: 'text',
9394 type: isset( info.mustbeposted ) ? 'POST' : 'GET',
9495 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+ }
96103 $output.html( data );
97104 },
98105 error: function( jqXHR, textStatus, errorThrown ) {

Status & tagging log