Skip to content

Commit

Permalink
Convert Special:CreateWiki to HTMLForm
Browse files Browse the repository at this point in the history
  • Loading branch information
Southparkfan committed Sep 12, 2015
1 parent 2b48326 commit 252781a
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 104 deletions.
222 changes: 123 additions & 99 deletions SpecialCreateWiki.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,89 @@
<?php
class SpecialCreateWiki extends SpecialPage {
class SpecialCreateWiki extends FormSpecialPage {
function __construct() {
parent::__construct( 'CreateWiki', 'createwiki' );
}

function execute( $par ) {
$request = $this->getRequest();
$out = $this->getOutput();
$this->setHeaders();
$this->checkPermissions();
protected function getFormFields() {
$par = $this->par;
$request = $this->getRequest();

$this->showInputForm();
$formDescriptor = array();

if ( $request->wasPosted() ) {
$this->handleInput();
}
}
$formDescriptor['dbname'] = array(
'label-message' => 'createwiki-label-dbname',
'type' => 'text',
'default' => $request->getVal( 'cwDBname' ) ? $request->getVal( 'cwDBname' ) : $par,
'size' => 20,
'required' => true,
'validation-callback' => array( __CLASS__, 'validateDBname' ),
'name' => 'cwDBname',

function showInputForm() {
$localpage = $this->getPageTitle()->getLocalUrl();
$request = $this->getRequest();
$language = $request->getVal( 'cwLanguage' ) ? $request->getVal( 'cwLanguage' ) : 'en';
$privateboxchecked = $request->getVal( 'cwPrivate' );

$form = Xml::openElement( 'form', array( 'action' => $localpage, 'method' => 'post' ) );
$form .= '<fieldset><legend>' . $this->msg( 'createwiki' )->escaped() . '</legend>';
$form .= Xml::openElement( 'table' );
$form .= '<tr><td>' . $this->msg( 'createwiki-label-dbname' )->escaped() . '</td>';
$form .= '<td>' . Xml::input( 'cwDBname', 20, $request->getVal( 'cwDBname' ), array( 'required' => '' ) ) . '</td></tr>';
$form .= '<tr><td>' . $this->msg( 'createwiki-label-founder' )->escaped() . '</td>';
$form .= '<td>' . Xml::input( 'cwFounder', 20, $request->getVal( 'cwFounder' ), array( 'required' => '' ) ) . '</td></tr>';
$form .= '<tr><td>' . $this->msg( 'createwiki-label-sitename' )->escaped() . '</td>';
$form .= '<td>' . Xml::input( 'cwSitename', 20, $request->getVal( 'cwSitename' ), array( 'required' => '' ) ) . '</td></tr>';
$form .= '<tr><td>' . $this->msg( 'createwiki-label-language' )->escaped() . '</td>';
$form .= '<td>' . Xml::languageSelector( $language, true, null, array( 'name' => 'cwLanguage' ) )[1] . '</td></tr>';
$form .= '<tr><td>' . $this->msg( 'createwiki-label-private' )->escaped() . '</td>';
$form .= '<td>' . Xml::check( 'cwPrivate', $privateboxchecked, array( 'value' => 0 ) ) . '</td></tr>';
$form .= '<tr><td>' . $this->msg( 'createwiki-label-reason' )->escaped() . '</td>';
$form .= '<td>' . Xml::input( 'cwReason', 45, $request->getVal( 'cwReason' ), array( 'required' => '' ) ) . '</td></tr>';
$form .= '<tr><td>' . Xml::submitButton( $this->msg( 'createwiki-label-submit' )->plain() ) . '</td></tr>';
$form .= Xml::closeElement( 'table' );
$form .= '</fieldset>';
$form .= Html::hidden( 'cwToken', $this->getUser()->getEditToken() );
$form .= Xml::closeElement( 'form' );

$this->getOutput()->addHTML( $form );
}
);

function handleInput() {
global $IP, $wgCreateWikiSQLfiles;
$formDescriptor['founder'] = array(
'label-message' => 'createwiki-label-founder',
'type' => 'text',
'default' => $request->getVal( 'cwFounder' ),
'size' => 20,
'required' => true,
'validation-callback' => array( __CLASS__, 'validateFounder' ),
'name' => 'cwFounder',
);

$request = $this->getRequest();
$out = $this->getOutput();
$formDescriptor['sitename'] = array(
'label-message' => 'createwiki-label-sitename',
'type' => 'text',
'default' => $request->getVal( 'cwSitename' ),
'size' => 20,
'name' => 'cwSitename',
);

$DBname = trim( $request->getVal( 'cwDBname' ) );
$founder = trim( $request->getVal( 'cwFounder' ) );
$sitename = trim( $request->getVal( 'cwSitename' ) );
$reason = $request->getVal( 'cwReason' );
$language = $request->getVal( 'cwLanguage' );
$private = is_null( $request->getVal( 'cwPrivate' ) ) ? 0 : 1;
// Building a language selector (attribution:
// includes/specials/SpecialPageLanguage.php L68)
$languages = Language::fetchLanguageNames( null, 'mwfile' );
ksort( $languages );
$options = array();
foreach ( $languages as $code => $name ) {
$options["$code - $name"] = $code;
}

$formDescriptor['language'] = array(
'type' => 'select',
'options' => $options,
'label-message' => 'createwiki-label-language',
'default' => $request->getVal( 'cwLanguage' ) ? $request->getVal( 'cwLanguage' ) : 'en',
'name' => 'cwLanguage',
);

$dbw = wfGetDB( DB_MASTER );
$formDescriptor['private'] = array(
'type' => 'check',
'label-message' => 'createwiki-label-private',
'name' => 'cwPrivate',
);

if ( !$this->getUser()->matchEditToken( $request->getVal( 'cwToken' ) ) ) {
$out->addWikiMsg( 'createwiki-error-csrf' );
return false;
}
$formDescriptor['reason'] = array(
'label-message' => 'createwiki-label-reason',
'type' => 'text',
'default' => $request->getVal( 'wpreason' ),
'size' => 45,
'required' => true,
);

$validation = $this->validateInput( $DBname, $founder );
return $formDescriptor;
}

if ( !$validation ) {
return false;
}
public function onSubmit( array $formData ) {
global $IP, $wgCreateWikiSQLfiles;

$DBname = $formData['dbname'];
$founderName = $formData['founder'];
$siteName = $formData['sitename'];
$language = $formData['language'];
$private = $formData['private'];
$reason = $formData['reason'];

$dbw = wfGetDB( DB_MASTER );

$farmerLogEntry = new ManualLogEntry( 'farmer', 'createwiki' );
$farmerLogEntry->setPerformer( $this->getUser() );
Expand All @@ -93,44 +105,38 @@ function handleInput() {
$dbw->sourceFile( $sqlfile );
}

$this->writeToDBlist( $DBname, $sitename, $language, $private );
$this->writeToDBlist( $DBname, $siteName, $language, $private );
$this->createMainPage( $language );

$shcreateaccount = exec( "/usr/bin/php $IP/extensions/CentralAuth/maintenance/createLocalAccount.php " . wfEscapeShellArg( $founder ) . ' --wiki ' . wfEscapeShellArg( $DBname ) );
$shcreateaccount = exec( "/usr/bin/php " .
"$IP/extensions/CentralAuth/maintenance/createLocalAccount.php " . wfEscapeShellArg( $founderName ) . " --wiki " . wfEscapeShellArg( $DBname ) );

if ( !strpos( $shcreateaccount, 'created' ) ) {
wfDebugLog( 'CreateWiki', 'Failed to create local account for founder. - error: ' . $shcreateaccount );

$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-usernotcreated' )->escaped() . '</div>' );
return false;
return wfMessage( 'createwiki-error-usernotcreated' )->escaped();
}

$shpromoteaccount = exec( "/usr/bin/php $IP/maintenance/createAndPromote.php " . wfEscapeShellArg( $founder ) . ' --bureaucrat --sysop --force --wiki ' . wfEscapeShellArg( $DBname ) );
$shpromoteaccount = exec( "/usr/bin/php " .
"$IP/maintenance/createAndPromote.php " . wfEscapeShellArg( $founderName ) . " --bureaucrat --sysop --force --wiki " . wfEscapeShellArg( $DBname ) );

$out->addHTML( '<div class="successbox">' . $this->msg( 'createwiki-success' )->escaped() . '</div>' );
$this->getOutput()->addHTML( '<div class="successbox">' . wfMessage( 'createwiki-success' )->escaped() . '</div>' );

return true;
}

function validateInput( $DBname, $founder ) {
$out = $this->getOutput();

$user = User::newFromName( $founder );
if ( !$user->getId() ) {
$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-foundernonexistent' )->escaped() . '</div>' );
return false;
}
public function validateDBname( $DBname, $allData ) {
global $wgConf;

if ( !$this->validateDBname( $DBname ) ) {
return false;
# HTMLForm's validation-callback somehow gets called, even
# while the form was not submitted yet. This should prevent
# the validation from failing because the submitted value is
# NULL, but it is a hack, and instead the validation just
# shouldn't be called unless the form actually has been
# submitted..
if ( is_null( $DBname ) ) {
return true;
}

return true;
}

function validateDBname( $DBname ) {
global $wgConf;
$out = $this->getOutput();

$suffixed = false;
foreach ( $wgConf->suffixes as $suffix ) {
if ( substr( $DBname, -strlen( $suffix ) ) === $suffix ) {
Expand All @@ -143,56 +149,74 @@ function validateDBname( $DBname ) {
$res = $dbw->query( 'SHOW DATABASES LIKE ' . $dbw->addQuotes( $DBname ) . ';' );

if ( $res->numRows() !== 0 ) {
$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-dbexists' )->escaped() . '</div>' );
return false;
return wfMessage( 'createwiki-error-dbexists' )->escaped();
}

if ( !$suffixed ) {
$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-notsuffixed' )->escaped() . '</div>' );
return false;
return wfMessage( 'createwiki-error-notsuffixed' )->escaped();
}

if ( !ctype_alnum( $DBname ) ) {
$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-notalnum' )->escaped() . '</div>' );
return false;
return wfMessage( 'createwiki-error-notalnum' )->escaped();
}

if ( strtolower( $DBname ) !== $DBname ) {
$out->addHTML( '<div class="errorbox">' . $this->msg( 'createwiki-error-notlowercase' )->escaped() . '</div>' );
return false;
return wfMessage( 'createwiki-error-notlowercase' )->escaped();
}

return true;
}

function writeToDBlist( $DBname, $sitename, $language, $private ) {
public function validateFounder( $founderName, $allData ) {
# HTMLForm's validation-callback somehow gets called, even
# while the form was not submitted yet. This should prevent
# the validation from failing because the submitted value is
# NULL, but it is a hack, and instead the validation just
# shouldn't be called unless the form actually has been
# submitted..
if ( is_null( $founderName ) ) {
return true;
}

$user = User::newFromName( $founderName );

if ( !$user->getId() ) {
return wfMessage( 'createwiki-error-foundernonexistent' )->escaped();
}

return true;
}

public function writeToDBlist( $DBname, $siteName, $language, $private ) {
global $IP;

$dbline = "$DBname|$sitename|$language|\n";
$dbline = "$DBname|$siteName|$language|\n";
file_put_contents( "/srv/mediawiki/dblist/all.dblist", $dbline, FILE_APPEND | LOCK_EX );

if ( $private !== 0 ) {
if ( $private ) {
file_put_contents( "/srv/mediawiki/dblist/private.dblist", "$DBname\n", FILE_APPEND | LOCK_EX );
}

return true;
}

function createMainPage( $lang ) {
public function createMainPage( $language ) {
// Don't use Meta's mainpage message!
if ( $lang !== 'en' ) {
if ( $language !== 'en' ) {
$page = wfMessage( 'mainpage' )->inLanguage( $lang )->plain();
} else {
$page = 'Main_Page';
}

$title = Title::newFromText( $page );
$article = WikiPage::factory( $title );

$article->doEditContent( new WikitextContent(
wfMessage( 'createwiki-defaultmainpage' )->inLanguage( $lang )->plain() ),
'Create main page',
wfMessage( 'createwiki-defaultmainpage' )->inLanguage( $language )->plain() ), // Text
'Create main page', // Edit summary
EDIT_NEW
);

return true;
}
}
1 change: 0 additions & 1 deletion i18n/cy.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"createwiki-label-dbname": "Enw'r Cronfa Ddata:",
"createwiki-label-founder": "Sylfaenydd:",
"createwiki-label-language": "Iaith:",
"createwiki-label-private": "Preifat:",
"createwiki-label-reason": "Rheswm:",
"createwiki-label-sitename": "Enw'r safle:",
"createwiki-label-submit": "Cyflwyno",
Expand Down
4 changes: 3 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"Southparkfan"
]
},
"action-createwiki": "create wikis",
"createwiki": "Create a wiki",
"createwiki-defaultmainpage": "__NOTOC__\n== Welcome at {{SITENAME}}! ==\nThis Main Page was automatically created by a wiki creator (a volunteer who created this wiki per a request), and it seems it isn't replaced yet.\n\n=== For the founder(s) of this wiki ===\nHello, and welcome at your new wiki! Thank you for choosing Miraheze for the hosting of your wiki, and we hope you will enjoy our hosting.\n\nYou can immediately start working on your wiki, whenever you want.\n\nNeed help? No problem! We will help you with your wiki as needed. To make a start we have added a few links about working with MediaWiki:\n* <span class=\"plainlinks\">[https://www.mediawiki.org/wiki/Help:Contents MediaWiki guide (e.g. navigation, editing, deleting pages, blocking users)]</span>\n* <span class=\"plainlinks\">[https://meta.miraheze.org/wiki/FAQ Miraheze FAQ]</span>\n\n==== But Miraheze, I still don't understand X! ====\nWell, that's no problem. Even if something isn't explained in the documentation/FAQ, we still are happy to help you. You can find us here:\n* <span class=\"plainlinks\">[https://meta.miraheze.org/wiki/Help_center On our own Miraheze wiki]</span>\n* On IRC in #miraheze on irc.freenode.net ([irc://irc.freenode.net/#miraheze direct link]; [http://webchat.freenode.net?channels=%23miraheze webchat])\n\n=== For a visitor of this wiki ===\nHello, the default Main Page of this wiki (this is the default Main Page) has not been replaced yet by the founder(s) of this wiki. The founder(s) might still be working on a Main Page, so please check this page again later!",
"createwiki-desc": "An extension useful for creating wikis in a wiki farm.",
Expand All @@ -17,10 +18,11 @@
"createwiki-label-dbname": "Database name:",
"createwiki-label-founder": "Founder:",
"createwiki-label-language": "Language:",
"createwiki-label-private": "Private:",
"createwiki-label-private": "Mark this wiki as private",
"createwiki-label-reason": "Reason:",
"createwiki-label-sitename": "Sitename:",
"createwiki-label-submit": "Submit",
"createwiki-legend": "Create a wiki",
"createwiki-success": "The wiki was successfully created.",
"log-description-farmer": "This log tracks wiki requests, wiki creations and everything related to it.",
"logentry-farmer-createwiki": "$1 created the wiki \"$4\"",
Expand Down
1 change: 0 additions & 1 deletion i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"createwiki-label-dbname": "Nom de database:",
"createwiki-label-founder": "Fondateur:",
"createwiki-label-language": "Langue:",
"createwiki-label-private": "Privée:",
"createwiki-label-reason": "Raison:",
"createwiki-label-sitename": "Nom du site:",
"createwiki-label-submit": "Soumettre",
Expand Down
1 change: 0 additions & 1 deletion i18n/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"createwiki-label-dbname": "Databasenaam:",
"createwiki-label-founder": "Oprichter:",
"createwiki-label-language": "Taal:",
"createwiki-label-private": "Privé:",
"createwiki-label-reason": "Reden:",
"createwiki-label-sitename": "Sitename:",
"createwiki-label-submit": "Verzenden",
Expand Down
1 change: 0 additions & 1 deletion i18n/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"createwiki-label-dbname": "Database name:",
"createwiki-label-founder": "Founder:",
"createwiki-label-language": "Language:",
"createwiki-label-private": "Private:",
"createwiki-label-reason": "Reason:",
"createwiki-label-sitename": "Sitename:",
"createwiki-label-submit": "Submit",
Expand Down

0 comments on commit 252781a

Please sign in to comment.