<?php
class SpecialMassCreatePages extends SpecialPage {
public function __construct() {
parent::__construct( 'MassCreatePages', 'masscreatepages' );
}
public function execute( $sub ) {
// Проверка прав пользователя
$this->checkPermissions();
$this->setHeaders();
$this->outputHeader();
$out = $this->getOutput();
$request = $this->getRequest();
// Создаем форму с помощью встроенного HTMLForm
$formDescriptor = [
'TemplateText' => [
'type' => 'textarea',
'label-message' => 'masscreatepages-template-text',
'rows' => 5,
'default' => '{{#invoke:TeamStatCompiler|build_page|%VAR1%}}',
'required' => true,
],
'PageList' => [
'type' => 'textarea',
'label-message' => 'masscreatepages-page-list',
'rows' => 15,
'help-message' => '',
'required' => true,
]
];
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext(), 'masscreatepages-form' );
$htmlForm->setSubmitTextMsg( 'masscreatepages-submit' );
$htmlForm->setSubmitCallback( [ $this, 'processForm' ] );
$htmlForm->show();
}
public function processForm( $formData ) {
$templateText = $formData['TemplateText'];
$pageListRaw = $formData['PageList'];
$out = $this->getOutput();
// Разбиваем список по строкам
$lines = explode( "\n", str_replace( "\r", "", $pageListRaw ) );
$results = [];
foreach ( $lines as $line ) {
$line = trim( $line );
if ( $line === '' ) {
continue;
}
// Разбиваем строку на части (Название | VAR1 | VAR2 ...)
$parts = explode( '|', $line );
$titleText = trim( array_shift( $parts ) );
$title = Title::newFromText( $titleText );
if ( !$title || !$title->isValid() ) {
$results[] = Html::rawElement( 'div', [ 'style' => 'color:red;' ],
$this->msg( 'masscreatepages-invalid', htmlspecialchars( $titleText ) )->escaped() );
continue;
}
// Проверяем, существует ли страница
if ( $title->exists() ) {
$results[] = Html::rawElement( 'div', [ 'style' => 'color:orange;' ],
$this->msg( 'masscreatepages-skipped', $title->getPrefixedText() )->parse() );
continue;
}
// Формируем текст страницы, заменяя переменные %VAR1%, %VAR2% и т.д.
$pageContent = $templateText;
foreach ( $parts as $index => $val ) {
$varName = '%VAR' . ( $index + 1 ) . '%';
$pageContent = str_replace( $varName, trim( $val ), $pageContent );
}
// Создаем страницу (API MediaWiki 1.34)
$wikiPage = WikiPage::factory( $title );
$content = ContentHandler::makeContent( $pageContent, $title );
$updater = $wikiPage->newPageUpdater( $this->getUser() );
$updater->setContent( 'main', $content );
$updater->saveRevision(
CommentStoreComment::newUnsavedComment( $this->msg( 'masscreatepages-summary' )->text() ),
EDIT_NEW | EDIT_SUPPRESS_RC
);
if ( $updater->wasSuccessful() ) {
$results[] = Html::rawElement( 'div', [ 'style' => 'color:green; font-weight:bold;' ],
$this->msg( 'masscreatepages-success', $title->getPrefixedText() )->parse() );
}
}
// Выводим результаты
$out->addHTML( '<h3>Результаты:</h3>' . implode( "\n", $results ) . '<hr>' );
return true; // Возвращаем true, чтобы форма отобразилась снова для новых задач
}
}