Legoktm has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/379724 )

Change subject: EditPage: Extract some edit conflict code into 
EditConflictHelper
......................................................................

EditPage: Extract some edit conflict code into EditConflictHelper

Extensions with custom content models (e.g. ProofreadPage) will extend
EditPage to provide a customized editing experience. However when
another extension like TwoColConflict wants to change just how edit
conflicts look, this causes problems since only one class can take the
place of EditPage.

So isntead, split most of the frontend code for edit conflicts into
TextConflictHelper, and call it from edit page. Extensions can override
with the instance created by calling
EditPage::setEditConflictHelperFactory().

And to make that split possible also move EditPage::addNewLineAtEnd()
and ::buildTextboxAttribs() into a separate TextboxBuilder class that
both EditPage and TextConflictHelper can use.

Bug: T176393
Change-Id: Ie415edd84329c02d5762477f8a171fced85b01a2
---
M autoload.php
M includes/EditPage.php
A includes/editpage/TextConflictHelper.php
A includes/editpage/TextboxBuilder.php
4 files changed, 364 insertions(+), 80 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/24/379724/1

diff --git a/autoload.php b/autoload.php
index 4dd5f12..dbe18f4 100644
--- a/autoload.php
+++ b/autoload.php
@@ -876,6 +876,8 @@
        'MediaWiki\\Auth\\UsernameAuthenticationRequest' => __DIR__ . 
'/includes/auth/UsernameAuthenticationRequest.php',
        'MediaWiki\\Diff\\ComplexityException' => __DIR__ . 
'/includes/diff/ComplexityException.php',
        'MediaWiki\\Diff\\WordAccumulator' => __DIR__ . 
'/includes/diff/WordAccumulator.php',
+       'MediaWiki\\EditPage\\TextConflictHelper' => __DIR__ . 
'/includes/editpage/TextConflictHelper.php',
+       'MediaWiki\\EditPage\\TextboxBuilder' => __DIR__ . 
'/includes/editpage/TextboxBuilder.php',
        'MediaWiki\\Edit\\PreparedEdit' => __DIR__ . 
'/includes/edit/PreparedEdit.php',
        'MediaWiki\\HeaderCallback' => __DIR__ . '/includes/HeaderCallback.php',
        'MediaWiki\\Interwiki\\ClassicInterwikiLookup' => __DIR__ . 
'/includes/interwiki/ClassicInterwikiLookup.php',
diff --git a/includes/EditPage.php b/includes/EditPage.php
index a8ed19d..88cd3b4 100644
--- a/includes/EditPage.php
+++ b/includes/EditPage.php
@@ -20,6 +20,8 @@
  * @file
  */
 
+use MediaWiki\EditPage\TextboxBuilder;
+use MediaWiki\EditPage\TextConflictHelper;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\ScopedCallback;
@@ -447,6 +449,18 @@
        private $unicodeCheck;
 
        /**
+        * Factory function to create a edit conflict helper
+        *
+        * @var callable
+        */
+       private $editConflictHelperFactory;
+
+       /**
+        * @var TextConflictHelper|null
+        */
+       private $editConflictHelper;
+
+       /**
         * @param Article $article
         */
        public function __construct( Article $article ) {
@@ -459,6 +473,7 @@
 
                $handler = ContentHandler::getForModelID( $this->contentModel );
                $this->contentFormat = $handler->getDefaultFormat();
+               $this->editConflictHelperFactory = [ $this, 
'newTextConflictHelper' ];
        }
 
        /**
@@ -1526,8 +1541,7 @@
                        return;
                }
 
-               $stats = 
MediaWikiServices::getInstance()->getStatsdDataFactory();
-               $stats->increment( 'edit.failures.conflict.resolved' );
+               $this->getEditConflictHelper()->incrementResolvedStats();
        }
 
        /**
@@ -2810,6 +2824,18 @@
                }
 
                $out->addHTML( $this->editFormTextBeforeContent );
+               if ( $this->isConflict ) {
+                       // In an edit conflict, we turn textbox2 into the 
user's text,
+                       // and textbox1 into the stored version
+                       $this->textbox2 = $this->textbox1;
+
+                       $content = $this->getCurrentContent();
+                       $this->textbox1 = $this->toEditText( $content );
+
+                       $editConflictHelper = $this->getEditConflictHelper();
+                       $editConflictHelper->setTextboxes( $this->textbox2, 
$this->textbox1 );
+                       $out->addHTML( 
$editConflictHelper->getEditFormTextBeforeContent() );
+               }
 
                if ( !$this->mTitle->isCssJsSubpage() && $showToolbar && 
$user->getOption( 'showtoolbar' ) ) {
                        $out->addHTML( self::getEditToolbar( $this->mTitle ) );
@@ -2824,12 +2850,8 @@
                        // and fallback to the raw wpTextbox1 since 
editconflicts can't be
                        // resolved between page source edits and custom ui 
edits using the
                        // custom edit ui.
-                       $this->textbox2 = $this->textbox1;
-
-                       $content = $this->getCurrentContent();
-                       $this->textbox1 = $this->toEditText( $content );
-
                        $this->showTextbox1();
+                       $out->addHTML( 
$editConflictHelper->getEditFormTextAfterContent() );
                } else {
                        $this->showContentForm();
                }
@@ -3353,11 +3375,17 @@
        }
 
        protected function showTextbox( $text, $name, $customAttribs = [] ) {
-               $wikitext = $this->addNewLineAtEnd( $text );
+               $builder = new TextboxBuilder();
+               $attribs = $builder->buildTextboxAttribs(
+                       $name,
+                       $customAttribs,
+                       $this->context->getUser(),
+                       $this->mTitle
+               );
 
-               $attribs = $this->buildTextboxAttribs( $name, $customAttribs, 
$this->context->getUser() );
-
-               $this->context->getOutput()->addHTML( Html::textarea( $name, 
$wikitext, $attribs ) );
+               $this->context->getOutput()->addHTML(
+                       Html::textarea( $name, $builder->addNewLineAtEnd( $text 
), $attribs )
+               );
        }
 
        protected function displayPreviewArea( $previewOutput, $isOnTop = false 
) {
@@ -3679,34 +3707,12 @@
                if ( Hooks::run( 'EditPageBeforeConflictDiff', [ &$editPage, 
&$out ] ) ) {
                        $this->incrementConflictStats();
 
-                       $out->wrapWikiMsg( '<h2>$1</h2>', "yourdiff" );
-
-                       $content1 = $this->toEditContent( $this->textbox1 );
-                       $content2 = $this->toEditContent( $this->textbox2 );
-
-                       $handler = ContentHandler::getForModelID( 
$this->contentModel );
-                       $de = $handler->createDifferenceEngine( $this->context 
);
-                       $de->setContent( $content2, $content1 );
-                       $de->showDiff(
-                               $this->context->msg( 'yourtext' )->parse(),
-                               $this->context->msg( 'storedversion' )->text()
-                       );
-
-                       $out->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
-                       $this->showTextbox2();
+                       
$this->getEditConflictHelper()->showEditFormTextAfterFooters();
                }
        }
 
        protected function incrementConflictStats() {
-               $stats = 
MediaWikiServices::getInstance()->getStatsdDataFactory();
-               $stats->increment( 'edit.failures.conflict' );
-               // Only include 'standard' namespaces to avoid creating unknown 
numbers of statsd metrics
-               if (
-                       $this->mTitle->getNamespace() >= NS_MAIN &&
-                       $this->mTitle->getNamespace() <= NS_CATEGORY_TALK
-               ) {
-                       $stats->increment( 
'edit.failures.conflict.byNamespaceId.' . $this->mTitle->getNamespace() );
-               }
+               $this->getEditConflictHelper()->incrementConflictStats();
        }
 
        /**
@@ -4352,9 +4358,10 @@
        /**
         * Get the message key of the label for the button to save the page
         *
+        * @since 1.31
         * @return string
         */
-       protected function getSubmitButtonLabel() {
+       public function getSubmitButtonLabel() {
                $labelAsPublish =
                        $this->context->getConfig()->get( 
'EditSubmitButtonLabelPublish' );
 
@@ -4628,9 +4635,8 @@
         * @since 1.29
         */
        protected function addExplainConflictHeader( OutputPage $out ) {
-               $out->wrapWikiMsg(
-                       "<div class='mw-explainconflict'>\n$1\n</div>",
-                       [ 'explainconflict', $this->context->msg( 
$this->getSubmitButtonLabel() )->text() ]
+               $out->addHTML(
+                       $this->getEditConflictHelper()->getExplainHeader()
                );
        }
 
@@ -4642,38 +4648,9 @@
         * @since 1.29
         */
        protected function buildTextboxAttribs( $name, array $customAttribs, 
User $user ) {
-               $attribs = $customAttribs + [
-                               'accesskey' => ',',
-                               'id' => $name,
-                               'cols' => 80,
-                               'rows' => 25,
-                               // Avoid PHP notices when appending preferences
-                               // (appending allows customAttribs['style'] to 
still work).
-                               'style' => ''
-                       ];
-
-               // The following classes can be used here:
-               // * mw-editfont-default
-               // * mw-editfont-monospace
-               // * mw-editfont-sans-serif
-               // * mw-editfont-serif
-               $class = 'mw-editfont-' . $user->getOption( 'editfont' );
-
-               if ( isset( $attribs['class'] ) ) {
-                       if ( is_string( $attribs['class'] ) ) {
-                               $attribs['class'] .= ' ' . $class;
-                       } elseif ( is_array( $attribs['class'] ) ) {
-                               $attribs['class'][] = $class;
-                       }
-               } else {
-                       $attribs['class'] = $class;
-               }
-
-               $pageLang = $this->mTitle->getPageLanguage();
-               $attribs['lang'] = $pageLang->getHtmlCode();
-               $attribs['dir'] = $pageLang->getDir();
-
-               return $attribs;
+               return ( new TextboxBuilder() )->buildTextboxAttribs(
+                       $name, $customAttribs, $user, $this->mTitle
+               );
        }
 
        /**
@@ -4682,15 +4659,7 @@
         * @since 1.29
         */
        protected function addNewLineAtEnd( $wikitext ) {
-               if ( strval( $wikitext ) !== '' ) {
-                       // Ensure there's a newline at the end, otherwise 
adding lines
-                       // is awkward.
-                       // But don't add a newline if the text is empty, or 
Firefox in XHTML
-                       // mode will show an extra newline. A bit annoying.
-                       $wikitext .= "\n";
-                       return $wikitext;
-               }
-               return $wikitext;
+               return ( new TextboxBuilder() )->addNewLineAtEnd( $wikitext );
        }
 
        /**
@@ -4715,4 +4684,40 @@
                // Meanwhile, real browsers get real anchors
                return $wgParser->guessSectionNameFromWikiText( $text );
        }
+
+       /**
+        * Set a factory function to create an EditConflictHelper
+        *
+        * @param callable $factory Factory function
+        * @since 1.31
+        */
+       public function setEditConflictHelperFactory( callable $factory ) {
+               $this->editConflictHelperFactory = $factory;
+               $this->editConflictHelper = null;
+       }
+
+       /**
+        * @return TextConflictHelper
+        */
+       private function getEditConflictHelper() {
+               if ( !$this->editConflictHelper ) {
+                       $this->editConflictHelper = call_user_func(
+                               $this->editConflictHelperFactory
+                       );
+               }
+
+               return $this->editConflictHelper;
+       }
+
+       /**
+        * @return TextConflictHelper
+        */
+       private function newTextConflictHelper() {
+               return new TextConflictHelper(
+                       $this->mTitle,
+                       $this->context->getOutput(),
+                       
MediaWikiServices::getInstance()->getStatsdDataFactory(),
+                       $this->getSubmitButtonLabel()
+               );
+       }
 }
diff --git a/includes/editpage/TextConflictHelper.php 
b/includes/editpage/TextConflictHelper.php
new file mode 100644
index 0000000..699c1a9
--- /dev/null
+++ b/includes/editpage/TextConflictHelper.php
@@ -0,0 +1,183 @@
+<?php
+/**
+ * Helper for displaying edit conflicts to users
+ *
+ * Copyright (C) 2017 Kunal Mehta <lego...@member.fsf.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\EditPage;
+
+use ContentHandler;
+use Html;
+use IBufferingStatsdDataFactory;
+use OutputPage;
+use TextContent;
+use Title;
+
+/**
+ * Helper for displaying edit conflicts in text content
+ * models to users
+ *
+ * @since 1.31
+ */
+class TextConflictHelper {
+
+       /**
+        * @var Title
+        */
+       protected $title;
+
+       /**
+        * @var OutputPage
+        */
+       protected $out;
+
+       /**
+        * @var IBufferingStatsdDataFactory
+        */
+       protected $stats;
+
+       /**
+        * @var string Message key for submit button's label
+        */
+       protected $submitLabel;
+
+       /**
+        * @var string
+        */
+       protected $yourtext = '';
+
+       /**
+        * @var string
+        */
+       protected $storedversion = '';
+
+       /**
+\       * @param Title $title
+        * @param OutputPage $out
+        * @param IBufferingStatsdDataFactory $stats
+        * @param string $submitLabel
+        */
+       public function __construct( Title $title, OutputPage $out, 
IBufferingStatsdDataFactory $stats,
+               $submitLabel
+       ) {
+               $this->title = $title;
+               $this->out = $out;
+               $this->stats = $stats;
+               $this->submitLabel = $submitLabel;
+       }
+
+       /**
+        * @param string $yourtext
+        * @param string $storedversion
+        */
+       public function setTextboxes( $yourtext, $storedversion ) {
+               $this->yourtext = $yourtext;
+               $this->storedversion = $storedversion;
+       }
+
+       /**
+        * Record a user encountering an edit conflict
+        */
+       public function incrementConflictStats() {
+               $this->stats->increment( 'edit.failures.conflict' );
+               // Only include 'standard' namespaces to avoid creating unknown 
numbers of statsd metrics
+               if (
+                       $this->title->getNamespace() >= NS_MAIN &&
+                       $this->title->getNamespace() <= NS_CATEGORY_TALK
+               ) {
+                       $this->stats->increment(
+                               'edit.failures.conflict.byNamespaceId.' . 
$this->title->getNamespace()
+                       );
+               }
+       }
+
+       /**
+        * Record when a user has resolved an edit conflict
+        */
+       public function incrementResolvedStats() {
+               $this->stats->increment( 'edit.failures.conflict.resolved' );
+       }
+
+       /**
+        * @return string HTML
+        */
+       public function getExplainHeader() {
+               return Html::rawElement(
+                       'div',
+                       [ 'class' => 'mw-explainconflict' ],
+                       $this->out->msg( 'explainconflict', $this->out->msg( 
$this->submitLabel )->text() )->parse()
+               );
+       }
+
+       /**
+        * Content to go in the edit form before textbox1
+        *
+        * @see EditPage::$editFormTextBeforeContent
+        * @return string
+        */
+       public function getEditFormTextBeforeContent() {
+               return '';
+       }
+
+       /**
+        * Content to go in the edit form after textbox1
+        *
+        * @see EditPage::$editFormTextAfterContent
+        * @return string
+        */
+       public function getEditFormTextAfterContent() {
+               return '';
+       }
+
+       /**
+        * Content to go in the edit form after the footers
+        * (templates on this page, hidden categories, limit report)
+        */
+       public function showEditFormTextAfterFooters() {
+               $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourdiff" );
+
+               // Treat it all as plain text
+               $yourContent = new TextContent( $this->yourtext );
+               $storedContent = new TextContent( $this->storedversion );
+               $handler = ContentHandler::getForModelID( CONTENT_FORMAT_TEXT );
+               $de = $handler->createDifferenceEngine( $this->out );
+
+               $de->setContent( $yourContent, $storedContent );
+               $de->showDiff(
+                       $this->out->msg( 'yourtext' )->parse(),
+                       $this->out->msg( 'storedversion' )->text()
+               );
+
+               $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
+
+               $builder = new TextboxBuilder();
+               $attribs = $builder->buildTextboxAttribs(
+                       'wpTextbox2',
+                       [ 'tabindex' => 6, 'readonly' ],
+                       $this->out->getUser(),
+                       $this->title
+               );
+
+               $this->out->addHTML(
+                       Html::textarea( 'wpTextbox2', 
$builder->addNewLineAtEnd( $this->yourtext ), $attribs )
+               );
+       }
+}
diff --git a/includes/editpage/TextboxBuilder.php 
b/includes/editpage/TextboxBuilder.php
new file mode 100644
index 0000000..7c8a85d
--- /dev/null
+++ b/includes/editpage/TextboxBuilder.php
@@ -0,0 +1,94 @@
+<?php
+/**
+ * Helps EditPage build textboxes
+ *
+ * (C) Copyright 2017 Kunal Mehta <lego...@member.fsf.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+namespace MediaWiki\EditPage;
+
+use Title;
+use User;
+
+/**
+ * Helps EditPage build textboxes
+ *
+ * @since 1.31
+ */
+class TextboxBuilder {
+
+       /**
+        * @param string $wikitext
+        * @return string
+        */
+       public function addNewLineAtEnd( $wikitext ) {
+               if ( strval( $wikitext ) !== '' ) {
+                       // Ensure there's a newline at the end, otherwise 
adding lines
+                       // is awkward.
+                       // But don't add a newline if the text is empty, or 
Firefox in XHTML
+                       // mode will show an extra newline. A bit annoying.
+                       $wikitext .= "\n";
+                       return $wikitext;
+               }
+               return $wikitext;
+       }
+
+       /**
+        * @param string $name
+        * @param mixed[] $customAttribs
+        * @param User $user
+        * @return mixed[]
+        */
+       public function buildTextboxAttribs( $name, array $customAttribs, User 
$user, Title $title ) {
+               $attribs = $customAttribs + [
+                               'accesskey' => ',',
+                               'id' => $name,
+                               'cols' => 80,
+                               'rows' => 25,
+                               // Avoid PHP notices when appending preferences
+                               // (appending allows customAttribs['style'] to 
still work).
+                               'style' => ''
+                       ];
+
+               // The following classes can be used here:
+               // * mw-editfont-default
+               // * mw-editfont-monospace
+               // * mw-editfont-sans-serif
+               // * mw-editfont-serif
+               $class = 'mw-editfont-' . $user->getOption( 'editfont' );
+
+               if ( isset( $attribs['class'] ) ) {
+                       if ( is_string( $attribs['class'] ) ) {
+                               $attribs['class'] .= ' ' . $class;
+                       } elseif ( is_array( $attribs['class'] ) ) {
+                               $attribs['class'][] = $class;
+                       }
+               } else {
+                       $attribs['class'] = $class;
+               }
+
+               $pageLang = $title->getPageLanguage();
+               $attribs['lang'] = $pageLang->getHtmlCode();
+               $attribs['dir'] = $pageLang->getDir();
+
+               return $attribs;
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/379724
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie415edd84329c02d5762477f8a171fced85b01a2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to