public function getData() { $db = wfGetDB(DB_MASTER); $conds = array('rt_page' => $this->handle->getTitle()->getArticleID(), 'rt_type' => RevTag::getType('tp:transver')); $options = array('ORDER BY' => 'rt_revision DESC'); $translationRevision = $db->selectField('revtag', 'rt_value', $conds, __METHOD__, $options); if ($translationRevision === false) { throw new TranslationHelperException("No definition revision recorded"); } $definitionTitle = Title::makeTitleSafe($this->handle->getTitle()->getNamespace(), $this->handle->getKey() . '/' . $this->group->getSourceLanguage()); if (!$definitionTitle || !$definitionTitle->exists()) { throw new TranslationHelperException("Definition page doesn't exist"); } // Using newFromId instead of newFromTitle, because the page might have been renamed $oldrev = Revision::newFromId($translationRevision); if (!$oldrev) { throw new TranslationHelperException("Old definition version doesn't exist anymore"); } $oldContent = $oldrev->getContent(); $newContent = $this->getDefinitionContent(); if (!$oldContent) { throw new TranslationHelperException("Old definition version doesn't exist anymore"); } if (!$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent) { throw new TranslationHelperException('Can only work on Wikitext content'); } if ($oldContent->equals($newContent)) { throw new TranslationHelperException('No changes'); } $diff = new DifferenceEngine($this->context); if (method_exists('DifferenceEngine', 'setTextLanguage')) { $diff->setTextLanguage($this->group->getSourceLanguage()); } $diff->setContent($oldContent, $newContent); $diff->setReducedLineNumbers(); $diff->showDiffStyle(); $html = $diff->getDiff($this->context->msg('tpt-diff-old')->escaped(), $this->context->msg('tpt-diff-new')->escaped()); return array('value_old' => $oldContent->getNativeData(), 'value_new' => $newContent->getNativeData(), 'revisionid_old' => $oldrev->getId(), 'revisionid_new' => $definitionTitle->getLatestRevId(), 'language' => $this->group->getSourceLanguage(), 'html' => $html); }
protected function getLastDiff() { // Shortcuts $title = $this->handle->getTitle(); $latestRevId = $title->getLatestRevID(); $previousRevId = $title->getPreviousRevisionID($latestRevId); $latestRev = Revision::newFromTitle($title, $latestRevId); $previousRev = Revision::newFromTitle($title, $previousRevId); $diffText = ''; if ($latestRev && $previousRev) { $latest = $latestRev->getText(); $previous = $previousRev->getText(); if ($previous !== $latest) { $diff = new DifferenceEngine(); if (method_exists('DifferenceEngine', 'setTextLanguage')) { $diff->setTextLanguage($this->getTargetLanguage()); } $diff->setText($previous, $latest); $diff->setReducedLineNumbers(); $diff->showDiffStyle(); $diffText = $diff->getDiff(false, false); } } if (!$latestRev) { return null; } global $wgUser; $user = $latestRev->getUserText(Revision::FOR_THIS_USER, $wgUser); $comment = $latestRev->getComment(); if ($diffText === '') { if (strval($comment) !== '') { $text = wfMessage('translate-dynagroup-byc', $user, $comment)->escaped(); } else { $text = wfMessage('translate-dynagroup-by', $user)->escaped(); } } else { if (strval($comment) !== '') { $text = wfMessage('translate-dynagroup-lastc', $user, $comment)->escaped(); } else { $text = wfMessage('translate-dynagroup-last', $user)->escaped(); } } return TranslateUtils::fieldset($text, $diffText, array('class' => 'mw-sp-translate-latestchange')); }
/** * Displays the sections and changes for the user to review * @param TranslatablePage $page * @param array $sections */ public function showPage(TranslatablePage $page, array $sections) { global $wgContLang; $out = $this->getOutput(); $out->setSubtitle(Linker::link($page->getTitle())); $out->addModules('ext.translate.special.pagetranslation'); $out->addWikiMsg('tpt-showpage-intro'); $formParams = array('method' => 'post', 'action' => $this->getPageTitle()->getFullURL(), 'class' => 'mw-tpt-sp-markform'); $out->addHTML(Xml::openElement('form', $formParams) . Html::hidden('title', $this->getPageTitle()->getPrefixedText()) . Html::hidden('revision', $page->getRevision()) . Html::hidden('target', $page->getTitle()->getPrefixedtext()) . Html::hidden('token', $this->getUser()->getEditToken())); $out->wrapWikiMsg('==$1==', 'tpt-sections-oldnew'); $diffOld = $this->msg('tpt-diff-old')->escaped(); $diffNew = $this->msg('tpt-diff-new')->escaped(); $hasChanges = false; // Check whether page title was previously marked for translation. // If the page is marked for translation the first time, default to checked. $defaultChecked = $page->hasPageDisplayTitle(); /** * @var TPSection $s */ foreach ($sections as $s) { if ($s->name === 'Page display title') { // Set section type as new if title previously unchecked $s->type = $defaultChecked ? $s->type : 'new'; // Checkbox for page title optional translation $this->getOutput()->addHTML(Xml::checkLabel($this->msg('tpt-translate-title')->text(), 'translatetitle', 'mw-translate-title', $defaultChecked)); } if ($s->type === 'new') { $hasChanges = true; $name = $this->msg('tpt-section-new', $s->name)->escaped(); } else { $name = $this->msg('tpt-section', $s->name)->escaped(); } if ($s->type === 'changed') { $hasChanges = true; $diff = new DifferenceEngine(); if (method_exists('DifferenceEngine', 'setTextLanguage')) { $diff->setTextLanguage($wgContLang); } $diff->setReducedLineNumbers(); $oldContent = ContentHandler::makeContent($s->getOldText(), $diff->getTitle()); $newContent = ContentHandler::makeContent($s->getText(), $diff->getTitle()); $diff->setContent($oldContent, $newContent); $text = $diff->getDiff($diffOld, $diffNew); $diffOld = $diffNew = null; $diff->showDiffStyle(); $id = "tpt-sect-{$s->id}-action-nofuzzy"; $checkLabel = Xml::checkLabel($this->msg('tpt-action-nofuzzy')->text(), $id, $id, false); $text = $checkLabel . $text; } else { $text = TranslateUtils::convertWhiteSpaceToHTML($s->getText()); } # For changed text, the language is set by $diff->setTextLanguage() $lang = $s->type === 'changed' ? null : $wgContLang; $out->addHTML(MessageWebImporter::makeSectionElement($name, $s->type, $text, $lang)); } $deletedSections = $page->getParse()->getDeletedSections(); if (count($deletedSections)) { $hasChanges = true; $out->wrapWikiMsg('==$1==', 'tpt-sections-deleted'); /** * @var TPSection $s */ foreach ($deletedSections as $s) { $name = $this->msg('tpt-section-deleted', $s->id)->escaped(); $text = TranslateUtils::convertWhiteSpaceToHTML($s->getText()); $out->addHTML(MessageWebImporter::makeSectionElement($name, $s->type, $text, $wgContLang)); } } // Display template changes if applicable if ($page->getMarkedTag() !== false) { $hasChanges = true; $newTemplate = $page->getParse()->getTemplatePretty(); $oldPage = TranslatablePage::newFromRevision($page->getTitle(), $page->getMarkedTag()); $oldTemplate = $oldPage->getParse()->getTemplatePretty(); if ($oldTemplate !== $newTemplate) { $out->wrapWikiMsg('==$1==', 'tpt-sections-template'); $diff = new DifferenceEngine(); if (method_exists('DifferenceEngine', 'setTextLanguage')) { $diff->setTextLanguage($wgContLang); } $oldContent = ContentHandler::makeContent($oldTemplate, $diff->getTitle()); $newContent = ContentHandler::makeContent($newTemplate, $diff->getTitle()); $diff->setContent($oldContent, $newContent); $text = $diff->getDiff($this->msg('tpt-diff-old')->escaped(), $this->msg('tpt-diff-new')->escaped()); $diff->showDiffStyle(); $diff->setReducedLineNumbers(); $contentParams = array('class' => 'mw-tpt-sp-content'); $out->addHTML(Xml::tags('div', $contentParams, $text)); } } if (!$hasChanges) { $out->wrapWikiMsg('<div class="successbox">$1</div>', 'tpt-mark-nochanges'); } $this->priorityLanguagesForm($page); $out->addHTML(Xml::submitButton($this->msg('tpt-submit')->text()) . Xml::closeElement('form')); }
protected function getPageDiff() { $this->mustBeKnownMessage(); $title = $this->handle->getTitle(); $key = $this->handle->getKey(); if (!$title->exists()) { return null; } $definitionTitle = Title::makeTitleSafe($title->getNamespace(), "{$key}/en"); if (!$definitionTitle || !$definitionTitle->exists()) { return null; } $db = wfGetDB(DB_MASTER); $conds = array('rt_page' => $title->getArticleID(), 'rt_type' => RevTag::getType('tp:transver')); $options = array('ORDER BY' => 'rt_revision DESC'); $latestRevision = $definitionTitle->getLatestRevID(); $translationRevision = $db->selectField('revtag', 'rt_value', $conds, __METHOD__, $options); if ($translationRevision === false) { return null; } // Using newFromId instead of newFromTitle, because the page might have been renamed $oldrev = Revision::newFromId($translationRevision); if (!$oldrev) { // And someone might still have deleted it return null; } $oldtext = ContentHandler::getContentText($oldrev->getContent()); $newContent = Revision::newFromTitle($definitionTitle, $latestRevision)->getContent(); $newtext = ContentHandler::getContentText($newContent); if ($oldtext === $newtext) { return null; } $diff = new DifferenceEngine(); if (method_exists('DifferenceEngine', 'setTextLanguage')) { $diff->setTextLanguage($this->group->getSourceLanguage()); } $oldContent = ContentHandler::makeContent($oldtext, $diff->getTitle()); $newContent = ContentHandler::makeContent($newtext, $diff->getTitle()); $diff->setContent($oldContent, $newContent); $diff->setReducedLineNumbers(); $diff->showDiffStyle(); return $diff->getDiff(wfMessage('tpt-diff-old')->escaped(), wfMessage('tpt-diff-new')->escaped()); }
/** * Displays the sections and changes for the user to review * @param $page TranslatablePage * @param $sections array */ public function showPage( TranslatablePage $page, Array $sections ) { global $wgOut, $wgContLang; $wgOut->setSubtitle( $this->user->getSkin()->link( $page->getTitle() ) ); $wgOut->addModules( 'ext.translate.special.pagetranslation' ); $wgOut->addWikiMsg( 'tpt-showpage-intro' ); $formParams = array( 'method' => 'post', 'action' => $this->getTitle()->getFullURL(), 'class' => 'mw-tpt-sp-markform', ); $wgOut->addHTML( Xml::openElement( 'form', $formParams ) . Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . Html::hidden( 'revision', $page->getRevision() ) . Html::hidden( 'target', $page->getTitle()->getPrefixedtext() ) . Html::hidden( 'token', $this->user->editToken() ) ); $wgOut->wrapWikiMsg( '==$1==', 'tpt-sections-oldnew' ); $diffOld = wfMsgHtml( 'tpt-diff-old' ); $diffNew = wfMsgHtml( 'tpt-diff-new' ); foreach ( $sections as $s ) { if ( $s->type === 'new' ) { $input = Xml::input( 'tpt-sect-' . $s->id, 15, $s->name ); $name = wfMsgHtml( 'tpt-section-new', $input ); } else { $name = wfMsgHtml( 'tpt-section', htmlspecialchars( $s->name ) ); } if ( $s->type === 'changed' ) { $diff = new DifferenceEngine; if ( method_exists( 'DifferenceEngine', 'setTextLanguage' ) ) { $diff->setTextLanguage( $wgContLang ); } $diff->setReducedLineNumbers(); $diff->setText( $s->getOldText(), $s->getText() ); $text = $diff->getDiff( $diffOld, $diffNew ); $diffOld = $diffNew = null; $diff->showDiffStyle(); $id = "tpt-sect-{$s->id}-action-nofuzzy"; $text = Xml::checkLabel( wfMsg( 'tpt-action-nofuzzy' ), $id, $id, false ) . $text; } else { $text = TranslateUtils::convertWhiteSpaceToHTML( $s->getText() ); } # For changed text, the language is set by $diff->setTextLanguage() $lang = $s->type === 'changed' ? null : $wgContLang; $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text, $lang ) ); } $deletedSections = $page->getParse()->getDeletedSections(); if ( count( $deletedSections ) ) { $wgOut->wrapWikiMsg( '==$1==', 'tpt-sections-deleted' ); foreach ( $deletedSections as $s ) { $name = wfMsgHtml( 'tpt-section-deleted', htmlspecialchars( $s->id ) ); $text = TranslateUtils::convertWhiteSpaceToHTML( $s->getText() ); $wgOut->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, $text, $wgContLang ) ); } } // Display template changes if applicable if ( $page->getMarkedTag() !== false ) { $newTemplate = $page->getParse()->getTemplatePretty(); $oldPage = TranslatablePage::newFromRevision( $page->getTitle(), $page->getMarkedTag() ); $oldTemplate = $oldPage->getParse()->getTemplatePretty(); if ( $oldTemplate !== $newTemplate ) { $wgOut->wrapWikiMsg( '==$1==', 'tpt-sections-template' ); $diff = new DifferenceEngine; if ( method_exists( 'DifferenceEngine', 'setTextLanguage' ) ) { $diff->setTextLanguage( $wgContLang ); } $diff->setText( $oldTemplate, $newTemplate ); $text = $diff->getDiff( wfMsgHtml( 'tpt-diff-old' ), wfMsgHtml( 'tpt-diff-new' ) ); $diff->showDiffStyle(); $diff->setReducedLineNumbers(); $contentParams = array( 'class' => 'mw-tpt-sp-content' ); $wgOut->addHTML( Xml::tags( 'div', $contentParams, $text ) ); } } $wgOut->addHTML( Xml::submitButton( wfMsg( 'tpt-submit' ) ) . Xml::closeElement( 'form' ) ); }