MediaWiki master
importTextFiles.php
Go to the documentation of this file.
1<?php
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Reads in text files and imports their content to pages of the wiki' );
44 $this->addOption( 'user', 'Username to which edits should be attributed. ' .
45 'Default: "Maintenance script"', false, true, 'u' );
46 $this->addOption( 'summary', 'Specify edit summary for the edits', false, true, 's' );
47 $this->addOption( 'use-timestamp', 'Use the modification date of the text file ' .
48 'as the timestamp for the edit' );
49 $this->addOption( 'overwrite', 'Overwrite existing pages. If --use-timestamp is passed, this ' .
50 'will only overwrite pages if the file has been modified since the page was last modified.' );
51 $this->addOption( 'prefix', 'A string to place in front of the file name', false, true, 'p' );
52 $this->addOption( 'bot', 'Mark edits as bot edits in the recent changes list.' );
53 $this->addOption( 'rc', 'Place revisions in RecentChanges.' );
54 $this->addArg( 'files', 'Files to import' );
55 }
56
57 public function execute() {
58 $userName = $this->getOption( 'user', false );
59 $summary = $this->getOption( 'summary', 'Imported from text file' );
60 $useTimestamp = $this->hasOption( 'use-timestamp' );
61 $rc = $this->hasOption( 'rc' );
62 $bot = $this->hasOption( 'bot' );
63 $overwrite = $this->hasOption( 'overwrite' );
64 $prefix = $this->getOption( 'prefix', '' );
65
66 // Get all the arguments. A loop is required since Maintenance doesn't
67 // support an arbitrary number of arguments.
68 $files = [];
69 $i = 0;
70 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
71 while ( $arg = $this->getArg( $i++ ) ) {
72 if ( file_exists( $arg ) ) {
73 $files[$arg] = file_get_contents( $arg );
74 } else {
75 // use glob to support the Windows shell, which doesn't automatically
76 // expand wildcards
77 $found = false;
78 foreach ( glob( $arg ) as $filename ) {
79 $found = true;
80 $files[$filename] = file_get_contents( $filename );
81 }
82 if ( !$found ) {
83 $this->fatalError( "Fatal error: The file '$arg' does not exist!" );
84 }
85 }
86 }
87
88 $count = count( $files );
89 $this->output( "Importing $count pages...\n" );
90
91 if ( $userName === false ) {
92 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
93 } else {
94 $user = User::newFromName( $userName );
95 }
96
97 if ( !$user ) {
98 $this->fatalError( "Invalid username\n" );
99 }
100 if ( $user->isAnon() ) {
101 $user->addToDatabase();
102 }
103
104 $exit = 0;
105
106 $successCount = 0;
107 $failCount = 0;
108 $skipCount = 0;
109
110 $revLookup = $this->getServiceContainer()->getRevisionLookup();
111 foreach ( $files as $file => $text ) {
112 $pageName = $prefix . pathinfo( $file, PATHINFO_FILENAME );
113 $timestamp = $useTimestamp ? wfTimestamp( TS_UNIX, filemtime( $file ) ) : wfTimestampNow();
114
115 $title = Title::newFromText( $pageName );
116 // Have to check for # manually, since it gets interpreted as a fragment
117 if ( !$title || $title->hasFragment() ) {
118 $this->error( "Invalid title $pageName. Skipping.\n" );
119 $skipCount++;
120 continue;
121 }
122
123 $exists = $title->exists();
124 $oldRevID = $title->getLatestRevID();
125 $oldRevRecord = $oldRevID ? $revLookup->getRevisionById( $oldRevID ) : null;
126 $actualTitle = $title->getPrefixedText();
127
128 if ( $exists ) {
129 $touched = wfTimestamp( TS_UNIX, $title->getTouched() );
130 if ( !$overwrite ) {
131 $this->output( "Title $actualTitle already exists. Skipping.\n" );
132 $skipCount++;
133 continue;
134 } elseif ( $useTimestamp && intval( $touched ) >= intval( $timestamp ) ) {
135 $this->output( "File for title $actualTitle has not been modified since the " .
136 "destination page was touched. Skipping.\n" );
137 $skipCount++;
138 continue;
139 }
140 }
141
142 $content = ContentHandler::makeContent( rtrim( $text ), $title );
143 $rev = new WikiRevision();
144 $rev->setContent( SlotRecord::MAIN, $content );
145 $rev->setTitle( $title );
146 $rev->setUserObj( $user );
147 $rev->setComment( $summary );
148 $rev->setTimestamp( $timestamp );
149
150 if ( $exists &&
151 $overwrite &&
152 $rev->getContent()->equals( $oldRevRecord->getContent( SlotRecord::MAIN ) )
153 ) {
154 $this->output( "File for title $actualTitle contains no changes from the current " .
155 "revision. Skipping.\n" );
156 $skipCount++;
157 continue;
158 }
159
160 $status = $rev->importOldRevision();
161 $newId = $title->getLatestRevID();
162
163 if ( $status ) {
164 $action = $exists ? 'updated' : 'created';
165 $this->output( "Successfully $action $actualTitle\n" );
166 $successCount++;
167 } else {
168 $action = $exists ? 'update' : 'create';
169 $this->output( "Failed to $action $actualTitle\n" );
170 $failCount++;
171 $exit = 1;
172 }
173
174 // Create the RecentChanges entry if necessary
175 if ( $rc && $status ) {
176 if ( $exists ) {
177 if ( is_object( $oldRevRecord ) ) {
178 RecentChange::notifyEdit(
179 $timestamp,
180 $title,
181 $rev->getMinor(),
182 $user,
183 $summary,
184 $oldRevID,
185 $oldRevRecord->getTimestamp(),
186 $bot,
187 '',
188 $oldRevRecord->getSize(),
189 $rev->getSize(),
190 $newId,
191 // the pages don't need to be patrolled
192 1
193 );
194 }
195 } else {
196 RecentChange::notifyNew(
197 $timestamp,
198 $title,
199 $rev->getMinor(),
200 $user,
201 $summary,
202 $bot,
203 '',
204 $rev->getSize(),
205 $newId,
206 1
207 );
208 }
209 }
210 }
211
212 $this->output( "Done! $successCount succeeded, $skipCount skipped.\n" );
213 if ( $exit ) {
214 $this->fatalError( "Import failed with $failCount failed pages.\n", $exit );
215 }
216 }
217}
218
219// @codeCoverageIgnoreStart
220$maintClass = ImportTextFiles::class;
221require_once RUN_MAINTENANCE_IF_MAIN;
222// @codeCoverageIgnoreEnd
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script which reads in text files and imports their content to a page of the wiki.
execute()
Do the actual work.
__construct()
Default constructor.
A content handler knows how do deal with a specific type of content on a wiki page.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:78
User class for the MediaWiki software.
Definition User.php:119
Represents a revision, log entry or upload during the import process.