From dcad83b11c62751f36fb002d71dcfcf86775b1c7 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Fri, 16 Jun 2023 05:08:01 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=A1=B9=E7=9B=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=B4=BB=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E8=80=85=E7=94=A8=E6=88=B7=E7=BB=84=E5=92=8C=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extension.json | 20 +++- i18n/en.json | 5 +- i18n/ja.json | 7 +- i18n/zh-hans.json | 7 +- i18n/zh-hant.json | 5 +- .../BlockQuoteTagHandler.php | 0 HideSpace.class.php => includes/HideSpace.php | 0 InfoBox.class.php => includes/InfoBox.php | 0 Isekai.class.php => includes/Isekai.php | 26 +++++ .../IsekaiChart.php | 0 includes/Job/CheckActivatedEditorJob.php | 32 ++++++ includes/MiscUtils.php | 66 +++++++++++++ maintenance/UpdateActivatedEditor.php | 97 +++++++++++++++++++ 13 files changed, 254 insertions(+), 11 deletions(-) rename BlockQuoteTagHandler.class.php => includes/BlockQuoteTagHandler.php (100%) rename HideSpace.class.php => includes/HideSpace.php (100%) rename InfoBox.class.php => includes/InfoBox.php (100%) rename Isekai.class.php => includes/Isekai.php (90%) rename IsekaiChart.class.php => includes/IsekaiChart.php (100%) create mode 100644 includes/Job/CheckActivatedEditorJob.php create mode 100644 includes/MiscUtils.php create mode 100644 maintenance/UpdateActivatedEditor.php diff --git a/extension.json b/extension.json index 69b9821..16e640b 100644 --- a/extension.json +++ b/extension.json @@ -1,5 +1,5 @@ { - "name": "Isekai wiki misc", + "name": "IsekaiMisc", "namemsg": "isekaiwiki-misc-name", "author": "Hyperzlib", "version": "1.0.1", @@ -15,10 +15,8 @@ "i18n" ] }, - "AutoloadClasses": { - "Isekai\\InfoBox": "InfoBox.class.php", - "Isekai\\IsekaiChart": "IsekaiChart.class.php", - "Isekai\\Isekai": "Isekai.class.php" + "AutoloadNamespaces": { + "Isekai\\": "includes/" }, "ExtensionMessagesFiles": { "IsekaiMagic": "Isekai.i18n.php" @@ -51,8 +49,14 @@ ], "MugenAppGetAvatar": [ "Isekai\\Isekai::onMugenAppGetAvatar" + ], + "onPageSaveComplete": [ + "Isekai\\Isekai::onPageSaveComplete" ] }, + "JobClasses": { + "CheckActivatedEditor": "Isekai\\Job\\CheckActivatedEditorJob" + }, "ResourceModules": { "ext.isekai": { "scripts": ["ext.isekai.js"], @@ -139,9 +143,15 @@ "isekai-show-ads-bottom": true, "isekai-show-ads-sidebar": true }, + "GroupPermissions": { + "activated-editor": {} + }, "config": { "IsekaiShowAds": { "value": false + }, + "IsekaiActivateEditorRequiredWords": { + "value": 300 } }, "manifest_version": 2 diff --git a/i18n/en.json b/i18n/en.json index 29d6500..f05ce81 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -35,5 +35,8 @@ "visualeditor-savedialog-label-set-categories": "Select categories", "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" } \ No newline at end of file diff --git a/i18n/ja.json b/i18n/ja.json index 23aab9b..2ef0875 100644 --- a/i18n/ja.json +++ b/i18n/ja.json @@ -10,6 +10,9 @@ "isekai-login-prompt-btn-createaccount": "登録", "isekai-login-prompt-btn-cancel": "いいぇ", - "isekai-function-btn-fullscreen": "全画面", - "isekai-function-btn-exit-fullscreen": "全画面を終了" + "isekai-function-btn-fullscreen": "全画面表示", + "isekai-function-btn-exit-fullscreen": "全画面表示を終了", + + "group-activated-editor": "筆者", + "group-activated-editor-member": "筆者" } \ No newline at end of file diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index 37a58aa..68bb190 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -8,7 +8,7 @@ "isekai-login-prompt-content": "加入异世界百科,贡献你的脑洞,帮助更多的人吧!", "isekai-login-prompt-btn-login": "登录", "isekai-login-prompt-btn-createaccount": "创建账户", - "isekai-login-prompt-btn-cancel": "残忍拒绝", + "isekai-login-prompt-btn-cancel": "取消", "isekai-background-param-error": "背景图片参数错误:", @@ -35,5 +35,8 @@ "visualeditor-savedialog-label-set-categories": "设置分类", "isekai-function-btn-fullscreen": "全屏", - "isekai-function-btn-exit-fullscreen": "退出全屏" + "isekai-function-btn-exit-fullscreen": "退出全屏", + + "group-activated-editor": "作者", + "group-activated-editor-member": "作者" } \ No newline at end of file diff --git a/i18n/zh-hant.json b/i18n/zh-hant.json index 839124c..5b8c338 100644 --- a/i18n/zh-hant.json +++ b/i18n/zh-hant.json @@ -35,5 +35,8 @@ "visualeditor-savedialog-label-set-categories": "設置分類", "isekai-function-btn-fullscreen": "全屏", - "isekai-function-btn-exit-fullscreen": "退出全屏" + "isekai-function-btn-exit-fullscreen": "退出全屏", + + "group-activated-editor": "作者", + "group-activated-editor-member": "作者" } \ No newline at end of file diff --git a/BlockQuoteTagHandler.class.php b/includes/BlockQuoteTagHandler.php similarity index 100% rename from BlockQuoteTagHandler.class.php rename to includes/BlockQuoteTagHandler.php diff --git a/HideSpace.class.php b/includes/HideSpace.php similarity index 100% rename from HideSpace.class.php rename to includes/HideSpace.php diff --git a/InfoBox.class.php b/includes/InfoBox.php similarity index 100% rename from InfoBox.class.php rename to includes/InfoBox.php diff --git a/Isekai.class.php b/includes/Isekai.php similarity index 90% rename from Isekai.class.php rename to includes/Isekai.php index f7542bd..fe122df 100644 --- a/Isekai.class.php +++ b/includes/Isekai.php @@ -11,6 +11,7 @@ use MediaWiki\Linker\LinkRenderer; use MediaWiki\Linker\LinkTarget; use User; use EditPage; +use Isekai\Job\CheckActivatedEditorJob; class Isekai { public static function onLoad(OutputPage $output) { @@ -360,4 +361,29 @@ HTML $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); + } + } + } } diff --git a/IsekaiChart.class.php b/includes/IsekaiChart.php similarity index 100% rename from IsekaiChart.class.php rename to includes/IsekaiChart.php diff --git a/includes/Job/CheckActivatedEditorJob.php b/includes/Job/CheckActivatedEditorJob.php new file mode 100644 index 0000000..f263e4b --- /dev/null +++ b/includes/Job/CheckActivatedEditorJob.php @@ -0,0 +1,32 @@ +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; + } +} \ No newline at end of file diff --git a/includes/MiscUtils.php b/includes/MiscUtils.php new file mode 100644 index 0000000..f78e684 --- /dev/null +++ b/includes/MiscUtils.php @@ -0,0 +1,66 @@ +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; + } +} \ No newline at end of file diff --git a/maintenance/UpdateActivatedEditor.php b/maintenance/UpdateActivatedEditor.php new file mode 100644 index 0000000..54455e8 --- /dev/null +++ b/maintenance/UpdateActivatedEditor.php @@ -0,0 +1,97 @@ +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;