$wgVisualEditorParsoidHTTPProxy ); } else { return array( 'noProxy' => true ); } } protected function requestParsoid( $method, $title, $params ) { global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix, $wgVisualEditorParsoidTimeout, $wgVisualEditorParsoidForwardCookies; $url = $wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix . '/' . urlencode( $title->getPrefixedDBkey() ); $data = array_merge( $this->getProxyConf(), array( 'method' => $method, 'timeout' => $wgVisualEditorParsoidTimeout, ) ); if ( $method === 'POST' ) { $data['postData'] = $params; } else { $url = wfAppendQuery( $url, $params ); } $req = MWHttpRequest::factory( $url, $data ); // Forward cookies, but only if configured to do so and if there are read restrictions if ( $wgVisualEditorParsoidForwardCookies && !User::isEveryoneAllowed( 'read' ) ) { $req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) ); } $status = $req->execute(); if ( $status->isOK() ) { // 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. $xCache = $req->getResponseHeader( 'X-Cache' ); if ( is_string( $xCache ) && strpos( $xCache, 'hit' ) !== false ) { $xpp = 'cached-response=true'; } else { $xpp = $req->getResponseHeader( 'X-Parsoid-Performance' ); } if ( $xpp !== null ) { $resp = $this->getRequest()->response(); $resp->header( 'X-Parsoid-Performance: ' . $xpp ); } } elseif ( $status->isGood() ) { $this->dieUsage( $req->getContent(), 'parsoidserver-http-' . $req->getStatus() ); } elseif ( $errors = $status->getErrorsByType( 'error' ) ) { $error = $errors[0]; $code = $error['message']; if ( count( $error['params'] ) ) { $message = $error['params'][0]; } else { $message = 'MWHttpRequest error'; } $this->dieUsage( $message, 'parsoidserver-' . $code ); } // TODO pass through X-Parsoid-Performance header, merge with getHTML above return $req->getContent(); } protected function getHTML( $title, $parserParams ) { global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix, $wgVisualEditorParsoidTimeout, $wgVisualEditorParsoidForwardCookies; $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', $title, $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, $wgVisualEditorSerializationCacheTimeout; $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, $wgVisualEditorSerializationCacheTimeout ); 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', $title, array( 'content' => $html, 'oldid' => $parserParams['oldid'], ) ); } protected function parseWikitextFragment( $title, $wikitext ) { return $this->requestParsoid( 'POST', $title, array( 'wt' => $wikitext, 'body' => 1, ) ); } protected function parseWikitext( $title ) { $apiParams = array( 'action' => 'parse', 'page' => $title->getPrefixedDBkey(), 'prop' => 'text|revid|categorieshtml', ); $api = new ApiMain( new DerivativeRequest( $this->getRequest(), $apiParams, false // was posted? ), true // enable write? ); $api->execute(); $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(); if ( $content === false || ( strlen( $content ) && $revision === null ) ) { return false; } return array( 'content' => $content, 'categorieshtml' => $categorieshtml, 'basetimestamp' => $timestamp, 'starttimestamp' => wfTimestampNow() ); } protected function diffWikitext( $title, $wikitext ) { $apiParams = array( 'action' => 'query', 'prop' => 'revisions', 'titles' => $title->getPrefixedDBkey(), 'rvdifftotext' => ContentHandler::makeContent( $wikitext, $title ) ->preSaveTransform( $title, $this->getUser(), WikiPage::factory( $title )->makeParserOptions( $this->getContext() ) ) ->serialize( 'text/x-wiki' ) ); $api = new ApiMain( new DerivativeRequest( $this->getRequest(), $apiParams, false // was posted? ), false // enable write? ); $api->execute(); $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, wfMessage( 'currentrev' )->parse(), wfMessage( '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(); $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() { global $wgVisualEditorNamespaces; $user = $this->getUser(); $params = $this->extractRequestParams(); $page = Title::newFromText( $params['page'] ); if ( !$page ) { $this->dieUsageMsg( 'invalidtitle', $params['page'] ); } if ( !in_array( $page->getNamespace(), $wgVisualEditorNamespaces ) ) { $this->dieUsage( "VisualEditor is not enabled in namespace " . $page->getNamespace(), 'novenamespace' ); } $parserParams = array(); if ( isset( $params['oldid'] ) ) { $parserParams['oldid'] = $params['oldid']; } 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' )->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(); } } if ( $page->getNamespace() != NS_MEDIAWIKI ) { // Page protected from editing if ( $page->isProtected( 'edit' ) ) { # Is the title semi-protected? if ( $page->isSemiProtected() ) { $noticeMsg = 'semiprotectedpagewarning'; } else { # 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'] ) ) { $notice = $this->msg( 'cascadeprotectedwarning' )->parseAsBlock() . '