-
Notifications
You must be signed in to change notification settings - Fork 1
/
RenderingAPI.php
381 lines (345 loc) · 10.2 KB
/
RenderingAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Base class for API that interacts with book rendering service
*/
abstract class CollectionRenderingAPI {
/** @var CollectionRenderingAPI */
private static $inst;
/** @var string */
protected $writer;
public static function instance( $writer = false ) {
if ( !self::$inst ) {
self::$inst = new MWServeRenderingAPI( $writer );
}
return self::$inst;
}
/**
* @param string|bool $writer: Writer or false if none specified/needed
*/
public function __construct( $writer ) {
$this->writer = $writer;
}
/**
* When overridden in derived class, performs a request to the service
*
* @param string $command
* @param array $params
* @return CollectionAPIResult
*/
protected abstract function makeRequest( $command, array $params );
/**
* @return String expanded $wgScriptPath to work around T39868
* @private
*/
function getBaseURL() {
global $wgScriptPath;
$scriptPath = $wgScriptPath ? $wgScriptPath : "/";
return wfExpandUrl( $scriptPath, PROTO_CANONICAL );
}
/**
* Requests a collection to be rendered
* @param array $collection
*
* @return CollectionAPIResult
*/
public function render( $collection ) {
return $this->doRender( array(
'metabook' => $this->buildJSONCollection( $collection ),
)
);
}
/**
* Requests a queued collection to be immediately rendered
*
* @param $collectionId
* @return CollectionAPIResult
*/
public function forceRender( $collectionId ) {
return $this->doRender( array(
'collection_id' => $collectionId,
'force_render' => true
)
);
}
protected function doRender( array $params ) {
global $wgContLang;
$params['base_url'] = $this->getBaseURL();
$params['script_extension'] = '.php';
$params['language'] = $wgContLang->getCode();
return $this->makeRequest( 'render', $params );
}
/**
* Requests the service to create a collection package and send it to an external server
* e.g. for printing
*
* @param $collection
* @param $url
*
* @return CollectionAPIResult
*/
public function postZip( $collection, $url ) {
return $this->makeRequest( 'zip_post',
array(
'metabook' => $this->buildJSONCollection( $collection ),
'base_url' => $this->getBaseURL(),
'script_extension' => '.php',
'pod_api_url' => $url,
)
);
}
/**
* Returns inromation about a collection's rendering status
*
* @param $collectionId
* @return CollectionAPIResult
*/
public function getRenderStatus( $collectionId ) {
return $this->makeRequest( 'render_status',
array(
'collection_id' => $collectionId,
),
'CollectionStatusAPIResult'
);
}
/**
* Requests a download of rendered collection
*
* @param $collectionId
* @return CollectionAPIResult
*/
public function download( $collectionId ) {
return $this->makeRequest( 'download',
array(
'collection_id' => $collectionId,
)
);
}
/**
* @return array
*/
protected function getLicenseInfos() {
global $wgCollectionLicenseName, $wgCollectionLicenseURL, $wgRightsIcon;
global $wgRightsPage, $wgRightsText, $wgRightsUrl;
$licenseInfo = array(
'type' => 'license',
);
$fromMsg = wfMessage( 'coll-license_url' )->inContentLanguage();
if ( !$fromMsg->isDisabled() ) {
$licenseInfo['mw_license_url'] = $fromMsg->text();
return array( $licenseInfo );
}
if ( $wgCollectionLicenseName ) {
$licenseInfo['name'] = $wgCollectionLicenseName;
} else {
$licenseInfo['name'] = wfMessage( 'coll-license' )->inContentLanguage()->text();
}
if ( $wgCollectionLicenseURL ) {
$licenseInfo['mw_license_url'] = $wgCollectionLicenseURL;
} else {
$licenseInfo['mw_rights_icon'] = $wgRightsIcon;
$licenseInfo['mw_rights_page'] = $wgRightsPage;
$licenseInfo['mw_rights_url'] = $wgRightsUrl;
$licenseInfo['mw_rights_text'] = $wgRightsText;
}
return array( $licenseInfo );
}
/**
* @param $collection array
* @return string
*/
protected function buildJSONCollection( $collection ) {
$result = array(
'type' => 'collection',
'licenses' => $this->getLicenseInfos()
);
if ( isset( $collection['title'] ) ) {
$result['title'] = $collection['title'];
}
if ( isset( $collection['subtitle'] ) ) {
$result['subtitle'] = $collection['subtitle'];
}
if ( isset( $collection['settings'] ) ) {
foreach ( $collection['settings'] as $key => $val ) {
$result[$key] = $val;
}
//Add text for the footer in appropriate language
$result['footerLeft'] = wfMessage('coll-setting-footer-text-left')->
inContentLanguage()->plain();
$result['footerMiddle'] = wfMessage('coll-setting-footer-text-middle')->
inContentLanguage()->plain();
$result['footerRight'] = wfMessage('coll-setting-footer-text-right')->
inContentLanguage()->plain();
// compatibility with old mw-serve
$result['settings'] = $collection['settings'];
}
$items = array();
if ( isset( $collection['items'] ) ) {
$currentChapter = null;
foreach ( $collection['items'] as $item ) {
if ( $item['type'] == 'article' ) {
if ( is_null( $currentChapter ) ) {
$items[] = $item;
} else {
$currentChapter['items'][] = $item;
}
} elseif ( $item['type'] == 'chapter' ) {
if ( !is_null( $currentChapter ) ) {
$items[] = $currentChapter;
}
$currentChapter = $item;
}
}
if ( !is_null( $currentChapter ) ) {
$items[] = $currentChapter;
}
}
$result['items'] = $items;
$result['wikis'] = array(
array(
'type' => 'wikiconf',
'baseurl' => $this->getBaseURL(),
'script_extension' => '.php',
'format' => 'nuwiki',
),
);
// Prefer VRS configuration if present.
$context = RequestContext::getMain();
$vrs = $context->getConfig()->get( 'VirtualRestConfig' );
if ( isset( $vrs['modules'] ) && isset( $vrs['modules']['restbase'] ) ) {
// if restbase is available, use it
$params = $vrs['modules']['restbase'];
$domain = preg_replace(
'/^(https?:\/\/)?([^\/:]+?)(\/|:\d+\/?)?$/',
'$2',
$params['domain']
);
$url = preg_replace(
'#/?$#',
'/' . $domain . '/v1/',
$params['url']
);
for ( $i = 0; $i < count( $result['wikis'] ); $i++ ) {
$result['wikis'][$i]['restbase1'] = $url;
}
} elseif ( isset( $vrs['modules'] ) && isset( $vrs['modules']['parsoid'] ) ) {
// there's a global parsoid config, use it next
$params = $vrs['modules']['parsoid'];
$domain = preg_replace(
'/^(https?:\/\/)?([^\/:]+?)(\/|:\d+\/?)?$/',
'$2',
$params['domain']
);
for ( $i = 0; $i < count( $result['wikis'] ); $i++ ) {
$result['wikis'][$i]['parsoid'] = $params['url'];
$result['wikis'][$i]['prefix'] = $params['prefix'];
$result['wikis'][$i]['domain'] = $domain;
}
} elseif ( class_exists( 'VisualEditorHooks' ) ) {
// fall back to Visual Editor configuration globals
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix,
$wgVisualEditorParsoidDomain, $wgVisualEditorRestbaseURL;
for ( $i = 0; $i < count( $result['wikis'] ); $i++ ) {
// Parsoid connection information
if ($wgVisualEditorParsoidURL) {
$result['wikis'][$i]['parsoid'] = $wgVisualEditorParsoidURL;
$result['wikis'][$i]['prefix'] = $wgVisualEditorParsoidPrefix;
$result['wikis'][$i]['domain'] = $wgVisualEditorParsoidDomain;
}
// RESTbase connection information
if ($wgVisualEditorRestbaseURL) {
// Strip the trailing "/page/html".
$restbase1 = preg_replace('|/page/html/?$|', '/', $wgVisualEditorRestbaseURL);
$result['wikis'][$i]['restbase1'] = $restbase1;
}
}
}
return FormatJson::encode( $result );
}
}
/**
* API for PediaPress' mw-serve
*/
class MWServeRenderingAPI extends CollectionRenderingAPI {
protected function makeRequest( $command, array $params ) {
global $wgCollectionMWServeURL, $wgCollectionMWServeCredentials, $wgCollectionFormatToServeURL, $wgCollectionCommandToServeURL;
$serveURL = $wgCollectionMWServeURL;
if ( $this->writer ) {
if ( isset( $wgCollectionFormatToServeURL[ $this->writer ] ) ) {
$serveURL = $wgCollectionFormatToServeURL[ $this->writer ];
}
$params['writer'] = $this->writer;
}
$params['command'] = $command;
if ( isset( $wgCollectionCommandToServeURL[ $command ] ) ) {
$serveURL = $wgCollectionCommandToServeURL[ $command ];
}
if ( $wgCollectionMWServeCredentials ) {
$params['login_credentials'] = $wgCollectionMWServeCredentials;
}
// If $serveURL has a | in it, we need to use a proxy.
list( $proxy, $serveURL ) = array_pad( explode( '|', $serveURL, 2 ), -2, '' );
$response = Http::post( $serveURL, array( 'postData' => $params, 'proxy' => $proxy ), __METHOD__ );
if ( $response === false ) {
wfDebugLog( 'collection', "Request to $serveURL resulted in error" );
}
return new CollectionAPIResult( $response );
}
}
/*
class NewRenderingAPI extends CollectionRenderingAPI {}
*/
/**
* A wrapper for data returned by the API
*/
class CollectionAPIResult {
/** @var array: Decoded JSON returned by server */
public $response = array();
/**
* @param string|null $data: Data returned by HTTP request
*/
public function __construct( $data ) {
if ( $data ) {
$this->response = FormatJson::decode( $data, true );
if ( $this->response === null ) {
wfDebugLog( 'collection', "Server returned bogus data: $data" );
$this->response = null;
}
if ( $this->isError() ) {
wfDebugLog( 'collection', "Server returned error: {$this->getError()}" );
}
}
}
/**
* Returns data for specified key(s)
* Has variable number of parameters, e.g. get( 'foo', 'bar', 'baz' )
* @param string $key
* @return mixed
*/
public function get( $key /*, ... */ ) {
$args = func_get_args();
$val = $this->response;
foreach( $args as $arg ) {
if ( !isset( $val[$arg] ) ) {
return '';
}
$val = $val[$arg];
}
return $val;
}
/**
* @return bool
*/
public function isError() {
return !$this->response
|| ( isset( $this->response['error'] ) && $this->response['error'] );
}
/**
* @return string: Internal (not user-facing) error description
*/
protected function getError() {
if ( isset ( $this->response['error'] ) ) {
return $this->response['error'];
}
return '(error unknown)';
}
}