Andrew-WMDE has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/376778 )

Change subject: Add an import notice to the top of an imported page's wikitext
......................................................................

Add an import notice to the top of an imported page's wikitext

Should the message be translatable/changeable in i18n or should it
be hardcoded?

Bug: T160182
Change-Id: Ic32f8025338f81a64ccd060f3bd534ed3231212d
---
M extension.json
M i18n/en.json
M i18n/qqq.json
M src/Data/ImportPlan.php
M src/ServiceWiring.php
M src/Services/Importer.php
D src/Services/NullRevisionCreator.php
7 files changed, 48 insertions(+), 93 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/FileImporter 
refs/changes/78/376778/1

diff --git a/extension.json b/extension.json
index fc39999..448db26 100644
--- a/extension.json
+++ b/extension.json
@@ -55,7 +55,6 @@
                "FileImporter\\Services\\Importer": "src/Services/Importer.php",
                "FileImporter\\Services\\ImportPlanFactory": 
"src/Services/ImportPlanFactory.php",
                "FileImporter\\Services\\ImportPlanValidator": 
"src/Services/ImportPlanValidator.php",
-               "FileImporter\\Services\\NullRevisionCreator": 
"src/Services/NullRevisionCreator.php",
                "FileImporter\\Services\\SourceSite": 
"src/Services/SourceSite.php",
                "FileImporter\\Services\\SourceSiteLocator": 
"src/Services/SourceSiteLocator.php",
                "FileImporter\\Services\\WikiRevisionFactory": 
"src/Services/WikiRevisionFactory.php",
diff --git a/i18n/en.json b/i18n/en.json
index e9857b2..923600f 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -19,15 +19,15 @@
        "fileimporter-cantimportmissingfile": "Can't import a missing file.",
        "fileimporter-badtoken": "CSRF token does not match",
        "fileimporter-badimporthash": "Import hash does not match. Please 
restart the import.",
-       "fileimporter-filenameerror-default" : "There is an unknown issue with 
your filename.",
-       "fileimporter-filenameerror-illegal" : "The file name you are currently 
trying to import to is invalid.",
-       "fileimporter-filenameerror-toolong" : "The file name you are currently 
trying to import to is too long.",
-       "fileimporter-filenameerror-automaticchanges" : "The file name you are 
currently trying to import to will be automatically changed to \"$1\" due to 
the use of invalid characters.",
-       "fileimporter-localtitleexists" : "The title you are currently trying 
to import to already exists on this wiki.",
-       "fileimporter-sourcetitleexists" : "The title you are currently trying 
to import to already exists on the source wiki.",
-       "fileimporter-illegalfilenamechars" : "The title you are currently 
trying to import to contains invalid filename characters.",
-       "fileimporter-duplicatefilesdetected" : "The file you are currently 
trying to import already exists on this wiki.",
-       "fileimporter-duplicatefilesdetected-prefix" : "Duplicates",
+       "fileimporter-filenameerror-default": "There is an unknown issue with 
your filename.",
+       "fileimporter-filenameerror-illegal": "The file name you are currently 
trying to import to is invalid.",
+       "fileimporter-filenameerror-toolong": "The file name you are currently 
trying to import to is too long.",
+       "fileimporter-filenameerror-automaticchanges": "The file name you are 
currently trying to import to will be automatically changed to \"$1\" due to 
the use of invalid characters.",
+       "fileimporter-localtitleexists": "The title you are currently trying to 
import to already exists on this wiki.",
+       "fileimporter-sourcetitleexists": "The title you are currently trying 
to import to already exists on the source wiki.",
+       "fileimporter-illegalfilenamechars": "The title you are currently 
trying to import to contains invalid filename characters.",
+       "fileimporter-duplicatefilesdetected": "The file you are currently 
trying to import already exists on this wiki.",
+       "fileimporter-duplicatefilesdetected-prefix": "Duplicates",
        "fileimporter-exampleprefix": "Example",
        "fileimporter-textrevisions": "$1 {{PLURAL:$1|revision|revisions}} of 
the file will be imported.",
        "fileimporter-submit": "Submit",
@@ -47,5 +47,7 @@
        "fileimporter-heading-filehistory": "File history",
        "fileimporter-revision-removed-text": "Suppressed text removed by 
FileImporter.",
        "fileimporter-revision-removed-comment": "Suppressed comment removed by 
FileImporter.",
-       "fileimporter-in-beta": "File import is in beta mode: We tested it 
carefully but it still might have some problems. If you encounter problems, 
please [//www.mediawiki.org/w/index.php?title=Extension_talk:FileImporter 
report them on our help page] - ideally with a step by step way to reproduce 
the problem, so we can understand and fix it."
+       "fileimporter-in-beta": "File import is in beta mode: We tested it 
carefully but it still might have some problems. If you encounter problems, 
please [//www.mediawiki.org/w/index.php?title=Extension_talk:FileImporter 
report them on our help page] - ideally with a step by step way to reproduce 
the problem, so we can understand and fix it.",
+       "fileimporter-comment-for-post-import-revision": "Imported with 
FileImporter from $1",
+       "fileimporter-text-for-post-import-revision": "<!--This file was moved 
here using FileImporter from $1-->\n"
 }
\ No newline at end of file
diff --git a/i18n/qqq.json b/i18n/qqq.json
index f6919e4..ecbf93f 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -49,5 +49,7 @@
        "fileimporter-heading-filehistory": "Heading of the file history on the 
import page.",
        "fileimporter-revision-removed-text": "Text inserted if the original 
text is suppressed.",
        "fileimporter-revision-removed-comment": "Text inserted as a revision 
comment if the original comment is suppressed.",
-       "fileimporter-in-beta": "Text shown when the special page is loaded 
warning the user that the extension is in beta."
+       "fileimporter-in-beta": "Text shown when the special page is loaded 
warning the user that the extension is in beta.",
+       "fileimporter-comment-for-post-import-revision": "Text used for the 
edit summary of a post import revision.",
+       "fileimporter-text-for-post-import-revision": "Text added to the top of 
the imported page's wikitext"
 }
diff --git a/src/Data/ImportPlan.php b/src/Data/ImportPlan.php
index 3f83a56..449dc1e 100644
--- a/src/Data/ImportPlan.php
+++ b/src/Data/ImportPlan.php
@@ -6,6 +6,7 @@
 use MediaWiki\MediaWikiServices;
 use RuntimeException;
 use Title;
+use Message;
 
 /**
  * Planned import.
@@ -44,6 +45,10 @@
        public function __construct( ImportRequest $request, ImportDetails 
$details ) {
                $this->request = $request;
                $this->details = $details;
+
+               $this->fileInfoText =
+                       ( new Message( 
"fileimporter-text-for-post-import-revision", [ $request->getUrl() ]
+                       ) )->plain() . 
$this->details->getTextRevisions()->getLatest()->getField( '*' );
        }
 
        /**
@@ -109,13 +114,17 @@
         * @return string
         */
        public function getFileInfoText() {
-               if ( $this->fileInfoText === null ) {
-                       $intendedWikiText = $this->request->getIntendedText();
-                       if ( $intendedWikiText ) {
-                               return $intendedWikiText;
-                       }
+               $intendedWikiText = $this->request->getIntendedText();
+               if ( $intendedWikiText ) {
+                       return $intendedWikiText;
                }
-               return 
$this->getDetails()->getTextRevisions()->getLatest()->getField( '*' );
+               return $this->fileInfoText;
        }
 
+       /**
+        * @return string
+        */
+       public function getOriginalFileInfoText() {
+               return $this->fileInfoText;
+       }
 }
diff --git a/src/ServiceWiring.php b/src/ServiceWiring.php
index 6910d24..3753113 100644
--- a/src/ServiceWiring.php
+++ b/src/ServiceWiring.php
@@ -6,7 +6,6 @@
 use FileImporter\Services\Http\HttpRequestExecutor;
 use FileImporter\Services\Importer;
 use FileImporter\Services\ImportPlanFactory;
-use FileImporter\Services\NullRevisionCreator;
 use FileImporter\Services\SourceSiteLocator;
 use FileImporter\Services\UploadBase\UploadBaseFactory;
 use FileImporter\Services\WikiRevisionFactory;
@@ -47,24 +46,17 @@
        'FileImporterImporter' => function ( MediaWikiServices $services ) {
                /** @var WikiRevisionFactory $wikiRevisionFactory */
                $wikiRevisionFactory = $services->getService( 
'FileImporterWikiRevisionFactory' );
-               /** @var NullRevisionCreator $nullRevisionCreator */
-               $nullRevisionCreator = $services->getService( 
'FileImporterNullRevisionCreator' );
                /** @var HttpRequestExecutor $httpRequestExecutor */
                $httpRequestExecutor = $services->getService( 
'FileImporterHttpRequestExecutor' );
                /** @var UploadBaseFactory $uploadBaseFactory */
                $uploadBaseFactory = $services->getService( 
'FileImporterUploadBaseFactory' );
                $importer = new Importer(
                        $wikiRevisionFactory,
-                       $nullRevisionCreator,
                        $httpRequestExecutor,
                        $uploadBaseFactory
                );
                $importer->setLogger( LoggerFactory::getInstance( 
'FileImporter' ) );
                return $importer;
-       },
-
-       'FileImporterNullRevisionCreator' => function ( MediaWikiServices 
$services ) {
-               return new NullRevisionCreator( $services->getDBLoadBalancer() 
);
        },
 
        'FileImporterWikiRevisionFactory' => function ( MediaWikiServices 
$services ) {
diff --git a/src/Services/Importer.php b/src/Services/Importer.php
index c697504..017e057 100644
--- a/src/Services/Importer.php
+++ b/src/Services/Importer.php
@@ -16,6 +16,7 @@
 use RuntimeException;
 use Title;
 use User;
+use Message;
 
 /**
  * Performs an import of a file to the local wiki based on an ImportPlan 
object for a given User.
@@ -26,11 +27,6 @@
         * @var WikiRevisionFactory
         */
        private $wikiRevisionFactory;
-
-       /**
-        * @var NullRevisionCreator
-        */
-       private $nullRevisionCreator;
 
        /**
         * @var HttpRequestExecutor
@@ -49,18 +45,15 @@
 
        /**
         * @param WikiRevisionFactory $wikiRevisionFactory
-        * @param NullRevisionCreator $nullRevisionCreator
         * @param HttpRequestExecutor $httpRequestExecutor
         * @param UploadBaseFactory $uploadBaseFactory
         */
        public function __construct(
                WikiRevisionFactory $wikiRevisionFactory,
-               NullRevisionCreator $nullRevisionCreator,
                HttpRequestExecutor $httpRequestExecutor,
                UploadBaseFactory $uploadBaseFactory
        ) {
                $this->wikiRevisionFactory = $wikiRevisionFactory;
-               $this->nullRevisionCreator = $nullRevisionCreator;
                $this->httpRequestExecutor = $httpRequestExecutor;
                $this->uploadBaseFactory = $uploadBaseFactory;
                $this->logger = new NullLogger();
@@ -121,7 +114,7 @@
 
                // TODO the below should be an ImportOperation
                $articleIdForUpdate = $this->getArticleIdForUpdate( $importPlan 
);
-               $this->createPostImportNullRevision( $importPlan, 
$articleIdForUpdate, $user );
+               $this->createPostImportRevision( $importPlan, 
$articleIdForUpdate, $user );
                $this->createPostImportEdit( $importPlan, $articleIdForUpdate, 
$user );
 
                // TODO do we need to call WikiImporter::finishImportPage??
@@ -148,17 +141,27 @@
         * @param int $articleIdForUpdate
         * @param User $user
         */
-       private function createPostImportNullRevision(
+       private function createPostImportRevision(
                ImportPlan $importPlan,
                $articleIdForUpdate,
                User $user
        ) {
-               $this->nullRevisionCreator->createForLinkTarget(
-                       $articleIdForUpdate,
-                       $user,
-                       'Imported from ' . $importPlan->getRequest()->getUrl(), 
// TODO i18n
-                       true
+
+               $page = \WikiPage::newFromID( $articleIdForUpdate );
+               $editResult = $page->doEditContent(
+                       new \WikitextContent( 
$importPlan->getOriginalFileInfoText() ),
+                       ( new Message(
+                               'fileimporter-comment-for-post-import-revision',
+                               [ $importPlan->getRequest()->getUrl() ]
+                       ) )->plain(),
+                       EDIT_MINOR,
+                       false,
+                       $user
                );
+
+               if ( !$editResult->isOK() ) {
+                       throw new RuntimeException( 'Failed to create import 
edit' );
+               }
        }
 
        /**
diff --git a/src/Services/NullRevisionCreator.php 
b/src/Services/NullRevisionCreator.php
deleted file mode 100644
index 84c5632..0000000
--- a/src/Services/NullRevisionCreator.php
+++ /dev/null
@@ -1,52 +0,0 @@
-<?php
-
-namespace FileImporter\Services;
-
-use Revision;
-use RuntimeException;
-use User;
-use Wikimedia\Rdbms\LoadBalancer;
-
-class NullRevisionCreator {
-
-       /**
-        * @var LoadBalancer
-        */
-       private $loadBalancer;
-
-       public function __construct(
-               LoadBalancer $loadBalancer
-       ) {
-               $this->loadBalancer = $loadBalancer;
-       }
-
-       /**
-        * @param int $pageId
-        * @param User $user
-        * @param string $summary
-        * @param bool $minor
-        *
-        * @throws RuntimeException
-        * @return Revision
-        */
-       public function createForLinkTarget( $pageId, User $user, $summary, 
$minor ) {
-               $dbw = $this->loadBalancer->getConnection( DB_MASTER );
-
-               $revision = Revision::newNullRevision(
-                       $dbw,
-                       $pageId,
-                       $summary,
-                       $minor,
-                       $user
-               );
-
-               if ( $revision === null ) {
-                       throw new RuntimeException( 'Failed to create null 
revision' );
-               }
-
-               $revision->insertOn( $dbw );
-
-               return $revision;
-       }
-
-}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic32f8025338f81a64ccd060f3bd534ed3231212d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/FileImporter
Gerrit-Branch: master
Gerrit-Owner: Andrew-WMDE <andrew.kos...@wikimedia.de>

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

Reply via email to