SpecialImport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /**
  3. * MediaWiki page data importer
  4. * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
  5. * http://www.mediawiki.org/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup SpecialPage
  24. */
  25. class SpecialImport extends SpecialPage {
  26. private $interwiki = false;
  27. private $namespace;
  28. private $frompage = '';
  29. private $logcomment= false;
  30. private $history = true;
  31. private $includeTemplates = false;
  32. /**
  33. * Constructor
  34. */
  35. public function __construct() {
  36. parent::__construct( 'Import', 'import' );
  37. global $wgImportTargetNamespace;
  38. $this->namespace = $wgImportTargetNamespace;
  39. }
  40. /**
  41. * Execute
  42. */
  43. function execute( $par ) {
  44. global $wgRequest;
  45. $this->setHeaders();
  46. $this->outputHeader();
  47. if ( wfReadOnly() ) {
  48. global $wgOut;
  49. $wgOut->readOnlyPage();
  50. return;
  51. }
  52. if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
  53. $this->doImport();
  54. }
  55. $this->showForm();
  56. }
  57. /**
  58. * Do the actual import
  59. */
  60. private function doImport() {
  61. global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
  62. $isUpload = false;
  63. $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
  64. $sourceName = $wgRequest->getVal( "source" );
  65. $this->logcomment = $wgRequest->getText( 'log-comment' );
  66. $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
  67. if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
  68. $source = new WikiErrorMsg( 'import-token-mismatch' );
  69. } elseif ( $sourceName == 'upload' ) {
  70. $isUpload = true;
  71. if( $wgUser->isAllowed( 'importupload' ) ) {
  72. $source = ImportStreamSource::newFromUpload( "xmlimport" );
  73. } else {
  74. return $wgOut->permissionRequired( 'importupload' );
  75. }
  76. } elseif ( $sourceName == "interwiki" ) {
  77. $this->interwiki = $wgRequest->getVal( 'interwiki' );
  78. if ( !in_array( $this->interwiki, $wgImportSources ) ) {
  79. $source = new WikiErrorMsg( "import-invalid-interwiki" );
  80. } else {
  81. $this->history = $wgRequest->getCheck( 'interwikiHistory' );
  82. $this->frompage = $wgRequest->getText( "frompage" );
  83. $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
  84. $source = ImportStreamSource::newFromInterwiki(
  85. $this->interwiki,
  86. $this->frompage,
  87. $this->history,
  88. $this->includeTemplates,
  89. $this->pageLinkDepth );
  90. }
  91. } else {
  92. $source = new WikiErrorMsg( "importunknownsource" );
  93. }
  94. if( WikiError::isError( $source ) ) {
  95. $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
  96. } else {
  97. $wgOut->addWikiMsg( "importstart" );
  98. $importer = new WikiImporter( $source );
  99. if( !is_null( $this->namespace ) ) {
  100. $importer->setTargetNamespace( $this->namespace );
  101. }
  102. $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
  103. $reporter->open();
  104. $result = $importer->doImport();
  105. $resultCount = $reporter->close();
  106. if( WikiError::isError( $result ) ) {
  107. # No source or XML parse error
  108. $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
  109. } elseif( WikiError::isError( $resultCount ) ) {
  110. # Zero revisions
  111. $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
  112. } else {
  113. # Success!
  114. $wgOut->addWikiMsg( 'importsuccess' );
  115. }
  116. $wgOut->addWikiText( '<hr />' );
  117. }
  118. }
  119. private function showForm() {
  120. global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources, $wgExportMaxLinkDepth;
  121. if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
  122. return $wgOut->permissionRequired( 'import' );
  123. $action = $wgTitle->getLocalUrl( 'action=submit' );
  124. if( $wgUser->isAllowed( 'importupload' ) ) {
  125. $wgOut->addWikiMsg( "importtext" );
  126. $wgOut->addHTML(
  127. Xml::fieldset( wfMsg( 'import-upload' ) ).
  128. Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
  129. 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
  130. Xml::hidden( 'action', 'submit' ) .
  131. Xml::hidden( 'source', 'upload' ) .
  132. Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
  133. "<tr>
  134. <td class='mw-label'>" .
  135. Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
  136. "</td>
  137. <td class='mw-input'>" .
  138. Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
  139. "</td>
  140. </tr>
  141. <tr>
  142. <td class='mw-label'>" .
  143. Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
  144. "</td>
  145. <td class='mw-input'>" .
  146. Xml::input( 'log-comment', 50, '',
  147. array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
  148. "</td>
  149. </tr>
  150. <tr>
  151. <td></td>
  152. <td class='mw-submit'>" .
  153. Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
  154. "</td>
  155. </tr>" .
  156. Xml::closeElement( 'table' ).
  157. Xml::hidden( 'editToken', $wgUser->editToken() ) .
  158. Xml::closeElement( 'form' ) .
  159. Xml::closeElement( 'fieldset' )
  160. );
  161. } else {
  162. if( empty( $wgImportSources ) ) {
  163. $wgOut->addWikiMsg( 'importnosources' );
  164. }
  165. }
  166. if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
  167. # Show input field for import depth only if $wgExportMaxLinkDepth > 0
  168. $importDepth = '';
  169. if( $wgExportMaxLinkDepth > 0 ) {
  170. $importDepth = "<tr>
  171. <td class='mw-label'>" .
  172. wfMsgExt( 'export-pagelinks', 'parseinline' ) .
  173. "</td>
  174. <td class='mw-input'>" .
  175. Xml::input( 'pagelink-depth', 3, 0 ) .
  176. "</td>
  177. </tr>";
  178. }
  179. $wgOut->addHTML(
  180. Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
  181. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
  182. wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
  183. Xml::hidden( 'action', 'submit' ) .
  184. Xml::hidden( 'source', 'interwiki' ) .
  185. Xml::hidden( 'editToken', $wgUser->editToken() ) .
  186. Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
  187. "<tr>
  188. <td class='mw-label'>" .
  189. Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
  190. "</td>
  191. <td class='mw-input'>" .
  192. Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
  193. );
  194. foreach( $wgImportSources as $prefix ) {
  195. $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
  196. $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
  197. }
  198. $wgOut->addHTML(
  199. Xml::closeElement( 'select' ) .
  200. Xml::input( 'frompage', 50, $this->frompage ) .
  201. "</td>
  202. </tr>
  203. <tr>
  204. <td>
  205. </td>
  206. <td class='mw-input'>" .
  207. Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
  208. "</td>
  209. </tr>
  210. <tr>
  211. <td>
  212. </td>
  213. <td class='mw-input'>" .
  214. Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
  215. "</td>
  216. </tr>
  217. $importDepth
  218. <tr>
  219. <td class='mw-label'>" .
  220. Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
  221. "</td>
  222. <td class='mw-input'>" .
  223. Xml::namespaceSelector( $this->namespace, '' ) .
  224. "</td>
  225. </tr>
  226. <tr>
  227. <td class='mw-label'>" .
  228. Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
  229. "</td>
  230. <td class='mw-input'>" .
  231. Xml::input( 'log-comment', 50, '',
  232. array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
  233. "</td>
  234. </tr>
  235. <tr>
  236. <td>
  237. </td>
  238. <td class='mw-submit'>" .
  239. Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
  240. "</td>
  241. </tr>" .
  242. Xml::closeElement( 'table' ).
  243. Xml::closeElement( 'form' ) .
  244. Xml::closeElement( 'fieldset' )
  245. );
  246. }
  247. }
  248. }
  249. /**
  250. * Reporting callback
  251. * @ingroup SpecialPage
  252. */
  253. class ImportReporter {
  254. private $reason=false;
  255. function __construct( $importer, $upload, $interwiki , $reason=false ) {
  256. $importer->setPageOutCallback( array( $this, 'reportPage' ) );
  257. $this->mPageCount = 0;
  258. $this->mIsUpload = $upload;
  259. $this->mInterwiki = $interwiki;
  260. $this->reason = $reason;
  261. }
  262. function open() {
  263. global $wgOut;
  264. $wgOut->addHTML( "<ul>\n" );
  265. }
  266. function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
  267. global $wgOut, $wgUser, $wgLang, $wgContLang;
  268. $skin = $wgUser->getSkin();
  269. $this->mPageCount++;
  270. $localCount = $wgLang->formatNum( $successCount );
  271. $contentCount = $wgContLang->formatNum( $successCount );
  272. if( $successCount > 0 ) {
  273. $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
  274. wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
  275. "</li>\n"
  276. );
  277. $log = new LogPage( 'import' );
  278. if( $this->mIsUpload ) {
  279. $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
  280. $contentCount );
  281. if ( $this->reason ) {
  282. $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
  283. }
  284. $log->addEntry( 'upload', $title, $detail );
  285. } else {
  286. $interwiki = '[[:' . $this->mInterwiki . ':' .
  287. $origTitle->getPrefixedText() . ']]';
  288. $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
  289. $contentCount, $interwiki );
  290. if ( $this->reason ) {
  291. $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
  292. }
  293. $log->addEntry( 'interwiki', $title, $detail );
  294. }
  295. $comment = $detail; // quick
  296. $dbw = wfGetDB( DB_MASTER );
  297. $latest = $title->getLatestRevID();
  298. $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
  299. $nullRevision->insertOn( $dbw );
  300. $article = new Article( $title );
  301. # Update page record
  302. $article->updateRevisionOn( $dbw, $nullRevision );
  303. wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
  304. } else {
  305. $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
  306. }
  307. }
  308. function close() {
  309. global $wgOut;
  310. if( $this->mPageCount == 0 ) {
  311. $wgOut->addHTML( "</ul>\n" );
  312. return new WikiErrorMsg( "importnopages" );
  313. }
  314. $wgOut->addHTML( "</ul>\n" );
  315. return $this->mPageCount;
  316. }
  317. }