WMDE-Fisch has uploaded a new change for review.

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

Change subject: Log edits in multiple sections that could trigger mentions.
......................................................................

Log edits in multiple sections that could trigger mentions.

This patch logs multiple section edits that could trigger mentions.

Since we only want to send mentions when userlinks and signature are present
in the same section a new method was added extracting sections and the related
content from an addition. The results are checked whether a section content
contains a signature and might be relevant for mentions.

Bug: T141863
Change-Id: Ib06cd855b2c7fbd51d8ab6602882cb38aadf8350
---
M includes/DiscussionParser.php
M tests/phpunit/DiscussionParserTest.php
2 files changed, 214 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/05/304805/1

diff --git a/includes/DiscussionParser.php b/includes/DiscussionParser.php
index 3265d65..af6d1b3 100644
--- a/includes/DiscussionParser.php
+++ b/includes/DiscussionParser.php
@@ -45,6 +45,17 @@
                                $content = $action['content'];
                                $header = self::extractHeader( $content );
                                self::generateMentionEvents( $header, $content, 
$revision, $user );
+                       } elseif (  $action['type'] == 'add-section-multiple' ) 
{
+                               $content = self::stripHeader( 
$action['content'] );
+                               $content = self::stripSignature( $content );
+                               $userLinks = self::getUserLinks( $content, 
$title );
+                               if ( $userLinks ) {
+                                       $diffUrl = $title->getFullURL( array(
+                                               'oldid' => 'prev',
+                                               'diff' => $revision->getId()
+                                       ) );
+                                       wfDebugLog( 'newMentions', 
'add-section-multiple action with user links: ' . $diffUrl );
+                               }
                        }
                }
 
@@ -417,9 +428,8 @@
         *    existing section.
         * - new-section-with-comment: A new section is added, containing
         *    a single comment signed by the user in question.
-        * - unknown-signed-addition: Some signed content is added, but it
-        *    includes section headers, is signed by another user or
-        *    otherwise confuses the interpretation engine.
+        * - add-section-multiple: A new section or additions to a section
+        *    while editing multiple sections at once.
         * - unknown-multi-signed-addition: Some signed content is added,
         *    but it contains multiple signatures.
         * - unknown-unsigned-addition: Some content is added, but it is
@@ -470,10 +480,21 @@
                                                        'content' => $content,
                                                );
                                        } else {
-                                               $actions[] = array(
-                                                       'type' => 
'unknown-signed-addition',
-                                                       'content' => $content,
-                                               );
+                                               $sectionData = 
self::extractSections( $content );
+                                               foreach ( $sectionData as 
$section ) {
+                                                       $sectionSignedUsers = 
self::extractSignatures( $section['content'], $title );
+                                                       if ( !empty( 
$sectionSignedUsers ) ) {
+                                                               if ( 
!$section['header'] ) {
+                                                                       
$fullSection = self::getFullSection( $changes['_info']['rhs'], 
$change['right-pos'] );
+                                                                       
$section['header'] = self::extractHeader( $fullSection );
+                                                               }
+                                                       }
+                                                       $actions[] = array(
+                                                               'type' => 
'add-section-multiple',
+                                                               'content' => 
$content,
+                                                               'header' => 
$section['header'],
+                                                       );
+                                               }
                                        }
                                } elseif ( count( $signedUsers ) >= 1 ) {
                                        $actions[] = array(
@@ -580,6 +601,49 @@
        }
 
        /**
+        * Extracts sections and their contents from text.
+        *
+        * @param string $text The text to parse.
+        * @return array[]
+        * Array of arrays containing sections with header and content.
+        * - [header]: The full header string of the section or false if there 
is preceding text without header.
+        * - [content]: The content of the section including the header string.
+        */
+       private static function extractSections( $text ) {
+               $matches = array();
+
+               if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, 
$matches, PREG_OFFSET_CAPTURE ) ) {
+                       return array( array(
+                               'header' => false,
+                               'content' => $text
+                       ) );
+               }
+
+               $sectionNum = count( $matches[0] );
+               $sections = array();
+
+               if ( $matches[0][0][1] > 1 ) { // is there text before the 
first headline?
+                       $sections[] = array(
+                               'header' => false,
+                               'content' =>  mb_substr( $text, 0, 
$matches[0][0][1] - 1 )
+                       );
+               }
+               for ( $i = 0; $i < $sectionNum; $i++ ) {
+                       if ( $i + 1 < $sectionNum ) {
+                               $content = substr( $text, $matches[0][$i][1], 
$matches[0][$i + 1][1] - $matches[0][$i][1] );
+                       } else {
+                               $content =  substr( $text, $matches[0][$i][1] );
+                       }
+                       $sections[] = array(
+                               'header' => $matches[0][$i][0],
+                               'content' =>  trim( $content )
+                       );
+               }
+
+               return $sections;
+       }
+
+       /**
         * Strips out a signature if possible.
         *
         * @param string $text The wikitext to strip
diff --git a/tests/phpunit/DiscussionParserTest.php 
b/tests/phpunit/DiscussionParserTest.php
index 1e7e3c0..4efedce 100644
--- a/tests/phpunit/DiscussionParserTest.php
+++ b/tests/phpunit/DiscussionParserTest.php
@@ -376,11 +376,13 @@
                                                'type' => 'mention-failure',
                                                'agent' => 'Admin',
                                                'section-title' => 'Hello 
Users',
+                                               'subject-name' => 'Ping',
                                        ),
                                        array(
                                                'type' => 'mention-failure',
                                                'agent' => 'Admin',
                                                'section-title' => 'Hello 
Users',
+                                               'subject-name' => 'Po?ng',
                                        ),
                                ),
                        ),
@@ -396,11 +398,13 @@
                                                'type' => 'mention',
                                                'agent' => 'Admin',
                                                'section-title' => 'Hello 
Users',
+                                               'subject-name' => null,
                                        ),
                                        array(
                                                'type' => 'mention-success',
                                                'agent' => 'Admin',
                                                'section-title' => 'Hello 
Users',
+                                               'subject-name' => 'Test11',
                                        ),
                                ),
                        ),
@@ -416,6 +420,7 @@
                                                'type' => 'mention-failure',
                                                'agent' => 'Admin',
                                                'section-title' => 'Section 2',
+                                               'subject-name' => 'NoUser',
                                        ),
                                ),
                        ),
@@ -431,6 +436,7 @@
                                                'type' => 'mention-failure',
                                                'agent' => 'Admin',
                                                'section-title' => 'Section 2',
+                                               'subject-name' => 'NoUser',
                                        ),
                                ),
                        ),
@@ -462,6 +468,7 @@
                                        'type' => $event->getType(),
                                        'agent' => 
$event->getAgent()->getName(),
                                        'section-title' => 
$event->getExtraParam( 'section-title' ),
+                                       'subject-name' => 
$event->getExtraParam( 'subject-name' ),
                                );
                                return false;
                        }
@@ -475,6 +482,142 @@
                $this->assertEquals( $expected, $events );
        }
 
+       public function provider_extractSections() {
+               return array(
+                       array(
+                               'content' => 'Just Text.',
+                               'result' => array(
+                                       array(
+                                               'header' => false,
+                                               'content' => 'Just Text.',
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'content' =>
+<<<TEXT
+Text and a
+== Headline ==
+with text
+TEXT
+                               ,
+                               'result' => array(
+                                       array(
+                                               'header' => false,
+                                               'content' =>
+<<<TEXT
+Text and a
+TEXT
+                                               ,
+                                       ),
+                                       array(
+                                               'header' => '== Headline ==',
+                                               'content' =>
+<<<TEXT
+== Headline ==
+with text
+TEXT
+                                               ,
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'content' =>
+<<<TEXT
+== Headline ==
+Text and a [[User:Test]]
+TEXT
+                       ,
+                               'result' => array(
+                                       array(
+                                               'header' => '== Headline ==',
+                                               'content' =>
+<<<TEXT
+== Headline ==
+Text and a [[User:Test]]
+TEXT
+                                       ,
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'content' =>
+<<<TEXT
+Content 0
+== Headline 1 ==
+Content 1
+=== Headline 2 ===
+Content 2
+TEXT
+                       ,
+                               'result' => array(
+                                       array(
+                                               'header' => false,
+                                               'content' => 'Content 0',
+                                       ),
+                                       array(
+                                               'header' => '== Headline 1 ==',
+                                               'content' =>
+<<<TEXT
+== Headline 1 ==
+Content 1
+TEXT
+                                       ,
+                                       ),
+                                       array(
+                                               'header' => '=== Headline 2 
===',
+                                               'content' =>
+<<<TEXT
+=== Headline 2 ===
+Content 2
+TEXT
+                                       ,
+                                       ),
+                               ),
+                       ),
+                       array(
+                               'content' =>
+<<<TEXT
+== Headline 1 ==
+مرحبا كيف حالك 
+=== Headline 2 ===
+انا بخير شكرا
+TEXT
+                       ,
+                               'result' => array(
+                                       array(
+                                               'header' => '== Headline 1 ==',
+                                               'content' =>
+<<<TEXT
+== Headline 1 ==
+مرحبا كيف حالك
+TEXT
+                                       ,
+                                       ),
+                                       array(
+                                               'header' => '=== Headline 2 
===',
+                                               'content' =>
+<<<TEXT
+=== Headline 2 ===
+انا بخير شكرا
+TEXT
+                                       ,
+                                       ),
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provider_extractSections
+        */
+       public function testExtractSections( $content, $result ) {
+               $discussionParser = TestingAccessWrapper::newFromClass( 
'EchoDiscussionParser' );
+               $sections = $discussionParser->extractSections( $content );
+
+               $this->assertEquals( $result, $sections );
+       }
+
        public function testGenerateEventsForRevision_tooManyMentionsFailure() {
                $expected = array(
                        array(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib06cd855b2c7fbd51d8ab6602882cb38aadf8350
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: WMDE-Fisch <christoph.fisc...@wikimedia.de>

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

Reply via email to