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 use MediaWiki\Revision\SlotRecord
;
25 use MediaWiki\Title\Title
;
26 use MediaWiki\User\User
;
28 require_once __DIR__
. '/Maintenance.php';
31 * Maintenance script which reads in text files
32 * and imports their content to a page of the wiki.
34 * @ingroup Maintenance
36 class ImportTextFiles
extends Maintenance
{
37 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( 'Reads in text files and imports their content to pages of the wiki' );
40 $this->addOption( 'user', 'Username to which edits should be attributed. ' .
41 'Default: "Maintenance script"', false, true, 'u' );
42 $this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' );
43 $this->addOption( 'use-timestamp', 'Use the modification date of the text file ' .
44 'as the timestamp for the edit' );
45 $this->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, this ' .
46 'will only overwrite pages if the file has been modified since the page was last modified.' );
47 $this->addOption( 'prefix', 'A string to place in front of the file name', false, true, 'p' );
48 $this->addOption( 'bot', 'Mark edits as bot edits in the recent changes list.' );
49 $this->addOption( 'rc', 'Place revisions in RecentChanges.' );
50 $this->addArg( 'files', 'Files to import' );
53 public function execute() {
54 $userName = $this->getOption( 'user', false );
55 $summary = $this->getOption( 'summary', 'Imported from text file' );
56 $useTimestamp = $this->hasOption( 'use-timestamp' );
57 $rc = $this->hasOption( 'rc' );
58 $bot = $this->hasOption( 'bot' );
59 $overwrite = $this->hasOption( 'overwrite' );
60 $prefix = $this->getOption( 'prefix', '' );
62 // Get all the arguments. A loop is required since Maintenance doesn't
63 // support an arbitrary number of arguments.
66 while ( $arg = $this->getArg( $i++
) ) {
67 if ( file_exists( $arg ) ) {
68 $files[$arg] = file_get_contents( $arg );
70 // use glob to support the Windows shell, which doesn't automatically
73 foreach ( glob( $arg ) as $filename ) {
75 $files[$filename] = file_get_contents( $filename );
78 $this->fatalError( "Fatal error: The file '$arg' does not exist!" );
83 $count = count( $files );
84 $this->output( "Importing $count pages...\n" );
86 if ( $userName === false ) {
87 $user = User
::newSystemUser( User
::MAINTENANCE_SCRIPT_USER
, [ 'steal' => true ] );
89 $user = User
::newFromName( $userName );
93 $this->fatalError( "Invalid username\n" );
95 if ( $user->isAnon() ) {
96 $user->addToDatabase();
105 $revLookup = $this->getServiceContainer()->getRevisionLookup();
106 foreach ( $files as $file => $text ) {
107 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME
);
108 $timestamp = $useTimestamp ?
wfTimestamp( TS_UNIX
, filemtime( $file ) ) : wfTimestampNow();
110 $title = Title
::newFromText( $pageName );
111 // Have to check for # manually, since it gets interpreted as a fragment
112 if ( !$title ||
$title->hasFragment() ) {
113 $this->error( "Invalid title $pageName. Skipping.\n" );
118 $exists = $title->exists();
119 $oldRevID = $title->getLatestRevID();
120 $oldRevRecord = $oldRevID ?
$revLookup->getRevisionById( $oldRevID ) : null;
121 $actualTitle = $title->getPrefixedText();
124 $touched = wfTimestamp( TS_UNIX
, $title->getTouched() );
126 $this->output( "Title $actualTitle already exists. Skipping.\n" );
129 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
130 $this->output( "File for title $actualTitle has not been modified since the " .
131 "destination page was touched. Skipping.\n" );
137 $content = ContentHandler
::makeContent( rtrim( $text ), $title );
138 $rev = new WikiRevision();
139 $rev->setContent( SlotRecord
::MAIN
, $content );
140 $rev->setTitle( $title );
141 $rev->setUserObj( $user );
142 $rev->setComment( $summary );
143 $rev->setTimestamp( $timestamp );
147 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord
::MAIN
) )
149 $this->output( "File for title $actualTitle contains no changes from the current " .
150 "revision. Skipping.\n" );
155 $status = $rev->importOldRevision();
156 $newId = $title->getLatestRevID();
159 $action = $exists ?
'updated' : 'created';
160 $this->output( "Successfully $action $actualTitle\n" );
163 $action = $exists ?
'update' : 'create';
164 $this->output( "Failed to $action $actualTitle\n" );
169 // Create the RecentChanges entry if necessary
170 if ( $rc && $status ) {
172 if ( is_object( $oldRevRecord ) ) {
173 RecentChange
::notifyEdit(
180 $oldRevRecord->getTimestamp(),
183 $oldRevRecord->getSize(),
186 // the pages don't need to be patrolled
191 RecentChange
::notifyNew(
207 $this->output( "Done! $successCount succeeded, $skipCount skipped.\n" );
209 $this->fatalError( "Import failed with $failCount failed pages.\n", $exit );
214 $maintClass = ImportTextFiles
::class;
215 require_once RUN_MAINTENANCE_IF_MAIN
;