ParsoidVirtualRESTService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Virtual HTTP service client for Parsoid
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. */
  20. /**
  21. * Virtual REST service for Parsoid
  22. * @since 1.25
  23. */
  24. class ParsoidVirtualRESTService extends VirtualRESTService {
  25. /**
  26. * Example Parsoid v3 requests:
  27. * GET /local/v3/page/html/$title/{$revision}
  28. * * $revision is optional
  29. * POST /local/v3/transform/html/to/wikitext/{$title}{/$revision}
  30. * * body: array( 'html' => ... )
  31. * * $title and $revision are optional
  32. * POST /local/v3/transform/wikitext/to/html/{$title}{/$revision}
  33. * * body: array( 'wikitext' => ... ) or array( 'wikitext' => ..., 'body_only' => true/false )
  34. * * $title is optional
  35. * * $revision is optional
  36. *
  37. * @param array $params Key/value map
  38. * - url : Parsoid server URL
  39. * - domain : Wiki domain to use
  40. * - timeout : Parsoid timeout (optional)
  41. * - forwardCookies : Cookies to forward to Parsoid, or false. (optional)
  42. * - HTTPProxy : Parsoid HTTP proxy (optional)
  43. * - restbaseCompat : whether to parse URL as if they were meant for RESTBase
  44. * boolean (optional)
  45. */
  46. public function __construct( array $params ) {
  47. // for backwards compatibility:
  48. if ( isset( $params['URL'] ) ) {
  49. $params['url'] = $params['URL'];
  50. unset( $params['URL'] );
  51. }
  52. // set up defaults and merge them with the given params
  53. $mparams = array_merge( [
  54. 'name' => 'parsoid',
  55. 'url' => 'http://localhost:8000/',
  56. 'prefix' => 'localhost',
  57. 'domain' => 'localhost',
  58. 'timeout' => null,
  59. 'forwardCookies' => false,
  60. 'HTTPProxy' => null,
  61. ], $params );
  62. // Ensure that the url parameter has a trailing slash.
  63. if ( substr( $mparams['url'], -1 ) !== '/' ) {
  64. $mparams['url'] .= '/';
  65. }
  66. // Ensure the correct domain format: strip protocol, port,
  67. // and trailing slash if present. This lets us use
  68. // $wgCanonicalServer as a default value, which is very convenient.
  69. $mparams['domain'] = preg_replace(
  70. '/^(https?:\/\/)?([^\/:]+?)(:\d+)?\/?$/',
  71. '$2',
  72. $mparams['domain']
  73. );
  74. parent::__construct( $mparams );
  75. }
  76. public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
  77. $result = [];
  78. foreach ( $reqs as $key => $req ) {
  79. $parts = explode( '/', $req['url'] );
  80. list(
  81. $targetWiki, // 'local'
  82. $version, // 'v3' ('v1' for restbase compatibility)
  83. $reqType, // 'page' or 'transform'
  84. $format, // 'html' or 'wikitext'
  85. // $title (optional)
  86. // $revision (optional)
  87. ) = $parts;
  88. if ( isset( $this->params['restbaseCompat'] ) && $this->params['restbaseCompat'] ) {
  89. if ( $version !== 'v1' ) {
  90. throw new Exception( "Only RESTBase v1 API is supported." );
  91. }
  92. # Map RESTBase v1 API to Parsoid v3 API (pretty easy)
  93. $req['url'] = preg_replace( '#^local/v1/#', 'local/v3/', $req['url'] );
  94. } elseif ( $version !== 'v3' ) {
  95. throw new Exception( "Only Parsoid v3 API is supported." );
  96. }
  97. if ( $targetWiki !== 'local' ) {
  98. throw new Exception( "Only 'local' target wiki is currently supported" );
  99. }
  100. if ( $reqType !== 'page' && $reqType !== 'transform' ) {
  101. throw new Exception( "Request action must be either 'page' or 'transform'" );
  102. }
  103. if ( $format !== 'html' && $format !== 'wikitext' ) {
  104. throw new Exception( "Request format must be either 'html' or 'wt'" );
  105. }
  106. // replace /local/ with the current domain
  107. $req['url'] = preg_replace( '#^local/#', $this->params['domain'] . '/', $req['url'] );
  108. // and prefix it with the service URL
  109. $req['url'] = $this->params['url'] . $req['url'];
  110. // set the appropriate proxy, timeout and headers
  111. if ( $this->params['HTTPProxy'] ) {
  112. $req['proxy'] = $this->params['HTTPProxy'];
  113. }
  114. if ( $this->params['timeout'] != null ) {
  115. $req['reqTimeout'] = $this->params['timeout'];
  116. }
  117. if ( $this->params['forwardCookies'] ) {
  118. $req['headers']['Cookie'] = $this->params['forwardCookies'];
  119. }
  120. $result[$key] = $req;
  121. }
  122. return $result;
  123. }
  124. }