jenkins-bot has submitted this change and it was merged.

Change subject: Import: Cleanup header by removing LQT magic word and adding 
template
......................................................................


Import: Cleanup header by removing LQT magic word and adding template

In support of this, the script user Flow uses (for this and for
ensureFlowRevision) has been made to actually exist.

Change-Id: I45b140358c32196018159cfc0c6cf0d7f16e339a
---
M i18n/en.json
M i18n/qqq.json
M includes/Import/LiquidThreadsApi/Objects.php
M includes/Import/LiquidThreadsApi/Source.php
M includes/TalkpageManager.php
M tests/phpunit/Import/TalkpageImportOperationTest.php
6 files changed, 149 insertions(+), 13 deletions(-)

Approvals:
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/i18n/en.json b/i18n/en.json
index 369a75c..70bb26c 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -378,6 +378,7 @@
        "flow-post-undo-hide": "(undo hide)",
        "flow-post-undo-delete": "(undo delete)",
        "flow-post-undo-suppress": "(undo suppress)",
+       "flow-importer-lqt-converted-template": "LQT page converted to Flow",
        "apihelp-flow-description": "Allows actions to be taken on Flow pages.",
        "apihelp-flow-param-submodule": "The Flow submodule to invoke.",
        "apihelp-flow-param-page": "The page to take the action on.",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index cfd195b..1e59ddc 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -15,7 +15,7 @@
        },
        "flow-desc": 
"{{desc|name=Flow|url=http://www.mediawiki.org/wiki/Extension:Flow}}";,
        "flow-talk-taken-over": "Content to replace existing page content by 
for pages that are turned into Flow boards.",
-       "flow-talk-username": "Username used for the revision added when Flow 
takes over a talk page.",
+       "flow-talk-username": "Username used for the revision added when Flow 
takes over a talk page.  Avoid changing this unnecessarily, as it will cause a 
new user to be used for future actions.",
        "log-name-flow": "{{doc-logpage}}\nName of the Flow log filter on the 
[[Special:Log]] page.",
        "logentry-delete-flow-delete-post": "Text for a deletion log entry when 
a post was deleted. Parameters:\n* $1 - the user: link to the user page\n* $2 - 
the username. Can be used for GENDER.\n* $3 - the page where the post was 
moderated\n* $4 - permalink URL to the moderated 
post\n{{Related|Flow-logentry}}",
        "logentry-delete-flow-restore-post": "Text for a deletion log entry 
when a deleted post was restored. Parameters:\n* $1 - the user: link to the 
user page\n* $2 - the username. Can be used for GENDER.\n* $3 - the page where 
the post was moderated\n* $4 - permalink URL to the moderated 
post\n{{Related|Flow-logentry}}",
@@ -382,6 +382,7 @@
        "flow-post-undo-hide": "Automatic moderation summary when undoing a 
hide that was just performed.",
        "flow-post-undo-delete": "Automatic moderation summary when undoing a 
delete that was just performed.",
        "flow-post-undo-suppress": "Automatic moderation summary when undoing a 
suppress that was just performed.",
+       "flow-importer-lqt-converted-template": "Name of a wikitext template 
that is added to the header of Flow boards that were converted from 
LiquidThreads",
        "apihelp-flow-description": "{{doc-apihelp-description|flow}}",
        "apihelp-flow-param-submodule": "{{doc-apihelp-param|flow|submodule}}",
        "apihelp-flow-param-page": "{{doc-apihelp-param|flow|page}}",
diff --git a/includes/Import/LiquidThreadsApi/Objects.php 
b/includes/Import/LiquidThreadsApi/Objects.php
index 734b1a7..3554604 100644
--- a/includes/Import/LiquidThreadsApi/Objects.php
+++ b/includes/Import/LiquidThreadsApi/Objects.php
@@ -3,6 +3,8 @@
 namespace Flow\Import\LiquidThreadsApi;
 
 use ArrayIterator;
+use DateTime;
+use DateTimeZone;
 use Flow\Import\IImportHeader;
 use Flow\Import\IImportObject;
 use Flow\Import\IImportPost;
@@ -12,6 +14,8 @@
 use Flow\Import\IObjectRevision;
 use Flow\Import\IRevisionableObject;
 use Iterator;
+use Title;
+use User;
 
 abstract class PageRevisionedObject implements IRevisionableObject {
        /** @var int **/
@@ -227,6 +231,55 @@
        }
 }
 
+// Represents a revision the script makes on its own behalf, using a script 
user
+class ScriptedImportRevision implements IObjectRevision {
+       /** @var IImportObject **/
+       protected $parentObject;
+
+       /** @var User */
+       protected $destinationScriptUser;
+
+       /** @var string */
+       protected $revisionText;
+
+       /** @var string */
+       protected $timestamp;
+
+       /**
+        * Creates a ScriptedImportRevision with the current timestamp, given a 
script user
+        * and arbitrary text.
+        *
+        * @param IImportObject $parentObject Object this is a revision of
+        * @parma User $destinationScriptUser User that performed this scripted 
edit
+        * @param string $revisionText Text of revision
+        */
+       function __construct( IImportObject $parentObject, User 
$destinationScriptUser, $revisionText ) {
+               $this->parent = $parentObject;
+               $this->destinationScriptUser = $destinationScriptUser;
+               $this->revisionText = $revisionText;
+               $this->timestamp = wfTimestampNow();
+       }
+
+       public function getText() {
+               return $this->revisionText;
+       }
+
+       public function getTimestamp() {
+               return $this->timestamp;
+       }
+
+       public function getAuthor() {
+               return $this->scriptUser->getName();
+       }
+
+       // XXX: This is called but never used, but if it were, including 
getText and getAuthor in
+       // the key might not be desirable, because we don't necessarily want to 
re-import
+       // the revision when these change.
+       public function getObjectKey() {
+               return $this->parent->getObjectKey() . ':rev:scripted:' . md5( 
$this->getText() . $this->getAuthor() );
+       }
+}
+
 class ImportHeader extends PageRevisionedObject implements IImportHeader {
        /** @var ApiBackend **/
        protected $api;
@@ -236,12 +289,20 @@
        protected $pageData;
        /** @var ImportSource **/
        protected $source;
+       /**
+        *  User used for script-originated actions, such as cleanup edits.
+        *  Does not apply to actual posts, which retain their original users.
+        *
+        *  @var User
+        */
+       protected $destinationScriptUser;
 
-       public function __construct( ApiBackend $api, ImportSource $source, 
$title ) {
+       public function __construct( ApiBackend $api, ImportSource $source, 
$title, User $destinationScriptUser ) {
                $this->api = $api;
                $this->title = $title;
                $this->source = $source;
                $this->pageData = null;
+               $this->destinationScriptUser = $destinationScriptUser;
        }
 
        public function getRevisions() {
@@ -252,7 +313,50 @@
                        $this->pageData = reset( $response );
                }
 
-               return new RevisionIterator( $this->pageData, $this );
+               $revisions = array();
+
+               if ( isset( $this->pageData['revisions'] ) && count( 
$this->pageData['revisions'] ) > 0 ) {
+                       $lastLqtRevision = new ImportRevision( end( 
$this->pageData['revisions'] ), $this );
+
+                       $titleObject = Title::newFromText( $this->title );
+                       $cleanupRevision = $this->createHeaderCleanupRevision( 
$lastLqtRevision, $titleObject );
+
+                       $revisions = array( $lastLqtRevision, $cleanupRevision 
);
+               }
+
+               return new ArrayIterator( $revisions );
+       }
+
+       /**
+        * @param IObjectRevision $lastRevision last imported header revision
+        * @param Title $archiveTitle archive page title associated with header
+        * @return IObjectRevision generated revision for cleanup edit
+        */
+       protected function createHeaderCleanupRevision( IObjectRevision 
$lastRevision, Title $archiveTitle ) {
+               $wikitextForLastRevision = $lastRevision->getText();
+               // This is will remove all instances, without attempting to 
check if it's in
+               // nowiki, etc.  It also ignores case and spaces in places 
where it doesn't
+               // matter.
+               $newWikitext = preg_replace(
+                       '/{{\s*#useliquidthreads:\s*1\s*}}/i',
+                       '',
+                       $wikitextForLastRevision
+               );
+               $templateName = wfMessage( 
'flow-importer-lqt-converted-template' )->inContentLanguage()->plain();
+               $now = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
+               $arguments = implode( '|', array(
+                       'archive=' . $archiveTitle->getPrefixedText(),
+                       'date=' . $now->format( 'Y-m-d' ),
+               ) );
+
+               $newWikitext .= "\n\n{{{$templateName}|$arguments}}";
+               $cleanupRevision = new ScriptedImportRevision(
+                       $this,
+                       $this->destinationScriptUser,
+                       $newWikitext,
+                       $lastRevision->getTimestamp()
+               );
+               return $cleanupRevision;
        }
 
        public function getObjectKey() {
diff --git a/includes/Import/LiquidThreadsApi/Source.php 
b/includes/Import/LiquidThreadsApi/Source.php
index d97a3b4..e311721 100644
--- a/includes/Import/LiquidThreadsApi/Source.php
+++ b/includes/Import/LiquidThreadsApi/Source.php
@@ -6,6 +6,7 @@
 use ApiMain;
 use Exception;
 use FauxRequest;
+use Flow\Container;
 use Flow\Import\ImportException;
 use Flow\Import\IImportSource;
 use Http;
@@ -48,7 +49,9 @@
         * {@inheritDoc}
         */
        public function getHeader() {
-               return new ImportHeader( $this->api, $this, $this->pageName );
+               $controller = Container::get( 'occupation_controller' );
+
+               return new ImportHeader( $this->api, $this, $this->pageName, 
$controller->getTalkpageManager() );
        }
 
        /**
diff --git a/includes/TalkpageManager.php b/includes/TalkpageManager.php
index 699c6de..ea33b0a 100644
--- a/includes/TalkpageManager.php
+++ b/includes/TalkpageManager.php
@@ -3,6 +3,7 @@
 namespace Flow;
 
 use Flow\Content\BoardContent;
+use Flow\Exception\FlowException;
 use Flow\Exception\InvalidInputException;
 use Flow\Model\Workflow;
 use Article;
@@ -132,19 +133,43 @@
         * Gives a user object used to manage talk pages
         *
         * @return User User to manage talkpages
+        * @throws MWException If both of the names already exist, but are not 
properly
+        *  configured.
         */
        public function getTalkpageManager() {
-               $user = User::newFromName(
-                       wfMessage( 'flow-talk-username' 
)->inContentLanguage()->text()
+               $userNameCandidates = array(
+                       wfMessage( 'flow-talk-username' 
)->inContentLanguage()->text(),
+                       'Flow talk page manager',
                );
-               // Use the English fallback if the localized username is 
invalid or if a user
-               // with the name exists.
-               if ( $user === false || $user->getId() !== 0 ) {
-                       $user = User::newFromName( 'Flow talk page manager', 
false );
+
+               $user = null;
+
+               foreach ( $userNameCandidates as $name ) {
+                       $candidateUser = User::newFromName( $name );
+
+                       if ( $candidateUser->getId() === 0 ) {
+                               $user = User::createNew( $name );
+                               $user->addGroup( 'bot' );
+                               break;
+                       } else {
+                               // Exists
+
+                               $groups = $candidateUser->getGroups();
+                               if ( in_array( 'bot', $groups ) ) {
+                                       // We created this user earlier.
+                                       $user = $candidateUser;
+                                       break;
+                               }
+
+                               // If it exists, but is not a bot, someone 
created this
+                               // without setting it up as expected, so go on 
to the next
+                               // user.
+                       }
                }
 
-               // prevent newtalk notification for takeover edit
-               $user->mRights[] = 'nominornewtalk';
+               if ( $user === null ) {
+                       throw new FlowException( 'All of the candidate 
usernames exist, but they are not configured as expected.' );
+               }
 
                return $user;
        }
diff --git a/tests/phpunit/Import/TalkpageImportOperationTest.php 
b/tests/phpunit/Import/TalkpageImportOperationTest.php
index 235a7cf..5b076c4 100644
--- a/tests/phpunit/Import/TalkpageImportOperationTest.php
+++ b/tests/phpunit/Import/TalkpageImportOperationTest.php
@@ -139,14 +139,16 @@
                }
 
                // Verify we wrote the expected objects to storage
+
                $this->assertEquals( 1, $storedHeader );
+
                $this->assertEquals( 1, $storedDiscussion );
                $this->assertEquals( 1, $storedTopics );
                $this->assertEquals( 1, $storedTopicListEntry );
                $this->assertEquals( 1, $storedSummary );
                $this->assertEquals( 3, $storedPosts );
 
-               // Eight objects should have been inserted in total
+               // This total expected number of insertions should match the 
sum of the left assertEquals parameters above.
                $this->assertCount( 8, array_unique( array_map( 
'spl_object_hash', $stored ) ) );
 
                // Other special cases we need to check

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I45b140358c32196018159cfc0c6cf0d7f16e339a
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <mflasc...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Mattflaschen <mflasc...@wikimedia.org>
Gerrit-Reviewer: SG <shah...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Werdna <agarr...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to