代码之家  ›  专栏  ›  技术社区  ›  Stroes

自动向MediaWiki短页面添加文本/内容

  •  -1
  • Stroes  · 技术社区  · 9 年前

    我们在内部为所有文档运行mediawiki。我们遇到的问题是,一些用户创建了空白页面,因为他们不知道mediawiki是如何工作的,也没有删除权限。

    我想做的是自动添加此项:

    :“此页是不必要的,因为它自创建以来一直为空”“” “”除非添加更多内容,否则应将此页面标记为删除。

    [[类别:删除]]

    所有小于84字节的页面(短页面)或空白页面。

    无论如何,这是可能的吗。?

    1 回复  |  直到 4 年前
        1
  •  2
  •   Florian    9 年前

    这可以通过使用 PageContentSave hook 。下面是一个在扩展中执行此操作的简短示例:

    <?php
    public static function onPageContentSave( WikiPage &$wikiPage, User &$user, Content &$content, &$summary,
        $isMinor, $isWatch, $section, &$flags,  Status&$status
    ) {
        // check, if the content model is wikitext to avoid adding wikitext to pages, that doesn't handle wikitext (e.g.
        // some JavaScript/JSON/CSS pages)
        if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
            // check the size of the _new_ content
            if ( $content->getSize() <= 84 ) {
                // getNativeData returns the plain wikitext, which this Content object represent..
                $data = $content->getNativeData();
                // ..so it's possible to simply add/remove/replace wikitext like you want
                $data .= ":''This page is unnecessary since it has remained blank since creation'' '''Unless more content is added, this page should be marked for deletion.";
                $data .= "[[Category:ForDeletion]]";
                // the new wikitext ahs to be saved as a new Content object (Content doesn't support to replace/add content by itself)
                $content = new WikitextContent( $data );
            }
        } else {
            // this could be omitted, but would log a debug message to the debug log, if the page couldn't be checked for a smaller edit as 84 bytes.
            // Maybe you want to add some more info (Title and/or content model of the Content object
            wfDebug( 'Could not check for empty or small remaining size after edit. False content model' );
        }
        return true;
    }
    

    您需要在扩展设置文件中注册此钩子处理程序:

    $wgHooks['PageContentSave'][] = 'ExtensionHooksClass::onPageContentSave';
    

    我希望这会有所帮助,但请考虑这个问题,有些(可能)页面没有超过84字节就可以了,而且上面的实现现在不允许任何异常;)