PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wwwroot/mediawiki/includes/specials/SpecialExport.php

https://github.com/abma/spring-website
PHP | 414 lines | 267 code | 51 blank | 96 comment | 63 complexity | e394cca019f742efd10023300f83c695 MD5 | raw file
  1. <?php
  2. # Copyright (C) 2003-2008 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. /**
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. class SpecialExport extends SpecialPage {
  24. private $curonly, $doExport, $pageLinkDepth, $templates;
  25. private $images;
  26. public function __construct() {
  27. parent::__construct( 'Export' );
  28. }
  29. public function execute( $par ) {
  30. global $wgOut, $wgRequest, $wgSitename, $wgExportAllowListContributors;
  31. global $wgExportAllowHistory, $wgExportMaxHistory, $wgExportMaxLinkDepth;
  32. global $wgExportFromNamespaces;
  33. $this->setHeaders();
  34. $this->outputHeader();
  35. // Set some variables
  36. $this->curonly = true;
  37. $this->doExport = false;
  38. $this->templates = $wgRequest->getCheck( 'templates' );
  39. $this->images = $wgRequest->getCheck( 'images' ); // Doesn't do anything yet
  40. $this->pageLinkDepth = $this->validateLinkDepth(
  41. $wgRequest->getIntOrNull( 'pagelink-depth' ) );
  42. $nsindex = '';
  43. if ( $wgRequest->getCheck( 'addcat' ) ) {
  44. $page = $wgRequest->getText( 'pages' );
  45. $catname = $wgRequest->getText( 'catname' );
  46. if ( $catname !== '' && $catname !== null && $catname !== false ) {
  47. $t = Title::makeTitleSafe( NS_MAIN, $catname );
  48. if ( $t ) {
  49. /**
  50. * @todo Fixme: this can lead to hitting memory limit for very large
  51. * categories. Ideally we would do the lookup synchronously
  52. * during the export in a single query.
  53. */
  54. $catpages = $this->getPagesFromCategory( $t );
  55. if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
  56. }
  57. }
  58. }
  59. else if( $wgRequest->getCheck( 'addns' ) && $wgExportFromNamespaces ) {
  60. $page = $wgRequest->getText( 'pages' );
  61. $nsindex = $wgRequest->getText( 'nsindex', '' );
  62. if ( strval( $nsindex ) !== '' ) {
  63. /**
  64. * Same implementation as above, so same @todo
  65. */
  66. $nspages = $this->getPagesFromNamespace( $nsindex );
  67. if ( $nspages ) $page .= "\n" . implode( "\n", $nspages );
  68. }
  69. }
  70. else if( $wgRequest->wasPosted() && $par == '' ) {
  71. $page = $wgRequest->getText( 'pages' );
  72. $this->curonly = $wgRequest->getCheck( 'curonly' );
  73. $rawOffset = $wgRequest->getVal( 'offset' );
  74. if( $rawOffset ) {
  75. $offset = wfTimestamp( TS_MW, $rawOffset );
  76. } else {
  77. $offset = null;
  78. }
  79. $limit = $wgRequest->getInt( 'limit' );
  80. $dir = $wgRequest->getVal( 'dir' );
  81. $history = array(
  82. 'dir' => 'asc',
  83. 'offset' => false,
  84. 'limit' => $wgExportMaxHistory,
  85. );
  86. $historyCheck = $wgRequest->getCheck( 'history' );
  87. if ( $this->curonly ) {
  88. $history = WikiExporter::CURRENT;
  89. } elseif ( !$historyCheck ) {
  90. if ( $limit > 0 && ($wgExportMaxHistory == 0 || $limit < $wgExportMaxHistory ) ) {
  91. $history['limit'] = $limit;
  92. }
  93. if ( !is_null( $offset ) ) {
  94. $history['offset'] = $offset;
  95. }
  96. if ( strtolower( $dir ) == 'desc' ) {
  97. $history['dir'] = 'desc';
  98. }
  99. }
  100. if( $page != '' ) $this->doExport = true;
  101. } else {
  102. // Default to current-only for GET requests
  103. $page = $wgRequest->getText( 'pages', $par );
  104. $historyCheck = $wgRequest->getCheck( 'history' );
  105. if( $historyCheck ) {
  106. $history = WikiExporter::FULL;
  107. } else {
  108. $history = WikiExporter::CURRENT;
  109. }
  110. if( $page != '' ) $this->doExport = true;
  111. }
  112. if( !$wgExportAllowHistory ) {
  113. // Override
  114. $history = WikiExporter::CURRENT;
  115. }
  116. $list_authors = $wgRequest->getCheck( 'listauthors' );
  117. if ( !$this->curonly || !$wgExportAllowListContributors ) $list_authors = false ;
  118. if ( $this->doExport ) {
  119. $wgOut->disable();
  120. // Cancel output buffering and gzipping if set
  121. // This should provide safer streaming for pages with history
  122. wfResetOutputBuffers();
  123. header( "Content-type: application/xml; charset=utf-8" );
  124. if( $wgRequest->getCheck( 'wpDownload' ) ) {
  125. // Provide a sane filename suggestion
  126. $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
  127. $wgRequest->response()->header( "Content-disposition: attachment;filename={$filename}" );
  128. }
  129. $this->doExport( $page, $history, $list_authors );
  130. return;
  131. }
  132. $wgOut->addWikiMsg( 'exporttext' );
  133. $form = Xml::openElement( 'form', array( 'method' => 'post',
  134. 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
  135. $form .= Xml::inputLabel( wfMsg( 'export-addcattext' ) , 'catname', 'catname', 40 ) . '&nbsp;';
  136. $form .= Xml::submitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
  137. if ( $wgExportFromNamespaces ) {
  138. $form .= Xml::namespaceSelector( $nsindex, null, 'nsindex', wfMsg( 'export-addnstext' ) ) . '&nbsp;';
  139. $form .= Xml::submitButton( wfMsg( 'export-addns' ), array( 'name' => 'addns' ) ) . '<br />';
  140. }
  141. $form .= Xml::element( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ), $page, false );
  142. $form .= '<br />';
  143. if( $wgExportAllowHistory ) {
  144. $form .= Xml::checkLabel( wfMsg( 'exportcuronly' ), 'curonly', 'curonly', true ) . '<br />';
  145. } else {
  146. $wgOut->addHTML( wfMsgExt( 'exportnohistory', 'parse' ) );
  147. }
  148. $form .= Xml::checkLabel( wfMsg( 'export-templates' ), 'templates', 'wpExportTemplates', false ) . '<br />';
  149. if( $wgExportMaxLinkDepth || $this->userCanOverrideExportDepth() ) {
  150. $form .= Xml::inputLabel( wfMsg( 'export-pagelinks' ), 'pagelink-depth', 'pagelink-depth', 20, 0 ) . '<br />';
  151. }
  152. // Enable this when we can do something useful exporting/importing image information. :)
  153. //$form .= Xml::checkLabel( wfMsg( 'export-images' ), 'images', 'wpExportImages', false ) . '<br />';
  154. $form .= Xml::checkLabel( wfMsg( 'export-download' ), 'wpDownload', 'wpDownload', true ) . '<br />';
  155. $form .= Xml::submitButton( wfMsg( 'export-submit' ), array( 'accesskey' => 's' ) );
  156. $form .= Xml::closeElement( 'form' );
  157. $wgOut->addHTML( $form );
  158. }
  159. private function userCanOverrideExportDepth() {
  160. global $wgUser;
  161. return $wgUser->isAllowed( 'override-export-depth' );
  162. }
  163. /**
  164. * Do the actual page exporting
  165. * @param string $page User input on what page(s) to export
  166. * @param mixed $history one of the WikiExporter history export constants
  167. */
  168. private function doExport( $page, $history, $list_authors ) {
  169. global $wgExportMaxHistory;
  170. $pageSet = array(); // Inverted index of all pages to look up
  171. // Split up and normalize input
  172. foreach( explode( "\n", $page ) as $pageName ) {
  173. $pageName = trim( $pageName );
  174. $title = Title::newFromText( $pageName );
  175. if( $title && $title->getInterwiki() == '' && $title->getText() !== '' ) {
  176. // Only record each page once!
  177. $pageSet[$title->getPrefixedText()] = true;
  178. }
  179. }
  180. // Set of original pages to pass on to further manipulation...
  181. $inputPages = array_keys( $pageSet );
  182. // Look up any linked pages if asked...
  183. if( $this->templates ) {
  184. $pageSet = $this->getTemplates( $inputPages, $pageSet );
  185. }
  186. if( $linkDepth = $this->pageLinkDepth ) {
  187. $pageSet = $this->getPageLinks( $inputPages, $pageSet, $linkDepth );
  188. }
  189. /*
  190. // Enable this when we can do something useful exporting/importing image information. :)
  191. if( $this->images ) ) {
  192. $pageSet = $this->getImages( $inputPages, $pageSet );
  193. }
  194. */
  195. $pages = array_keys( $pageSet );
  196. // Normalize titles to the same format and remove dupes, see bug 17374
  197. foreach( $pages as $k => $v ) {
  198. $pages[$k] = str_replace( " ", "_", $v );
  199. }
  200. $pages = array_unique( $pages );
  201. /* Ok, let's get to it... */
  202. if( $history == WikiExporter::CURRENT ) {
  203. $lb = false;
  204. $db = wfGetDB( DB_SLAVE );
  205. $buffer = WikiExporter::BUFFER;
  206. } else {
  207. // Use an unbuffered query; histories may be very long!
  208. $lb = wfGetLBFactory()->newMainLB();
  209. $db = $lb->getConnection( DB_SLAVE );
  210. $buffer = WikiExporter::STREAM;
  211. // This might take a while... :D
  212. wfSuppressWarnings();
  213. set_time_limit(0);
  214. wfRestoreWarnings();
  215. }
  216. $exporter = new WikiExporter( $db, $history, $buffer );
  217. $exporter->list_authors = $list_authors;
  218. $exporter->openStream();
  219. foreach( $pages as $page ) {
  220. /*
  221. if( $wgExportMaxHistory && !$this->curonly ) {
  222. $title = Title::newFromText( $page );
  223. if( $title ) {
  224. $count = Revision::countByTitle( $db, $title );
  225. if( $count > $wgExportMaxHistory ) {
  226. wfDebug( __FUNCTION__ .
  227. ": Skipped $page, $count revisions too big\n" );
  228. continue;
  229. }
  230. }
  231. }*/
  232. #Bug 8824: Only export pages the user can read
  233. $title = Title::newFromText( $page );
  234. if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
  235. if( !$title->userCanRead() ) continue; #TODO: perhaps output an <error> tag or something.
  236. $exporter->pageByTitle( $title );
  237. }
  238. $exporter->closeStream();
  239. if( $lb ) {
  240. $lb->closeAll();
  241. }
  242. }
  243. private function getPagesFromCategory( $title ) {
  244. global $wgContLang;
  245. $name = $title->getDBkey();
  246. $dbr = wfGetDB( DB_SLAVE );
  247. $res = $dbr->select( array('page', 'categorylinks' ),
  248. array( 'page_namespace', 'page_title' ),
  249. array('cl_from=page_id', 'cl_to' => $name ),
  250. __METHOD__, array('LIMIT' => '5000'));
  251. $pages = array();
  252. while ( $row = $dbr->fetchObject( $res ) ) {
  253. $n = $row->page_title;
  254. if ($row->page_namespace) {
  255. $ns = $wgContLang->getNsText( $row->page_namespace );
  256. $n = $ns . ':' . $n;
  257. }
  258. $pages[] = $n;
  259. }
  260. $dbr->freeResult($res);
  261. return $pages;
  262. }
  263. private function getPagesFromNamespace( $nsindex ) {
  264. global $wgContLang;
  265. $dbr = wfGetDB( DB_SLAVE );
  266. $res = $dbr->select( 'page', array('page_namespace', 'page_title'),
  267. array('page_namespace' => $nsindex),
  268. __METHOD__, array('LIMIT' => '5000') );
  269. $pages = array();
  270. while ( $row = $dbr->fetchObject( $res ) ) {
  271. $n = $row->page_title;
  272. if ($row->page_namespace) {
  273. $ns = $wgContLang->getNsText( $row->page_namespace );
  274. $n = $ns . ':' . $n;
  275. }
  276. $pages[] = $n;
  277. }
  278. $dbr->freeResult($res);
  279. return $pages;
  280. }
  281. /**
  282. * Expand a list of pages to include templates used in those pages.
  283. * @param $inputPages array, list of titles to look up
  284. * @param $pageSet array, associative array indexed by titles for output
  285. * @return array associative array index by titles
  286. */
  287. private function getTemplates( $inputPages, $pageSet ) {
  288. return $this->getLinks( $inputPages, $pageSet,
  289. 'templatelinks',
  290. array( 'tl_namespace AS namespace', 'tl_title AS title' ),
  291. array( 'page_id=tl_from' ) );
  292. }
  293. /**
  294. * Validate link depth setting, if available.
  295. */
  296. private function validateLinkDepth( $depth ) {
  297. global $wgExportMaxLinkDepth, $wgExportMaxLinkDepthLimit;
  298. if( $depth < 0 ) {
  299. return 0;
  300. }
  301. if ( !$this->userCanOverrideExportDepth() ) {
  302. if( $depth > $wgExportMaxLinkDepth ) {
  303. return $wgExportMaxLinkDepth;
  304. }
  305. }
  306. /*
  307. * There's a HARD CODED limit of 5 levels of recursion here to prevent a
  308. * crazy-big export from being done by someone setting the depth
  309. * number too high. In other words, last resort safety net.
  310. */
  311. return intval( min( $depth, 5 ) );
  312. }
  313. /** Expand a list of pages to include pages linked to from that page. */
  314. private function getPageLinks( $inputPages, $pageSet, $depth ) {
  315. for( $depth=$depth; $depth>0; --$depth ) {
  316. $pageSet = $this->getLinks( $inputPages, $pageSet, 'pagelinks',
  317. array( 'pl_namespace AS namespace', 'pl_title AS title' ),
  318. array( 'page_id=pl_from' ) );
  319. $inputPages = array_keys( $pageSet );
  320. }
  321. return $pageSet;
  322. }
  323. /**
  324. * Expand a list of pages to include images used in those pages.
  325. * @param $inputPages array, list of titles to look up
  326. * @param $pageSet array, associative array indexed by titles for output
  327. * @return array associative array index by titles
  328. */
  329. private function getImages( $inputPages, $pageSet ) {
  330. return $this->getLinks( $inputPages, $pageSet,
  331. 'imagelinks',
  332. array( NS_FILE . ' AS namespace', 'il_to AS title' ),
  333. array( 'page_id=il_from' ) );
  334. }
  335. /**
  336. * Expand a list of pages to include items used in those pages.
  337. * @private
  338. */
  339. private function getLinks( $inputPages, $pageSet, $table, $fields, $join ) {
  340. $dbr = wfGetDB( DB_SLAVE );
  341. foreach( $inputPages as $page ) {
  342. $title = Title::newFromText( $page );
  343. if( $title ) {
  344. $pageSet[$title->getPrefixedText()] = true;
  345. /// @todo Fixme: May or may not be more efficient to batch these
  346. /// by namespace when given multiple input pages.
  347. $result = $dbr->select(
  348. array( 'page', $table ),
  349. $fields,
  350. array_merge( $join,
  351. array(
  352. 'page_namespace' => $title->getNamespace(),
  353. 'page_title' => $title->getDBkey() ) ),
  354. __METHOD__ );
  355. foreach( $result as $row ) {
  356. $template = Title::makeTitle( $row->namespace, $row->title );
  357. $pageSet[$template->getPrefixedText()] = true;
  358. }
  359. }
  360. }
  361. return $pageSet;
  362. }
  363. }