Commit 0df75a15 authored by Marijn van Wezel's avatar Marijn van Wezel
Browse files

Merge branch 'add-space-parser-function' into 'master'

Add space parser function

See merge request extensions/wsspaces!2
parents 66705789 12d9ec49
Pipeline #1322 passed with stages
in 25 seconds
......@@ -3,4 +3,5 @@ $magicWords = [];
$magicWords['en'] = [
'spaceadmins' => [ 0, "spaceadmins" ],
'spaces' => [ 0, "spaces" ],
];
......@@ -2,7 +2,7 @@
"name": "wikibase-solutions/w-s-spaces",
"type": "mediawiki-extension",
"description": "Dynamic namespace management system for MediaWiki.",
"keywords": "",
"keywords": [],
"license": "GPL-2.0-or-later",
"require": {
"ext-openssl": "*"
......
{
"name": "WSSpaces",
"version": "2.0.1",
"version": "2.1.0",
"namemsg": "wss-extensionname",
"url": "https://wikibase-solutions.com",
"type": "csp",
......
......@@ -32,10 +32,11 @@
"wss-add-space-form-namespace-label": "Namespace Key",
"wss-add-space-form-namespacename-label": "Namespace Name",
"wss-add-space-form-namespace-help": "Namespace key must be unique. It will be used as a prefix for the pages in this space.",
"wss-add-space-form-namespacename-help": "Namespace name must be unique.",
"wss-add-space-form-namespacename-help": "Namespace name must be unique. It is used for display purposes only.",
"wss-manage-space-form-submit-text": "Save changes",
"wss-manage-space-form-namespaceid-label": "Namespace ID",
"wss-manage-space-form-namespace-label": "Namespace Key",
"wss-manage-space-form-namespace-help": "Changing this will cause existing links to pages in this space to break. Please be careful.",
"wss-manage-space-form-namespacename-label": "Namespace Name",
"wss-manage-space-form-createdby-label": "Created by",
"wss-manage-space-form-administrators-label": "Space administrators",
......
......@@ -3,9 +3,7 @@
namespace WSS\API;
use ApiUsageException;
use MediaWiki\MediaWikiServices;
use MWException;
use User;
use WSS\NamespaceRepository;
use WSS\Space;
use WSS\Validation\AddSpaceValidationCallback;
......@@ -79,25 +77,25 @@ class ApiAddSpace extends ApiBase {
$ns_key = isset( $request_params["nskey"] ) ? $request_params["nskey"] : false;
if ( $ns_key === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nskey" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nskey" ) );
}
$ns_name = isset( $request_params["nsname"] ) ? $request_params["nsname"] : false;
if ( $ns_name === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsname" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsname" ) );
}
$ns_description = isset( $request_params["nsdescription"] ) ? $request_params["nsdescription"] : false;
if ( $ns_description === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsdescription" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsdescription" ) );
}
$ns_admins = isset( $request_params["nsadmins"] ) ? $request_params["nsadmins"] : false;
if ( $ns_admins === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsadmins" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsadmins" ) );
}
// Although this validation is made for HTMLForm, we use it here to avoid repeating ourselves
......@@ -111,18 +109,18 @@ class ApiAddSpace extends ApiBase {
// Validate "nskey"
$error_or_true = $add_space_validation_callback->validateField( "namespace", $ns_key, $request_data );
if ( $error_or_true !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-detailed-nskey", $error_or_true ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-detailed-nskey", $error_or_true ) );
}
// Validate "nsname"
$error_or_true = $add_space_validation_callback->validateField( "namespace_name", $ns_name, $request_data );
if ( $error_or_true !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-detailed-nsname", $error_or_true ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-detailed-nsname", $error_or_true ) );
}
// Validate "nsdescription"
if ( $add_space_validation_callback->validateRequired( $ns_description ) !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-nsdescription" ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-nsdescription" ) );
}
}
......
......@@ -35,16 +35,16 @@ class ApiArchiveSpace extends ApiBase {
$request_params = $this->extractRequestParams();
if ( !isset( $request_params["nsid"] ) ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsid" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsid" ) );
}
$space = Space::newFromConstant( $request_params["nsid"] );
if ( !$space instanceof Space ) {
$this->dieWithError(
wfMessage(
$this->msg(
"wss-api-invalid-param-detailed-nsid",
wfMessage( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
$this->msg( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
)->parse()
);
}
......
......@@ -3,9 +3,7 @@
namespace WSS\API;
use ApiUsageException;
use MediaWiki\MediaWikiServices;
use MWException;
use User;
use WSS\NamespaceRepository;
use WSS\Space;
use WSS\Validation\AddSpaceValidationCallback;
......@@ -74,40 +72,40 @@ class ApiEditSpace extends ApiBase {
$ns_id = isset( $request_params["nsid"] ) ? $request_params["nsid"] : false;
if ( $ns_id === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsid" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsid" ) );
}
$ns_key = isset( $request_params["nskey"] ) ? $request_params["nskey"] : false;
if ( $ns_key === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nskey" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nskey" ) );
}
$ns_name = isset( $request_params["nsname"] ) ? $request_params["nsname"] : false;
if ( $ns_name === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsname" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsname" ) );
}
$ns_description = isset( $request_params["nsdescription"] ) ? $request_params["nsdescription"] : false;
if ( $ns_description === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsdescription" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsdescription" ) );
}
$ns_admins = isset( $request_params["nsadmins"] ) ? $request_params["nsadmins"] : false;
if ( $ns_admins === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsadmins" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsadmins" ) );
}
$space = Space::newFromConstant( $ns_id );
if ( !$space instanceof Space ) {
$this->dieWithError(
wfMessage(
$this->msg(
"wss-api-invalid-param-detailed-nsid",
wfMessage( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
$this->msg( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
)
);
}
......@@ -127,17 +125,17 @@ class ApiEditSpace extends ApiBase {
// Validate "nskey"
if ( $add_space_validation_callback->validateField( "namespace", $ns_key, $request_data ) !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-nskey" ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-nskey" ) );
}
// Validate "nsname"
if ( $add_space_validation_callback->validateField( "namespace_name", $ns_name, $request_data ) !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-nsname" ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-nsname" ) );
}
// Validate "nsdescription"
if ( $add_space_validation_callback->validateRequired( $ns_description ) !== true ) {
$this->dieWithError( wfMessage( "wss-api-invalid-param-nsdescription" ) );
$this->dieWithError( $this->msg( "wss-api-invalid-param-nsdescription" ) );
}
}
......
<?php
namespace WSS\API;
use MediaWiki\MediaWikiServices;
use User;
use WSS\NamespaceRepository;
......@@ -12,24 +11,16 @@ class ApiQuerySpaceAdmins extends \ApiQueryBase {
* @throws \ApiUsageException
*/
public function execute() {
$this->checkUserRightsAny( 'wss-view-space-admins' );
$this->checkUserRightsAny( 'wss-view-space-admins' );
$request_params = $this->extractRequestParams();
$request_params = $this->extractRequestParams();
$space_id = isset( $request_params["namespace"] ) ? $request_params["namespace"] : false;
if ( $space_id === false ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-namespace" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-namespace" ) );
}
// $user = \RequestContext::getMain()->getUser();
// $group_manager = MediaWikiServices::getInstance()->getUserGroupManager();
// $user_groups = $group_manager->getUserGroups( $user );
// if ( !in_array( $space_id . "Admin", $user_groups ) && !in_array( "sysop", $user_groups ) ) {
// $this->dieWithError( wfMessage( "wss-permission-denied-spaceadmins" ) );
// }
$namespace_repository = new NamespaceRepository();
// Get all admin ids for a namespace
......
......@@ -35,16 +35,16 @@ class ApiUnarchiveSpace extends ApiBase {
$request_params = $this->extractRequestParams();
if ( !isset( $request_params["nsid"] ) ) {
$this->dieWithError( wfMessage( "wss-api-missing-param-nsid" ) );
$this->dieWithError( $this->msg( "wss-api-missing-param-nsid" ) );
}
$space = Space::newFromConstant( $request_params["nsid"] );
if ( !$space instanceof Space ) {
$this->dieWithError(
wfMessage(
$this->msg(
"wss-api-invalid-param-detailed-nsid",
wfMessage( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
$this->msg( "wss-api-space-does-not-exist", $request_params["nsid"] )->parse()
)->parse()
);
}
......
......@@ -90,6 +90,9 @@ class ArchivedSpacePager extends Pager {
return 'namespace_id';
}
/**
* @inheritDoc
*/
public function getDefaultDirections() {
return parent::DIR_DESCENDING;
}
......
......@@ -36,7 +36,11 @@ abstract class AbstractForm {
* @param SubmitCallback $submit_callback
* @param AbstractValidationCallback|null $validation_callback
*/
public function __construct( OutputPage $page, SubmitCallback $submit_callback, AbstractValidationCallback $validation_callback = null ) {
public function __construct(
OutputPage $page,
SubmitCallback $submit_callback,
AbstractValidationCallback $validation_callback = null
) {
$this->page = $page;
$this->setSubmitCallback( $submit_callback );
......
......@@ -13,6 +13,14 @@ class EditSpaceForm extends AbstractForm {
*/
private $space;
/**
* EditSpaceForm constructor.
*
* @param Space $space
* @param OutputPage $page
* @param SubmitCallback $submit_callback
* @param AbstractValidationCallback|null $validation_callback
*/
public function __construct(
Space $space,
OutputPage $page,
......@@ -44,6 +52,7 @@ class EditSpaceForm extends AbstractForm {
],
'namespace' => [
'label-message' => 'wss-manage-space-form-namespace-label',
'help-message' => 'wss-manage-space-form-namespace-help',
'type' => 'text',
'size' => 32,
'maxlength' => 24,
......
......@@ -13,6 +13,14 @@ class UnarchiveSpaceForm extends AbstractForm {
*/
private $space;
/**
* UnarchiveSpaceForm constructor.
*
* @param Space $space
* @param OutputPage $page
* @param SubmitCallback $submit_callback
* @param AbstractValidationCallback|null $validation_callback
*/
public function __construct(
Space $space,
OutputPage $page,
......
......@@ -9,6 +9,7 @@ use Title;
use User;
abstract class Log {
// phpcs:ignore
const LOG_TYPE = 'space';
/**
......@@ -57,7 +58,6 @@ abstract class Log {
* Logger constructor.
*
* @param Title $title
* @param string $type
* @param string $subtype
*/
public function __construct( Title $title, string $subtype ) {
......
......@@ -30,7 +30,9 @@ class UpdateSpaceLog extends Log {
$old_admins_comment = $old_admins === [] ? "(none)" : implode( ", ", $old_admins );
$new_admins_comment = $new_admins === [] ? "(none)" : implode( ", ", $new_admins );
$this->setComment( wfMessage( 'wss-update-log-changed-admins', $old_admins_comment, $new_admins_comment )->plain() );
$this->setComment(
wfMessage( 'wss-update-log-changed-admins', $old_admins_comment, $new_admins_comment )->plain()
);
}
$this->space = $new_space;
......
......@@ -18,6 +18,7 @@ use WSS\Log\UpdateSpaceLog;
class NamespaceRepository {
// Lowest allowed ID for a space.
// phpcs:ignore
const MIN_SPACE_ID = 50000;
/**
......@@ -42,6 +43,8 @@ class NamespaceRepository {
/**
* Returns the next available namespace id.
*
* @return int
*/
public static function getNextAvailableNamespaceId(): int {
$dbr = wfGetDB( DB_MASTER );
......@@ -196,7 +199,9 @@ class NamespaceRepository {
*/
public function addSpace( Space $space ): int {
if ( $space->exists() ) {
throw new \InvalidArgumentException( "Cannot add existing space to database, use NamespaceRepository::updateSpace() instead." );
throw new \InvalidArgumentException(
"Cannot add existing space to database, use NamespaceRepository::updateSpace() instead."
);
}
// We publish the log first, since we ...?
......@@ -226,11 +231,11 @@ class NamespaceRepository {
[ $space ]
);
// Set the admins. Do this after running the WSSpacesAfterCreateSpace hook!
$space->setSpaceAdministrators( [ $space->getOwner()->getName() ] );
$this->updateSpaceAdministrators( $database, $space );
// Set the admins. Do this after running the WSSpacesAfterCreateSpace hook!
$space->setSpaceAdministrators( [ $space->getOwner()->getName() ] );
$this->updateSpaceAdministrators( $database, $space );
$log->publish();
$log->publish();
return $namespace_id;
}
......@@ -248,7 +253,9 @@ class NamespaceRepository {
*/
public function updateSpace( $old_space, Space $new_space, bool $force = false, bool $log = true ) {
if ( $old_space === false || !$old_space->exists() ) {
throw new \InvalidArgumentException( "Cannot update non-existing space in database, use NamespaceRepository::addSpace() instead." );
throw new \InvalidArgumentException(
"Cannot update non-existing space in database, use NamespaceRepository::addSpace() instead."
);
}
// Last minute check to see if the user actually does have enough permissions to edit this space.
......@@ -493,7 +500,7 @@ class NamespaceRepository {
/**
* @param array $administrators
* @param $namespace_id
* @param int $namespace_id
* @return array
*/
private function createRowsFromSpaceAdministrators( array $administrators, $namespace_id ) {
......
......@@ -2,8 +2,6 @@
namespace WSS;
use Wikimedia\Rdbms\ResultWrapper;
/**
* Class WSSPager
*
......
......@@ -7,6 +7,7 @@ use RequestContext;
use User;
class Space {
// phpcs:ignore
const DEFAULT_NAMESPACE_CONSTANT = 0;
/**
......@@ -70,7 +71,9 @@ class Space {
array $namespace_administrators = []
) {
if ( $namespace_id % 2 !== 0 ) {
throw new \InvalidArgumentException( "Namespace ID must be an even number; '$namespace_id' is not even'" );
throw new \InvalidArgumentException(
"Namespace ID must be an even number; '$namespace_id' is not even'"
);
}
if ( empty( $namespace_key ) ) {
......@@ -78,7 +81,10 @@ class Space {
}
if ( !ctype_alnum( $namespace_key ) ) {
throw new \InvalidArgumentException( "A namespace name can only consist of alphanumerical characters, therefore $namespace_key is invalid." );
throw new \InvalidArgumentException(
"A namespace name can only consist of alphanumerical characters, " .
"therefore $namespace_key is invalid."
);
}
$this->namespace_id = $namespace_id;
......
......@@ -98,6 +98,9 @@ class SpacePager extends Pager {
return 'created_on';
}
/**
* @inheritDoc
*/
public function getDefaultDirections() {
return parent::DIR_DESCENDING;
}
......
......@@ -53,7 +53,7 @@ class SpecialActiveSpaces extends SpecialPage {
* @inheritDoc
*/
public function getDescription() {
return wfMessage( 'wss-active-spaces-header' )->plain();
return $this->msg( 'wss-active-spaces-header' )->plain();
}
/**
......@@ -67,11 +67,11 @@ class SpecialActiveSpaces extends SpecialPage {
* The main method that gets invoked upon successfully loading the special page, after preExecuteChecks have
* been performed.
*
* @param $parameter
* @param string $parameter
* @return void
* @throws \MWException
*/
function doExecute( string $parameter ) {
public function doExecute( string $parameter ) {
$output = $this->getOutput();
$renderer = $this->getLinkRenderer();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment