Go to the documentation of this file.
35 require_once __DIR__ .
'/Maintenance.php';
40 parent::__construct();
42 $this->
addDescription(
'Imports images and other media files into the wiki' );
43 $this->
addArg(
'dir',
'Path to the directory containing images to be imported' );
46 'Comma-separated list of allowable extensions, defaults to $wgFileExtensions',
51 'Overwrite existing images with the same name (default is to skip them)' );
53 'Limit the number of images to process. Ignored or skipped images are not counted',
58 "Ignore all files until the one with the given name. Useful for resuming aborted "
59 .
"imports. The name should be the file's canonical database form.",
64 'Skip images that were already uploaded under a different name (check SHA1)' );
65 $this->
addOption(
'search-recursively',
'Search recursively for files in subdirectories' );
67 'Sleep between files. Useful mostly for debugging',
72 "Set username of uploader, default 'Maintenance script'",
78 $this->
addOption(
'check-userblock',
'Check if the user got blocked during import' );
80 "Set file description, default 'Importing file'",
85 'Set description to the content of this file',
90 'Causes the description for each file to be loaded from a file with the same name, but '
91 .
'the extension provided. If a global description is also given, it is appended.',
96 'Upload summary, description will be used if not provided',
101 'Use an optional license template',
106 'Override upload time/date, all MediaWiki timestamp formats are accepted',
111 'Specify the protect value (autoconfirmed,sysop)',
115 $this->
addOption(
'unprotect',
'Unprotects all uploaded images' );
117 'If specified, take User and Comment data for each imported file from this URL. '
118 .
'For example, --source-wiki-url="http://en.wikipedia.org/',
122 $this->
addOption(
'dry',
"Dry run, don't import anything" );
128 $processed = $added = $ignored = $skipped = $overwritten = $failed = 0;
130 $this->
output(
"Import Images\n\n" );
136 $this->
error(
"Cannot specify both protect and unprotect. Only 1 is allowed.\n", 1 );
140 $this->
error(
"You must specify a protection option.\n", 1 );
143 # Prepare the list of allowed extensions
144 $extensions = $this->
hasOption(
'extensions' )
145 ? explode(
',', strtolower( $this->
getOption(
'extensions' ) ) )
148 # Search the path provided for candidates for import
151 # Initialise the user for this operation
160 # Get block check. If a value is given, this specified how often the check is performed
161 $checkUserBlock = (int)$this->
getOption(
'check-userblock' );
164 $sleep = (int)$this->
getOption(
'sleep' );
166 $timestamp = $this->
getOption(
'timestamp',
false );
168 # Get the upload comment. Provide a default one in case there's no comment given.
169 $commentFile = $this->
getOption(
'comment-file' );
170 if ( $commentFile !==
null ) {
171 $comment = file_get_contents( $commentFile );
172 if ( $comment ===
false || $comment ===
null ) {
173 $this->
error(
"failed to read comment file: {$commentFile}\n", 1 );
176 $comment = $this->
getOption(
'comment',
'Importing file' );
178 $commentExt = $this->
getOption(
'comment-ext' );
179 $summary = $this->
getOption(
'summary',
'' );
181 $license = $this->
getOption(
'license',
'' );
183 $sourceWikiUrl = $this->
getOption(
'source-wiki-url' );
185 # Batch "upload" operation
186 $count =
count( $files );
189 foreach ( $files
as $file ) {
191 if ( $sleep && ( $processed > 0 ) ) {
199 if ( !is_object(
$title ) ) {
201 "{$base} could not be imported; a valid title cannot be produced\n" );
206 if ( $from ==
$title->getDBkey() ) {
214 if ( $checkUserBlock && ( ( $processed % $checkUserBlock ) == 0 ) ) {
215 $user->clearInstanceCache(
'name' );
216 if (
$user->isBlocked() ) {
217 $this->
output(
$user->getName() .
" was blocked! Aborting.\n" );
226 $this->
output(
"{$base} exists, overwriting..." );
227 $svar =
'overwritten';
229 $this->
output(
"{$base} exists, skipping\n" );
234 if ( $this->
hasOption(
'skip-dupes' ) ) {
235 $repo =
$image->getRepo();
236 # XXX: we end up calculating this again when actually uploading. that sucks.
239 $dupes = $repo->findBySha1( $sha1 );
243 "{$base} already exists as {$dupes[0]->getName()}, skipping\n" );
249 $this->
output(
"Importing {$base}..." );
253 if ( $sourceWikiUrl ) {
256 if ( $real_comment ===
false ) {
257 $commentText = $comment;
259 $commentText = $real_comment;
264 if ( $real_user ===
false ) {
269 # user does not exist in target wiki
271 "failed: user '$real_user' does not exist in target wiki." );
277 $commentText =
false;
282 $this->
output(
" No comment file with extension {$commentExt} found "
283 .
"for {$file}, using default comment. " );
285 $commentText = file_get_contents( $f );
286 if ( !$commentText ) {
288 " Failed to load comment file {$f}, using default comment. " );
293 if ( !$commentText ) {
294 $commentText = $comment;
301 " publishing {$file} by '{$wgUser->getName()}', comment '$commentText'... "
305 $props = $mwProps->getPropsFromPath( $file,
true );
307 $publishOptions = [];
310 $publishOptions[
'headers'] =
$handler->getStreamHeaders( $props[
'metadata'] );
312 $publishOptions[
'headers'] = [];
314 $archive =
$image->publish( $file,
$flags, $publishOptions );
315 if ( !$archive->isGood() ) {
316 $this->
output(
"failed. (" .
317 $archive->getWikiText(
false,
false,
'en' ) .
326 $summary = $commentText;
330 $this->
output(
"done.\n" );
331 } elseif (
$image->recordUpload2(
339 $this->
output(
"done.\n" );
343 $protectLevel = $this->
getOption(
'protect' );
345 if ( $protectLevel && in_array( $protectLevel, $wgRestrictionLevels ) ) {
355 $this->
output(
"\nWaiting for replica DBs...\n" );
357 sleep( 2.0 ); # Why
this sleep?
360 $this->
output(
"\nSetting image restrictions ... " );
365 $restrictions[
$type] = $protectLevel;
370 $this->
output( (
$status->isOK() ?
'done' :
'failed' ) .
"\n" );
373 $this->
output(
"failed. (at recordUpload stage)\n" );
385 # Print out some statistics
391 'ignored' =>
'Ignored',
393 'skipped' =>
'Skipped',
394 'overwritten' =>
'Overwritten',
399 $this->
output(
"{$desc}: {$$var}\n" );
403 $this->
output(
"No suitable files could be found for import.\n" );
416 if ( is_dir(
$dir ) ) {
417 $dhl = opendir(
$dir );
420 while ( ( $file = readdir( $dhl ) ) !==
false ) {
421 if ( is_file(
$dir .
'/' . $file ) ) {
423 if ( array_search( strtolower(
$ext ), $exts ) !==
false ) {
424 $files[] =
$dir .
'/' . $file;
426 } elseif ( $recurse && is_dir(
$dir .
'/' . $file ) && $file !==
'..' && $file !==
'.' ) {
427 $files = array_merge( $files, $this->
findFiles(
$dir .
'/' . $file, $exts,
true ) );
447 $parts = explode(
'.', $filename );
449 unset( $parts[
count( $parts ) - 1] );
450 $fname = implode(
'.', $parts );
469 private function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
470 if ( strpos( $auxExtension,
'.' ) !== 0 ) {
471 $auxExtension =
'.' . $auxExtension;
474 $d = dirname( $file );
475 $n = basename( $file );
477 while ( $maxStrip >= 0 ) {
478 $f = $d .
'/' . $n . $auxExtension;
480 if ( file_exists( $f ) ) {
484 $idx = strrpos( $n,
'.' );
489 $n = substr( $n, 0, $idx );
496 # @todo FIXME: Access the api in a saner way and performing just one query
497 # (preferably batching files too).
499 $url = $wiki_host .
'/api.php?action=query&format=xml&titles=File:'
500 . rawurlencode( $file ) .
'&prop=imageinfo&&iiprop=comment';
501 $body =
Http::get( $url, [], __METHOD__ );
502 if ( preg_match(
'#<ii comment="([^"]*)" />#', $body,
$matches ) == 0 ) {
506 return html_entity_decode(
$matches[1] );
510 $url = $wiki_host .
'/api.php?action=query&format=xml&titles=File:'
511 . rawurlencode( $file ) .
'&prop=imageinfo&&iiprop=user';
512 $body =
Http::get( $url, [], __METHOD__ );
513 if ( preg_match(
'#<ii user="([^"]*)" />#', $body,
$matches ) == 0 ) {
517 return html_entity_decode(
$matches[1] );
static getInitialPageText( $comment='', $license='', $copyStatus='', $source='', Config $config=null)
Get the initial image page text based on a comment and optional file status information.
addDescription( $text)
Set the description text.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
splitFilename( $filename)
Split a filename into filename and extension.
$wgFileExtensions
This is the list of preferred extensions for uploading files.
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
require_once RUN_MAINTENANCE_IF_MAIN
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
getFileUserFromSourceWiki( $wiki_host, $file)
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
namespace and then decline to actually register it file or subcat img or subcat $title
findFiles( $dir, $exts, $recurse=false)
Search a directory for files with one of a set of extensions.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
getFileCommentFromSourceWiki( $wiki_host, $file)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers please use GetContentModels hook to make them known to core if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
MimeMagic helper wrapper.
when a variable name is used in a it is silently declared as a new masking the global
static singleton()
Get an instance of this class.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
static get( $url, $options=[], $caller=__METHOD__)
Simple wrapper for Http::request( 'GET' )
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
findAuxFile( $file, $auxExtension, $maxStrip=1)
Find an auxilliary file with the given extension, matching the give base file path.
getOption( $name, $default=null)
Get an option, or return the default.
addArg( $arg, $description, $required=true)
Add some args that are needed.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular param exists.
getArg( $argId=0, $default=null)
Get an argument.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
wfLocalFile( $title)
Get an object referring to a locally registered file.
execute()
Do the actual work.
it s the revision text itself In either if gzip is the revision text is gzipped $flags