Maintenance script to import the contents of a text file into a wiki page
authorRob Church <robchurch@users.mediawiki.org>
Fri, 14 Apr 2006 17:24:43 +0000 (17:24 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Fri, 14 Apr 2006 17:24:43 +0000 (17:24 +0000)
maintenance/README
maintenance/importTextFile.inc [new file with mode: 0644]
maintenance/importTextFile.php [new file with mode: 0644]

index c9f92ee..1b49ed3 100644 (file)
@@ -50,6 +50,9 @@ installations.
 
        importDump.php
        XML dump importer
+       
+       importTextFile.php
+       Imports the contents of a text file into a wiki page
 
        nukePage.php
        Wipe a page and all revisions from the database
diff --git a/maintenance/importTextFile.inc b/maintenance/importTextFile.inc
new file mode 100644 (file)
index 0000000..7794827
--- /dev/null
@@ -0,0 +1,52 @@
+<?php\r
+\r
+/**\r
+ * Support functions for the importTextFile script\r
+ *\r
+ * @package MediaWiki\r
+ * @subpackage Maintenance\r
+ * @author Rob Church <robchur@gmail.com>\r
+ */\r
\r
+/**\r
+ * Insert a new article\r
+ *\r
+ * @param $title Title of the article\r
+ * @param $text Text of the article\r
+ * @param $user User associated with the edit\r
+ * @param $comment Edit summary\r
+ * @return bool\r
+ */\r
+function insertNewArticle( &$title, $text, &$user, $comment ) {\r
+       if( !$title->exists() ) {\r
+               # Create the article\r
+               $dbw =& wfGetDB( DB_MASTER );\r
+               $dbw->immediateBegin();\r
+               $article = new Article( $title );\r
+               $articleId = $article->insertOn( $dbw );\r
+               # Prepare and save associated revision\r
+               $revision = new Revision( array( 'page' => $articleId, 'text' => $text, 'user' => $user->mId, 'user_text' => $user->getName(), 'comment' => $comment ) );\r
+               $revisionId = $revision->insertOn( $dbw );\r
+               # Make it the current revision\r
+               $article->updateRevisionOn( $dbw, $revision );\r
+               $dbw->immediateCommit();\r
+               return( true );\r
+       } else {\r
+               # Title exists; touch nothing\r
+               return( false );\r
+       }\r
+}\r
+\r
+/**\r
+ * Turn a filename into a title\r
+ *\r
+ * @param $filename Filename to be transformed\r
+ * @return Title\r
+ */\r
+function titleFromFilename( $filename ) {\r
+       $parts = explode( '/', $filename );\r
+       $parts = explode( '.', $parts[ count( $parts ) - 1 ] );\r
+       return( Title::newFromText( $parts[0] ) );\r
+}\r
+\r
+?>
\ No newline at end of file
diff --git a/maintenance/importTextFile.php b/maintenance/importTextFile.php
new file mode 100644 (file)
index 0000000..c8b00cb
--- /dev/null
@@ -0,0 +1,102 @@
+<?php\r
+\r
+/**\r
+ * Maintenance script to insert an article, importing text from a file\r
+ *\r
+ * @package MediaWiki\r
+ * @subpackage Maintenance\r
+ * @author Rob Church <robchur@gmail.com>\r
+ */\r
+\r
+$options = array( 'help' ); \r
+$optionsWithArgs = array( 'title', 'user', 'comment' );\r
+require_once( 'commandLine.inc' );\r
+require_once( 'importTextFile.inc' );\r
+echo( "Import Text File\n\n" );\r
+\r
+if( !isset( $options['help'] ) || !$options['help'] ) {\r
+\r
+       # Check file existence\r
+       $filename = $args[0];\r
+       echo( "Using file '{$filename}'..." );\r
+       if( file_exists( $filename ) ) {\r
+               echo( "found.\n" );\r
+       \r
+               # Work out the title for the page       \r
+               if( isset( $option['title'] ) || trim( $options['title'] ) != '' ) {\r
+                       $titleText = $options['title'];\r
+                       # Use the supplied title\r
+                       echo( "Using title '{$titleText}'..." );\r
+                       $title = Title::newFromText( $options['title'] );\r
+               } else {\r
+                       # Attempt to make a title out of the filename\r
+                       echo( "Using title from filename..." );\r
+                       $title = titleFromFilename( $filename );\r
+               }\r
+               \r
+               # Check the title's valid\r
+               if( !is_null( $title ) && is_object( $title ) ) {\r
+                       echo( "ok.\n" );\r
+               \r
+                       # Read in the text\r
+                       $text = file_get_contents( $filename );\r
+                       \r
+                       # Check the supplied user and fall back to a default if needed\r
+                       if( isset( $options['user'] ) && trim( $options['user'] ) != '' ) {\r
+                               $username = $options['user'];\r
+                       } else {\r
+                               $username = 'MediaWiki default';\r
+                       }\r
+                       echo( "Using user '{$username}'..." );\r
+                       $user = User::newFromName( $username );\r
+                       \r
+                       # Check the user's valid\r
+                       if( !is_null( $user ) && is_object( $user ) ) {\r
+                               echo( "ok.\n" );\r
+                       \r
+                               # If a comment was supplied, use it (replace _ with spaces ) else use a default\r
+                               if( isset( $options['comment'] ) || trim( $options['comment'] != '' ) ) {\r
+                                       $comment = str_replace( '_', ' ', $options['comment'] );\r
+                               } else {\r
+                                       $comment = 'Importing text file';\r
+                               }\r
+                               echo( "Using edit summary '{$comment}'.\n" );\r
+                       \r
+                               # Attempt the insertion\r
+                               echo( "Attempting to insert page..." );\r
+                               $success = insertNewArticle( $title, $text, $user, $comment );\r
+                               if( $success ) {\r
+                                       echo( "done.\n" );\r
+                               } else {\r
+                                       echo( "failed. Title exists.\n" );\r
+                               }\r
+                       \r
+                       } else {\r
+                               # Dud user\r
+                               echo( "invalid username.\n" );\r
+                       }\r
+                       \r
+               } else {\r
+                       # Dud title\r
+                       echo( "invalid title.\n" );\r
+               }\r
+               \r
+       } else {\r
+               # File not found\r
+               echo( "not found.\n" );\r
+       }\r
+\r
+} else {\r
+       # Show help\r
+       echo( "Imports the contents of a text file into a wiki page.\n\n" );\r
+       echo( "USAGE: php importTextFile.php [--help|--title <title>|--user <user>|--comment <comment>] <filename>\n\n" );\r
+       echo( "              --help: Show this help information\n" );\r
+       echo( "    --title <title> : Title for the new page; if not supplied, the filename is used as a base for the title\n" );\r
+       echo( "      --user <user> : User to be associated with the edit; if not supplied, a default is used\n" );\r
+       echo( "--comment <comment> : Edit summary to be associated with the edit; underscores are transformed into spaces; if not supplied, a default is used\n" );\r
+       echo( "         <filename> : Path to the file containing the wikitext to import\n" );\r
+\r
+}\r
+echo( "\n" );  \r
+\r
+?>
\ No newline at end of file