veConfig = $config; $this->serviceClient = new VirtualRESTServiceClient( new MultiHttpClient( array() ) ); $this->serviceClient->mount( '/parsoid/', $this->getVRSObject() ); } /** * Creates the virtual REST service object to be used in VE's API calls. The * method determines whether to instantiate a ParsoidVirtualRESTService or a * RestbaseVirtualRESTService object based on configuration directives: if * $wgVirtualRestConfig['modules']['restbase'] is defined, RESTBase is chosen, * otherwise Parsoid is used (either by using the MW Core config, or the * VE-local one). * * @return VirtualRESTService the VirtualRESTService object to use */ private function getVRSObject() { // the params array to create the service object with $params = array(); // the VRS class to use, defaults to Parsoid $class = 'ParsoidVirtualRESTService'; $config = $this->veConfig; // the global virtual rest service config object, if any $vrs = $this->getConfig()->get( 'VirtualRestConfig' ); if ( isset( $vrs['modules'] ) && isset( $vrs['modules']['restbase'] ) ) { // if restbase is available, use it $params = $vrs['modules']['restbase']; $class = 'RestbaseVirtualRESTService'; // remove once VE generates restbase paths $params['parsoidCompat'] = true; } elseif ( isset( $vrs['modules'] ) && isset( $vrs['modules']['parsoid'] ) ) { // there's a global parsoid config, use it next $params = $vrs['modules']['parsoid']; } else { // no global modules defined, fall back to old defaults $params = array( 'URL' => $config->get( 'VisualEditorParsoidURL' ), 'prefix' => $config->get( 'VisualEditorParsoidPrefix' ), 'timeout' => $config->get( 'VisualEditorParsoidTimeout' ), 'HTTPProxy' => $config->get( 'VisualEditorParsoidHTTPProxy' ), 'forwardCookies' => $config->get( 'VisualEditorParsoidForwardCookies' ) ); } // merge the global and service-specific params if ( isset( $vrs['global'] ) ) { $params = array_merge( $vrs['global'], $params ); } // set up cookie forwarding if ( $params['forwardCookies'] && !User::isEveryoneAllowed( 'read' ) ) { $params['forwardCookies'] = RequestContext::getMain()->getRequest()->getHeader( 'Cookie' ); } else { $params['forwardCookies'] = false; } // create the VRS object and return it return new $class( $params ); } private function requestParsoid( $method, $path, $params ) { $request = array( 'method' => $method, 'url' => '/parsoid/local/v1/' . $path ); if ( $method === 'GET' ) { $request['query'] = $params; } else { $request['body'] = $params; } $response = $this->serviceClient->run( $request ); if ( $response['code'] === 200 && $response['error'] === "" ) { // Pass thru performance data from Parsoid to the client, unless the response was // served directly from Varnish, in which case discard the value of the XPP header // and use it to declare the cache hit instead. $headers = $response['headers']; if ( isset( $headers['x-cache'] ) && strpos( $headers['x-cache'], 'hit' ) !== false ) { $xpp = 'cached-response=true'; } else { $xpp = $headers['x-parsoid-performance']; } if ( $xpp !== null ) { $resp = $this->getRequest()->response(); $resp->header( 'X-Parsoid-Performance: ' . $xpp ); } } elseif ( $response['error'] !== '' ) { $this->dieUsage( 'parsoidserver-http-error: ' . $response['error'], $response['error'] ); } else { // error null, code not 200 $this->dieUsage( 'parsoidserver-http: HTTP ' . $response['code'], $response['code'] ); } return $response['body']; } protected function getHTML( $title, $parserParams ) { $restoring = false; if ( $title->exists() ) { $latestRevision = Revision::newFromTitle( $title ); if ( $latestRevision === null ) { return false; } $revision = null; if ( !isset( $parserParams['oldid'] ) || $parserParams['oldid'] === 0 ) { $parserParams['oldid'] = $latestRevision->getId(); $revision = $latestRevision; } else { $revision = Revision::newFromId( $parserParams['oldid'] ); if ( $revision === null ) { return false; } } $restoring = $revision && !$revision->isCurrent(); $oldid = $parserParams['oldid']; $content = $this->requestParsoid( 'GET', 'page/' . urlencode( $title->getPrefixedDBkey() ) . '/html', $parserParams ); if ( $content === false ) { return false; } $timestamp = $latestRevision->getTimestamp(); } else { $content = ''; $timestamp = wfTimestampNow(); $oldid = 0; } return array( 'result' => array( 'content' => $content, 'basetimestamp' => $timestamp, 'starttimestamp' => wfTimestampNow(), 'oldid' => $oldid, ), 'restoring' => $restoring, ); } protected function storeInSerializationCache( $title, $oldid, $html ) { global $wgMemc; $content = $this->postHTML( $title, $html, array( 'oldid' => $oldid ) ); if ( $content === false ) { return false; } $hash = md5( $content ); $key = wfMemcKey( 'visualeditor', 'serialization', $hash ); $wgMemc->set( $key, $content, $this->veConfig->get( 'VisualEditorSerializationCacheTimeout' ) ); return $hash; } protected function trySerializationCache( $hash ) { global $wgMemc; $key = wfMemcKey( 'visualeditor', 'serialization', $hash ); return $wgMemc->get( $key ); } protected function postHTML( $title, $html, $parserParams ) { if ( $parserParams['oldid'] === 0 ) { $parserParams['oldid'] = ''; } return $this->requestParsoid( 'POST', 'transform/html/to/wikitext/' . urlencode( $title->getPrefixedDBkey() ), array( 'html' => $html, 'oldid' => $parserParams['oldid'], ) ); } protected function pstWikitext( $title, $wikitext ) { return ContentHandler::makeContent( $wikitext, $title ) ->preSaveTransform( $title, $this->getUser(), WikiPage::factory( $title )->makeParserOptions( $this->getContext() ) ) ->serialize( 'text/x-wiki' ); } protected function parseWikitextFragment( $title, $wikitext ) { return $this->requestParsoid( 'POST', 'transform/wikitext/to/html/' . urlencode( $title->getPrefixedDBkey() ), array( 'wikitext' => $wikitext, 'body' => 1, ) ); } protected function parseWikitext( $title ) { $apiParams = array( 'action' => 'parse', 'page' => $title->getPrefixedDBkey(), 'prop' => 'text|revid|categorieshtml|displaytitle', ); $api = new ApiMain( new DerivativeRequest( $this->getRequest(), $apiParams, false // was posted? ), true // enable write? ); $api->execute(); if ( defined( 'ApiResult::META_CONTENT' ) ) { $result = $api->getResult()->getResultData(); // Transform content nodes to '*' $result = ApiResult::transformForBC( $result ); // Remove any metadata keys from the links array $result = ApiResult::removeMetadata( $result ); } else { $result = $api->getResultData(); } $content = isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false; $categorieshtml = isset( $result['parse']['categorieshtml']['*'] ) ? $result['parse']['categorieshtml']['*'] : false; $links = isset( $result['parse']['links'] ) ? $result['parse']['links'] : array(); $revision = Revision::newFromId( $result['parse']['revid'] ); $timestamp = $revision ? $revision->getTimestamp() : wfTimestampNow(); $displaytitle = isset( $result['parse']['displaytitle'] ) ? $result['parse']['displaytitle'] : false; if ( $content === false || ( strlen( $content ) && $revision === null ) ) { return false; } if ( $displaytitle !== false ) { // Escape entities as in OutputPage::setPageTitle() $displaytitle = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $displaytitle ) ); } return array( 'content' => $content, 'categorieshtml' => $categorieshtml, 'basetimestamp' => $timestamp, 'starttimestamp' => wfTimestampNow(), 'displayTitleHtml' => $displaytitle ); } protected function diffWikitext( $title, $wikitext ) { $apiParams = array( 'action' => 'query', 'prop' => 'revisions', 'titles' => $title->getPrefixedDBkey(), 'rvdifftotext' => $this->pstWikitext( $title, $wikitext ) ); $api = new ApiMain( new DerivativeRequest( $this->getRequest(), $apiParams, false // was posted? ), false // enable write? ); $api->execute(); if ( defined( 'ApiResult::META_CONTENT' ) ) { $result = $api->getResult()->getResultData(); // Transform content nodes to '*' $result = ApiResult::transformForBC( $result ); } else { $result = $api->getResultData(); } if ( !isset( $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'] ) ) { return array( 'result' => 'fail' ); } $diffRows = $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*']; if ( $diffRows !== '' ) { $context = new DerivativeContext( $this->getContext() ); $context->setTitle( $title ); $engine = new DifferenceEngine( $context ); return array( 'result' => 'success', 'diff' => $engine->addHeader( $diffRows, $context->msg( 'currentrev' )->parse(), $context->msg( 'yourtext' )->parse() ) ); } else { return array( 'result' => 'nochanges' ); } } protected function getLangLinks( $title ) { $apiParams = array( 'action' => 'query', 'prop' => 'langlinks', 'lllimit' => 500, 'titles' => $title->getPrefixedDBkey(), 'indexpageids' => 1, ); $api = new ApiMain( new DerivativeRequest( $this->getRequest(), $apiParams, false // was posted? ), true // enable write? ); $api->execute(); if ( defined( 'ApiResult::META_CONTENT' ) ) { $result = $api->getResult()->getResultData(); // Remove any metadata keys from the langlinks array $result = ApiResult::removeMetadata( $result ); } else { $result = $api->getResultData(); } if ( !isset( $result['query']['pages'][$title->getArticleID()]['langlinks'] ) ) { return false; } $langlinks = $result['query']['pages'][$title->getArticleID()]['langlinks']; $langnames = Language::fetchLanguageNames(); foreach ( $langlinks as $i => $lang ) { $langlinks[$i]['langname'] = $langnames[$langlinks[$i]['lang']]; } return $langlinks; } public function execute() { $user = $this->getUser(); $params = $this->extractRequestParams(); $page = Title::newFromText( $params['page'] ); if ( !$page ) { $this->dieUsageMsg( 'invalidtitle', $params['page'] ); } if ( !in_array( $page->getNamespace(), $this->veConfig->get( 'VisualEditorNamespaces' ) ) ) { $this->dieUsage( "VisualEditor is not enabled in namespace " . $page->getNamespace(), 'novenamespace' ); } $parserParams = array(); if ( isset( $params['oldid'] ) ) { $parserParams['oldid'] = $params['oldid']; } $html = $params['html']; if ( substr( $html, 0, 11 ) === 'rawdeflate,' ) { $html = gzinflate( base64_decode( substr( $html, 11 ) ) ); } wfDebugLog( 'visualeditor', "called on '$page' with paction: '{$params['paction']}'" ); switch ( $params['paction'] ) { case 'parse': $parsed = $this->getHTML( $page, $parserParams ); // Dirty hack to provide the correct context for edit notices global $wgTitle; // FIXME NOOOOOOOOES $wgTitle = $page; RequestContext::getMain()->setTitle( $page ); $notices = $page->getEditNotices(); if ( $user->isAnon() ) { $notices[] = $this->msg( 'anoneditwarning', // Log-in link '{{fullurl:Special:UserLogin|returnto={{FULLPAGENAMEE}}}}', // Sign-up link '{{fullurl:Special:UserLogin/signup|returnto={{FULLPAGENAMEE}}}}' )->parseAsBlock(); } if ( $parsed && $parsed['restoring'] ) { $notices[] = $this->msg( 'editingold' )->parseAsBlock(); } // Creating new page if ( !$page->exists() ) { $notices[] = $this->msg( $user->isLoggedIn() ? 'newarticletext' : 'newarticletextanon', Skin::makeInternalOrExternalUrl( $this->msg( 'helppage' )->inContentLanguage()->text() ) )->parseAsBlock(); // Page protected from creation if ( $page->getRestrictions( 'create' ) ) { $notices[] = $this->msg( 'titleprotectedwarning' )->parseAsBlock(); } } // Look at protection status to set up notices + surface class(es) $protectedClasses = array(); if ( MWNamespace::getRestrictionLevels( $page->getNamespace() ) !== array( '' ) ) { // Page protected from editing if ( $page->isProtected( 'edit' ) ) { # Is the title semi-protected? if ( $page->isSemiProtected() ) { $protectedClasses[] = 'mw-textarea-sprotected'; $noticeMsg = 'semiprotectedpagewarning'; } else { $protectedClasses[] = 'mw-textarea-protected'; # Then it must be protected based on static groups (regular) $noticeMsg = 'protectedpagewarning'; } $notices[] = $this->msg( $noticeMsg )->parseAsBlock() . $this->getLastLogEntry( $page, 'protect' ); } // Deal with cascading edit protection list( $sources, $restrictions ) = $page->getCascadeProtectionSources(); if ( isset( $restrictions['edit'] ) ) { $protectedClasses[] = ' mw-textarea-cprotected'; $notice = $this->msg( 'cascadeprotectedwarning' )->parseAsBlock() . '