importSiteScripts.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Import all scripts in the MediaWiki namespace from a local site.
  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 Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to import all scripts in the MediaWiki namespace from a
  26. * local site.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class ImportSiteScripts extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Import site scripts from a site' );
  34. $this->addArg( 'api', 'API base url' );
  35. $this->addArg( 'index', 'index.php base url' );
  36. $this->addOption( 'username', 'User name of the script importer' );
  37. }
  38. public function execute() {
  39. global $wgUser;
  40. $username = $this->getOption( 'username', false );
  41. if ( $username === false ) {
  42. $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
  43. } else {
  44. $user = User::newFromName( $username );
  45. }
  46. $wgUser = $user;
  47. $baseUrl = $this->getArg( 1 );
  48. $pageList = $this->fetchScriptList();
  49. $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
  50. foreach ( $pageList as $page ) {
  51. $title = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
  52. if ( !$title ) {
  53. $this->error( "$page is an invalid title; it will not be imported\n" );
  54. continue;
  55. }
  56. $this->output( "Importing $page\n" );
  57. $url = wfAppendQuery( $baseUrl, [
  58. 'action' => 'raw',
  59. 'title' => "MediaWiki:{$page}" ] );
  60. $text = Http::get( $url, [], __METHOD__ );
  61. $wikiPage = WikiPage::factory( $title );
  62. $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
  63. $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
  64. }
  65. }
  66. protected function fetchScriptList() {
  67. $data = [
  68. 'action' => 'query',
  69. 'format' => 'json',
  70. 'list' => 'allpages',
  71. 'apnamespace' => '8',
  72. 'aplimit' => '500',
  73. 'continue' => '',
  74. ];
  75. $baseUrl = $this->getArg( 0 );
  76. $pages = [];
  77. while ( true ) {
  78. $url = wfAppendQuery( $baseUrl, $data );
  79. $strResult = Http::get( $url, [], __METHOD__ );
  80. $result = FormatJson::decode( $strResult, true );
  81. $page = null;
  82. foreach ( $result['query']['allpages'] as $page ) {
  83. if ( substr( $page['title'], -3 ) === '.js' ) {
  84. strtok( $page['title'], ':' );
  85. $pages[] = strtok( '' );
  86. }
  87. }
  88. if ( $page !== null ) {
  89. $this->output( "Fetched list up to {$page['title']}\n" );
  90. }
  91. if ( isset( $result['continue'] ) ) { // >= 1.21
  92. $data = array_replace( $data, $result['continue'] );
  93. } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
  94. $data = array_replace( $data, $result['query-continue']['allpages'] );
  95. } else {
  96. break;
  97. }
  98. }
  99. return $pages;
  100. }
  101. }
  102. $maintClass = 'ImportSiteScripts';
  103. require_once RUN_MAINTENANCE_IF_MAIN;