(bug 15946) Add hidepatrolled option to Special:Watchlist
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
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
26 /**
27 * Constructor
28 */
29 function wfSpecialImport( $page = '' ) {
30 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
31 global $wgImportTargetNamespace;
32
33 $interwiki = false;
34 $namespace = $wgImportTargetNamespace;
35 $frompage = '';
36 $history = true;
37
38 if ( wfReadOnly() ) {
39 $wgOut->readOnlyPage();
40 return;
41 }
42
43 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
44 $isUpload = false;
45 $namespace = $wgRequest->getIntOrNull( 'namespace' );
46
47 switch( $wgRequest->getVal( "source" ) ) {
48 case "upload":
49 $isUpload = true;
50 if( $wgUser->isAllowed( 'importupload' ) ) {
51 $source = ImportStreamSource::newFromUpload( "xmlimport" );
52 } else {
53 return $wgOut->permissionRequired( 'importupload' );
54 }
55 break;
56 case "interwiki":
57 $interwiki = $wgRequest->getVal( 'interwiki' );
58 $history = $wgRequest->getCheck( 'interwikiHistory' );
59 $frompage = $wgRequest->getText( "frompage" );
60 $source = ImportStreamSource::newFromInterwiki(
61 $interwiki,
62 $frompage,
63 $history );
64 break;
65 default:
66 $source = new WikiErrorMsg( "importunknownsource" );
67 }
68
69 if( WikiError::isError( $source ) ) {
70 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
71 } else {
72 $wgOut->addWikiMsg( "importstart" );
73
74 $importer = new WikiImporter( $source );
75 if( !is_null( $namespace ) ) {
76 $importer->setTargetNamespace( $namespace );
77 }
78 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
79
80 $reporter->open();
81 $result = $importer->doImport();
82 $resultCount = $reporter->close();
83
84 if( WikiError::isError( $result ) ) {
85 # No source or XML parse error
86 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
87 } elseif( WikiError::isError( $resultCount ) ) {
88 # Zero revisions
89 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
90 } else {
91 # Success!
92 $wgOut->addWikiMsg( 'importsuccess' );
93 }
94 $wgOut->addWikiText( '<hr />' );
95 }
96 }
97
98 $action = $wgTitle->getLocalUrl( 'action=submit' );
99
100 if( $wgUser->isAllowed( 'importupload' ) ) {
101 $wgOut->addWikiMsg( "importtext" );
102 $wgOut->addHTML(
103 Xml::openElement( 'fieldset' ).
104 Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) .
105 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
106 Xml::hidden( 'action', 'submit' ) .
107 Xml::hidden( 'source', 'upload' ) .
108 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
109 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
110 Xml::closeElement( 'form' ) .
111 Xml::closeElement( 'fieldset' )
112 );
113 } else {
114 if( empty( $wgImportSources ) ) {
115 $wgOut->addWikiMsg( 'importnosources' );
116 }
117 }
118
119 if( !empty( $wgImportSources ) ) {
120 $wgOut->addHTML(
121 Xml::openElement( 'fieldset' ) .
122 Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
123 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
124 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
125 Xml::hidden( 'action', 'submit' ) .
126 Xml::hidden( 'source', 'interwiki' ) .
127 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
128 "<tr>
129 <td>" .
130 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
131 );
132 foreach( $wgImportSources as $prefix ) {
133 $selected = ( $interwiki === $prefix ) ? ' selected="selected"' : '';
134 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
135 }
136 $wgOut->addHTML(
137 Xml::closeElement( 'select' ) .
138 "</td>
139 <td>" .
140 Xml::input( 'frompage', 50, $frompage ) .
141 "</td>
142 </tr>
143 <tr>
144 <td>
145 </td>
146 <td>" .
147 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $history ) .
148 "</td>
149 </tr>
150 <tr>
151 <td>
152 </td>
153 <td>" .
154 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
155 Xml::namespaceSelector( $namespace, '' ) .
156 "</td>
157 </tr>
158 <tr>
159 <td>
160 </td>
161 <td>" .
162 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
163 "</td>
164 </tr>" .
165 Xml::closeElement( 'table' ).
166 Xml::closeElement( 'form' ) .
167 Xml::closeElement( 'fieldset' )
168 );
169 }
170 }
171
172 /**
173 * Reporting callback
174 * @ingroup SpecialPage
175 */
176 class ImportReporter {
177 function __construct( $importer, $upload, $interwiki ) {
178 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
179 $this->mPageCount = 0;
180 $this->mIsUpload = $upload;
181 $this->mInterwiki = $interwiki;
182 }
183
184 function open() {
185 global $wgOut;
186 $wgOut->addHtml( "<ul>\n" );
187 }
188
189 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
190 global $wgOut, $wgUser, $wgLang, $wgContLang;
191
192 $skin = $wgUser->getSkin();
193
194 $this->mPageCount++;
195
196 $localCount = $wgLang->formatNum( $successCount );
197 $contentCount = $wgContLang->formatNum( $successCount );
198
199 if( $successCount > 0 ) {
200 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
201 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
202 "</li>\n"
203 );
204
205 $log = new LogPage( 'import' );
206 if( $this->mIsUpload ) {
207 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
208 $contentCount );
209 $log->addEntry( 'upload', $title, $detail );
210 } else {
211 $interwiki = '[[:' . $this->mInterwiki . ':' .
212 $origTitle->getPrefixedText() . ']]';
213 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
214 $contentCount, $interwiki );
215 $log->addEntry( 'interwiki', $title, $detail );
216 }
217
218 $comment = $detail; // quick
219 $dbw = wfGetDB( DB_MASTER );
220 $latest = $title->getLatestRevID();
221 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
222 $nullRevision->insertOn( $dbw );
223 $article = new Article( $title );
224 # Update page record
225 $article->updateRevisionOn( $dbw, $nullRevision );
226 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest) );
227 } else {
228 $wgOut->addHtml( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
229 }
230 }
231
232 function close() {
233 global $wgOut;
234 if( $this->mPageCount == 0 ) {
235 $wgOut->addHtml( "</ul>\n" );
236 return new WikiErrorMsg( "importnopages" );
237 }
238 $wgOut->addHtml( "</ul>\n" );
239
240 return $this->mPageCount;
241 }
242 }