MediaWiki master
InitialContentTask.php
Go to the documentation of this file.
1<?php
2
4
14
20class InitialContentTask extends Task {
22 private $wikiPageFactory;
23
25 private $pageStore;
26
28 public function getName() {
29 return 'mainpage';
30 }
31
33 public function getDependencies() {
34 return [ 'services', 'extension-tables' ];
35 }
36
37 public function execute(): Status {
38 $this->initServices( $this->getServices() );
40 $status = Status::newGood();
41 foreach ( $pages as $pageInfo ) {
42 $status->merge( $this->createPage( $pageInfo ) );
43 }
44 return $status;
45 }
46
55 private function createPage( array $pageInfo ) {
56 if ( isset( $pageInfo['title'] ) ) {
57 $titleText = $pageInfo['title'];
58 } elseif ( isset( $pageInfo['titlemsg'] ) ) {
59 $titleText = wfMessage( $pageInfo['titlemsg'] )->inContentLanguage()->text();
60 } else {
61 throw new \InvalidArgumentException(
62 'InstallerInitialPages is missing title/titlemsg' );
63 }
64 $title = $this->pageStore->getPageByText( $titleText );
65 $status = Status::newGood();
66 if ( !$title ) {
67 $status->warning( 'config-install-mainpage-failed', 'invalid title' );
68 return $status;
69 }
70 if ( $title->getId() ) {
71 $status->warning( 'config-install-mainpage-exists', $titleText );
72 return $status;
73 }
74 $page = $this->wikiPageFactory->newFromTitle( $title );
75
76 if ( isset( $pageInfo['text'] ) ) {
77 $text = $pageInfo['text'];
78 } elseif ( isset( $pageInfo['textmsg'] ) ) {
79 $text = wfMessage( $pageInfo['textmsg'] )->inContentLanguage()->text();
80 } else {
81 throw new \InvalidArgumentException(
82 'InstallerInitialPages is missing text/textmsg' );
83 }
84 $text = $this->replaceVariables( $text );
85
86 $content = new WikitextContent( $text );
87
88 try {
89 $updater = $page->newPageUpdater( User::newSystemUser( 'MediaWiki default' ) );
90 $updater
91 ->setContent( SlotRecord::MAIN, $content )
92 ->saveRevision(
93 CommentStoreComment::newUnsavedComment( '' )
94 );
95 $status = $updater->getStatus();
96 } catch ( \Exception $e ) {
97 // using raw, because $wgShowExceptionDetails can not be set yet
98 $status->fatal( 'config-install-mainpage-failed', $e->__toString() );
99 }
100
101 return $status;
102 }
103
107 private function initServices( MediaWikiServices $services ) {
108 $this->wikiPageFactory = $services->getWikiPageFactory();
109 $this->pageStore = $services->getPageStore();
110 }
111
118 private function replaceVariables( string $text ): string {
119 $text = preg_replace_callback(
120 '/\{\{ *InstallerOption: *(\w+) *}}/',
121 function ( $match ) {
122 return (string)$this->getOption( $match[1] );
123 },
124 $text
125 );
126 $text = preg_replace_callback(
127 '/\{\{ *InstallerConfig: *(\w+) *}}/',
128 function ( $match ) {
129 return (string)$this->getConfigVar( $match[1] );
130 },
131 $text
132 );
133 return $text;
134 }
135
136}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Value object for a comment stored by CommentStore.
Content object for wiki text pages.
getDependencies()
Get a list of names or aliases of tasks that must be done prior to this task.to override string|strin...
getName()
Get the symbolic name of the task.string
Base class for installer tasks.
Definition Task.php:24
getConfigVar(string $name)
Get a configuration variable for the wiki being created.
Definition Task.php:180
getServices()
Get the restored services.
Definition Task.php:292
A class containing constants representing the names of configuration variables.
const InstallerInitialPages
Name constant for the InstallerInitialPages setting, for use with Config::get()
Service locator for MediaWiki core services.
Service for creating WikiPage objects.
Value object representing a content slot associated with a page revision.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
User class for the MediaWiki software.
Definition User.php:130