r87239 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87238‎ | r87239 | r87240 >
Date:09:00, 2 May 2011
Author:maxsem
Status:ok
Tags:
Comment:
Requests mostly work now, except for action=query
Modified paths:
  • /trunk/extensions/ApiSandbox/ApiSandbox.i18n.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/SpecialApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.css (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ApiSandbox/SpecialApiSandbox.php
@@ -25,16 +25,28 @@
2626 $this->setHeaders();
2727 $out->addModules( 'ext.apiSandbox' );
2828
29 - $out->addHTML( '<noscript>' . wfMessage( 'apisb-no-js' )->escaped() . '</noscript>
 29+ $out->addHTML( '<noscript>' . wfMessage( 'apisb-no-js' )->parse() . '</noscript>
3030 <div id="api-sandbox-content" style="display: none">' );
3131 $out->addWikiMsg( 'apisb-intro' );
32 - $out->addHTML( $this->openFieldset( 'parameters' )
33 - . $this->getInputs() . '
34 -</fieldset>
35 -' . $this->openFieldset( 'result' ) . '
36 -</fieldset>' );
 32+ $out->addHTML( $this->openFieldset( 'parameters' )
 33+ . $this->getInputs()
 34+ . '</fieldset>'
 35+ . $this->openFieldset( 'result' )
 36+ . '<table class="api-sandbox-result-container"><tbody>
 37+'
 38+ . '<tr><td class="api-sandbox-result-label"><label for="api-sandbox-url">'
 39+ . wfMessage( 'apisb-request-url' )->parse() . '</label></td>'
 40+ . '<td><input id="api-sandbox-url" readonly="readonly" /></td></tr>
 41+'
 42+ . '<tr id="api-sandbox-post-row"><td class="api-sandbox-result-label"><label for="api-sandbox-post">'
 43+ . wfMessage( 'apisb-request-post' )->parse() . '</label></td>'
 44+ . '<td><input id="api-sandbox-post" readonly="readonly" /></td></tr>
 45+'
 46+ . '<tr><td colspan="2"><div id="api-sandbox-output"></div></td></tr>'
 47+ . "\n</tbody></table>"
 48+ . "\n</fieldset>" );
3749
38 - $out->addHTML( '</div>' ); # <div id="api-sandbox-content">
 50+ $out->addHTML( "\n</div>" ); # <div id="api-sandbox-content">
3951 }
4052
4153 private function getInputs() {
@@ -46,9 +58,11 @@
4759 $s = '<table class="api-sandbox-options">
4860 <tbody>
4961 <tr><td class="api-sandbox-label"><label for="api-sandbox-action">action=</label></td><td class="api-sandbox-value">'
50 - . $this->getSelect( 'action', $apiMain->getModules() ) . '</td><td id="api-sandbox-help" rowspan="2"></td></tr>
 62+ . $this->getSelect( 'action', $apiMain->getModules() )
 63+ . '</td><td id="api-sandbox-help" rowspan="2"></td></tr>
5164 ';
52 - $s .= '<tr id="api-sandbox-prop-row" style="display: none"><td class="api-sandbox-label"><label for="api-sandbox-prop">prop=</label></td><td class="api-sandbox-value">'
 65+ $s .= '<tr id="api-sandbox-prop-row" style="display: none"><td class="api-sandbox-label">'
 66+ . '<label for="api-sandbox-prop">prop=</label></td><td class="api-sandbox-value">'
5367 . $this->getSelect( 'prop', $apiQuery->getModules() )
5468 . '</td></tr>
5569 </table>
@@ -61,14 +75,18 @@
6276 'value' => wfMessage( 'apisb-submit' )->text(),
6377 'disabled' => 'disabled',
6478 )
65 - );
 79+ ) . "\n";
6680 return $s;
6781 }
6882
6983 private function getSelect( $name, $items ) {
7084 $items = array_keys( $items );
7185 sort( $items );
72 - $s = Html::openElement( 'select', array( 'class' => 'api-sandbox-input', 'name' => $name, 'id' => "api-sandbox-$name" ) );
 86+ $s = Html::openElement( 'select', array(
 87+ 'class' => 'api-sandbox-input',
 88+ 'name' => $name,
 89+ 'id' => "api-sandbox-$name" )
 90+ );
7391 $s .= "\n\t" . Html::element( 'option',
7492 array( 'value' => '-', 'selected' => 'selected' ),
7593 wfMessage( "apisb-select-$name" )->text()
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js
@@ -11,12 +11,18 @@
1212 var help = $( '#api-sandbox-help' );
1313 var further = $( '#api-sandbox-further-inputs' );
1414 var submit = $( '#api-sandbox-submit' );
 15+ var requestUrl = $( '#api-sandbox-url' );
 16+ var requestPost = $( '#api-sandbox-post' );
 17+ var output = $( '#api-sandbox-output' );
 18+ var postRow = $( '#api-sandbox-post-row' );
1519 var actionCache = [];
1620 var propCache = [];
1721 var namespaces = [];
 22+ var currentInfo = {};
 23+ var apiPhp = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' );
1824
1925 // load namespaces
20 - $.getJSON( mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' ),
 26+ $.getJSON( apiPhp,
2127 { format: 'json', action: 'query', meta: 'siteinfo', siprop: 'namespaces' },
2228 function( data ) {
2329 if ( isset( data.query ) && isset( data.query.namespaces ) ) {
@@ -36,6 +42,59 @@
3743 }
3844 );
3945
 46+ action.change( updateBasics );
 47+ prop.change( updateBasics );
 48+
 49+ submit.click( function() {
 50+ var url = apiPhp + '?action=' + action.val();
 51+ if ( action.val() == 'query' ) {
 52+ url += '&prop=' + prop.val();
 53+ }
 54+ url += '&format=json'; // @todo:
 55+ var params = '';
 56+ for ( var i = 0; i < currentInfo.parameters.length; i++ ) {
 57+ var param = currentInfo.parameters[i];
 58+ var name = currentInfo.prefix + param.name;
 59+ var value = $( '#param-' + name ).val();
 60+ if ( param.value == ''
 61+ && ( param.type != 'boolean' && param.type != 'bool' )
 62+ && !isset( param.required ) )
 63+ {
 64+ value = null;
 65+ }
 66+ if ( typeof value != 'undefined' ) {
 67+ params += '&' + name + '=' + encodeURIComponent( value );
 68+ }
 69+ }
 70+ showLoading( output );
 71+ if ( isset( currentInfo.mustbeposted ) ) {
 72+ requestUrl.val( url );
 73+ requestPost.val( params );
 74+ postRow.show();
 75+ } else {
 76+ requestUrl.val( url + params );
 77+ postRow.hide();
 78+ }
 79+ url = url.replace( /(&format=[^&]+)/, '$1fm' );
 80+ var data = {
 81+ url: url,
 82+ data: params,
 83+ dataType: 'text',
 84+ type: isset( currentInfo.mustbeposted ) ? 'POST' : 'GET',
 85+ success: function( data ) {
 86+ data = data.match( /<pre>[\s\S]*<\/pre>/ )[0];
 87+ output.html( data );
 88+ },
 89+ error: function( jqXHR, textStatus, errorThrown ) {
 90+ output.text( 'FAIL' );
 91+ }
 92+ };
 93+ $.ajax( data );
 94+ });
 95+
 96+ /**
 97+ * Shamelessly borrowed from PHP
 98+ */
4099 function isset( x ) {
41100 return typeof x != 'undefined';
42101 }
@@ -83,7 +142,7 @@
84143 showLoading( further );
85144 var data = {
86145 format: 'json',
87 - action: 'paraminfo',
 146+ action: 'paraminfo'
88147 };
89148 if (isQuery ) {
90149 data.querymodules = prop;
@@ -113,6 +172,7 @@
114173 }
115174
116175 function createInputs( info ) {
 176+ currentInfo = info;
117177 help.html( smartEscape( info.description ) );
118178 var s = '<table class="api-sandbox-options">\n<tbody>';
119179 for ( var i = 0; i < info.parameters.length; i++ ) {
@@ -128,7 +188,7 @@
129189 }
130190
131191 function input( param, name ) {
132 - var s = param.type;
 192+ var s;
133193 var value = '';
134194 switch ( param.type ) {
135195 case 'limit':
@@ -155,7 +215,9 @@
156216 } else {
157217 s = select( param.type, attributes, true );
158218 }
159 - }
 219+ } else {
 220+ s = mw.html.element( 'code', [], mw.msg( 'parentheses', param.type ) );
 221+ }
160222 }
161223 return s;
162224 }
@@ -199,8 +261,4 @@
200262 getQueryInfo( a, p );
201263 }
202264
203 - action.change( updateBasics );
204 - prop.change( updateBasics );
205 -
206 -
207265 });
\ No newline at end of file
Index: trunk/extensions/ApiSandbox/ApiSandbox.i18n.php
@@ -10,6 +10,8 @@
1111 'apisb-api-disabled' => 'API is disabled on this site.',
1212 'apisb-parameters' => 'Parameters',
1313 'apisb-result' => 'Result',
 14+ 'apisb-request-url' => 'Request URL:',
 15+ 'apisb-request-post' => 'POST data:',
1416 'apisb-select-action' => 'Select action',
1517 'apisb-select-prop' => 'Select property',
1618 'apisb-select-value' => 'Select value',
Index: trunk/extensions/ApiSandbox/ApiSandbox.php
@@ -33,5 +33,7 @@
3434 'apisb-load-error',
3535 'apisb-select-value',
3636 'apisb-namespaces-error',
37 - 'apisb-ns-main' ),
 37+ 'apisb-ns-main',
 38+ 'parentheses',
 39+ ),
3840 );
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.css
@@ -9,12 +9,30 @@
1010 font-family: monospace;
1111 }
1212
 13+td.api-sandbox-result-label {
 14+ width: 12em;
 15+}
 16+
1317 td.api-sandbox-value {
1418 width: 18em;
1519 vertical-align: top;
1620 overflow: auto;
1721 }
1822
19 -.api-sandbox-input {
 23+table.api-sandbox-result-container {
 24+ width: 100%;
 25+}
 26+
 27+#api-sandbox-input {
2028 width: 17em;
21 -}
\ No newline at end of file
 29+}
 30+
 31+#api-sandbox-url, #api-sandbox-post {
 32+ width: 100%;
 33+}
 34+
 35+#api-sandbox-output {
 36+ width: 100%;
 37+ height: 15em;
 38+ overflow: scroll;
 39+}

Status & tagging log