更改项目结构,增加活动作者用户组和相关自动规则

master
落雨楓 2 years ago
parent e50608265b
commit dcad83b11c

@ -1,5 +1,5 @@
{ {
"name": "Isekai wiki misc", "name": "IsekaiMisc",
"namemsg": "isekaiwiki-misc-name", "namemsg": "isekaiwiki-misc-name",
"author": "Hyperzlib", "author": "Hyperzlib",
"version": "1.0.1", "version": "1.0.1",
@ -15,10 +15,8 @@
"i18n" "i18n"
] ]
}, },
"AutoloadClasses": { "AutoloadNamespaces": {
"Isekai\\InfoBox": "InfoBox.class.php", "Isekai\\": "includes/"
"Isekai\\IsekaiChart": "IsekaiChart.class.php",
"Isekai\\Isekai": "Isekai.class.php"
}, },
"ExtensionMessagesFiles": { "ExtensionMessagesFiles": {
"IsekaiMagic": "Isekai.i18n.php" "IsekaiMagic": "Isekai.i18n.php"
@ -51,8 +49,14 @@
], ],
"MugenAppGetAvatar": [ "MugenAppGetAvatar": [
"Isekai\\Isekai::onMugenAppGetAvatar" "Isekai\\Isekai::onMugenAppGetAvatar"
],
"onPageSaveComplete": [
"Isekai\\Isekai::onPageSaveComplete"
] ]
}, },
"JobClasses": {
"CheckActivatedEditor": "Isekai\\Job\\CheckActivatedEditorJob"
},
"ResourceModules": { "ResourceModules": {
"ext.isekai": { "ext.isekai": {
"scripts": ["ext.isekai.js"], "scripts": ["ext.isekai.js"],
@ -139,9 +143,15 @@
"isekai-show-ads-bottom": true, "isekai-show-ads-bottom": true,
"isekai-show-ads-sidebar": true "isekai-show-ads-sidebar": true
}, },
"GroupPermissions": {
"activated-editor": {}
},
"config": { "config": {
"IsekaiShowAds": { "IsekaiShowAds": {
"value": false "value": false
},
"IsekaiActivateEditorRequiredWords": {
"value": 300
} }
}, },
"manifest_version": 2 "manifest_version": 2

@ -35,5 +35,8 @@
"visualeditor-savedialog-label-set-categories": "Select categories", "visualeditor-savedialog-label-set-categories": "Select categories",
"isekai-function-btn-fullscreen": "Full screen", "isekai-function-btn-fullscreen": "Full screen",
"isekai-function-btn-exit-fullscreen": "Exit full screen" "isekai-function-btn-exit-fullscreen": "Exit full screen",
"group-activated-editor": "Editor",
"group-activated-editor-member": "Editor"
} }

@ -10,6 +10,9 @@
"isekai-login-prompt-btn-createaccount": "登録", "isekai-login-prompt-btn-createaccount": "登録",
"isekai-login-prompt-btn-cancel": "いいぇ", "isekai-login-prompt-btn-cancel": "いいぇ",
"isekai-function-btn-fullscreen": "全画面", "isekai-function-btn-fullscreen": "全画面表示",
"isekai-function-btn-exit-fullscreen": "全画面を終了" "isekai-function-btn-exit-fullscreen": "全画面表示を終了",
"group-activated-editor": "筆者",
"group-activated-editor-member": "筆者"
} }

@ -8,7 +8,7 @@
"isekai-login-prompt-content": "加入异世界百科,贡献你的脑洞,帮助更多的人吧!", "isekai-login-prompt-content": "加入异世界百科,贡献你的脑洞,帮助更多的人吧!",
"isekai-login-prompt-btn-login": "登录", "isekai-login-prompt-btn-login": "登录",
"isekai-login-prompt-btn-createaccount": "创建账户", "isekai-login-prompt-btn-createaccount": "创建账户",
"isekai-login-prompt-btn-cancel": "残忍拒绝", "isekai-login-prompt-btn-cancel": "取消",
"isekai-background-param-error": "背景图片参数错误:", "isekai-background-param-error": "背景图片参数错误:",
@ -35,5 +35,8 @@
"visualeditor-savedialog-label-set-categories": "设置分类", "visualeditor-savedialog-label-set-categories": "设置分类",
"isekai-function-btn-fullscreen": "全屏", "isekai-function-btn-fullscreen": "全屏",
"isekai-function-btn-exit-fullscreen": "退出全屏" "isekai-function-btn-exit-fullscreen": "退出全屏",
"group-activated-editor": "作者",
"group-activated-editor-member": "作者"
} }

@ -35,5 +35,8 @@
"visualeditor-savedialog-label-set-categories": "設置分類", "visualeditor-savedialog-label-set-categories": "設置分類",
"isekai-function-btn-fullscreen": "全屏", "isekai-function-btn-fullscreen": "全屏",
"isekai-function-btn-exit-fullscreen": "退出全屏" "isekai-function-btn-exit-fullscreen": "退出全屏",
"group-activated-editor": "作者",
"group-activated-editor-member": "作者"
} }

@ -11,6 +11,7 @@ use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget; use MediaWiki\Linker\LinkTarget;
use User; use User;
use EditPage; use EditPage;
use Isekai\Job\CheckActivatedEditorJob;
class Isekai { class Isekai {
public static function onLoad(OutputPage $output) { public static function onLoad(OutputPage $output) {
@ -360,4 +361,29 @@ HTML
$avatarUrl = "$avatarBase?size=xxl"; $avatarUrl = "$avatarBase?size=xxl";
} }
} }
/**
* @param \WikiPage $wikiPage
* @param \MediaWiki\User\UserIdentity $user
* @param string $summary
* @param int $flags
* @param \MediaWiki\Revision\RevisionRecord $revisionRecord
* @param \MediaWiki\Storage\EditResult $editResult
*/
public static function onPageSaveComplete($wikiPage, $user, $summary, $flags, $revisionRecord, $editResult) {
$services = MediaWikiServices::getInstance();
$userFactory = $services->getUserFactory();
$userGroupManager = $services->getUserGroupManager();
if ($wikiPage->getTitle()->getNamespace() === NS_MAIN && $user->isRegistered()) {
$userGroups = $userGroupManager->getUserGroups($user);
if (!in_array('activated-editor', $userGroups)) {
$realUser = $userFactory->newFromId($user->getId());
$job = new CheckActivatedEditorJob($realUser->getUserPage(), [
'user_id' => $user->getId()
]);
MediaWikiServices::getInstance()->getJobQueueGroup()->push($job);
}
}
}
} }

@ -0,0 +1,32 @@
<?php
namespace Isekai\Job;
use Exception;
use Isekai\MiscUtils;
use Job;
use MediaWiki\MediaWikiServices;
use Title;
class CheckActivatedEditorJob extends Job {
public function __construct(Title $title, array $params){
parent::__construct('CheckActivatedEditor', $title, $params);
}
/**
* 检测用户是否满足加入编辑者用户组的条件
*/
public function run() {
$services = MediaWikiServices::getInstance();
if (!$this->params['user_id']) {
return true;
}
$user = $services->getUserFactory()->newFromId($this->params['user_id']);
if ($user->isRegistered() && MiscUtils::checkUserIsActivatedEditor($user)) {
$services->getUserGroupManager()->addUserToGroup($user, 'activated-editor');
}
return true;
}
}

@ -0,0 +1,66 @@
<?php
namespace Isekai;
use ApiMain;
use FauxRequest;
use User;
use MediaWiki\MediaWikiServices;
class MiscUtils {
/**
* @param User $user
*/
public static function checkUserIsActivatedEditor($user) {
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
$userGroupManager = $services->getUserGroupManager();
$userGroups = $userGroupManager->getUserGroups($user);
if (in_array('activated-editor', $userGroups)) {
return true;
}
$requiredWords = $config->get('IsekaiActivateEditorRequiredWords');
$continueKey = null;
$userCreditSize = 0;
do {
$apiParams = [
'action' => 'query',
'list' => 'usercontribs',
'ucprop' => 'sizediff',
'uclimit' => 20,
'ucuserids' => $user->getId(),
];
if ($continueKey) {
$apiParams['uccontinue'] = $continueKey;
}
$api = new ApiMain( new FauxRequest( $apiParams ) );
$api->execute();
$result = $api->getResult();
$continueKey = $result->getResultData( [ 'continue', 'uccontinue' ] );
if ($continueKey === '||') {
$continueKey = null;
}
$userCredits = $result->getResultData( [ 'query', 'usercontribs' ] );
foreach ($userCredits as $userCreditData) {
if (is_array($userCreditData) && isset($userCreditData['sizediff'])) {
if ($userCreditData['sizediff'] > 0) {
$userCreditSize += $userCreditData['sizediff'];
}
}
}
if ($userCreditSize > $requiredWords) {
return true;
}
} while($continueKey);
return false;
}
}

@ -0,0 +1,97 @@
<?php
namespace Isekai\Maintenance;
use Maintenance;
use Config;
use ApiMain;
use FauxRequest;
use Isekai\MiscUtills;
use Isekai\MiscUtils;
use MediaWiki\MediaWikiServices;
use Title;
use Wikimedia\Rdbms\IDatabase;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class UpdateActivatedEditor extends Maintenance {
/**
* MediaWiki Sservices
*
* @var MediaWikiServices
*/
public $services;
/**
* A database replica DB object
*
* @var IDatabase
*/
public $dbr;
/**
* Config
*
* @var Config
*/
public $config;
public function __construct() {
parent::__construct();
$this->addDescription( 'Update activated-editor users by editor history.' );
$this->setBatchSize( 300 );
$this->requireExtension( 'IsekaiMisc' );
}
/**
* Return all users
* @return IResultWrapper
*/
private function getUserRes() {
return $this->dbr->select(
[ 'user' ],
[
'user_id',
'user_name',
],
[ 'user_editcount > 0' ],
__METHOD__
);
}
public function execute() {
$this->services = MediaWikiServices::getInstance();
$this->dbr = $this->services->getDBLoadBalancer()->getConnection(DB_REPLICA);
$this->config = $this->services->getMainConfig();
$userFactory = $this->services->getUserFactory();
$userGroupManager = $this->services->getUserGroupManager();
$requiredWords = $this->config->get('IsekaiActivateEditorRequiredWords');
$groupedUserIds = [];
$res = $this->getUserRes(NS_MAIN);
foreach ($res as $row) {
$continueKey = null;
$userCreditSize = 0;
$user = $userFactory->newFromId($row->user_id);
$this->output('Checking user "' . $user->getName() . '"...' . PHP_EOL);
if (MiscUtils::checkUserIsActivatedEditor($user)) {
$this->output(' Adding user "' . $user->getName() . '" to activated-editor group' . PHP_EOL);
$userGroupManager->addUserToGroup($user, 'activated-editor');
}
}
}
}
$maintClass = UpdateActivatedEditor::class;
require_once RUN_MAINTENANCE_IF_MAIN;
Loading…
Cancel
Save