Merge "Revert "Convert Special:EmailUser to use OOUIHTMLForm""
[lhc/web/wiklou.git] / maintenance / importTextFiles.php
1 <?php
2 /**
3 * Import pages from text files
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script which reads in text files
28 * and imports their content to a page of the wiki.
29 *
30 * @ingroup Maintenance
31 */
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' );
42 }
43
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' );
48
49 // Get all the arguments. A loop is required since Maintenance doesn't
50 // suppport an arbitrary number of arguments.
51 $files = array();
52 $i = 0;
53 while ( $arg = $this->getArg( $i++ ) ) {
54 if ( file_exists( $arg ) ) {
55 $files[$arg] = file_get_contents( $arg );
56 } else {
57 $this->error( "Fatal error: The file '$arg' does not exist!", 1 );
58 }
59 };
60
61 $count = count( $files );
62 $this->output( "Creating $count pages...\n" );
63
64 if ( $userName === false ) {
65 $user = User::newSystemUser( 'Maintenance script', array( 'steal' => true ) );
66 } else {
67 $user = User::newFromName( $userName );
68 }
69
70 if ( !$user ) {
71 $this->error( "Invalid username\n", true );
72 }
73 if ( $user->isAnon() ) {
74 $user->addToDatabase();
75 }
76
77 $exit = 0;
78
79 $successCount = 0;
80 $failCount = 0;
81 $skipCount = 0;
82
83 foreach ( $files as $file => $text ) {
84 $pageName = pathinfo( $file, PATHINFO_FILENAME );
85 $title = Title::newFromText( $pageName );
86 if ( !$title ) {
87 $this->error( "Invalid title $pageName. Skipping.\n" );
88 $skipCount++;
89 continue;
90 }
91
92 if ( $title->exists() ) {
93 $actualTitle = $title->getPrefixedText();
94 $this->output( "Title $pageName already exists. Skipping.\n" );
95 $skipCount++;
96 continue;
97 }
98
99 $actualTitle = $title->getPrefixedText();
100
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 ) ) );
108 } else {
109 $rev->setTimestamp( wfTimestampNow() );
110 }
111
112 $status = $rev->importOldRevision();
113 if ( $status ) {
114 $this->output( "Successfully created $actualTitle\n" );
115 $successCount++;
116 } else {
117 $actualTitle = $title->getPrefixedText();
118 $this->output( "Failed to create $actualTitle\n" );
119 $failCount++;
120 $exit = 1;
121 }
122 }
123 $this->output( "Done! $successCount successfully created, $skipCount skipped.\n" );
124 if ( $exit ) {
125 $this->error( "Import failed with $failCount failed pages.\n", $exit );
126 }
127 }
128 }
129
130 $maintClass = "ImportTextFiles";
131 require_once RUN_MAINTENANCE_IF_MAIN;