public function setUp()
 {
     parent::setUp();
     // Check for Parsoid
     try {
         Utils::convert('html', 'wikitext', 'Foo', Title::newFromText('UTPage'));
     } catch (WikitextException $excep) {
         $this->markTestSkipped('Parsoid not enabled');
     }
 }
 /**
  * Test full roundtrip (wikitext -> html -> wikitext)
  *
  * It doesn't make sense to test only a specific path, since Parsoid's HTML
  * may change beyond our control & it doesn't really matter to us what
  * exactly the HTML looks like, as long as Parsoid is able to understand it.
  *
  * @dataProvider wikitextRoundtripProvider
  */
 public function testwikitextRoundtrip($message, $expect, Title $title)
 {
     // Check for Parsoid
     try {
         $html = Utils::convert('wikitext', 'html', $expect, $title);
         $wikitext = Utils::convert('html', 'wikitext', $html, $title);
         $this->assertEquals($expect, trim($wikitext), $message);
     } catch (WikitextException $excep) {
         $this->markTestSkipped('Parsoid not enabled');
     }
 }
 public function execute()
 {
     $params = $this->extractRequestParams();
     $page = $this->getTitleOrPageId($params);
     try {
         $content = Utils::convert($params['from'], $params['to'], $params['content'], $page->getTitle());
     } catch (WikitextException $e) {
         $code = $e->getErrorCode();
         $this->dieUsage($this->msg($code)->inContentLanguage()->useDatabase(false)->plain(), $code, $e->getStatusCode(), array('detail' => $e->getMessage()));
         return;
         // helps static analysis know execution does not continue past self::dieUsage
     }
     $result = array('format' => $params['to'], 'content' => $content);
     $this->getResult()->addValue(null, $this->getModuleName(), $result);
 }
 /**
  * @dataProvider referenceExtractorProvider
  */
 public function testReferenceExtractor($description, $wikitext, $expectedClass, $expectedType, $expectedTarget, $page = 'UTPage')
 {
     $referenceExtractor = Container::get('reference.extractor');
     $workflow = $this->getMock('Flow\\Model\\Workflow');
     $workflow->expects($this->any())->method('getId')->will($this->returnValue(UUID::create()));
     $workflow->expects($this->any())->method('getArticleTitle')->will($this->returnValue(Title::newMainPage()));
     $factory = new ReferenceFactory($workflow, 'foo', UUID::create());
     $reflMethod = new ReflectionMethod($referenceExtractor, 'extractReferences');
     $reflMethod->setAccessible(true);
     $reflProperty = new \ReflectionProperty($referenceExtractor, 'extractors');
     $reflProperty->setAccessible(true);
     $extractors = $reflProperty->getValue($referenceExtractor);
     $html = Utils::convert('wt', 'html', $wikitext, Title::newFromText($page));
     $result = $reflMethod->invoke($referenceExtractor, $factory, $extractors['post'], $html);
     $this->assertCount(1, $result, $html);
     $result = reset($result);
     $this->assertInstanceOf($expectedClass, $result, $description);
     $this->assertEquals($expectedType, $result->getType(), $description);
     $this->assertEquals($expectedTarget, $result->getTargetIdentifier(), $description);
 }
 /**
  * @dataProvider provideGetReferencesFromRevisionContent
  */
 public function testGetReferencesAfterRevisionInsert($content, $expectedReferences)
 {
     $content = Utils::convert('wikitext', 'html', $content, $this->workflow->getOwnerTitle());
     $revision = $this->generatePost(array('rev_content' => $content));
     // Save to storage to test if ReferenceRecorder listener picks this up
     $this->store($this->revision);
     $this->store($revision);
     $expectedReferences = $this->expandReferences($this->workflow, $revision, $expectedReferences);
     // References will be stored as linked from Topic:<id>
     $title = Title::newFromText($this->workflow->getId()->getAlphadecimal(), NS_TOPIC);
     // Retrieve references from storage
     $foundReferences = $this->updater->getReferencesForTitle($title);
     $this->assertReferenceListsEqual($expectedReferences, $foundReferences);
 }
 /**
  * Only extract templates to copy to Flow description.
  * Requires Parsoid, to reliably extract templates.
  *
  * @param string $content
  * @return string
  */
 protected function extractTemplates($content)
 {
     $content = Utils::convert('wikitext', 'html', $content, $this->title);
     $dom = Utils::createDOM($content);
     $xpath = new \DOMXPath($dom);
     $templates = $xpath->query('//*[@typeof="mw:Transclusion"]');
     $content = '';
     foreach ($templates as $template) {
         $content .= $dom->saveHTML($template) . "\n";
     }
     return Utils::convert('html', 'wikitext', $content, $this->title);
 }
 public function processPostCollection(array $context, array $collection, $indentLevel = 0)
 {
     $indent = str_repeat(':', $indentLevel);
     $output = '';
     foreach ($collection as $postId) {
         $revisionId = reset($context['posts'][$postId]);
         $revision = $context['revisions'][$revisionId];
         // Skip moderated posts
         if ($revision['isModerated']) {
             continue;
         }
         $user = User::newFromName($revision['author']['name'], false);
         $postId = Flow\Model\UUID::create($postId);
         $content = $revision['content']['content'];
         $contentFormat = $revision['content']['format'];
         if ($contentFormat !== 'wikitext') {
             $content = Utils::convert($contentFormat, 'wikitext', $content, $this->pageTitle);
         }
         $thisPost = $indent . trim($content) . ' ' . $this->getSignature($user, $postId->getTimestamp()) . "\n";
         if ($indentLevel > 0) {
             $thisPost = preg_replace("/\n+/", "\n", $thisPost);
         }
         $output .= str_replace("\n", "\n{$indent}", trim($thisPost)) . "\n";
         if (isset($revision['replies'])) {
             $output .= $this->processPostCollection($context, $revision['replies'], $indentLevel + 1);
         }
         if ($indentLevel == 0) {
             $output .= "\n";
         }
     }
     return $output;
 }
 /**
  * Should only be used for setting the initial content.  To set subsequent content
  * use self::setNextContent
  *
  * @param string $content
  * @param string $format wikitext|html
  * @param Title|null $title When null the related workflow will be lazy-loaded to locate the title
  * @throws DataModelException
  */
 protected function setContent($content, $format, Title $title = null)
 {
     if ($this->moderationState !== self::MODERATED_NONE) {
         throw new DataModelException('TODO: Cannot change content of restricted revision', 'process-data');
     }
     if ($this->content !== null) {
         throw new DataModelException('Updating content must use setNextContent method', 'process-data');
     }
     if (!$title) {
         $title = $this->getCollection()->getTitle();
     }
     // never trust incoming html - roundtrip to wikitext first
     if ($format !== 'wikitext') {
         $content = Utils::convert($format, 'wikitext', $content, $title);
         $format = 'wikitext';
     }
     // Run pre-save transform
     $content = ContentHandler::makeContent($content, $title, CONTENT_MODEL_WIKITEXT)->preSaveTransform($title, $this->getUser(), WikiPage::factory($title)->makeParserOptions($this->getUser()))->serialize('text/x-wiki');
     // Keep consistent with normal edit page, trim only trailing whitespaces
     $content = rtrim($content);
     $this->convertedContent = array($format => $content);
     // convert content to desired storage format
     $storageFormat = $this->getStorageFormat();
     if ($this->isFormatted() && $storageFormat !== $format) {
         $this->convertedContent[$storageFormat] = Utils::convert($format, $storageFormat, $content, $title);
     }
     $this->content = $this->decompressedContent = $this->convertedContent[$storageFormat];
     $this->contentUrl = null;
     // should this only remove a subset of flags?
     $this->flags = array_filter(explode(',', \Revision::compressRevisionText($this->content)));
     $this->flags[] = $storageFormat;
     $this->contentLength = mb_strlen($this->getContent('wikitext'));
 }