r87403 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87402‎ | r87403 | r87404 >
Date:15:27, 4 May 2011
Author:maxsem
Status:ok
Tags:
Comment:
Proper separation of action=query into prop, list and meta
Modified paths:
  • /trunk/extensions/ApiSandbox/ApiSandbox.i18n.php (modified) (history)
  • /trunk/extensions/ApiSandbox/SpecialApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ApiSandbox/SpecialApiSandbox.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class SpecialApiSandbox extends SpecialPage {
5 -
 5+ private $apiQuery;
66 /**
77 * Constructor
88 */
@@ -53,24 +53,32 @@
5454 global $wgEnableWriteAPI;
5555
5656 $apiMain = new ApiMain( new FauxRequest( array() ), $wgEnableWriteAPI );
57 - $apiQuery = new ApiQuery( $apiMain, 'query' );
58 - $formats = array_flip( array_filter( array_keys( $apiMain->getFormats() ),
59 - 'SpecialApiSandbox::filterFormats' ) );
 57+ $this->apiQuery = new ApiQuery( $apiMain, 'query' );
 58+ $formats = array_filter( array_keys( $apiMain->getFormats() ),
 59+ 'SpecialApiSandbox::filterFormats' );
 60+ sort( $formats );
 61+ $modules = array_keys( $apiMain->getModules() );
 62+ sort( $modules );
 63+ $queryModules = array_merge(
 64+ $this->getQueryModules( 'prop' ),
 65+ $this->getQueryModules( 'list' ),
 66+ $this->getQueryModules( 'meta' )
 67+ );
6068
6169 $s = '<table class="api-sandbox-options">
6270 <tbody>
6371 ';
6472 $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>
 73+ . self::getSelect( 'format', $formats, 'json' )
 74+ . '</td><td></td></tr>
6775 ';
6876 $s .= '<tr><td class="api-sandbox-label"><label for="api-sandbox-action">action=</label></td><td class="api-sandbox-value">'
69 - . $this->getSelect( 'action', $apiMain->getModules() )
70 - . '</td><td id="api-sandbox-help" rowspan="2"></td></tr>
 77+ . self::getSelect( 'action', $modules )
 78+ . '</td><td id="api-sandbox-help" rowspan="2"></td></tr>
7179 ';
72 - $s .= '<tr id="api-sandbox-prop-row" style="display: none"><td class="api-sandbox-label">'
73 - . '<label for="api-sandbox-prop">prop=</label></td><td class="api-sandbox-value">'
74 - . $this->getSelect( 'prop', $apiQuery->getModules() )
 80+ $s .= '<tr id="api-sandbox-query-row" style="display: none"><td class="api-sandbox-label">'
 81+ . '</td><td class="api-sandbox-value">'
 82+ . self::getSelect( 'query', $queryModules )
7583 . '</td></tr>
7684 </table>
7785 <div id="api-sandbox-further-inputs"></div>
@@ -86,35 +94,65 @@
8795 return $s;
8896 }
8997
90 - private function getSelect( $name, $items, $default = false ) {
91 - $items = array_keys( $items );
92 - sort( $items );
 98+ private function getQueryModules( $type ) {
 99+ $res = array();
 100+ $params = $this->apiQuery->getAllowedParams();
 101+ foreach ( $params[$type][ApiBase::PARAM_TYPE] as $module ) {
 102+ $res[] = array(
 103+ 'value' => "$type=$module",
 104+ 'text' => /* &nbsp; */ "\xc2\xa0\xc2\xa0\xc2\xa0$type=$module",
 105+ );
 106+ }
 107+ sort( $res );
 108+ array_unshift( $res,
 109+ array(
 110+ 'value' => '-',
 111+ 'text' => wfMessage( "apisb-query-$type" )->parse(),
 112+ 'attributes' => array( 'disabled' => 'disabled' )
 113+ )
 114+ );
 115+
 116+ return $res;
 117+ }
 118+
 119+ private static function getSelect( $name, $items, $default = false ) {
93120 $s = Html::openElement( 'select', array(
94121 'class' => 'api-sandbox-input',
95122 'name' => $name,
96123 'id' => "api-sandbox-$name" )
97124 );
98125 if ( $default === false ) {
99 - $s .= "\n\t" . Html::element( 'option',
100 - array( 'value' => '-', 'selected' => 'selected' ),
101 - wfMessage( "apisb-select-$name" )->text()
 126+ $s .= "\n\t" . self::option( '-', wfMessage( "apisb-select-$name" )->text(),
 127+ array( 'selected' => 'selected' )
102128 );
103129 }
104130 foreach ( $items as $item ) {
105 - $attributes = array( 'value' => $item );
106 - if ( $item === $default ) {
 131+ $attributes = array();
 132+ if ( is_array( $item ) ) {
 133+ $value = $item['value'];
 134+ $text = $item['text'];
 135+ $attributes = isset( $item['attributes'] ) ? $item['attributes'] : array();
 136+ } else {
 137+ $value = $text = $item;
 138+ }
 139+ if ( $value === $default ) {
107140 $attributes['selected'] = 'selected';
108141 }
109 - $s .= "\n\t" . Html::element( 'option', $attributes, $item );
 142+ $s .= "\n\t" . self::option( $value, $text, $attributes );
110143 }
111144 $s .= "\n" . Html::closeElement( 'select' ) . "\n";
112145 return $s;
113146 }
114147
 148+ private static function option( $value, $text, $attributes = array() ) {
 149+ $attributes['value'] = $value;
 150+ return Html::element( 'option', $attributes, $text );
 151+ }
 152+
115153 private function openFieldset( $name ) {
116 - return "\n" . Html::openElement( 'fieldset', array( 'id' => "api-sandbox-$name" ) )
117 - . "\n\t" . Html::rawElement( 'legend', array(), wfMessage( "apisb-$name" )->parse() )
118 - . "\n";
 154+ return "\n" . Html::openElement( 'fieldset', array( 'id' => "api-sandbox-$name" ) )
 155+ . "\n\t" . Html::rawElement( 'legend', array(), wfMessage( "apisb-$name" )->parse() )
 156+ . "\n";
119157 }
120158
121159 /**
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js
@@ -8,8 +8,8 @@
99 // page elements
1010 var $format = $( '#api-sandbox-format' ),
1111 $action = $( '#api-sandbox-action' ),
12 - $prop = $( '#api-sandbox-prop' ),
13 - $propRow = $( '#api-sandbox-prop-row' ),
 12+ $query = $( '#api-sandbox-query' ),
 13+ $queryRow = $( '#api-sandbox-query-row' ),
1414 $help = $( '#api-sandbox-help' ),
1515 $further = $( '#api-sandbox-further-inputs' ),
1616 $submit = $( '#api-sandbox-submit' ),
@@ -47,13 +47,13 @@
4848 );
4949
5050 $action.change( updateBasics );
51 - $prop.change( updateBasics );
 51+ $query.change( updateBasics );
5252
5353 $submit.click( function() {
5454 var url = apiPhp + '?action=' + $action.val(),
5555 info = currentInfo; // in case it changes later
5656 if ( $action.val() == 'query' ) {
57 - url += '&prop=' + $prop.val();
 57+ url += '&' + $query.val();
5858 }
5959 url += '&format=' + $format.val();
6060 var params = '';
@@ -145,15 +145,16 @@
146146 $submit.removeAttr( 'disabled' );
147147 }
148148
149 - function getQueryInfo( action, prop ) {
 149+ function getQueryInfo( action, query ) {
150150 var isQuery = action == 'query';
151 - if ( action == '-' || ( isQuery && prop == '-' ) ) {
 151+ if ( action == '-' || ( isQuery && query == '-' ) ) {
152152 $submit.attr( 'disabled', 'disabled' );
153153 return;
154154 }
 155+ query = query.replace( /^.*=/, '' );
155156 var cached;
156157 if ( isQuery ) {
157 - cached = propCache[prop];
 158+ cached = propCache[query];
158159 } else {
159160 cached = actionCache[action];
160161 }
@@ -164,7 +165,7 @@
165166 action: 'paraminfo'
166167 };
167168 if (isQuery ) {
168 - data.querymodules = prop;
 169+ data.querymodules = query;
169170 } else {
170171 data.modules = action;
171172 }
@@ -269,16 +270,16 @@
270271
271272 function updateBasics() {
272273 var a = $action.val(),
273 - p = $prop.val(),
 274+ q = $query.val(),
274275 isQuery = a == 'query';
275276 if ( isQuery ) {
276 - $propRow.show();
 277+ $queryRow.show();
277278 } else {
278 - $propRow.hide();
 279+ $queryRow.hide();
279280 }
280281 $further.text( '' );
281282 $help.text( '' );
282 - getQueryInfo( a, p );
 283+ getQueryInfo( a, q );
283284 }
284285
285286 });
\ No newline at end of file
Index: trunk/extensions/ApiSandbox/ApiSandbox.i18n.php
@@ -13,7 +13,7 @@
1414 'apisb-request-url' => 'Request URL:',
1515 'apisb-request-post' => 'POST data:',
1616 'apisb-select-action' => 'Select action',
17 - 'apisb-select-prop' => 'Select property',
 17+ 'apisb-select-query' => 'What to query?',
1818 'apisb-select-value' => 'Select value',
1919 'apisb-loading' => 'Loading...',
2020 'apisb-load-error' => 'Error loading API description',
@@ -21,4 +21,7 @@
2222 'apisb-namespaces-error' => 'Error loading namespaces',
2323 'apisb-ns-main' => '(Main)',
2424 'apisb-submit' => 'Make request',
 25+ 'apisb-query-prop' => 'Properties',
 26+ 'apisb-query-list' => 'Lists',
 27+ 'apisb-query-meta' => 'Meta information',
2528 );
\ No newline at end of file

Status & tagging log