jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/283769 )

Change subject: Initial code
......................................................................


Initial code

Skeleton of the extension. Allows to create an user page.

Change-Id: I6daf2abe2292710653c45c554fc42e085ffdbbfe
---
M composer.json
A extension.json
A i18n/en.json
A i18n/qqq.json
M phpcs.xml
A src/Actions.php
A src/Actions/Action.php
A src/Actions/CreateNewUserPage.php
A src/Actions/CreateWikiPage.php
A src/Config.php
A src/Hooks.php
A tests/phpunit/ConfigTest.php
12 files changed, 528 insertions(+), 7 deletions(-)

Approvals:
  Umherirrender: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/composer.json b/composer.json
index da7e13f..ee3fe5c 100644
--- a/composer.json
+++ b/composer.json
@@ -8,12 +8,10 @@
        "scripts": {
                "test": [
                        "parallel-lint . --exclude node_modules --exclude 
vendor",
-                       "phpcs -p -s src",
-                       "phpcs -p -s tests/phpunit"
+                       "phpcs -p -s"
                ],
                "fix": [
-                       "phpcbf src",
-                       "phpcbf tests/phpunit"
+                       "phpcbf"
                ]
        }
 }
diff --git a/extension.json b/extension.json
new file mode 100644
index 0000000..71cc3e5
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,45 @@
+{
+       "name": "NewUserActions",
+       "author": [
+               "Sébastien Santoro aka Dereckson"
+       ],
+       "url": "https://www.mediawiki.org/wiki/Extension:NewUserActions";,
+       "descriptionmsg": "newuseractions-desc",
+       "license-name": "GPL-2.0+",
+       "type": "other",
+       "MessagesDirs": {
+               "NewUserActions": [
+                       "i18n"
+               ]
+       },
+       "AutoloadClasses": {
+               "MediaWiki\\Extensions\\NewUserActions\\Actions": 
"src/Actions.php",
+               "MediaWiki\\Extensions\\NewUserActions\\Config": 
"src/Config.php",
+               "MediaWiki\\Extensions\\NewUserActions\\Hooks": "src/Hooks.php",
+               "MediaWiki\\Extensions\\NewUserActions\\Actions\\Action": 
"src/Actions/Action.php",
+               
"MediaWiki\\Extensions\\NewUserActions\\Actions\\CreateNewUserPage": 
"src/Actions/CreateNewUserPage.php",
+               
"MediaWiki\\Extensions\\NewUserActions\\Actions\\CreateWikiPage": 
"src/Actions/CreateWikiPage.php"
+       },
+       "Hooks": {
+               "AddNewAccount": [
+                       
"MediaWiki\\Extensions\\NewUserActions\\Hooks::onAddNewAccount"
+               ],
+               "LocalUserCreated": [
+                       
"MediaWiki\\Extensions\\NewUserActions\\Hooks::onLocalUserCreated"
+               ],
+               "UserGetReservedNames": [
+                       
"MediaWiki\\Extensions\\NewUserActions\\Hooks::onUserGetReservedNames"
+               ],
+               "UnitTestsList": [
+                       
"MediaWiki\\Extensions\\NewUserActions\\Hooks::onUnitTestsList"
+               ]
+       },
+       "load_composer_autoloader": true,
+       "config": {
+               "NewUserActionsSuppressRC": false,
+               "NewUserActionsMinorEdit": true,
+               "NewUserActionsOnAutoCreate": false,
+               "NewUserActionsCreateUserPage": false
+       },
+       "manifest_version": 1
+}
diff --git a/i18n/en.json b/i18n/en.json
new file mode 100644
index 0000000..7a3de26
--- /dev/null
+++ b/i18n/en.json
@@ -0,0 +1,11 @@
+{
+       "@metadata": {
+               "authors": [
+                       "Sébastien Santoro aka Dereckson"
+               ]
+       },
+       "newuseractions-desc": "Onboarding workflow for new users accounts",
+       "newuseractions-userpage-template": "Template:New user page",
+       "newuseractions-userpage-summary": "Initial content from [[Template:New 
user page]].",
+       "newuseractions-editor": "New user actions"
+}
diff --git a/i18n/qqq.json b/i18n/qqq.json
new file mode 100644
index 0000000..7d0aa5c
--- /dev/null
+++ b/i18n/qqq.json
@@ -0,0 +1,11 @@
+{
+       "@metadata": {
+               "authors": [
+                       "Sébastien Santoro aka Dereckson"
+               ]
+       },
+       "newuseractions-desc": 
"{{desc|name=NewUserActions|url=https://www.mediawiki.org/wiki/Extension:NewUserActions}}";,
+       "newuseractions-userpage-template": "In-wiki configuration setting. 
Specify a template to use as default new user page.",
+       "newuseractions-userpage-summary": "In-wiki configuration setting. Edit 
summary for the recent changes entry of a new users message. Always in content 
language.",
+       "newuseractions-editor": "In-wiki configuration setting. Set the 
username of the user that makes the edits. If this user does not exist, 
'MediaWiki default' will show up as editing user. The user set here is marked 
as reserved and won't be able to login."
+}
diff --git a/phpcs.xml b/phpcs.xml
index 3b00eba..91141cf 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -1,8 +1,12 @@
 <?xml version="1.0"?>
 <ruleset>
-       <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
+       <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
+               <exclude 
name="MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic" />
+               <exclude 
name="MediaWiki.Commenting.FunctionComment.MissingDocumentationProtected" />
+               <exclude 
name="MediaWiki.Commenting.FunctionComment.MissingParamComment" />
+               <exclude name="MediaWiki.Commenting.FunctionComment.WrongStyle" 
/>
+       </rule>
        <file>.</file>
        <arg name="extensions" value="php,php5,inc"/>
-       <arg name="encoding" value="utf8"/>
-       <exclude-pattern>vendor</exclude-pattern>
+       <arg name="encoding" value="UTF-8"/>
 </ruleset>
diff --git a/src/Actions.php b/src/Actions.php
new file mode 100644
index 0000000..33d6225
--- /dev/null
+++ b/src/Actions.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions;
+
+use MediaWiki\Extensions\NewUserActions\Actions\CreateNewUserPage;
+
+use User;
+
+class Actions {
+
+       /**
+        * The newly created user
+        *
+        * @var User
+        */
+       private $user;
+
+       /**
+        * The list of actions to run
+        *
+        * @var MediaWiki\Extensions\NewUserActions\Actions\Action[]
+        */
+       private $actions = [];
+
+       ///
+       /// Constructor
+       ///
+
+       /**
+        * Initializes a new instance of the Actions class.
+        *
+        * @param User $user The newly created user
+        */
+       public function __construct( User $user ) {
+               $this->user = $user;
+       }
+
+       ///
+       /// Public methods
+       ///
+
+       /**
+        * Configures the actions to run.
+        */
+       public function configureActions() {
+               global $wgNewUserActionsCreateUserPage;
+
+               if ( $wgNewUserActionsCreateUserPage ) {
+                       $this->actions[] = new CreateNewUserPage( $this->user );
+               }
+       }
+
+       /**
+        * Runs each action sequentially.
+        */
+       public function runActions() {
+               foreach ( $this->actions as $action ) {
+                       $action->run();
+               }
+       }
+
+       ///
+       /// Static helper methods
+       ///
+
+       /**
+        * Initializes a new instance of the Actions class, populates and runs 
it.
+        * @param User $user
+        */
+       static function run( User $user ) {
+               $instance = new static( $user );
+               $instance->configureActions();
+               $instance->runActions();
+       }
+
+}
diff --git a/src/Actions/Action.php b/src/Actions/Action.php
new file mode 100644
index 0000000..a1109eb
--- /dev/null
+++ b/src/Actions/Action.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions\Actions;
+
+use User;
+
+abstract class Action {
+       /**
+        * The newly created user
+        *
+        * @var User
+        */
+       protected $user;
+
+       public function __construct( User $user ) {
+               $this->user = $user;
+       }
+
+       abstract public function run();
+}
diff --git a/src/Actions/CreateNewUserPage.php 
b/src/Actions/CreateNewUserPage.php
new file mode 100644
index 0000000..6516da7
--- /dev/null
+++ b/src/Actions/CreateNewUserPage.php
@@ -0,0 +1,110 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions\Actions;
+
+use MediaWiki\Extensions\NewUserActions\Config;
+
+use ContentHandler;
+use Title;
+use User;
+use WikiPage;
+
+use RuntimeException;
+
+class CreateNewUserPage extends CreateWikiPage {
+
+       public function run() {
+               $this->setPageProperties();
+
+               if ( $this->content !== "" ) {
+                       // Don't create the page if content can't be found
+                       // (ie template doesn't exist).
+                       parent::run();
+               }
+       }
+
+       ///
+       /// Set page properties: editor, title, content and edit summary
+       ///
+
+       protected function setPageProperties() {
+               $this->setEditor();
+               $this->setTitle();
+               $this->setContent();
+               $this->setFlags();
+               $this->setEditSummary();
+       }
+
+       protected function setEditor() {
+               try {
+                       $this->editor = $this->fetchEditor();
+               } catch ( RuntimeException $ex ) {
+                       // TODO: consider some reporting to Sentry/Logstash
+                       // (but currently MediaWiki doesn't have such report 
feature)
+                       wfDebug( __METHOD__ . ": Can't fetch editor.\n" );
+                       return;
+               }
+       }
+
+       protected function setTitle() {
+               $this->title = $this->user->getUserPage();
+       }
+
+       protected function setContent() {
+               $template = Config::getLocalisedMessage( 
'newuseractions-userpage-template' );
+               $this->content = $this->fetchPageContent( $template );
+       }
+
+       protected function setFlags() {
+               $this->flags = $this->flags | Config::getEditFlags();
+       }
+
+       protected function setEditSummary() {
+               $this->editSummary = Config::getLocalisedMessage( 
'newuseractions-userpage-summary' );
+       }
+
+       ///
+       /// Helper methods
+       ///
+
+       /**
+        * Gets the editor to use, adding it to the database if necessary.
+        *
+        * @return User
+        */
+       static function fetchEditor() {
+               // Create a user object for the editing user and add it to the
+               // database if it is not there already
+               $editorName = Config::getLocalisedMessage( 
'newuseractions-editor' );
+               $editor = User::newFromName( $editorName );
+
+               if ( $editor === false ) {
+                       throw new RuntimeException( "The editor username is 
invalid." );
+               }
+
+               if ( !$editor->isLoggedIn() ) {
+                       $editor->addToDatabase();
+               }
+
+               return $editor;
+       }
+
+       /**
+        * Fetches page content.
+        *
+        * @param string $pageTitle The page title
+        * @param string The page content or "" if the page isn't found
+        */
+       private function fetchPageContent( $pageTitle ) {
+               $title = Title::newFromText( $pageTitle );
+               if ( $title === null ) {
+                       wfDebug( __METHOD__ . ": '$pageTitle' is not a valid 
title.\n" );
+                       return "";
+               }
+
+               $page = WikiPage::factory( $title );
+               $content = $page->getContent();
+               return ContentHandler::getContentText( $content );
+       }
+
+}
diff --git a/src/Actions/CreateWikiPage.php b/src/Actions/CreateWikiPage.php
new file mode 100644
index 0000000..2b30273
--- /dev/null
+++ b/src/Actions/CreateWikiPage.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions\Actions;
+
+use User;
+use WikiPage;
+
+abstract class CreateWikiPage extends Action {
+
+       ///
+       /// Protected properties
+       ///
+
+       /**
+        * The editor of the page to create
+        *
+        * @var User
+        */
+       protected $editor;
+
+       /**
+        * The title of the page
+        *
+        * @var Title
+        */
+       protected $title;
+
+       /**
+        * The content of the page
+        *
+        * @var string
+        */
+       protected $content;
+
+       /**
+        * The edit summary of the page
+        *
+        * @var string
+        */
+       protected $editSummary;
+
+       /**
+        * The flags
+        *
+        * @var int
+        */
+       protected $flags = EDIT_NEW;
+
+       ///
+       /// Run action
+       ///
+
+       public function run() {
+               if ( $this->shouldCreatePage() ) {
+                       $this->createPage();
+               }
+       }
+
+       /**
+        * Creates a page.
+        */
+       public function createPage() {
+               $page = WikiPage::factory( $this->title );
+               $flags = $page->checkFlags( $this->flags );
+
+               $status = $page->doEdit(
+                       $this->content,
+                       $this->editSummary,
+                       $flags,
+                       false,
+                       $this->editor
+               );
+
+               if ( !$status->isGood() ) {
+                       throw new RuntimeException( "Can't create page." );
+               }
+       }
+
+       ///
+       /// Helper methods
+       ///
+
+       /**
+        * Determines if the page should be created.
+        *
+        * @return bool
+        */
+       protected function shouldCreatePage() {
+               return !$this->title->exists() && $this->editorCanEdit();
+       }
+
+       /**
+        * Determines if the editor is able and allowed to create a page.
+        *
+        * @return bool
+        */
+       protected function editorCanEdit() {
+               return $this->editor && !$this->editor->isBlocked();
+       }
+
+}
diff --git a/src/Config.php b/src/Config.php
new file mode 100644
index 0000000..38787f0
--- /dev/null
+++ b/src/Config.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions;
+
+use User;
+
+class Config {
+
+       /**
+        * Gets the text of a MediaWiki message, localized in content language.
+        *
+        * @param string $name The message text
+        * @return string
+        */
+       public static function getLocalisedMessage( $name ) {
+               return \wfMessage( $name )->inContentLanguage()->text();
+       }
+
+       /**
+        * Gets the edit flags to use for revisions created by the extension.
+        *
+        * @return int
+        */
+       public static function getEditFlags() {
+               global $wgNewUserActionsMinorEdit, $wgNewUserActionsSuppressRC;
+
+               $flags = 0;
+
+               if ( $wgNewUserActionsMinorEdit ) {
+                       $flags = $flags | EDIT_MINOR;
+               }
+
+               if ( $wgNewUserActionsSuppressRC ) {
+                       $flags = $flags | EDIT_SUPPRESS_RC;
+               }
+
+               return $flags;
+       }
+
+}
diff --git a/src/Hooks.php b/src/Hooks.php
new file mode 100644
index 0000000..5cee7dd
--- /dev/null
+++ b/src/Hooks.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace MediaWiki\Extensions\NewUserActions;
+
+use User;
+
+class Hooks {
+
+       /**
+        * Hook function to run specific actions when a new account is created.
+        *
+        * @param User $user User object of the user
+        * @param bool $byEmail if the account has been created by mail, true
+        * @return bool
+        */
+       public static function onAddNewAccount( User $user, $byEmail ) {
+               if ( class_exists( 'AuthManager' ) ) {
+                       // This feature can be triggered by LocalUserCreated if 
needed.
+                       return true;
+               }
+
+               Actions::run( $user );
+               return true;
+       }
+
+       /**
+        * Hook function to run specific actions when an account is created
+        * through AuthManager.
+        *
+        * @param User $user User object of the user
+        * @param bool $autocreated
+        * @return bool
+        */
+       public static function onLocalUserCreated( User $user, $autocreated ) {
+               global $wgNewUserActionsOnAutoCreate;
+
+               if ( $wgNewUserActionsOnAutoCreate || !$autocreated ) {
+                       Actions::run( $user );
+               }
+
+               return true;
+       }
+
+       /**
+        * Hook function to modify $wgReservedUsernames at run time.
+        *
+        * @param array &$reservedUsernames The list of reserved (can't be 
registered) usernames
+        * @return bool
+        */
+       public static function onUserGetReservedNames( &$reservedUsernames ) {
+               $reservedUsernames[] = 'msg:newuseractions-editor';
+               return true;
+       }
+
+       /**
+        * Hook functions to extend core's PHPUnit test suite.
+        *
+        * @param array &$paths The list of the test files or directories for 
PHPUnit
+        * @return bool
+        */
+       public static function onUnitTestsList( &$paths ) {
+               $paths[] = __DIR__ . '/tests/phpunit';
+               return true;
+       }
+
+}
diff --git a/tests/phpunit/ConfigTest.php b/tests/phpunit/ConfigTest.php
new file mode 100644
index 0000000..89de600
--- /dev/null
+++ b/tests/phpunit/ConfigTest.php
@@ -0,0 +1,39 @@
+<?php
+
+use MediaWiki\Extensions\NewUserActions\Config;
+
+class ConfigTest extends MediaWikiTestCase {
+       public function testGetLocalisedMessage() {
+               $this->assertEquals( "⧼⧽", Config::getLocalisedMessage( "" ) );
+               $this->assertEquals(
+                       "⧼notexistingmessage⧽",
+                       Config::getLocalisedMessage( "notexistingmessage" )
+               );
+               $this->assertEquals(
+                       "Template:New user page",
+                       Config::getLocalisedMessage( 
"newuseractions-userpage-template" )
+               );
+       }
+
+       public function testGetEditFlags() {
+               global $wgNewUserActionsMinorEdit, $wgNewUserActionsSuppressRC;
+
+               // Default configuration
+               $this->assertEquals( 4, Config::getEditFlags() );
+
+               // Custom configurations
+               $wgNewUserActionsMinorEdit = $wgNewUserActionsSuppressRC = true;
+               $this->assertEquals( 12, Config::getEditFlags() );
+
+               $wgNewUserActionsMinorEdit = false;
+               $wgNewUserActionsSuppressRC = true;
+               $this->assertEquals( 8, Config::getEditFlags() );
+
+               $wgNewUserActionsMinorEdit = $wgNewUserActionsSuppressRC = 
false;
+               $this->assertEquals( 0, Config::getEditFlags() );
+
+               // Restore default configuration
+               $wgNewUserActionsMinorEdit = true;
+               $wgNewUserActionsSuppressRC = false;
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/283769
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6daf2abe2292710653c45c554fc42e085ffdbbfe
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/NewUserActions
Gerrit-Branch: master
Gerrit-Owner: Dereckson <dereck...@espace-win.org>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Umherirrender <umherirrender_de...@web.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to