summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/importTextFiles.php
blob: c99aa155313b9a7321bda55aa57ca1853a6acb7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?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
 */

use MediaWiki\MediaWikiServices;

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->addDescription( '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->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, this ' .
			'will only overwrite pages if the file has been modified since the page was last modified.' );
		$this->addOption( 'prefix', 'A string to place in front of the file name', false, true, 'p' );
		$this->addOption( 'bot', 'Mark edits as bot edits in the recent changes list.' );
		$this->addOption( 'rc', 'Place revisions in RecentChanges.' );
		$this->addArg( 'files', 'Files to import' );
	}

	public function execute() {
		$userName = $this->getOption( 'user', false );
		$summary = $this->getOption( 'summary', 'Imported from text file' );
		$useTimestamp = $this->hasOption( 'use-timestamp' );
		$rc = $this->hasOption( 'rc' );
		$bot = $this->hasOption( 'bot' );
		$overwrite = $this->hasOption( 'overwrite' );
		$prefix = $this->getOption( 'prefix', '' );

		// Get all the arguments. A loop is required since Maintenance doesn't
		// support an arbitrary number of arguments.
		$files = [];
		$i = 0;
		while ( $arg = $this->getArg( $i++ ) ) {
			if ( file_exists( $arg ) ) {
				$files[$arg] = file_get_contents( $arg );
			} else {
				// use glob to support the Windows shell, which doesn't automatically
				// expand wildcards
				$found = false;
				foreach ( glob( $arg ) as $filename ) {
					$found = true;
					$files[$filename] = file_get_contents( $filename );
				}
				if ( !$found ) {
					$this->fatalError( "Fatal error: The file '$arg' does not exist!" );
				}
			}
		};

		$count = count( $files );
		$this->output( "Importing $count pages...\n" );

		if ( $userName === false ) {
			$user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
		} else {
			$user = User::newFromName( $userName );
		}

		if ( !$user ) {
			$this->fatalError( "Invalid username\n" );
		}
		if ( $user->isAnon() ) {
			$user->addToDatabase();
		}

		$exit = 0;

		$successCount = 0;
		$failCount = 0;
		$skipCount = 0;

		foreach ( $files as $file => $text ) {
			$pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
			$timestamp = $useTimestamp ? wfTimestamp( TS_UNIX, filemtime( $file ) ) : wfTimestampNow();

			$title = Title::newFromText( $pageName );
			// Have to check for # manually, since it gets interpreted as a fragment
			if ( !$title || $title->hasFragment() ) {
				$this->error( "Invalid title $pageName. Skipping.\n" );
				$skipCount++;
				continue;
			}

			$exists = $title->exists();
			$oldRevID = $title->getLatestRevID();
			$oldRev = $oldRevID ? Revision::newFromId( $oldRevID ) : null;
			$actualTitle = $title->getPrefixedText();

			if ( $exists ) {
				$touched = wfTimestamp( TS_UNIX, $title->getTouched() );
				if ( !$overwrite ) {
					$this->output( "Title $actualTitle already exists. Skipping.\n" );
					$skipCount++;
					continue;
				} elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
					$this->output( "File for title $actualTitle has not been modified since the " .
						"destination page was touched. Skipping.\n" );
					$skipCount++;
					continue;
				}
			}

			$rev = new WikiRevision( MediaWikiServices::getInstance()->getMainConfig() );
			$rev->setText( rtrim( $text ) );
			$rev->setTitle( $title );
			$rev->setUserObj( $user );
			$rev->setComment( $summary );
			$rev->setTimestamp( $timestamp );

			if ( $exists && $overwrite && $rev->getContent()->equals( $oldRev->getContent() ) ) {
				$this->output( "File for title $actualTitle contains no changes from the current " .
					"revision. Skipping.\n" );
				$skipCount++;
				continue;
			}

			$status = $rev->importOldRevision();
			$newId = $title->getLatestRevID();

			if ( $status ) {
				$action = $exists ? 'updated' : 'created';
				$this->output( "Successfully $action $actualTitle\n" );
				$successCount++;
			} else {
				$action = $exists ? 'update' : 'create';
				$this->output( "Failed to $action $actualTitle\n" );
				$failCount++;
				$exit = 1;
			}

			// Create the RecentChanges entry if necessary
			if ( $rc && $status ) {
				if ( $exists ) {
					if ( is_object( $oldRev ) ) {
						$oldContent = $oldRev->getContent();
						RecentChange::notifyEdit(
							$timestamp,
							$title,
							$rev->getMinor(),
							$user,
							$summary,
							$oldRevID,
							$oldRev->getTimestamp(),
							$bot,
							'',
							$oldContent ? $oldContent->getSize() : 0,
							$rev->getContent()->getSize(),
							$newId,
							1 /* the pages don't need to be patrolled */
						);
					}
				} else {
					RecentChange::notifyNew(
						$timestamp,
						$title,
						$rev->getMinor(),
						$user,
						$summary,
						$bot,
						'',
						$rev->getContent()->getSize(),
						$newId,
						1
					);
				}
			}
		}

		$this->output( "Done! $successCount succeeded, $skipCount skipped.\n" );
		if ( $exit ) {
			$this->fatalError( "Import failed with $failCount failed pages.\n", $exit );
		}
	}
}

$maintClass = ImportTextFiles::class;
require_once RUN_MAINTENANCE_IF_MAIN;