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
27 public function getName() {
28 return 'mainpage';
29 }
30
31 public function getDependencies() {
32 return [ 'services', 'extension-tables' ];
33 }
34
35 public function execute(): Status {
36 $this->initServices( $this->getServices() );
38 $status = Status::newGood();
39 foreach ( $pages as $pageInfo ) {
40 $status->merge( $this->createPage( $pageInfo ) );
41 }
42 return $status;
43 }
44
53 private function createPage( array $pageInfo ) {
54 if ( isset( $pageInfo['title'] ) ) {
55 $titleText = $pageInfo['title'];
56 } elseif ( isset( $pageInfo['titlemsg'] ) ) {
57 $titleText = wfMessage( $pageInfo['titlemsg'] )->inContentLanguage()->text();
58 } else {
59 throw new \InvalidArgumentException(
60 'InstallerInitialPages is missing title/titlemsg' );
61 }
62 $title = $this->pageStore->getPageByText( $titleText );
63 $status = Status::newGood();
64 if ( !$title ) {
65 $status->warning( 'config-install-mainpage-failed', 'invalid title' );
66 return $status;
67 }
68 if ( $title->getId() ) {
69 $status->warning( 'config-install-mainpage-exists', $titleText );
70 return $status;
71 }
72 $page = $this->wikiPageFactory->newFromTitle( $title );
73
74 if ( isset( $pageInfo['text'] ) ) {
75 $text = $pageInfo['text'];
76 } elseif ( isset( $pageInfo['textmsg'] ) ) {
77 $text = wfMessage( $pageInfo['textmsg'] )->inContentLanguage()->text();
78 } else {
79 throw new \InvalidArgumentException(
80 'InstallerInitialPages is missing text/textmsg' );
81 }
82 $text = $this->replaceVariables( $text );
83
84 $content = new WikitextContent( $text );
85
86 try {
87 $updater = $page->newPageUpdater( User::newSystemUser( 'MediaWiki default' ) );
88 $updater
89 ->setContent( SlotRecord::MAIN, $content )
90 ->saveRevision(
91 CommentStoreComment::newUnsavedComment( '' )
92 );
93 $status = $updater->getStatus();
94 } catch ( \Exception $e ) {
95 // using raw, because $wgShowExceptionDetails can not be set yet
96 $status->fatal( 'config-install-mainpage-failed', $e->__toString() );
97 }
98
99 return $status;
100 }
101
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.
getName()
Get the symbolic name of the task.
Base class for installer tasks.
Definition Task.php:24
getConfigVar(string $name)
Get a configuration variable for the wiki being created.
Definition Task.php:182
getServices()
Get the restored services.
Definition Task.php:296
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:54
User class for the MediaWiki software.
Definition User.php:119