Debian Codegraph @

maintenance/importTextFiles.php

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