Bartosz Dziewoński has uploaded a new change for review.

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

Change subject: Generate automatic summary /* Section */ when a section heading 
is added manually
......................................................................

Generate automatic summary /* Section */ when a section heading is added 
manually

Seems to work! I'm surprised myself.

TODO:
* Verify that this doesn't break non-wikitext content models (it should do
  nothing for them)
* Verify that the weird things we're doing to the Parser will not break
  unrelated stuff
* Verify that this doesn't negatively affect performance of regular page
  saves (should be fine, the preprocessToObj() call should be cached)
* Find a better place for the getSectionList() function

Bug: T22307
Change-Id: I5041070b4783df2b71ad830727bbabd480223882
---
M includes/content/ContentHandler.php
M includes/page/WikiPage.php
2 files changed, 68 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/70/281470/1

diff --git a/includes/content/ContentHandler.php 
b/includes/content/ContentHandler.php
index 7430caf..7c9c1dc 100644
--- a/includes/content/ContentHandler.php
+++ b/includes/content/ContentHandler.php
@@ -871,11 +871,70 @@
                        return wfMessage( 'autosumm-newblank' 
)->inContentLanguage()->text();
                }
 
+               // New section added auto-summary
+               $oldSections = $this->getSectionList( 
$oldContent->getNativeData() );
+               $newSections = $this->getSectionList( 
$newContent->getNativeData() );
+               $addedSectionWikitext = null;
+               if ( count( $newSections ) > count( $oldSections ) ) {
+                       $oldIdx = 0;
+                       $newIdx = 0;
+                       while ( isset( $newSections[$newIdx] ) ) {
+                               if ( isset( $oldSections[$oldIdx] ) && 
$oldSections[$oldIdx] === $newSections[$newIdx] ) {
+                                       // Common prefix continues
+                                       $oldIdx++;
+                                       $newIdx++;
+                               } elseif ( $addedSectionWikitext == null ) {
+                                       // First mismatch - if this is the only 
one, we've detected a newly added section
+                                       $addedSectionWikitext = 
$newSections[$newIdx];
+                                       $newIdx++;
+                               } else {
+                                       // Second mismatch - the situation is 
more complicated, either multiple sections were added
+                                       // or they were moved around, bail
+                                       $addedSectionWikitext = null;
+                                       break;
+                               }
+                       }
+               }
+               // If $addedSectionWikitext is null, either we found no new 
sections, or we had multiple mismatches
+               // and bailed
+               if ( $addedSectionWikitext ) {
+                       global $wgParser;
+                       $cleanSectionTitle = trim( $wgParser->stripSectionName(
+                               preg_replace( '/^(=+)(.+)\1$/', '$2', 
$addedSectionWikitext )
+                       ) );
+                       return wfMessage( 'newsectionsummary' )
+                               ->rawParams( $cleanSectionTitle 
)->inContentLanguage()->text();
+               }
+
                // If we reach this point, there's no applicable auto-summary 
for our
                // case, so our auto-summary is empty.
                return '';
        }
 
+       private function getSectionList( $text ) {
+               global $wgParser, $wgTitle;
+               $wgParser->startExternalParse( $wgTitle, new ParserOptions, 
Parser::OT_PLAIN, true );
+               $preprocessor = $wgParser->getPreprocessor();
+
+               $frame = $preprocessor->newFrame();
+               $root = $preprocessor->preprocessToObj( $text );
+
+               # <h> nodes indicate section breaks
+               # They can only occur at the top level, so we can find them by 
iterating the root's children
+               $node = $root->getFirstChild();
+               $sections = [];
+               while ( $node ) {
+                       if ( $node->getName() === 'h' ) {
+                               $bits = $node->splitHeading();
+                               $headingWikitext = $frame->expand( 
$bits['contents'], PPFrame::RECOVER_ORIG );
+                               $sections[] = $headingWikitext;
+                       }
+                       $node = $node->getNextSibling();
+               }
+
+               return $sections;
+       }
+
        /**
         * Auto-generates a deletion reason
         *
diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php
index 5e5532f..da45c8e 100644
--- a/includes/page/WikiPage.php
+++ b/includes/page/WikiPage.php
@@ -1628,9 +1628,16 @@
                $old_content = $this->getContent( Revision::RAW ); // current 
revision's content
 
                // Provide autosummaries if one is not provided and 
autosummaries are enabled
-               if ( $wgUseAutomaticEditSummaries && ( $flags & 
EDIT_AUTOSUMMARY ) && $summary == '' ) {
+               if (
+                       $wgUseAutomaticEditSummaries && ( $flags & 
EDIT_AUTOSUMMARY ) &&
+                       // No summary, or only automatic '/* Section */ ' 
summary
+                       ( $summary == '' || preg_match( '/^\/\*(.*?)\*\/ */', 
$summary ) )
+               ) {
                        $handler = $content->getContentHandler();
-                       $summary = $handler->getAutosummary( $old_content, 
$content, $flags );
+                       $autosummary = $handler->getAutosummary( $old_content, 
$content, $flags );
+                       if ( $autosummary ) {
+                               $summary = $autosummary;
+                       }
                }
 
                // Get the pre-save transform content and final parser output

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5041070b4783df2b71ad830727bbabd480223882
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

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

Reply via email to