public function testTranslationPageRestrictions()
 {
     $superUser = new MockSuperUser();
     $title = Title::newFromText('Translatable page');
     $page = WikiPage::factory($title);
     $content = ContentHandler::makeContent('<translate>Hello</translate>', $title);
     $status = $page->doEditContent($content, 'New page', 0, false, $superUser);
     $revision = $status->value['revision']->getId();
     $translatablePage = TranslatablePage::newFromRevision($title, $revision);
     $translatablePage->addMarkedTag($revision);
     MessageGroups::singleton()->recache();
     $translationPage = Title::newFromText('Translatable page/fi');
     TranslateRenderJob::newJob($translationPage)->run();
     $this->assertTrue($translationPage->userCan('read', $superUser), 'Users can read existing translation pages');
     $this->assertFalse($translationPage->userCan('edit', $superUser), 'Users can not edit existing translation pages');
     $translationPage = Title::newFromText('Translatable page/ab');
     $this->assertTrue($translationPage->userCan('read', $superUser), 'Users can read non-existing translation pages');
     $this->assertFalse($translationPage->userCan('edit', $superUser), 'Users can not edit non-existing translation pages');
 }
Пример #2
0
 protected function getTranslationPageDiff()
 {
     global $wgEnablePageTranslation;
     if (!$wgEnablePageTranslation) {
         return null;
     }
     $this->mustBeKnownMessage();
     if (!$this->group instanceof WikiPageMessageGroup) {
         return null;
     }
     // Shortcuts
     $code = $this->handle->getCode();
     $key = $this->handle->getKey();
     // TODO: encapsulate somewhere
     $page = TranslatablePage::newFromTitle($this->group->getTitle());
     $rev = $page->getTransRev("{$key}/{$code}");
     $latest = $page->getMarkedTag();
     if ($rev === $latest) {
         return null;
     }
     $oldpage = TranslatablePage::newFromRevision($this->group->getTitle(), $rev);
     $oldtext = $newtext = null;
     foreach ($oldpage->getParse()->getSectionsForSave() as $section) {
         if ($this->group->getTitle()->getPrefixedDBKey() . '/' . $section->id === $key) {
             $oldtext = $section->getTextForTrans();
         }
     }
     foreach ($page->getParse()->getSectionsForSave() as $section) {
         if ($this->group->getTitle()->getPrefixedDBKey() . '/' . $section->id === $key) {
             $newtext = $section->getTextForTrans();
         }
     }
     if ($oldtext === $newtext) {
         return null;
     }
     $diff = new DifferenceEngine();
     if (method_exists('DifferenceEngine', 'setTextLanguage')) {
         $diff->setTextLanguage($this->group->getSourceLanguage());
     }
     $diff->setText($oldtext, $newtext);
     $diff->setReducedLineNumbers();
     $diff->showDiffStyle();
     return $diff->getDiff(wfMsgHtml('tpt-diff-old'), wfMsgHtml('tpt-diff-new'));
 }
 /**
  * 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'));
 }
	/**
	 * 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' )
		);
	}