3 * Import pages from text files
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script which reads in text files
28 * and imports their content to a page of the wiki.
30 * @ingroup Maintenance
32 class ImportTextFiles
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->mDescription
= "Reads in text files and imports their content to pages of the wiki";
36 $this->addOption( 'user', 'Username to which edits should be attributed. ' .
37 'Default: "Maintenance script"', false, true, 'u' );
38 $this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' );
39 $this->addOption( 'use-timestamp', 'Use the modification date of the text file ' .
40 'as the timestamp for the edit' );
41 $this->addArg( 'titles', 'Titles of article to edit' );
44 public function execute() {
45 $userName = $this->getOption( 'user', false );
46 $summary = $this->getOption( 'summary', 'Imported from text file' );
47 $useTimestamp = $this->hasOption( 'use-timestamp' );
49 // Get all the arguments. A loop is required since Maintenance doesn't
50 // suppport an arbitrary number of arguments.
53 while ( $arg = $this->getArg( $i++
) ) {
54 if ( file_exists( $arg ) ) {
55 $files[$arg] = file_get_contents( $arg );
57 $this->error( "Fatal error: The file '$arg' does not exist!", 1 );
61 $count = count( $files );
62 $this->output( "Creating $count pages...\n" );
64 if ( $userName === false ) {
65 $user = User
::newSystemUser( 'Maintenance script', array( 'steal' => true ) );
67 $user = User
::newFromName( $userName );
71 $this->error( "Invalid username\n", true );
73 if ( $user->isAnon() ) {
74 $user->addToDatabase();
83 foreach ( $files as $file => $text ) {
84 $pageName = pathinfo( $file, PATHINFO_FILENAME
);
85 $title = Title
::newFromText( $pageName );
87 $this->error( "Invalid title $pageName. Skipping.\n" );
92 if ( $title->exists() ) {
93 $actualTitle = $title->getPrefixedText();
94 $this->output( "Title $pageName already exists. Skipping.\n" );
99 $actualTitle = $title->getPrefixedText();
101 $rev = new WikiRevision( ConfigFactory
::getDefaultInstance()->makeConfig( 'main' ) );
102 $rev->setText( $text );
103 $rev->setTitle( $title );
104 $rev->setUserObj( $user );
105 $rev->setComment( $summary );
106 if ( $useTimestamp ) {
107 $rev->setTimestamp( wfTimestamp( TS_UNIX
, filemtime( $file ) ) );
109 $rev->setTimestamp( wfTimestampNow() );
112 $status = $rev->importOldRevision();
114 $this->output( "Successfully created $actualTitle\n" );
117 $actualTitle = $title->getPrefixedText();
118 $this->output( "Failed to create $actualTitle\n" );
123 $this->output( "Done! $successCount successfully created, $skipCount skipped.\n" );
125 $this->error( "Import failed with $failCount failed pages.\n", $exit );
130 $maintClass = "ImportTextFiles";
131 require_once RUN_MAINTENANCE_IF_MAIN
;