MediaWiki  1.34.0
importSiteScripts.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Import site scripts from a site' );
38  $this->addArg( 'api', 'API base url' );
39  $this->addArg( 'index', 'index.php base url' );
40  $this->addOption( 'username', 'User name of the script importer' );
41  }
42 
43  public function execute() {
44  global $wgUser;
45 
46  $username = $this->getOption( 'username', false );
47  if ( $username === false ) {
48  $user = User::newSystemUser( 'ScriptImporter', [ 'steal' => true ] );
49  } else {
50  $user = User::newFromName( $username );
51  }
52  $wgUser = $user;
53 
54  $baseUrl = $this->getArg( 1 );
55  $pageList = $this->fetchScriptList();
56  $this->output( 'Importing ' . count( $pageList ) . " pages\n" );
57 
58  foreach ( $pageList as $page ) {
60  if ( !$title ) {
61  $this->error( "$page is an invalid title; it will not be imported\n" );
62  continue;
63  }
64 
65  $this->output( "Importing $page\n" );
66  $url = wfAppendQuery( $baseUrl, [
67  'action' => 'raw',
68  'title' => "MediaWiki:{$page}" ] );
69  $text = MediaWikiServices::getInstance()->getHttpRequestFactory()->
70  get( $url, [], __METHOD__ );
71 
72  $wikiPage = WikiPage::factory( $title );
73  $content = ContentHandler::makeContent( $text, $wikiPage->getTitle() );
74  $wikiPage->doEditContent( $content, "Importing from $url", 0, false, $user );
75  }
76  }
77 
78  protected function fetchScriptList() {
79  $data = [
80  'action' => 'query',
81  'format' => 'json',
82  'list' => 'allpages',
83  'apnamespace' => '8',
84  'aplimit' => '500',
85  'continue' => '',
86  ];
87  $baseUrl = $this->getArg( 0 );
88  $pages = [];
89 
90  while ( true ) {
91  $url = wfAppendQuery( $baseUrl, $data );
92  $strResult = MediaWikiServices::getInstance()->getHttpRequestFactory()->
93  get( $url, [], __METHOD__ );
94  $result = FormatJson::decode( $strResult, true );
95 
96  $page = null;
97  foreach ( $result['query']['allpages'] as $page ) {
98  if ( substr( $page['title'], -3 ) === '.js' ) {
99  strtok( $page['title'], ':' );
100  $pages[] = strtok( '' );
101  }
102  }
103 
104  if ( $page !== null ) {
105  $this->output( "Fetched list up to {$page['title']}\n" );
106  }
107 
108  if ( isset( $result['continue'] ) ) { // >= 1.21
109  $data = array_replace( $data, $result['continue'] );
110  } elseif ( isset( $result['query-continue']['allpages'] ) ) { // <= 1.20
111  $data = array_replace( $data, $result['query-continue']['allpages'] );
112  } else {
113  break;
114  }
115  }
116 
117  return $pages;
118  }
119 }
120 
121 $maintClass = ImportSiteScripts::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
ImportSiteScripts\execute
execute()
Do the actual work.
Definition: importSiteScripts.php:43
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:737
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:439
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:174
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
ImportSiteScripts\fetchScriptList
fetchScriptList()
Definition: importSiteScripts.php:78
$title
$title
Definition: testCompression.php:34
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:135
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
$content
$content
Definition: router.php:78
ImportSiteScripts
Maintenance script to import all scripts in the MediaWiki namespace from a local site.
Definition: importSiteScripts.php:34
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
ImportSiteScripts\__construct
__construct()
Default constructor.
Definition: importSiteScripts.php:35
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371
$maintClass
$maintClass
Definition: importSiteScripts.php:121