00001 <?php
00002 # Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00024 class SpecialExport extends SpecialPage {
00025
00026 private $curonly, $doExport, $pageLinkDepth, $templates;
00027 private $images;
00028
00029 public function __construct() {
00030 parent::__construct( 'Export' );
00031 }
00032
00033 public function execute( $par ) {
00034 global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
00035 global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
00036 global $wgExportFromNamespaces;
00037
00038 $this->setHeaders();
00039 $this->outputHeader();
00040
00041
00042 $this->curonly = true;
00043 $this->doExport = false;
00044 $this->templates = $wgRequest->getCheck( 'templates' );
00045 $this->images = $wgRequest->getCheck( 'images' );
00046 $this->pageLinkDepth = $this->validateLinkDepth(
00047 $wgRequest->getIntOrNull( 'pagelink-depth' ) );
00048 $nsindex = '';
00049
00050 if ( $wgRequest->getCheck( 'addcat' ) ) {
00051 $page = $wgRequest->getText( 'pages' );
00052 $catname = $wgRequest->getText( 'catname' );
00053
00054 if ( $catname !== '' && $catname !== null && $catname !== false ) {
00055 $t = Title::makeTitleSafe( NS_MAIN, $catname );
00056 if ( $t ) {
00062 $catpages = $this->getPagesFromCategory( $t );
00063 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
00064 }
00065 }
00066 }
00067 else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
00068 $page = $wgRequest->getText( 'pages' );
00069 $nsindex = $wgRequest->getText( 'nsindex', '' );
00070
00071 if ( strval( $nsindex ) !== '' ) {
00075 $nspages = $this->getPagesFromNamespace( $nsindex );
00076 if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
00077 }
00078 }
00079 else if( $wgRequest->wasPosted() && $par == '' ) {
00080 $page = $wgRequest->getText( 'pages' );
00081 $this->curonly = $wgRequest->getCheck( 'curonly' );
00082 $rawOffset = $wgRequest->getVal( 'offset' );
00083 if( $rawOffset ) {
00084 $offset = wfTimestamp( TS_MW, $rawOffset );
00085 } else {
00086 $offset = null;
00087 }
00088 $limit = $wgRequest->getInt( 'limit' );
00089 $dir = $wgRequest->getVal( 'dir' );
00090 $history = array(
00091 'dir' => 'asc',
00092 'offset' => false,
00093 'limit' => $wgExportMaxHistory,
00094 );
00095 $historyCheck = $wgRequest->getCheck( 'history' );
00096 if ( $this->curonly ) {
00097 $history = WikiExporter::CURRENT;
00098 } elseif ( !$historyCheck ) {
00099 if ( $limit > 0 && ($wgExportMaxHistory == 0 || $limit < $wgExportMaxHistory ) ) {
00100 $history['limit'] = $limit;
00101 }
00102 if ( !is_null( $offset ) ) {
00103 $history['offset'] = $offset;
00104 }
00105 if ( strtolower( $dir ) == 'desc' ) {
00106 $history['dir'] = 'desc';
00107 }
00108 }
00109
00110 if( $page != '' ) $this->doExport = true;
00111 } else {
00112
00113 $page = $wgRequest->getText( 'pages', $par );
00114 $historyCheck = $wgRequest->getCheck( 'history' );
00115 if( $historyCheck ) {
00116 $history = WikiExporter::FULL;
00117 } else {
00118 $history = WikiExporter::CURRENT;
00119 }
00120
00121 if( $page != '' ) $this->doExport = true;
00122 }
00123
00124 if( !$wgExportAllowHistory ) {
00125
00126 $history = WikiExporter::CURRENT;
00127 }
00128
00129 $list_authors = $wgRequest->getCheck( 'listauthors' );
00130 if ( !$this->curonly || !$wgExportAllowListContributors ) $list_authors = false ;
00131
00132 if ( $this->doExport ) {
00133 $wgOut->disable();
00134
00135
00136 wfResetOutputBuffers();
00137 header( "Content-type: application/xml; charset=utf-8" );
00138 if( $wgRequest->getCheck( 'wpDownload' ) ) {
00139
00140 $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
00141 $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
00142 }
00143 $this->doExport( $page, $history, $list_authors );
00144 return;
00145 }
00146
00147 $wgOut->addWikiMsg( 'exporttext' );
00148
00149 $form = Xml::openElement( 'form', array( 'method' => 'post',
00150 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
00151 $form .= Xml::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . ' ';
00152 $form .= Xml::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
00153
00154 if ( $wgExportFromNamespaces ) {
00155 $form .= Xml::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . ' ';
00156 $form .= Xml::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
00157 }
00158
00159 $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
00160 $form .= '<br />';
00161
00162 if( $wgExportAllowHistory ) {
00163 $form .= Xml::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
00164 } else {
00165 $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
00166 }
00167 $form .= Xml::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
00168 if( $wgExportMaxLinkDepth || $this->userCanOverrideExportDepth() ) {
00169 $form .= Xml::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
00170 }
00171
00172
00173 $form .= Xml::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
00174
00175 $form .= Xml::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
00176 $form .= Xml::closeElement( 'form' );
00177 $wgOut->addHTML( $form );
00178 }
00179
00180 private function userCanOverrideExportDepth() {
00181 global $wgUser;
00182
00183 return $wgUser->isAllowed( 'override-export-depth' );
00184 }
00185
00191 private function doExport( $page, $history, $list_authors ) {
00192 global $wgExportMaxHistory;
00193
00194 $pageSet = array();
00195
00196
00197 foreach( explode( "\n", $page ) as $pageName ) {
00198 $pageName = trim( $pageName );
00199 $title = Title::newFromText( $pageName );
00200 if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
00201
00202 $pageSet[$title->getPrefixedText()] = true;
00203 }
00204 }
00205
00206
00207 $inputPages = array_keys( $pageSet );
00208
00209
00210 if( $this->templates ) {
00211 $pageSet = $this->getTemplates( $inputPages, $pageSet );
00212 }
00213
00214 if( $linkDepth = $this->pageLinkDepth ) {
00215 $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225 $pages = array_keys( $pageSet );
00226
00227
00228 foreach( $pages as $k => $v ) {
00229 $pages[$k] = str_replace( " ", "_", $v );
00230 }
00231 $pages = array_unique( $pages );
00232
00233
00234 if( $history == WikiExporter::CURRENT ) {
00235 $lb = false;
00236 $db = wfGetDB( DB_SLAVE );
00237 $buffer = WikiExporter::BUFFER;
00238 } else {
00239
00240 $lb = wfGetLBFactory()->newMainLB();
00241 $db = $lb->getConnection( DB_SLAVE );
00242 $buffer = WikiExporter::STREAM;
00243
00244
00245 wfSuppressWarnings();
00246 set_time_limit(0);
00247 wfRestoreWarnings();
00248 }
00249 $exporter = new WikiExporter( $db, $history, $buffer );
00250 $exporter->list_authors = $list_authors;
00251 $exporter->openStream();
00252 foreach( $pages as $page ) {
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 #Bug 8824: Only export pages the user can read
00266 $title = Title::newFromText( $page );
00267 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
00268 if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
00269
00270 $exporter->pageByTitle( $title );
00271 }
00272
00273 $exporter->closeStream();
00274 if( $lb ) {
00275 $lb->closeAll();
00276 }
00277 }
00278
00279 private function getPagesFromCategory( $title ) {
00280 global $wgContLang;
00281
00282 $name = $title->getDBkey();
00283
00284 $dbr = wfGetDB( DB_SLAVE );
00285 $res = $dbr->select( array('page', 'categorylinks' ),
00286 array( 'page_namespace', 'page_title' ),
00287 array('cl_from=page_id', 'cl_to' => $name ),
00288 __METHOD__, array('LIMIT' => '5000'));
00289
00290 $pages = array();
00291 while ( $row = $dbr->fetchObject( $res ) ) {
00292 $n = $row->page_title;
00293 if ($row->page_namespace) {
00294 $ns = $wgContLang->getNsText( $row->page_namespace );
00295 $n = $ns . ':' . $n;
00296 }
00297
00298 $pages[] = $n;
00299 }
00300 $dbr->freeResult($res);
00301
00302 return $pages;
00303 }
00304
00305 private function getPagesFromNamespace( $nsindex ) {
00306 global $wgContLang;
00307
00308 $dbr = wfGetDB( DB_SLAVE );
00309 $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
00310 array('page_namespace' => $nsindex),
00311 __METHOD__, array('LIMIT' => '5000') );
00312
00313 $pages = array();
00314 while ( $row = $dbr->fetchObject( $res ) ) {
00315 $n = $row->page_title;
00316 if ($row->page_namespace) {
00317 $ns = $wgContLang->getNsText( $row->page_namespace );
00318 $n = $ns . ':' . $n;
00319 }
00320
00321 $pages[] = $n;
00322 }
00323 $dbr->freeResult($res);
00324
00325 return $pages;
00326 }
00333 private function getTemplates( $inputPages, $pageSet ) {
00334 return $this->getLinks( $inputPages, $pageSet,
00335 'templatelinks',
00336 array( 'tl_namespace AS namespace', 'tl_title AS title' ),
00337 array( 'page_id=tl_from' ) );
00338 }
00339
00343 private function validateLinkDepth( $depth ) {
00344 global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
00345 if( $depth < 0 ) {
00346 return 0;
00347 }
00348 if ( !$this->userCanOverrideExportDepth() ) {
00349 if( $depth > $wgExportMaxLinkDepth ) {
00350 return $wgExportMaxLinkDepth;
00351 }
00352 }
00353
00354
00355
00356
00357
00358 return intval( min( $depth, 5 ) );
00359 }
00360
00362 private function getPageLinks( $inputPages, $pageSet, $depth ) {
00363 for( $depth=$depth; $depth>0; --$depth ) {
00364 $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
00365 array( 'pl_namespace AS namespace', 'pl_title AS title' ),
00366 array( 'page_id=pl_from' ) );
00367 $inputPages = array_keys( $pageSet );
00368 }
00369 return $pageSet;
00370 }
00371
00378 private function getImages( $inputPages, $pageSet ) {
00379 return $this->getLinks( $inputPages, $pageSet,
00380 'imagelinks',
00381 array( NS_FILE . ' AS namespace', 'il_to AS title' ),
00382 array( 'page_id=il_from' ) );
00383 }
00384
00389 private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
00390 $dbr = wfGetDB( DB_SLAVE );
00391 foreach( $inputPages as $page ) {
00392 $title = Title::newFromText( $page );
00393 if( $title ) {
00394 $pageSet[$title->getPrefixedText()] = true;
00397 $result = $dbr->select(
00398 array( 'page', $table ),
00399 $fields,
00400 array_merge( $join,
00401 array(
00402 'page_namespace' => $title->getNamespace(),
00403 'page_title' => $title->getDBkey() ) ),
00404 __METHOD__ );
00405 foreach( $result as $row ) {
00406 $template = Title::makeTitle( $row->namespace, $row->title );
00407 $pageSet[$template->getPrefixedText()] = true;
00408 }
00409 }
00410 }
00411 return $pageSet;
00412 }
00413 }
00414