Add maintenance script importTextFiles.php
authorAndrew H <crazy4sb@gmail.com>
Thu, 7 Jan 2016 03:09:47 +0000 (03:09 +0000)
committerAndrew H <crazy4sb@gmail.com>
Fri, 8 Jan 2016 00:57:11 +0000 (00:57 +0000)
importTextFiles.php can be used to import pages from text files
containing wikitext.

Also, added $userObj to WikiRevision so that it can accept a User
object instead of just a username.

This is a GCI task.

Change-Id: I20eaf2005bdd3d041f55d8c0b108f001c064d638

autoload.php
includes/import/WikiRevision.php
maintenance/importTextFiles.php [new file with mode: 0644]

index 5ee73a5..1f8c93a 100644 (file)
@@ -575,6 +575,7 @@ $wgAutoloadLocalClasses = array(
        'ImportSource' => __DIR__ . '/includes/import/ImportSource.php',
        'ImportStreamSource' => __DIR__ . '/includes/import/ImportStreamSource.php',
        'ImportStringSource' => __DIR__ . '/includes/import/ImportStringSource.php',
+       'ImportTextFiles' => __DIR__ . '/maintenance/importTextFiles.php',
        'ImportTitleFactory' => __DIR__ . '/includes/title/ImportTitleFactory.php',
        'IncludableSpecialPage' => __DIR__ . '/includes/specialpage/IncludableSpecialPage.php',
        'IndexPager' => __DIR__ . '/includes/pager/IndexPager.php',
index 8705bb0..d2cf7f6 100644 (file)
@@ -51,6 +51,9 @@ class WikiRevision {
        /** @var string */
        public $user_text = "";
 
+       /** @var User */
+       public $userObj = null;
+
        /** @var string */
        public $model = null;
 
@@ -154,6 +157,13 @@ class WikiRevision {
                $this->user_text = $user;
        }
 
+       /**
+        * @param User $user
+        */
+       function setUserObj( $user ) {
+               $this->userObj = $user;
+       }
+
        /**
         * @param string $ip
         */
@@ -296,6 +306,13 @@ class WikiRevision {
                return $this->user_text;
        }
 
+       /**
+        * @return User
+        */
+       function getUserObj() {
+               return $this->userObj;
+       }
+
        /**
         * @return string
         *
@@ -446,15 +463,14 @@ class WikiRevision {
                $dbw = wfGetDB( DB_MASTER );
 
                # Sneak a single revision into place
-               $user = User::newFromName( $this->getUser() );
+               $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
                if ( $user ) {
                        $userId = intval( $user->getId() );
                        $userText = $user->getName();
-                       $userObj = $user;
                } else {
                        $userId = 0;
                        $userText = $this->getUser();
-                       $userObj = new User;
+                       $user = new User;
                }
 
                // avoid memory leak...?
@@ -534,7 +550,7 @@ class WikiRevision {
                        // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
                        $page->doEditUpdates(
                                $revision,
-                               $userObj,
+                               $user,
                                array( 'created' => $created, 'oldcountable' => 'no-change' )
                        );
                }
@@ -545,7 +561,7 @@ class WikiRevision {
        function importLogItem() {
                $dbw = wfGetDB( DB_MASTER );
 
-               $user = User::newFromName( $this->getUser() );
+               $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
                if ( $user ) {
                        $userId = intval( $user->getId() );
                        $userText = $user->getName();
@@ -644,7 +660,7 @@ class WikiRevision {
                        return false;
                }
 
-               $user = User::newFromName( $this->user_text );
+               $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
 
                # Do the actual upload
                if ( $archiveName ) {
diff --git a/maintenance/importTextFiles.php b/maintenance/importTextFiles.php
new file mode 100644 (file)
index 0000000..b26eaa5
--- /dev/null
@@ -0,0 +1,131 @@
+<?php
+/**
+ * Import pages from text files
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+
+require_once __DIR__ . '/Maintenance.php';
+
+/**
+ * Maintenance script which reads in text files
+ * and imports their content to a page of the wiki.
+ *
+ * @ingroup Maintenance
+ */
+class ImportTextFiles extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "Reads in text files and imports their content to pages of the wiki";
+               $this->addOption( 'user', 'Username to which edits should be attributed. ' .
+                       'Default: "Maintenance script"', false, true, 'u' );
+               $this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' );
+               $this->addOption( 'use-timestamp', 'Use the modification date of the text file ' .
+                       'as the timestamp for the edit' );
+               $this->addArg( 'titles', 'Titles of article to edit' );
+       }
+
+       public function execute() {
+               $userName = $this->getOption( 'user', false );
+               $summary = $this->getOption( 'summary', 'Imported from text file' );
+               $useTimestamp = $this->hasOption( 'use-timestamp' );
+
+               // Get all the arguments. A loop is required since Maintenance doesn't
+               // suppport an arbitrary number of arguments.
+               $files = array();
+               $i = 0;
+               while ( $arg = $this->getArg( $i++ ) ) {
+                       if ( file_exists( $arg ) ) {
+                               $files[$arg] = file_get_contents( $arg );
+                       } else {
+                               $this->error( "Fatal error: The file '$arg' does not exist!", 1 );
+                       }
+               };
+
+               $count = count( $files );
+               $this->output( "Creating $count pages...\n" );
+
+               if ( $userName === false ) {
+                       $user = User::newSystemUser( 'Maintenance script', array( 'steal' => true ) );
+               } else {
+                       $user = User::newFromName( $userName );
+               }
+
+               if ( !$user ) {
+                       $this->error( "Invalid username\n", true );
+               }
+               if ( $user->isAnon() ) {
+                       $user->addToDatabase();
+               }
+
+               $exit = 0;
+
+               $successCount = 0;
+               $failCount = 0;
+               $skipCount = 0;
+
+               foreach ( $files as $file => $text ) {
+                       $pageName = pathinfo( $file, PATHINFO_FILENAME );
+                       $title = Title::newFromText( $pageName );
+                       if ( !$title ) {
+                               $this->error( "Invalid title $pageName. Skipping.\n" );
+                               $skipCount++;
+                               continue;
+                       }
+
+                       if ( $title->exists() ) {
+                               $actualTitle = $title->getPrefixedText();
+                               $this->output( "Title $pageName already exists. Skipping.\n" );
+                               $skipCount++;
+                               continue;
+                       }
+
+                       $actualTitle = $title->getPrefixedText();
+
+                       $rev = new WikiRevision( ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
+                       $rev->setText( $text );
+                       $rev->setTitle( $title );
+                       $rev->setUserObj( $user );
+                       $rev->setComment( $summary );
+                       if ( $useTimestamp ) {
+                               $rev->setTimestamp( wfTimestamp( TS_UNIX, filemtime( $file ) ) );
+                       } else {
+                               $rev->setTimestamp( wfTimestampNow() );
+                       }
+
+                       $status = $rev->importOldRevision();
+                       if ( $status ) {
+                               $this->output( "Successfully created $actualTitle\n" );
+                               $successCount++;
+                       } else {
+                               $actualTitle = $title->getPrefixedText();
+                               $this->output( "Failed to create $actualTitle\n" );
+                               $failCount++;
+                               $exit = 1;
+                       }
+               }
+               $this->output( "Done! $successCount successfully created, $skipCount skipped.\n" );
+               if ( $exit ) {
+                       $this->error( "Import failed with $failCount failed pages.\n", $exit );
+               }
+       }
+}
+
+$maintClass = "ImportTextFiles";
+require_once RUN_MAINTENANCE_IF_MAIN;