Sbisson has uploaded a new change for review.

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

Change subject: Optin: Copy templates before first heading from talk page to 
Flow board
......................................................................

Optin: Copy templates before first heading from talk page to Flow board

* Browser tests for templates copying for Special:EnableFlow
* Browser tests for templates copying for Optin
* Extract templates extraction to TemplateHelper
* Copy extracted templates during optin process

Bug: T113426
Change-Id: I57d46ae14bf412b16a26bb7a07c3a2d1de5afe6a
---
M includes/Import/OptInController.php
M includes/Import/TemplateHelper.php
M includes/Import/Wikitext/ImportSource.php
M tests/browser/features/opt_in.feature
M tests/browser/features/special_enableflow.feature
M tests/browser/features/step_definitions/opt_in_steps.rb
M tests/browser/features/step_definitions/special_enable_flow_steps.rb
M tests/browser/features/support/components/board_description.rb
8 files changed, 71 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/29/240729/1

diff --git a/includes/Import/OptInController.php 
b/includes/Import/OptInController.php
index 96402ef..3963a16 100644
--- a/includes/Import/OptInController.php
+++ b/includes/Import/OptInController.php
@@ -16,6 +16,8 @@
 use Flow\WorkflowLoaderFactory;
 use IContextSource;
 use MovePage;
+use Parser;
+use ParserOptions;
 use RequestContext;
 use Revision;
 use Title;
@@ -75,7 +77,9 @@
 
                // archive existing wikitext talk page
                $linkToArchivedTalkpage = null;
+               $templatesFromTalkpage = null;
                if ( $title->exists( Title::GAID_FOR_UPDATE ) ) {
+                       $templatesFromTalkpage = 
$this->extractTemplatesAboveFirstSection( $title );
                        $wikitextTalkpageArchiveTitle = 
$this->archiveExistingTalkpage( $title );
                        $this->addArchiveTemplate( 
$wikitextTalkpageArchiveTitle, $title );
                        $linkToArchivedTalkpage = 
$this->buildLinkToArchivedTalkpage( $wikitextTalkpageArchiveTitle );
@@ -86,7 +90,7 @@
                if ( $archivedFlowPage ) {
                        $this->restoreExistingFlowBoard( $archivedFlowPage, 
$title, $linkToArchivedTalkpage );
                } else {
-                       $this->createFlowBoard( $title, $linkToArchivedTalkpage 
);
+                       $this->createFlowBoard( $title, $templatesFromTalkpage 
. "\n\n" . $linkToArchivedTalkpage );
                        
$this->notificationController->notifyFlowEnabledOnTalkpage( $user );
                }
        }
@@ -504,4 +508,23 @@
                        wfMessage( 
'flow-beta-feature-remove-archive-template-edit-summary' 
)->inContentLanguage()->plain());
        }
 
+       /**
+        * @param Title $title
+        * @return string
+        */
+       private function extractTemplatesAboveFirstSection( Title $title ) {
+               $content = $this->getContent( $title );
+               if ( !$content ) {
+                       return '';
+               }
+
+               $parser = new Parser();
+               $output = $parser->parse( $content, $title, new ParserOptions );
+               $sections = $output->getSections();
+               if ( $sections ) {
+                       $content = substr( $content, 0, 
$sections[0]['byteoffset'] );
+               }
+               return TemplateHelper::extractTemplates( $content, $title );
+       }
+
 }
diff --git a/includes/Import/TemplateHelper.php 
b/includes/Import/TemplateHelper.php
index 67ccac3..9e19f08 100644
--- a/includes/Import/TemplateHelper.php
+++ b/includes/Import/TemplateHelper.php
@@ -6,6 +6,7 @@
 use DOMElement;
 use DOMXPath;
 use Flow\Parsoid\Utils;
+use Title;
 
 class TemplateHelper {
 
@@ -63,4 +64,26 @@
                }
        }
 
+       /**
+        * Extract templates from wikitext content.
+        * Requires Parsoid to reliably extract templates.
+        *
+        * @param string $content
+        * @param Title $title
+        * @return string
+        */
+       public static function extractTemplates( $content, Title $title ) {
+               $content = Utils::convert( 'wikitext', 'html', $content, $title 
);
+               $dom = Utils::createDOM( $content );
+               $xpath = new \DOMXPath( $dom );
+               $templates = $xpath->query( '//*[@typeof="mw:Transclusion"]' );
+
+               $content = '';
+               foreach ( $templates as $template ) {
+                       $content .= $dom->saveHTML( $template ) . "\n";
+               }
+
+               return Utils::convert( 'html', 'wikitext', $content, $title );
+       }
+
 }
\ No newline at end of file
diff --git a/includes/Import/Wikitext/ImportSource.php 
b/includes/Import/Wikitext/ImportSource.php
index 93f0d41..ded0e5d 100644
--- a/includes/Import/Wikitext/ImportSource.php
+++ b/includes/Import/Wikitext/ImportSource.php
@@ -3,6 +3,7 @@
 namespace Flow\Import\Wikitext;
 
 use ArrayIterator;
+use Flow\Import\TemplateHelper;
 use Flow\Parsoid\Utils;
 use FlowHooks;
 use Flow\Import\ImportException;
@@ -63,7 +64,7 @@
                        $content = substr( $content, 0, 
$sections[0]['byteoffset'] );
                }
 
-               $content = $this->extractTemplates( $content );
+               $content = TemplateHelper::extractTemplates( $content, 
$this->title );
 
                $template = wfMessage( 'flow-importer-wt-converted-template' 
)->inContentLanguage()->plain();
                $arguments = implode( '|', array(
@@ -85,27 +86,6 @@
                        ) ),
                        
"wikitext-import:header:{$this->title->getPrefixedText()}"
                );
-       }
-
-       /**
-        * Only extract templates to copy to Flow description.
-        * Requires Parsoid, to reliably extract templates.
-        *
-        * @param string $content
-        * @return string
-        */
-       protected function extractTemplates( $content ) {
-               $content = Utils::convert( 'wikitext', 'html', $content, 
$this->title );
-               $dom = Utils::createDOM( $content );
-               $xpath = new \DOMXPath( $dom );
-               $templates = $xpath->query( '//*[@typeof="mw:Transclusion"]' );
-
-               $content = '';
-               foreach ( $templates as $template ) {
-                       $content .= $dom->saveHTML( $template ) . "\n";
-               }
-
-               return Utils::convert( 'html', 'wikitext', $content, 
$this->title );
        }
 
        /**
diff --git a/tests/browser/features/opt_in.feature 
b/tests/browser/features/opt_in.feature
index f8ddd0a..0000bb2 100644
--- a/tests/browser/features/opt_in.feature
+++ b/tests/browser/features/opt_in.feature
@@ -19,6 +19,7 @@
     When I enable Flow beta feature
     Then my talk page is a Flow board
     And my flow board contains a link to my archived talk page
+    And the board description contains the templates from my talk page
     And my previous talk page is archived
 
   Scenario: Opt-out: I didn't have a talk page
diff --git a/tests/browser/features/special_enableflow.feature 
b/tests/browser/features/special_enableflow.feature
index d842512..617d2e7 100644
--- a/tests/browser/features/special_enableflow.feature
+++ b/tests/browser/features/special_enableflow.feature
@@ -29,5 +29,6 @@
     Then I get confirmation for enabling a new Flow board
     And I click on the new Flow board link
     And The page I am on is a Flow board
+    And the board description contains the templates from my talk page
     And I click the archive link
     And The archive contains the original text
diff --git a/tests/browser/features/step_definitions/opt_in_steps.rb 
b/tests/browser/features/step_definitions/opt_in_steps.rb
index 5adb12e..7042cb1 100644
--- a/tests/browser/features/step_definitions/opt_in_steps.rb
+++ b/tests/browser/features/step_definitions/opt_in_steps.rb
@@ -23,8 +23,12 @@
 
 Given(/^my talk page has wiktext content$/) do
   talk_page = "User_talk:#{@username}"
-  @talk_page_content = 'this is the content of my talk page'
-  api.create_page talk_page, @talk_page_content
+  @talk_page_content = "this is the content of my talk page"
+  content = @talk_page_content
+  content += "\n{{template_before_first_heading}}"
+  content += "\n== this is the first section =="
+  content += "\n{{template_after_first_heading}}"
+  api.create_page talk_page, content
 end
 
 Then(/^my previous talk page is archived$/) do
diff --git 
a/tests/browser/features/step_definitions/special_enable_flow_steps.rb 
b/tests/browser/features/step_definitions/special_enable_flow_steps.rb
index 82c392d..a15d5f9 100644
--- a/tests/browser/features/step_definitions/special_enable_flow_steps.rb
+++ b/tests/browser/features/step_definitions/special_enable_flow_steps.rb
@@ -4,7 +4,11 @@
 
 Given(/^I have an existing talk page$/) do
   @new_board_page = @data_manager.get_talk 'Test_Prefilled_Random_Board'
-  api.create_page @new_board_page, '<p class="flow-test-archive-content">Some 
wikitext here.</p>'
+  content = "<p class=\"flow-test-archive-content\">Some wikitext here.</p>"
+  content += "\n\n{{template_before_first_heading}}"
+  content += "\n\n== this is the first section =="
+  content += "\n\n{{template_after_first_heading}}"
+  api.create_page @new_board_page, content
 end
 
 When(/^I enable a new Flow board on the talk page$/) do
@@ -64,3 +68,11 @@
     page.description.content_element.when_present.text.should match 
@custom_header
   end
 end
+
+Then(/^the board description contains the templates from my talk page$/) do
+  on(AbstractFlowPage) do |page|
+    description = page.description.content_element.when_present.text
+    expect(description).to match 'Template:Template before first heading'
+    expect(description).to_not match 'Template:Template after first heading'
+  end
+end
diff --git a/tests/browser/features/support/components/board_description.rb 
b/tests/browser/features/support/components/board_description.rb
index a456533..0d67502 100644
--- a/tests/browser/features/support/components/board_description.rb
+++ b/tests/browser/features/support/components/board_description.rb
@@ -21,6 +21,6 @@
   # If page has an archive template from a flow conversion
   # find the link
   link(:archive_link) do
-    content_element.link_element
+    content_element.link_element(text: /Archive/)
   end
 end

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I57d46ae14bf412b16a26bb7a07c3a2d1de5afe6a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Sbisson <[email protected]>

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

Reply via email to