Sbisson has uploaded a new change for review.

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

Change subject: [WIP] Add templates to talkpage and Flow board during opt-out
......................................................................

[WIP] Add templates to talkpage and Flow board during opt-out

On opt-out:
  - add a template to the restored wikitext talkpage that
    links to the archived Flow board.

  - add a template to the archived Flow board that links
    to the wikitext talkpage.

Bug: T112957
Change-Id: I29e76ed6b090a636bc3427b6d588d825e4967eb3
---
M includes/Import/OptInController.php
M tests/browser/features/step_definitions/opt_in_steps.rb
2 files changed, 124 insertions(+), 48 deletions(-)


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

diff --git a/includes/Import/OptInController.php 
b/includes/Import/OptInController.php
index 22f1de9..fd0c9b7 100644
--- a/includes/Import/OptInController.php
+++ b/includes/Import/OptInController.php
@@ -76,21 +76,20 @@
                }
 
                // archive existing wikitext talk page
-               $linkToArchivedTalkpage = null;
+               $currentTemplate = 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 );
+                       $currentTemplate = $this->getFormattedCurrentTemplate( 
$wikitextTalkpageArchiveTitle );
                }
 
                // create or restore flow board
                $archivedFlowPage = $this->findLatestFlowArchive( $title );
                if ( $archivedFlowPage ) {
-                       $this->restoreExistingFlowBoard( $archivedFlowPage, 
$title, $linkToArchivedTalkpage );
+                       $this->restoreExistingFlowBoard( $archivedFlowPage, 
$title, $currentTemplate );
                } else {
-                       $this->createFlowBoard( $title, $templatesFromTalkpage 
. "\n\n" . $linkToArchivedTalkpage );
+                       $this->createFlowBoard( $title, $templatesFromTalkpage 
. "\n\n" . $currentTemplate );
                        
$this->notificationController->notifyFlowEnabledOnTalkpage( $user );
                }
        }
@@ -104,24 +103,23 @@
                }
 
                // archive the flow board
-               $flowArchiveTitle = $this->findNextFlowArchive( $title );
-               $archiveReason = wfMessage( 'flow-optin-archive-flow-board' 
)->inContentLanguage()->text();
-               $this->movePage( $title, $flowArchiveTitle, $archiveReason );
-               $this->removeArchivedTalkpageTemplateFromFlowBoardDescription( 
$flowArchiveTitle );
+               $flowArchiveTitle = $this->archiveFlowBoard( $title );
 
                // restore the original wikitext talk page
                $archivedTalkpage = $this->findLatestArchive( $title );
                if ( $archivedTalkpage ) {
+                       $this->removeArchiveTemplateFromWikitextTalkpage( 
$archivedTalkpage );
+                       $this->addCurrentTemplate( $archivedTalkpage, 
$flowArchiveTitle );
                        $restoreReason = wfMessage( 
'flow-optin-restore-wikitext' )->inContentLanguage()->text();
                        $this->movePage( $archivedTalkpage, $title , 
$restoreReason);
-                       $this->removeArchiveTemplateFromWikitextTalkpage( 
$title );
                }
        }
 
        /**
         * Check whether the current user has a flow board archived already.
         *
-        * @return boolean Flow board archive exists
+        * @param User $user
+        * @return bool Flow board archive exists
         */
        public function hasFlowBoardArchive( User $user ) {
                return $this->findLatestFlowArchive( $user->getTalkPage() ) !== 
false;
@@ -308,23 +306,42 @@
                $archiveTitle = $this->findNextArchive( $title );
                $archiveReason = wfMessage( 'flow-optin-archive-wikitext' 
)->inContentLanguage()->text();
                $this->movePage( $title, $archiveTitle, $archiveReason );
+
+               $content =  $this->getContent( $archiveTitle );
+               $content = $this->removeCurrentTemplateFromWikitext( $content, 
$archiveTitle );
+               $content = $this->getFormattedArchiveTemplate( $title ) . 
"\n\n" . $content;
+
+               $addTemplateReason = wfMessage( 
'flow-beta-feature-add-archive-template-edit-summary' 
)->inContentLanguage()->plain();
+               $this->createRevision(
+                       $archiveTitle,
+                       $content,
+                       $addTemplateReason);
+
                return $archiveTitle;
        }
 
        /**
         * @param Title $archivedFlowPage
         * @param Title $title
-        * @param string|null $addToHeader
+        * @param string|null $currentTemplate
         */
-       private function restoreExistingFlowBoard( Title $archivedFlowPage, 
Title $title, $addToHeader = null ) {
+       private function restoreExistingFlowBoard( Title $archivedFlowPage, 
Title $title, $currentTemplate = null ) {
+               $this->editBoardDescription(
+                       $archivedFlowPage,
+                       function( $content ) use ( $currentTemplate, 
$archivedFlowPage ) {
+                               $templateName = wfMessage( 
'flow-importer-wt-converted-archive-template' )->inContentLanguage()->plain();
+                               $content = TemplateHelper::removeFromHtml( 
$content, $templateName );
+                               if ( $currentTemplate ) {
+                                       $content = $currentTemplate . 
"<br/><br/>" . Utils::convert( 'wikitext', 'html', $currentTemplate, 
$archivedFlowPage );
+                               }
+                               return $content;
+                       },
+                       'html'
+               );
+
                $restoreReason = wfMessage( 'flow-optin-restore-flow-board' 
)->inContentLanguage()->text();
                $this->movePage( $archivedFlowPage, $title, $restoreReason );
 
-               if ( $addToHeader ) {
-                       $this->editBoardDescription( $title, function( $oldDesc 
) use ( $addToHeader ) {
-                               return $oldDesc . "\n\n" . $addToHeader;
-                       }, 'wikitext' );
-               }
        }
 
        /**
@@ -350,7 +367,7 @@
         * @param Title $archiveTitle
         * @return string
         */
-       private function buildLinkToArchivedTalkpage( Title $archiveTitle ) {
+       private function getFormattedCurrentTemplate( Title $archiveTitle ) {
                $now = new DateTime( "now", new DateTimeZone( "GMT" ) );
                $arguments = array(
                        'archive' => $archiveTitle->getPrefixedText(),
@@ -375,16 +392,6 @@
                                array_values( $args ) )
                );
                return "{{{$name}|$arguments}}";
-       }
-
-       /**
-        * @param Title $flowArchiveTitle
-        */
-       private function 
removeArchivedTalkpageTemplateFromFlowBoardDescription( Title $flowArchiveTitle 
) {
-               $this->editBoardDescription( $flowArchiveTitle, function( 
$oldDesc ) {
-                       $templateName = wfMessage( 
'flow-importer-wt-converted-template' )->inContentLanguage()->plain();
-                       return TemplateHelper::removeFromHtml( $oldDesc, 
$templateName );
-               }, 'html' );
        }
 
        /**
@@ -472,24 +479,16 @@
        }
 
        /**
-        * @param Title $archive
         * @param Title $current
-        * @throws ImportException
+        * @return string
         */
-       private function addArchiveTemplate( Title $archive, Title $current ) {
+       private function getFormattedArchiveTemplate( Title $current ) {
                $templateName = wfMessage( 
'flow-importer-wt-converted-archive-template' )->inContentLanguage()->plain();
                $now = new DateTime( "now", new DateTimeZone( "GMT" ) );
-               $template = $this->formatTemplate( $templateName, array(
+               return $this->formatTemplate( $templateName, array(
                        'from' => $current->getPrefixedText(),
                        'date' => $now->format( 'Y-m-d' ),
                ) );
-
-               $content = $this->getContent( $archive );
-
-               $this->createRevision(
-                       $archive,
-                       $template . "\n\n" . $content,
-                       wfMessage( 
'flow-beta-feature-add-archive-template-edit-summary' 
)->inContentLanguage()->plain());
        }
 
        /**
@@ -514,6 +513,18 @@
        }
 
        /**
+        * @param string $wikitextContent
+        * @param Title $title
+        * @return string
+        */
+       private function removeCurrentTemplateFromWikitext( $wikitextContent, 
Title $title ) {
+               $templateName = wfMessage( 
'flow-importer-wt-converted-template' )->inContentLanguage()->plain();
+               $contentAsHtml = Utils::convert( 'wikitext', 'html', 
$wikitextContent, $title );
+               $contentWithoutTemplate = TemplateHelper::removeFromHtml( 
$contentAsHtml, $templateName );
+               return Utils::convert( 'html', 'wikitext', 
$contentWithoutTemplate, $title );
+       }
+
+       /**
         * @param Title $title
         * @return string
         */
@@ -532,4 +543,61 @@
                return TemplateHelper::extractTemplates( $content, $title );
        }
 
+       /**
+        * @param Title $title
+        * @param $reason
+        * @param callable $newDescriptionCallback
+        * @param string $format
+        * @throws ImportException
+        * @throws InvalidDataException
+        */
+       private function editWikitextContent( Title $title, $reason, callable 
$newDescriptionCallback, $format = 'html' ) {
+               $content =  Utils::convert( 'wikitext', $format, 
$this->getContent( $title ), $title );
+               $newContent = call_user_func( $newDescriptionCallback, $content 
);
+               $this->createRevision(
+                       $title,
+                       Utils::convert( $format, 'wikitext', $newContent, 
$title ),
+                       $reason);
+       }
+
+       /**
+        * Add the "current" template to the page considered the current 
talkpage
+        * and link to the archived talkpage.
+        *
+        * @param Title $currentTalkpageTitle
+        * @param Title $archivedTalkpageTitle
+        */
+       private function addCurrentTemplate( Title $currentTalkpageTitle, Title 
$archivedTalkpageTitle ) {
+               $template = $this->getFormattedCurrentTemplate( 
$archivedTalkpageTitle );
+               $this->editWikitextContent(
+                       $currentTalkpageTitle,
+                       null,
+                       function( $content ) use ( $template ) { return 
$template . "\n\n" . $content; },
+                       'wikitext' );
+       }
+
+       /**
+        * @param Title $title
+        * @return Title
+        * @throws InvalidDataException
+        */
+       private function archiveFlowBoard( Title $title ) {
+               $flowArchiveTitle = $this->findNextFlowArchive( $title );
+               $archiveReason = wfMessage( 'flow-optin-archive-flow-board' 
)->inContentLanguage()->text();
+               $this->movePage( $title, $flowArchiveTitle, $archiveReason );
+
+               $template = $this->getFormattedArchiveTemplate( $title );
+               $template = Utils::convert( 'wikitext', 'html', $template, 
$title );
+
+               $this->editBoardDescription(
+                       $flowArchiveTitle,
+                       function( $content ) use ( $template ) {
+                               $templateName = wfMessage( 
'flow-importer-wt-converted-template' )->inContentLanguage()->plain();
+                               $content = TemplateHelper::removeFromHtml( 
$content, $templateName );
+                               return $template . "<br/><br/>" . $content;
+                       },
+                       'html' );
+
+               return $flowArchiveTitle;
+       }
 }
diff --git a/tests/browser/features/step_definitions/opt_in_steps.rb 
b/tests/browser/features/step_definitions/opt_in_steps.rb
index 981fb61..1f32a30 100644
--- a/tests/browser/features/step_definitions/opt_in_steps.rb
+++ b/tests/browser/features/step_definitions/opt_in_steps.rb
@@ -37,6 +37,9 @@
   visit(WikiPage, using_params: { page: archive_name }) do |page|
     expect(page.content_element.when_present.text).to match @talk_page_content
     expect(page.content_element.when_present.text).to match archive_template
+
+    expect(page.content).to match 'This page is an archive.'
+    expect(page.content).to_not match 'Previous discussion was archived at'
   end
 end
 
@@ -55,21 +58,24 @@
 
 Then(/^my wikitext talk page is restored$/) do
   talk_page_link = "User_talk:#{@username}".gsub '_', ' '
+  flow_archive_link = "User_talk:#{@username}/Flow_Archive_1".gsub '_', ' '
   visit(UserTalkPage, using_params: { username: @username }) do |page|
     page.refresh_until do
       page.content.match @talk_page_content
     end
-    expect(page.content).to_not match talk_page_link
+    expect(page.content).to_not match 'This page is an archive.'
+    expect(page.content).to match 'Previous discussion was archived at'
+    expect(page.content).to match flow_archive_link
   end
 end
 
 Then(/^my Flow board is archived$/) do
   flow_archive_name = "./User_talk:#{@username}/Flow_Archive_1"
-  talk_page_link = "User_talk:#{@username}".gsub '_', ' '
   visit(WikiPage, using_params: { page: flow_archive_name }) do |page|
     page.refresh_until { page.flow.board_element.visible? }
     page.flow.board_element.when_present
-    expect(page.flow.header).to_not match talk_page_link
+    expect(page.flow.header).to match 'This page is an archive.'
+    expect(page.flow.header).to_not match 'Previous discussion was archived at'
   end
 end
 
@@ -84,11 +90,13 @@
 
 Then(/^my talk page is my old Flow board$/) do
   archive_name = "User_talk:#{@username}/Archive_1".gsub '_', ' '
-  visit(UserTalkPage, using_params: { username: @username }) do |page|
-    page.refresh_until do
-      page.flow.header_element.exists? && page.flow.header.match(archive_name)
-    end
-    expect(page.content_element.when_present.text).to match @topic_title
+  visit(WikiPage, using_params: { page: "./User_talk:#{@username}" }) do |page|
+    page.refresh_until { page.flow.board_element.visible? }
+    page.flow.board_element.when_present
+
+    expect(page.flow.header).to match archive_name
+    expect(page.flow.header).to match 'Previous discussion was archived at'
+    expect(page.flow.header).to_not match 'This page is an archive.'
   end
 end
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I29e76ed6b090a636bc3427b6d588d825e4967eb3
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