00001 <?php
00026 class SpecialImport extends SpecialPage {
00027
00028 private $interwiki = false;
00029 private $namespace;
00030 private $frompage = '';
00031 private $logcomment= false;
00032 private $history = true;
00033 private $includeTemplates = false;
00034
00038 public function __construct() {
00039 parent::__construct( 'Import', 'import' );
00040 global $wgImportTargetNamespace;
00041 $this->namespace = $wgImportTargetNamespace;
00042 }
00043
00047 function execute( $par ) {
00048 global $wgRequest;
00049
00050 $this->setHeaders();
00051 $this->outputHeader();
00052
00053 if ( wfReadOnly() ) {
00054 global $wgOut;
00055 $wgOut->readOnlyPage();
00056 return;
00057 }
00058
00059 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
00060 $this->doImport();
00061 }
00062 $this->showForm();
00063 }
00064
00068 private function doImport() {
00069 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
00070 $isUpload = false;
00071 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
00072 $sourceName = $wgRequest->getVal( "source" );
00073
00074 $this->logcomment = $wgRequest->getText( 'log-comment' );
00075 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
00076
00077 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
00078 $source = new WikiErrorMsg( 'import-token-mismatch' );
00079 } elseif ( $sourceName == 'upload' ) {
00080 $isUpload = true;
00081 if( $wgUser->isAllowed( 'importupload' ) ) {
00082 $source = ImportStreamSource::newFromUpload( "xmlimport" );
00083 } else {
00084 return $wgOut->permissionRequired( 'importupload' );
00085 }
00086 } elseif ( $sourceName == "interwiki" ) {
00087 $this->interwiki = $wgRequest->getVal( 'interwiki' );
00088 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
00089 $source = new WikiErrorMsg( "import-invalid-interwiki" );
00090 } else {
00091 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
00092 $this->frompage = $wgRequest->getText( "frompage" );
00093 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
00094 $source = ImportStreamSource::newFromInterwiki(
00095 $this->interwiki,
00096 $this->frompage,
00097 $this->history,
00098 $this->includeTemplates,
00099 $this->pageLinkDepth );
00100 }
00101 } else {
00102 $source = new WikiErrorMsg( "importunknownsource" );
00103 }
00104
00105 if( WikiError::isError( $source ) ) {
00106 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
00107 } else {
00108 $wgOut->addWikiMsg( "importstart" );
00109
00110 $importer = new WikiImporter( $source );
00111 if( !is_null( $this->namespace ) ) {
00112 $importer->setTargetNamespace( $this->namespace );
00113 }
00114 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
00115
00116 $reporter->open();
00117 $result = $importer->doImport();
00118 $resultCount = $reporter->close();
00119
00120 if( WikiError::isError( $result ) ) {
00121 # No source or XML parse error
00122 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
00123 } elseif( WikiError::isError( $resultCount ) ) {
00124 # Zero revisions
00125 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
00126 } else {
00127 # Success!
00128 $wgOut->addWikiMsg( 'importsuccess' );
00129 }
00130 $wgOut->addWikiText( '<hr />' );
00131 }
00132 }
00133
00134 private function showForm() {
00135 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources, $wgExportMaxLinkDepth;
00136 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
00137 return $wgOut->permissionRequired( 'import' );
00138
00139 $action = $wgTitle->getLocalUrl( 'action=submit' );
00140
00141 if( $wgUser->isAllowed( 'importupload' ) ) {
00142 $wgOut->addWikiMsg( "importtext" );
00143 $wgOut->addHTML(
00144 Xml::fieldset( wfMsg( 'import-upload' ) ).
00145 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
00146 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
00147 Xml::hidden( 'action', 'submit' ) .
00148 Xml::hidden( 'source', 'upload' ) .
00149 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
00150
00151 "<tr>
00152 <td class='mw-label'>" .
00153 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
00154 "</td>
00155 <td class='mw-input'>" .
00156 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
00157 "</td>
00158 </tr>
00159 <tr>
00160 <td class='mw-label'>" .
00161 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
00162 "</td>
00163 <td class='mw-input'>" .
00164 Xml::input( 'log-comment', 50, '',
00165 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
00166 "</td>
00167 </tr>
00168 <tr>
00169 <td></td>
00170 <td class='mw-submit'>" .
00171 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
00172 "</td>
00173 </tr>" .
00174 Xml::closeElement( 'table' ).
00175 Xml::hidden( 'editToken', $wgUser->editToken() ) .
00176 Xml::closeElement( 'form' ) .
00177 Xml::closeElement( 'fieldset' )
00178 );
00179 } else {
00180 if( empty( $wgImportSources ) ) {
00181 $wgOut->addWikiMsg( 'importnosources' );
00182 }
00183 }
00184
00185 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
00186 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
00187 $importDepth = '';
00188 if( $wgExportMaxLinkDepth > 0 ) {
00189 $importDepth = "<tr>
00190 <td class='mw-label'>" .
00191 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
00192 "</td>
00193 <td class='mw-input'>" .
00194 Xml::input( 'pagelink-depth', 3, 0 ) .
00195 "</td>
00196 </tr>";
00197 }
00198
00199 $wgOut->addHTML(
00200 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
00201 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
00202 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
00203 Xml::hidden( 'action', 'submit' ) .
00204 Xml::hidden( 'source', 'interwiki' ) .
00205 Xml::hidden( 'editToken', $wgUser->editToken() ) .
00206 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
00207 "<tr>
00208 <td class='mw-label'>" .
00209 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
00210 "</td>
00211 <td class='mw-input'>" .
00212 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
00213 );
00214 foreach( $wgImportSources as $prefix ) {
00215 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
00216 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
00217 }
00218
00219 $wgOut->addHTML(
00220 Xml::closeElement( 'select' ) .
00221 Xml::input( 'frompage', 50, $this->frompage ) .
00222 "</td>
00223 </tr>
00224 <tr>
00225 <td>
00226 </td>
00227 <td class='mw-input'>" .
00228 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
00229 "</td>
00230 </tr>
00231 <tr>
00232 <td>
00233 </td>
00234 <td class='mw-input'>" .
00235 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
00236 "</td>
00237 </tr>
00238 $importDepth
00239 <tr>
00240 <td class='mw-label'>" .
00241 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
00242 "</td>
00243 <td class='mw-input'>" .
00244 Xml::namespaceSelector( $this->namespace, '' ) .
00245 "</td>
00246 </tr>
00247 <tr>
00248 <td class='mw-label'>" .
00249 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
00250 "</td>
00251 <td class='mw-input'>" .
00252 Xml::input( 'log-comment', 50, '',
00253 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
00254 "</td>
00255 </tr>
00256 <tr>
00257 <td>
00258 </td>
00259 <td class='mw-submit'>" .
00260 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
00261 "</td>
00262 </tr>" .
00263 Xml::closeElement( 'table' ).
00264 Xml::closeElement( 'form' ) .
00265 Xml::closeElement( 'fieldset' )
00266 );
00267 }
00268 }
00269 }
00270
00275 class ImportReporter {
00276 private $reason=false;
00277
00278 function __construct( $importer, $upload, $interwiki , $reason=false ) {
00279 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
00280 $this->mPageCount = 0;
00281 $this->mIsUpload = $upload;
00282 $this->mInterwiki = $interwiki;
00283 $this->reason = $reason;
00284 }
00285
00286 function open() {
00287 global $wgOut;
00288 $wgOut->addHTML( "<ul>\n" );
00289 }
00290
00291 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
00292 global $wgOut, $wgUser, $wgLang, $wgContLang;
00293
00294 $skin = $wgUser->getSkin();
00295
00296 $this->mPageCount++;
00297
00298 $localCount = $wgLang->formatNum( $successCount );
00299 $contentCount = $wgContLang->formatNum( $successCount );
00300
00301 if( $successCount > 0 ) {
00302 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
00303 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
00304 "</li>\n"
00305 );
00306
00307 $log = new LogPage( 'import' );
00308 if( $this->mIsUpload ) {
00309 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
00310 $contentCount );
00311 if ( $this->reason ) {
00312 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
00313 }
00314 $log->addEntry( 'upload', $title, $detail );
00315 } else {
00316 $interwiki = '[[:' . $this->mInterwiki . ':' .
00317 $origTitle->getPrefixedText() . ']]';
00318 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
00319 $contentCount, $interwiki );
00320 if ( $this->reason ) {
00321 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
00322 }
00323 $log->addEntry( 'interwiki', $title, $detail );
00324 }
00325
00326 $comment = $detail;
00327 $dbw = wfGetDB( DB_MASTER );
00328 $latest = $title->getLatestRevID();
00329 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
00330 $nullRevision->insertOn( $dbw );
00331 $article = new Article( $title );
00332 # Update page record
00333 $article->updateRevisionOn( $dbw, $nullRevision );
00334 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
00335 } else {
00336 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
00337 }
00338 }
00339
00340 function close() {
00341 global $wgOut;
00342 if( $this->mPageCount == 0 ) {
00343 $wgOut->addHTML( "</ul>\n" );
00344 return new WikiErrorMsg( "importnopages" );
00345 }
00346 $wgOut->addHTML( "</ul>\n" );
00347
00348 return $this->mPageCount;
00349 }
00350 }