【问题标题】:How to show custom error messages to the user?如何向用户显示自定义错误消息?
【发布时间】:2023-03-06 16:01:01
【问题描述】:

我有一个简单的扩展程序,它通过确保所有具有特定类别的页面都包含特定模板来验证文章页面。我的检查发生在 onPageContentSave 函数中。

我按照文档使用了 $status->fatal(),并返回 false 以中止保存。但是,没有向用户显示任何内容,因此他们不知道出了什么问题。

我尝试过的:

public static function onPageContentSave($wikiPage, $user, $content, $summary, $isMinor, $isWatch, $section, $flags, $status) { 

    $content = $content->getNativeData();
    $response = self::httpPost("http://api.oneorzero.org:9999/theo", ['data' => json_encode(['wikiPage' => $wikiPage, 'user' => $user, 'content' => $content, 'summary' => $summary, 'isWatch' => $isWatch, 'section' => $section, 'flags' => $flags, 'status' => $status])]);
    $response = json_decode($response, true);

    $continue = false;
    if(json_encode($response) == '{"message":"success"}') {

        $continue = true;
    } else {

        $status->fatal( new RawMessage(json_encode($response)));
    }

    // $response should be true on success, and false on fail
    // false cancels the article save
    return $continue;
}

此代码的工作原理是 RawMessage 永远不会向用户显示。

感谢任何帮助

【问题讨论】:

  • 这是正确的方法(除了 PageContentSave 自 MediaWiki 1.35 以来已被弃用,使用 MultiContentSave 作为替代;但它仍然有效),因此您的代码中可能存在错误。跨度>

标签: mediawiki mediawiki-extensions


【解决方案1】:

您似乎想要AbuseFilter。定义一个显示错误消息的规则(您可以选择哪个),如果用户尝试保存带有类别且不带模板的页面而不是保存页面,则如下所示:

action == 'edit'
& new_wikitext irlike '\[\[category:your_category(\|[^\]]*)?\]\]'
& !new_wikitext irlike '\{\{your_template(\|[^}]*)?\}\}'
  1. 第一个条件仅适用于页面编辑/创建。
  2. 它将不区分大小写地根据正则表达式检查页面的新 wikitext,该正则表达式应该找到对类别 Your_category 的分配。
  3. 最后,它会检查新的wikitext是否包含对模板{{Your_template}}的调用。

【讨论】:

  • 感谢您花时间回答。您的解决方案似乎是一个很好的解决方案。我想我自己找到了更好的解决方案。我将它添加到这个问题中。很抱歉在这个问题上浪费了您的时间。再次感谢您
【解决方案2】:

我已经找到了解决问题的方法,所以我将把它留给其他人:

解决方案是使用另一个名为onEditFilter 的钩子。这个钩子有一个向用户显示自定义错误消息的内置方法,功能类似于onPageContentSave

这里是钩子文档的链接:https://www.mediawiki.org/wiki/Manual:Hooks/EditFilter

这是一个例子:

public static function onEditFilter($editor, $text, $section, &$error, $summary) { 

    $response = self::httpPost("http:/api.oneorzero.org:9999/theo", ['data' => json_encode(['wikiPage' => $text])]);
    $response = json_decode($response, true);

    if ($response == '') {

        $error = '<li class="edit-error">' . "ERROR: The edit validation service cannot be reached. Please contact User:Jobgh to get this issue resolved." . "</li>";
        return true;
    }

    if (json_encode($response) != '{"message":"success"}') {

        $error = '<li class="edit-error">' . $response['message'] . "</li>";
    }

    // Return value should be true on success and fail. Set $error to cancel the save and show an error to the user
    return true;
}

【讨论】:

  • 如果只编辑一个部分,并且类别和所需模板位于不同部分,您的解决方案是否有效?
  • EditFilter 特定于正常编辑(与以编程方式生成的编辑相反)。它只会提供编辑表单的内容,不一定是整篇文章,并且会以在表单中输入的确切形式(没有预保存转换或编辑冲突解决)。这些可能是好事也可能是坏事,具体取决于您的用例。
  • 此外,EditFilter 的运行时间比 PageSaveComplete 早得多,因此在性能方面,如果您可以在那里发现问题(在用户看到错误消息之前的延迟更少),性能会好得多。另一方面,在调用 EditFilter 时页面渲染尚未发生,因此页面上使用的类别和模板列表尚未计算,您只能尝试重现 wikitext 解析。
  • @AlexanderMashin 感谢您的提醒!今天我会做一些测试并回复你们
  • 你们是对的。如果您编辑单个部分,则过滤器无法正常工作。我禁用了部分编辑。
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 2021-12-01
  • 1970-01-01
相关资源
最近更新 更多