Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/234979

Change subject: Fix confusion abotu $pre and $post params in 
AutoCommentFormatter.
......................................................................

Fix confusion abotu $pre and $post params in AutoCommentFormatter.

Out code should be in sync with what Linker::formatAutocomments does.
Apparently, that changed over the last years. Thsi change puts it back
in sync.

Change-Id: I0438de9002b920c5fdc6715bc8f0de5d4d4d8a28
---
M lib/includes/formatters/AutoCommentFormatter.php
M lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
M repo/Wikibase.hooks.php
3 files changed, 88 insertions(+), 84 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/79/234979/1

diff --git a/lib/includes/formatters/AutoCommentFormatter.php 
b/lib/includes/formatters/AutoCommentFormatter.php
index df7a8b0..e864f24 100644
--- a/lib/includes/formatters/AutoCommentFormatter.php
+++ b/lib/includes/formatters/AutoCommentFormatter.php
@@ -45,14 +45,13 @@
         * strings that may be in the database.
         *
         * @see https://www.mediawiki.org/wiki/Manual:Hooks/FormatAutocomments
+        * @see docs/summaries.txt
         *
-        * @param string|false $pre content before the autocomment
         * @param string $auto the autocomment unformatted
-        * @param string|false $post content after the autocomment
         *
-        * @return string|null
+        * @return string|null The localized summary, or null
         */
-       public function formatAutoComment( $pre, $auto, $post ) {
+       public function formatAutoComment( $auto ) {
                if ( !preg_match( '/^([a-z\-]+)\s*(:\s*(.*?)\s*)?$/', $auto, 
$matches ) ) {
                        return null;
                }
@@ -69,29 +68,34 @@
 
                // parse the autocomment
                $auto = $msg->params( $args )->parse();
+               return $auto;
+       }
 
-               // add pre and post fragments
-               if ( $pre === true ) {
-                       // written summary $presep autocomment (summary /* 
section */)
-                       $pre = wfMessage( 'autocomment-prefix' )->escaped();
-               } elseif ( $pre !== '' && $pre !== false ) {
-                       // written summary $presep autocomment (summary /* 
section */)
-                       $pre .= wfMessage( 'autocomment-prefix' )->escaped();
-               } elseif ( $pre === false ) {
-                       $pre = '';
+       /**
+        * Wrapps a comment by applying the appropriate directionality markers 
and pre and/or postfix
+        * separators.
+        *
+        * @note This code should be kept in sync with what 
Linker::formatAutocomments does.
+        *
+        * @param boolean $pre True if there is text before the comment, so a 
prefix separator is needed.
+        * @param string $comment the localized comment, as returned by 
formatAutoComment()
+        * @param boolean $post True if there is text after the comment, so a 
postfix separator is needed.
+        *
+        * @return string
+        */
+       public function wrapAutoComment( $pre, $comment, $post ) {
+               if ( $pre ) {
+                       # written summary $presep autocomment (summary /* 
section */)
+                       $pre = wfMessage( 'autocomment-prefix' )->inLanguage( 
$this->language )->escaped();
                }
-               if ( $post !== '' && $post !== false ) {
-                       // autocomment $postsep written summary (/* section */ 
summary)
-                       $auto .= wfMessage( 'colon-separator' )->escaped();
-                       if ( $post === true ) {
-                               $post = '';
-                       }
-               } elseif ( $post === false ) {
-                       $post = '';
+               if ( $post ) {
+                       # autocomment $postsep written summary (/* section */ 
summary)
+                       $comment .= wfMessage( 'colon-separator' )->inLanguage( 
$this->language )->escaped();
                }
-
-               $auto = '<span class="autocomment">' . $auto . '</span>';
-               $comment = $pre . $this->language->getDirMark() . '<span 
dir="auto">' . $auto . '</span>' . $post;
+               $comment = '<span class="autocomment">' . $comment . '</span>';
+               $comment = $pre . $this->language->getDirMark()
+                       . '<span dir="auto">' . $comment;
+               $comment .= '</span>';
 
                return $comment;
        }
diff --git a/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php 
b/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
index 59b883f..60eba6b 100644
--- a/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
+++ b/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
@@ -35,71 +35,22 @@
 
        public function provideTestAutoComment() {
                return array(
-                       'Empty comment' => array( '', '', '', '', null ),
-                       'Non existant message' => array( 'wikibase', '', 
'##########', '', null ),
+                       'Empty comment' => array( '', '', null ),
+                       'Non existant message' => array( 'wikibase', 
'##########', null ),
                        'Existing message with no params' => array(
                                'wikibase-item',
-                               '',
                                'wbsetitem',
-                               '',
-                               self::$lrm .
-                               '<span dir="auto"><span 
class="autocomment">(wikibase-item-summary-wbsetitem)</span></span>',
+                               '(wikibase-item-summary-wbsetitem)',
                        ),
                        'Existing message with 1 parameter' => array(
                                'wikibase-item',
-                               '',
                                'wbsetlabel-add:|FOO',
-                               '',
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               '(wikibase-item-summary-wbsetlabel-add: , 
FOO)</span></span>',
+                               '(wikibase-item-summary-wbsetlabel-add: , FOO)',
                        ),
                        'Existing message with 2 parameters' => array(
                                'wikibase-item',
-                               '',
                                'wbsetaliases-set:10|FOO',
-                               '',
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               '(wikibase-item-summary-wbsetaliases-set: 10, 
FOO)</span></span>',
-                       ),
-                       'Pre and Post set to false' => array(
-                               'wikibase-item',
-                               false,
-                               'wbsetitem',
-                               false,
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               
'(wikibase-item-summary-wbsetitem)</span></span>',
-                       ),
-                       'Pre is true, post is false' => array(
-                               'wikibase-item',
-                               true,
-                               'wbsetitem',
-                               false,
-                               '(autocomment-prefix)' .
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               
'(wikibase-item-summary-wbsetitem)</span></span>',
-                       ),
-                       'Pre is false, post is true' => array(
-                               'wikibase-item',
-                               false,
-                               'wbsetitem',
-                               true,
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               
'(wikibase-item-summary-wbsetitem)(colon-separator)</span></span>',
-                       ),
-                       'Pre and post set to strings' => array(
-                               'wikibase-item',
-                               'AAA',
-                               'wbsetitem',
-                               'ZZZ',
-                               'AAA(autocomment-prefix)' .
-                               self::$lrm .
-                               '<span dir="auto"><span class="autocomment">' .
-                               
'(wikibase-item-summary-wbsetitem)(colon-separator)</span></span>ZZZ',
+                               '(wikibase-item-summary-wbsetaliases-set: 10, 
FOO)',
                        ),
                );
        }
@@ -107,9 +58,58 @@
        /**
         * @dataProvider provideTestAutoComment
         */
-       public function testFormatAutoComment( $prefix, $pre, $auto, $post, 
$expected ) {
+       public function testFormatAutoComment( $prefix, $auto, $expected ) {
                $formatter = new AutoCommentFormatter( $this->language, $prefix 
);
-               $value = $formatter->formatAutoComment( $pre, $auto, $post );
+               $value = $formatter->formatAutoComment( $auto );
+               $this->assertEquals( $expected, $value );
+       }
+
+
+       public function provideWrapAutoComment() {
+               return array(
+                       'Pre and Post set to false' => array(
+                               false,
+                               '--FOO--',
+                               false,
+                               self::$lrm .
+                               '<span dir="auto"><span class="autocomment">' .
+                               '--FOO--</span></span>',
+                       ),
+                       'Pre is true, post is false' => array(
+                               true,
+                               '--FOO--',
+                               false,
+                               '(autocomment-prefix)' .
+                               self::$lrm .
+                               '<span dir="auto"><span class="autocomment">' .
+                               '--FOO--</span></span>',
+                       ),
+                       'Pre is false, post is true' => array(
+                               false,
+                               '--FOO--',
+                               true,
+                               self::$lrm .
+                               '<span dir="auto"><span class="autocomment">' .
+                               '--FOO--(colon-separator)</span></span>',
+                       ),
+                       'Pre and post set to strings' => array(
+                               true,
+                               '--FOO--',
+                               true,
+                               '(autocomment-prefix)' .
+                               self::$lrm .
+                               '<span dir="auto"><span class="autocomment">' .
+                               '--FOO--(colon-separator)</span></span>',
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideWrapAutoComment
+        */
+       public function testWrapAutoComment( $pre, $comment, $post, $expected ) 
{
+               $formatter = new AutoCommentFormatter( $this->language, 'DUMMY' 
);
+               $value = $formatter->wrapAutoComment( $pre, $comment, $post );
                $this->assertEquals( $expected, $value );
        }
 
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 8a3548d..f85af0d 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -798,9 +798,9 @@
         * @param string[] $data Extra data supplied when registering the hook 
function,
         *        matches list( $contentModel, $messagePrefix ).
         * @param string &$comment reference to the autocomment text
-        * @param string|bool $pre content before the autocomment
+        * @param bool $pre true if there is content before the autocomment
         * @param string $auto the autocomment unformatted
-        * @param string|bool $post content after the autocomment
+        * @param bool $post true if there is content after the autocomment
         * @param Title|null $title use for further information
         * @param bool $local shall links be generated locally or globally
         *
@@ -821,10 +821,10 @@
                }
 
                $formatter = new AutoCommentFormatter( $wgLang, $messagePrefix 
);
-               $formattedComment = $formatter->formatAutoComment( $pre, $auto, 
$post );
+               $formattedComment = $formatter->formatAutoComment( $auto );
 
                if ( is_string( $formattedComment ) ) {
-                       $comment = $formattedComment;
+                       $comment = $formatter->wrapAutoComment( $pre, 
$formattedComment, $post );
                }
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0438de9002b920c5fdc6715bc8f0de5d4d4d8a28
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to