| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Content\ContentHandler; |
| 25 |
use MediaWiki\Json\FormatJson; |
| 26 |
use MediaWiki\StubObject\StubGlobalUser; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use MediaWiki\User\User; |
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/Maintenance.php'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ImportSiteScripts extends Maintenance { |
| 41 |
public function __construct() { |
| 42 |
parent::__construct(); |
| 43 |
$this->addDescription( 'Import site scripts from a site' ); |
| 44 |
$this->addArg( 'api', 'API base url' ); |
| 45 |
$this->addArg( 'index', 'index.php base url' ); |
| 46 |
$this->addOption( 'username', 'User name of the script importer' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function execute() { |
| 50 |
$username = $this->getOption( 'username', false ); |
| 51 |
if ( $username === false ) { |
| 52 |
$user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] ); |
| 53 |
} else { |
| 54 |
$user = User::newFromName( $username ); |
| 55 |
} |
| 56 |
'@phan-var User $user'; |
| 57 |
StubGlobalUser::setUser( $user ); |
| 58 |
|
| 59 |
$baseUrl = $this->getArg( 1 ); |
| 60 |
$pageList = $this->fetchScriptList(); |
| 61 |
$this->output( 'Importing ' . count( $pageList ) . " pages\n" ); |
| 62 |
$services = $this->getServiceContainer(); |
| 63 |
$wikiPageFactory = $services->getWikiPageFactory(); |
| 64 |
$httpRequestFactory = $services->getHttpRequestFactory(); |
| 65 |
|
| 66 |
foreach ( $pageList as $page ) { |
| 67 |
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $page ); |
| 68 |
if ( !$title ) { |
| 69 |
$this->error( "$page is an invalid title; it will not be imported\n" ); |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$this->output( "Importing $page\n" ); |
| 74 |
$url = wfAppendQuery( $baseUrl, [ |
| 75 |
'action' => 'raw', |
| 76 |
'title' => "MediaWiki:{$page}" ] ); |
| 77 |
$text = $httpRequestFactory->get( $url, [], __METHOD__ ); |
| 78 |
|
| 79 |
$wikiPage = $wikiPageFactory->newFromTitle( $title ); |
| 80 |
$content = ContentHandler::makeContent( $text, $wikiPage->getTitle() ); |
| 81 |
$wikiPage->doUserEditContent( $content, $user, "Importing from $url" ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
protected function fetchScriptList() { |
| 86 |
$data = [ |
| 87 |
'action' => 'query', |
| 88 |
'format' => 'json', |
| 89 |
'list' => 'allpages', |
| 90 |
'apnamespace' => '8', |
| 91 |
'aplimit' => '500', |
| 92 |
'continue' => '', |
| 93 |
]; |
| 94 |
$baseUrl = $this->getArg( 0 ); |
| 95 |
$pages = []; |
| 96 |
|
| 97 |
while ( true ) { |
| 98 |
$url = wfAppendQuery( $baseUrl, $data ); |
| 99 |
$strResult = $this->getServiceContainer()->getHttpRequestFactory()-> |
| 100 |
get( $url, [], __METHOD__ ); |
| 101 |
$result = FormatJson::decode( $strResult, true ); |
| 102 |
|
| 103 |
$page = null; |
| 104 |
foreach ( $result['query']['allpages'] as $page ) { |
| 105 |
if ( str_ends_with( $page['title'], '.js' ) ) { |
| 106 |
strtok( $page['title'], ':' ); |
| 107 |
$pages[] = strtok( '' ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ( $page !== null ) { |
| 112 |
$this->output( "Fetched list up to {$page['title']}\n" ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( isset( $result['continue'] ) ) { |
| 116 |
$data = array_replace( $data, $result['continue'] ); |
| 117 |
} elseif ( isset( $result['query-continue']['allpages'] ) ) { |
| 118 |
$data = array_replace( $data, $result['query-continue']['allpages'] ); |
| 119 |
} else { |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return $pages; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
$maintClass = ImportSiteScripts::class; |
| 130 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 131 |
|