r78152 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78151‎ | r78152 | r78153 >
Date:22:50, 9 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Modified Special:Push to make use of the new API module and added batch support
Modified paths:
  • /trunk/extensions/Push/Push.i18n.php (modified) (history)
  • /trunk/extensions/Push/Push.php (modified) (history)
  • /trunk/extensions/Push/Push_Settings.php (modified) (history)
  • /trunk/extensions/Push/api/ApiPush.php (modified) (history)
  • /trunk/extensions/Push/specials/Push_Body.php (modified) (history)
  • /trunk/extensions/Push/specials/ext.push.special.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Push/Push.i18n.php
@@ -17,6 +17,10 @@
1818 $messages['en'] = array(
1919 'push-desc' => 'Lightweight extension to push content to other wikis',
2020
 21+ 'right-push' => 'Authorization to use push functionality.',
 22+ 'right-bulkpush' => 'Authorization to use bulk push functionality (ie Special:Push).',
 23+ 'right-pushadmin' => 'Authorization to modify push targets and push settings.',
 24+
2125 // Tab
2226 'push-tab-text' => 'Push',
2327 'push-button-text' => 'Push',
@@ -46,13 +50,13 @@
4751 'push-special-button-text' => 'Push pages',
4852 'push-special-target-is' => 'Target wiki: $1',
4953 'push-special-select-targets' => 'Target wikis:',
50 - 'push-special-item-getting' => '$1: Getting page',
51 - 'push-special-item-pushing-to' => '$1: Pushing to $2',
 54+ 'push-special-item-pushing' => '$1: Pushing',
5255 'push-special-item-completed' => '$1: Push completed',
5356 'push-special-item-failed' => '$1: Push failed: $2',
54 - 'push-special-push-done' => 'Push completed'
55 - # 'right-push' => '', // Please add a description of this userright which is shown at [[Special:ListGroupRights]]
56 - # 'right-pushadmin' => '', // Please add a description of this userright which is shown at [[Special:ListGroupRights]]
 57+ 'push-special-push-done' => 'Push completed',
 58+ 'push-special-err-token-failed' => 'Could not obtain an edit token on the target wiki.',
 59+ 'push-special-err-pageget-failed' => 'Could not obtain local page content.',
 60+ 'push-special-err-push-failed' => 'Target wiki refused the pushed page.'
5761 );
5862
5963 /** Message documentation (Message documentation) */
Index: trunk/extensions/Push/specials/Push_Body.php
@@ -119,7 +119,7 @@
120120 * @param string $pages
121121 */
122122 protected function doPush( $pages ) {
123 - global $wgOut, $wgLang, $wgRequest, $wgSitename, $egPushTargets, $egPushBulkWorkers;
 123+ global $wgOut, $wgLang, $wgRequest, $wgSitename, $egPushTargets, $egPushBulkWorkers, $egPushBatchSize;
124124
125125 $pageSet = array(); // Inverted index of all pages to look up
126126
@@ -142,18 +142,18 @@
143143
144144 $targets = array();
145145 $links = array();
146 - $revisions = array();
147146
148 - foreach ( $pages as $page ) {
149 - $revisions[$page] = PushFunctions::getRevisionToPush( Title::newFromText( $page ) );
150 - }
151 -
152 - foreach ( $egPushTargets as $targetName => $targetUrl ) {
153 - if ( $wgRequest->getCheck( str_replace( ' ', '_', $targetName ) ) ) {
154 - $targets[$targetName] = $targetUrl;
155 - $links[] = "[$targetUrl $targetName]";
 147+ if ( count( $egPushTargets ) > 1 ) {
 148+ foreach ( $egPushTargets as $targetName => $targetUrl ) {
 149+ if ( $wgRequest->getCheck( str_replace( ' ', '_', $targetName ) ) ) {
 150+ $targets[$targetName] = $targetUrl;
 151+ $links[] = "[$targetUrl $targetName]";
 152+ }
156153 }
157154 }
 155+ else {
 156+ $targets = $egPushTargets;
 157+ }
158158
159159 $wgOut->addWikiMsg( 'push-special-pushing-desc', $wgLang->listToText( $links ), $wgLang->formatNum( count( $pages ) ) );
160160
@@ -175,9 +175,9 @@
176176
177177 $wgOut->addInlineScript(
178178 'var wgPushPages = ' . json_encode( $pages ) . ';' .
179 - 'var wgPushRevs = ' . json_encode( $revisions ) . ';' .
180179 'var wgPushTargets = ' . json_encode( $targets ) . ';' .
181 - 'var wgPushWorkerCount = ' . $egPushBulkWorkers . ';'
 180+ 'var wgPushWorkerCount = ' . $egPushBulkWorkers . ';' .
 181+ 'var wgPushBatchSize = ' . $egPushBatchSize . ';'
182182 );
183183
184184 $this->loadJs();
Index: trunk/extensions/Push/specials/ext.push.special.js
@@ -12,20 +12,27 @@
1313 if ( typeof mediaWiki === 'undefined' ) {
1414 mediaWiki = new Object();
1515
16 - mediaWiki.msg = function( message ) {
17 - return window.wgPushMessages[message];
 16+ mediaWiki.msg = function() {
 17+ message = window.wgPushMessages[arguments[0]];
 18+
 19+ for ( var i = arguments.length - 1; i > 0; i-- ) {
 20+ message = message.replace( '$' + i, arguments[i] );
 21+ }
 22+
 23+ return message;
1824 }
1925 }
2026
2127 var resultList = $('#pushResultList');
2228
23 - var targets = window.wgPushTargets;
 29+ var targets = [];
 30+ for (targetName in window.wgPushTargets) targets.push( window.wgPushTargets[targetName] );
 31+
2432 var pages = window.wgPushPages;
25 - var revs = window.wgPushRevs;
 33+
2634 var requestAmount = Math.min( pages.length, window.wgPushWorkerCount );
 35+ var batchSize = Math.min( targets.length, window.wgPushBatchSize );
2736
28 - var pageTargets = [];
29 -
3037 for ( i = requestAmount; i > 0; i-- ) {
3138 initiateNextPush();
3239 }
@@ -34,139 +41,70 @@
3542 var page = pages.pop();
3643
3744 if ( page ) {
38 - startPush( page );
 45+ startPush( page, 0, null );
3946 }
4047 else if ( !--requestAmount ) {
4148 showCompletion();
4249 }
4350 }
4451
45 - function startPush( pageName ) {
46 - var listItem = $( '<li />' );
47 - listItem.text( pageName );
48 -
49 - var box = $('#pushResultDiv');
50 - var innerBox = $('#pushResultDiv > .innerResultBox');
51 - var atBottom = Math.abs(innerBox.offset().top) + box.height() + box.offset().top >= innerBox.outerHeight();
52 -
53 - resultList.append( listItem );
54 -
55 - if ( atBottom ) {
56 - box.attr( {'scrollTop': box.attr( 'scrollHeight' )} );
 52+ function startPush( pageName, targetOffset, listItem ) {
 53+ if ( targetOffset == 0 ) {
 54+ var listItem = $( '<li />' );
 55+ listItem.text( mediaWiki.msg( 'push-special-item-pushing', pageName ) );
 56+
 57+ var box = $('#pushResultDiv');
 58+ var innerBox = $('#pushResultDiv > .innerResultBox');
 59+ var atBottom = Math.abs(innerBox.offset().top) + box.height() + box.offset().top >= innerBox.outerHeight();
 60+
 61+ resultList.append( listItem );
 62+
 63+ if ( atBottom ) {
 64+ box.attr( {'scrollTop': box.attr( 'scrollHeight' )} );
 65+ }
5766 }
5867
59 - getLocalArtcileAndContinue( listItem, pageName );
60 - }
61 -
62 - function showCompletion() {
63 - resultList.append( $( '<li />' ).append( $( '<b />' ).text( mediaWiki.msg( 'push-special-push-done' ) ) ) );
64 - }
65 -
66 - function getLocalArtcileAndContinue( listItem, pageName ) {
67 - listItem.text( msgReplace( 'push-special-item-getting', pageName ) );
 68+ var currentBatchLimit = Math.min( targetOffset + batchSize, targets.length );
 69+ var currentBatchStart = targetOffset;
6870
69 - $.getJSON(
70 - wgScriptPath + '/api.php',
71 - {
72 - 'action': 'query',
73 - 'format': 'json',
74 - 'prop': 'revisions',
75 - 'rvprop': 'timestamp|user|comment|content',
76 - 'titles': pageName,
77 - 'rvstartid': revs[pageName],
78 - 'rvendid': revs[pageName],
79 - },
80 - function( data ) {
81 - if ( data.error ) {
82 - handleError( listItem, pageName, data.error );
 71+ if ( targetOffset < targets.length ) {
 72+ listItem.text( listItem.text() + '...' );
 73+
 74+ targetOffset = currentBatchLimit;
 75+
 76+ $.getJSON(
 77+ wgScriptPath + '/api.php',
 78+ {
 79+ 'action': 'push',
 80+ 'format': 'json',
 81+ 'page': pageName,
 82+ 'targets': targets.slice( currentBatchStart, currentBatchLimit ).join( '|' )
 83+ },
 84+ function( data ) {
 85+ if ( data.error ) {
 86+ handleError( listItem, pageName, data.error );
 87+ }
 88+ else {
 89+ startPush( pageName, targetOffset, listItem );
 90+ }
8391 }
84 - else {
85 - for (first in data.query.pages) break;
86 - var remainingTargets = [];
87 - for (targetName in targets) remainingTargets.push( targetName );
88 - initiateEdit( listItem, pageName, data.query.pages[first], remainingTargets );
89 - }
90 - }
91 - );
92 - }
93 -
94 - function initiateEdit( listItem, pageName, page, remainingTargets ) {
95 - targetName = remainingTargets.pop();
96 -
97 - if ( targetName ) {
98 - getEditTokenAndContinue( listItem, pageName, targets[targetName], targetName, page, remainingTargets );
 92+ );
9993 }
10094 else {
101 - listItem.text( msgReplace( 'push-special-item-completed', pageName ) );
 95+ listItem.text( mediaWiki.msg( 'push-special-item-completed', pageName ) );
10296 listItem.css( 'color', 'darkgray' );
103 - initiateNextPush();
 97+ initiateNextPush();
10498 }
10599 }
106100
107 - function getEditTokenAndContinue( listItem, pageName, targetUrl, targetName, page, remainingTargets ) {
108 - listItem.text( msgReplace( 'push-special-item-pushing-to', pageName, targetName ) );
109 -
110 - $.getJSON(
111 - targetUrl + '/api.php',
112 - {
113 - 'action': 'query',
114 - 'format': 'json',
115 - 'intoken': 'edit',
116 - 'prop': 'info',
117 - 'titles': page.title,
118 - },
119 - function( data ) {
120 - if ( data.error ) {
121 - handleError( listItem, pageName, data.error );
122 - }
123 - else {
124 - for (first in data.query.pages) break;
125 - doPush( listItem, pageName, targetUrl, page, data.query.pages[first].edittoken, remainingTargets );
126 - }
127 - }
128 - );
129 - }
130 -
131 - function doPush( listItem, pageName, targetUrl, page, token, remainingTargets ) {
132 - var summary = msgReplace(
133 - 'push-import-revision-message',
134 - $('#siteName').attr('value'),
135 - page.revisions[0].user,
136 - page.revisions[0].comment ? msgReplace( 'push-import-revision-comment', page.revisions[0].comment ) : ''
137 - );
138 -
139 - $.post(
140 - targetUrl + '/api.php',
141 - {
142 - 'action': 'edit',
143 - 'format': 'json',
144 - 'token': token,
145 - 'title': page.title,
146 - 'summary': summary,
147 - 'text': page.revisions[0].*
148 - },
149 - function( data ) {
150 - if ( data.error ) {
151 - handleError( listItem, pageName, data.error );
152 - }
153 - else {
154 - initiateEdit( listItem, pageName, page, remainingTargets );
155 - }
156 - }
157 - );
 101+ function showCompletion() {
 102+ resultList.append( $( '<li />' ).append( $( '<b />' ).text( mediaWiki.msg( 'push-special-push-done' ) ) ) );
158103 }
159104
160105 function handleError( listItem, pageName, error ) {
161 - listItem.text( msgReplace( 'push-special-item-failed', pageName, error.info ) );
 106+ listItem.text( mediaWiki.msg( 'push-special-item-failed', pageName, error.info ) );
 107+ listItem.css( 'color', 'darkred' );
162108 initiateNextPush();
163109 }
164110
165 - function msgReplace() {
166 - message = mediaWiki.msg( arguments[0] );
167 - for ( var i = arguments.length - 1; i > 0; i-- ) {
168 - message = message.replace( '$' + i, arguments[i] );
169 - }
170 - return message;
171 - }
172 -
173111 } ); })(jQuery);
\ No newline at end of file
Index: trunk/extensions/Push/Push.php
@@ -71,8 +71,7 @@
7272 'push-import-revision-comment',
7373 'push-button-text',
7474 'push-button-all',
75 - 'push-special-item-getting',
76 - 'push-special-item-pushing-to',
 75+ 'push-special-item-pushing',
7776 'push-special-item-completed',
7877 'push-special-item-failed',
7978 'push-special-push-done',
Index: trunk/extensions/Push/Push_Settings.php
@@ -28,3 +28,4 @@
2929 $egPushShowTab = false;
3030
3131 $egPushBulkWorkers = 3;
 32+$egPushBatchSize = 3;
Index: trunk/extensions/Push/api/ApiPush.php
@@ -64,7 +64,8 @@
6565
6666 $revision = false;
6767
68 - if ( array_key_exists( 'query', $response )
 68+ if ( $response !== false
 69+ && array_key_exists( 'query', $response )
6970 && array_key_exists( 'pages', $response['query'] )
7071 && count( $response['query']['pages'] ) > 0 ) {
7172
@@ -78,11 +79,11 @@
7980 $revision = $response['query']['pages'][$first]['revisions'][0];
8081 }
8182 else {
82 - // TODO
 83+ $this->dieUsage( wfMsg( 'push-special-err-pageget-failed' ), 'page-get-failed' );
8384 }
8485 }
8586 else {
86 - // TODO
 87+ $this->dieUsage( wfMsg( 'push-special-err-pageget-failed' ), 'page-get-failed' );
8788 }
8889
8990 return $revision;
@@ -138,11 +139,12 @@
139140 foreach ( $requestData as $key => $value ) {
140141 $parts[] = $key . '=' . urlencode( $value );
141142 }
142 -
 143+
143144 $response = FormatJson::decode( Http::get( $target . '?' . implode( '&', $parts ) ) );
144145 $token = false;
145146
146 - if ( property_exists( $response, 'query' )
 147+ if ( !is_null( $response )
 148+ && property_exists( $response, 'query' )
147149 && property_exists( $response->query, 'pages' )
148150 && count( $response->query->pages ) > 0 ) {
149151
@@ -154,12 +156,15 @@
155157 if ( property_exists( $response->query->pages->$first, 'edittoken' ) ) {
156158 $token = $response->query->pages->$first->edittoken;
157159 }
 160+ elseif ( !is_null( $response ) && property_exists( $response, 'query' ) && property_exists( $response->query, 'error' ) ) {
 161+ $this->dieUsage( $response->query->error->message, 'token-request-failed' );
 162+ }
158163 else {
159 - // TODO
160 - }
 164+ $this->dieUsage( wfMsg( 'push-special-err-token-failed' ), 'token-request-failed' );
 165+ }
161166 }
162167 else {
163 - // TODO
 168+ $this->dieUsage( wfMsg( 'push-special-err-token-failed' ), 'token-request-failed' );
164169 }
165170
166171 return $token;
@@ -196,20 +201,25 @@
197202 );
198203
199204 $response = Http::post( $target, array( 'postData' => $requestData ) );
200 -//var_dump($response);exit;
201 - // TODO
 205+
 206+ if ( $response !== false ) {
 207+ die( $response );
 208+ }
 209+ else {
 210+ $this->dieUsage( wfMsg( 'push-special-err-push-failed' ), 'page-push-failed' );
 211+ }
202212 }
203213
204214 public function getAllowedParams() {
205215 return array(
206216 'page' => array(
207217 ApiBase::PARAM_TYPE => 'string',
208 - ApiBase::PARAM_REQUIRED => true,
 218+ //ApiBase::PARAM_REQUIRED => true,
209219 ),
210220 'targets' => array(
211221 ApiBase::PARAM_TYPE => 'string',
212222 ApiBase::PARAM_ISMULTI => true,
213 - ApiBase::PARAM_REQUIRED => true,
 223+ //ApiBase::PARAM_REQUIRED => true,
214224 ),
215225 );
216226 }

Status & tagging log