MediaWiki  1.23.8
ApiImport.php
Go to the documentation of this file.
1 <?php
32 class ApiImport extends ApiBase {
33 
34  public function execute() {
35  $user = $this->getUser();
36  $params = $this->extractRequestParams();
37 
38  $isUpload = false;
39  if ( isset( $params['interwikisource'] ) ) {
40  if ( !$user->isAllowed( 'import' ) ) {
41  $this->dieUsageMsg( 'cantimport' );
42  }
43  if ( !isset( $params['interwikipage'] ) ) {
44  $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
45  }
47  $params['interwikisource'],
48  $params['interwikipage'],
49  $params['fullhistory'],
50  $params['templates']
51  );
52  } else {
53  $isUpload = true;
54  if ( !$user->isAllowed( 'importupload' ) ) {
55  $this->dieUsageMsg( 'cantimport-upload' );
56  }
58  }
59  if ( !$source->isOK() ) {
60  $this->dieStatus( $source );
61  }
62 
63  $importer = new WikiImporter( $source->value );
64  if ( isset( $params['namespace'] ) ) {
65  $importer->setTargetNamespace( $params['namespace'] );
66  }
67  if ( isset( $params['rootpage'] ) ) {
68  $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
69  if ( !$statusRootPage->isGood() ) {
70  $this->dieStatus( $statusRootPage );
71  }
72  }
73  $reporter = new ApiImportReporter(
74  $importer,
75  $isUpload,
76  $params['interwikisource'],
77  $params['summary']
78  );
79 
80  try {
81  $importer->doImport();
82  } catch ( MWException $e ) {
83  $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
84  }
85 
86  $resultData = $reporter->getData();
87  $result = $this->getResult();
88  $result->setIndexedTagName( $resultData, 'page' );
89  $result->addValue( null, $this->getModuleName(), $resultData );
90  }
91 
92  public function mustBePosted() {
93  return true;
94  }
95 
96  public function isWriteMode() {
97  return true;
98  }
99 
100  public function getAllowedParams() {
101  global $wgImportSources;
102 
103  return array(
104  'token' => array(
105  ApiBase::PARAM_TYPE => 'string',
107  ),
108  'summary' => null,
109  'xml' => array(
110  ApiBase::PARAM_TYPE => 'upload',
111  ),
112  'interwikisource' => array(
113  ApiBase::PARAM_TYPE => $wgImportSources
114  ),
115  'interwikipage' => null,
116  'fullhistory' => false,
117  'templates' => false,
118  'namespace' => array(
119  ApiBase::PARAM_TYPE => 'namespace'
120  ),
121  'rootpage' => null,
122  );
123  }
124 
125  public function getParamDescription() {
126  return array(
127  'token' => 'Import token obtained through prop=info',
128  'summary' => 'Import summary',
129  'xml' => 'Uploaded XML file',
130  'interwikisource' => 'For interwiki imports: wiki to import from',
131  'interwikipage' => 'For interwiki imports: page to import',
132  'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
133  'templates' => 'For interwiki imports: import all included templates as well',
134  'namespace' => 'For interwiki imports: import to this namespace',
135  'rootpage' => 'Import as subpage of this page',
136  );
137  }
138 
139  public function getResultProperties() {
140  return array(
141  ApiBase::PROP_LIST => true,
142  '' => array(
143  'ns' => 'namespace',
144  'title' => 'string',
145  'revisions' => 'integer'
146  )
147  );
148  }
149 
150  public function getDescription() {
151  return array(
152  'Import a page from another wiki, or an XML file.',
153  'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
154  'sending a file for the "xml" parameter.'
155  );
156  }
157 
158  public function getPossibleErrors() {
159  return array_merge( parent::getPossibleErrors(), array(
160  array( 'cantimport' ),
161  array( 'missingparam', 'interwikipage' ),
162  array( 'cantimport-upload' ),
163  array( 'import-unknownerror', 'source' ),
164  array( 'import-unknownerror', 'result' ),
165  array( 'import-rootpage-nosubpage', 'namespace' ),
166  array( 'import-rootpage-invalid' ),
167  ) );
168  }
169 
170  public function needsToken() {
171  return true;
172  }
173 
174  public function getTokenSalt() {
175  return '';
176  }
177 
178  public function getExamples() {
179  return array(
180  'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
181  'namespace=100&fullhistory=&token=123ABC'
182  => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
183  );
184  }
185 
186  public function getHelpUrls() {
187  return 'https://www.mediawiki.org/wiki/API:Import';
188  }
189 }
190 
196  private $mResultArr = array();
197 
206  function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
207  // Add a result entry
208  $r = array();
209 
210  if ( $title === null ) {
211  # Invalid or non-importable title
212  $r['title'] = $pageInfo['title'];
213  $r['invalid'] = '';
214  } else {
216  $r['revisions'] = intval( $successCount );
217  }
218 
219  $this->mResultArr[] = $r;
220 
221  // Piggyback on the parent to do the logging
222  parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
223  }
224 
225  function getData() {
226  return $this->mResultArr;
227  }
228 }
ApiBase\dieStatus
dieStatus( $status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1419
WikiImporter
XML file reader for the page data importer.
Definition: Import.php:33
ApiImport\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiImport.php:150
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:62
ApiImportReporter\$mResultArr
$mResultArr
Definition: ApiImport.php:196
ApiImport\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiImport.php:92
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1933
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
$params
$params
Definition: styleTest.css.php:40
ImportReporter
Reporting callback.
Definition: SpecialImport.php:408
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:132
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiImport\needsToken
needsToken()
Returns whether this module requires a token to execute It is used to show possible errors in action=...
Definition: ApiImport.php:170
ApiBase\PROP_LIST
const PROP_LIST
Definition: ApiBase.php:73
MWException
MediaWiki exception.
Definition: MWException.php:26
ApiImportReporter\getData
getData()
Definition: ApiImport.php:225
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ApiImport\getHelpUrls
getHelpUrls()
Definition: ApiImport.php:186
ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition: Import.php:1726
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiImport\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiImport.php:158
ApiImport\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiImport.php:178
ApiImport\getTokenSalt
getTokenSalt()
Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the mo...
Definition: ApiImport.php:174
ApiImport\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiImport.php:125
$user
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
Definition: hooks.txt:237
ApiImportReporter\reportPage
reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo)
Definition: ApiImport.php:206
ApiImport\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiImport.php:34
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
$source
if(PHP_SAPI !='cli') $source
Definition: mwdoc-filter.php:18
ApiImport\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiImport.php:96
ApiImport
API module that imports an XML file like Special:Import does.
Definition: ApiImport.php:32
ApiImport\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiImport.php:100
$e
if( $useReadline) $e
Definition: eval.php:66
ApiImport\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiImport.php:139
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:339
ApiImportReporter
Import reporter for the API.
Definition: ApiImport.php:195
ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition: Import.php:1667