MediaWiki-挂钩编辑/更改页面标题创建

发布于 2025-02-13 11:02:41 字数 3877 浏览 1 评论 0原文

我正在寻找(通过编程)编辑/更改页面创建和保存之前的标题。我尝试了几个钩子无济于事。我可以访问保存的标题以及其他可用于更改标题的信息,但找不到正确的呼叫来保存更改的标题。

它是针对私人/本地LAN Wiki(当前MediaWiki版本1.38.1)。

当使用Certon类别中创建新文章时,我想根据####的前缀进行编号 - 基于该类别中已经存在的文章数量。在类别页面本身的顶部,我有一个< inputbox>,该模板拉一个模板,该模板具有Wiki语法[[类别:Blabla]]> 。保存文章后,我正在进行检查,以确保其新文章和一些我需要的信息检查,该信息正常工作,但我无法保存新的更改的页面名称。

我尝试了以下钩子,但无济于事。

onMultiContentSave
onArticlePrepareTextForEdit


HIRES ACOUPLE片段IVE除了保存更改的页面名称外,还可以按照我的意愿进行测试。

    public static function onArticlePrepareTextForEdit( WikiPage $wikiPage, ParserOptions $parserOptions ) {

        return;

        $exists = $wikiPage->exists();
        if ($exists == 1) {
            #return true;
        }
        
        $getTitle = $wikiPage->getTitle();
        # check if title starts with 0000, exit if so, no work needs to be done
        if (self::titleCheck($getTitle)) {
            #return true;
        }

        $checkCategories = $wikiPage->getCategories();
        $inMalak = false;
        foreach ($checkCategories as $value) {
            if ($value == "Category:Malak") {
                $inMalak = true;
            }
        }

        if ($inMalak == 1) {
            $newTitle = self::newTitlePre() . $getTitle;
            #$wikiPage->setTitle($newTitle);
            print(">" . $newTitle . "<br>");
        }

        self::pr($newTitle);
    }

    public static function onMultiContentSave(RenderedRevision $renderedRevision, UserIdentity $user, CommentStoreComment $summary, $flags, Status $hookStatus){
        

        #return;

        $revision = $renderedRevision->getRevision();

        $getTitle = $revision->getPageAsLinkTarget();
        if (self::titleCheck($getTitle)) {
            return true;
        }

        #$titleOBJ = $revision->Title();
        $title = $revision->getId();
        $parent_id = $revision->getId();

        $content = $revision->getContent( SlotRecord::MAIN );
        $new_content = $content->getText();

        
        #$test = $revision->ParserOutput();

        $parent_id = "";
        if ($parent_id == "") {

            $pos = strpos($new_content, "[[Category:Malak]]");
            if ($pos) {
                $newTitle = self::newTitlePre() . $getTitle;
                #$wikiPage->setTitle($newTitle);
            }

        }

        self::pr($newTitle);

    }



编辑........

仍然没有找到正确的方法,但是提出了适合我需求的工作(黑客)。

使用 oneDitformPreloadtext 代码> malakhere '),编辑了'标题'变更标题的参数,然后使用新页名重定向。在挂钩函数中,如果找到(仅是重定向的原因),则需要检查“ malakhere”参数,然后它将退出该函数,以免创建循环。

    public static function onEditFormPreloadText(string &$text, Title &$title ) {
        global $wgOut;

        if ( isset( $_GET["MalakHere"] ) ) {
            return true;
        }

        $pos = strpos($text, "[[Category:Malak]]");
        if ($pos) {
            $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $urlTitle = urlencode($_GET["title"]);

            $newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
            $newTitle = self::newTitlePre() . $title->prefixedText;
            $url = $newURL . "&title=" . $newTitle;

            return $wgOut->redirect($url);
        }

        return true;
    }

I'm looking to (programmatically) edit/alter the title of a page when its created and before its saved. I've tried a couple hooks to no avail. I can access the title being saved along with other info which ill be used to alter the title, but can not find the right call to save the altered title.

It is for a private/local lan wiki (currently MediaWiki version 1.38.1 ).

When a new article is created with in a certon category I want to number it with a prefix of #### - based on the number of articles already in the category. At the top of the category page itself I have a <inputbox> which pulls a template that has the wiki syntax for the category in it [[Category:BlaBla]]. When the article is saved I'm doing a check to make sure its a new article and some ofther checks for the info I need, which is working fine, but I can not save the new altered page name.

I've tried the following hooks to no avail.

onMultiContentSave
onArticlePrepareTextForEdit

https://www.mediawiki.org/wiki/Manual:Hooks/MultiContentSave
https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePrepareTextForEdit

Heres acouple snippets ive been testing with, both do as i want, aside from saving the altered page name.

    public static function onArticlePrepareTextForEdit( WikiPage $wikiPage, ParserOptions $parserOptions ) {

        return;

        $exists = $wikiPage->exists();
        if ($exists == 1) {
            #return true;
        }
        
        $getTitle = $wikiPage->getTitle();
        # check if title starts with 0000, exit if so, no work needs to be done
        if (self::titleCheck($getTitle)) {
            #return true;
        }

        $checkCategories = $wikiPage->getCategories();
        $inMalak = false;
        foreach ($checkCategories as $value) {
            if ($value == "Category:Malak") {
                $inMalak = true;
            }
        }

        if ($inMalak == 1) {
            $newTitle = self::newTitlePre() . $getTitle;
            #$wikiPage->setTitle($newTitle);
            print(">" . $newTitle . "<br>");
        }

        self::pr($newTitle);
    }

    public static function onMultiContentSave(RenderedRevision $renderedRevision, UserIdentity $user, CommentStoreComment $summary, $flags, Status $hookStatus){
        

        #return;

        $revision = $renderedRevision->getRevision();

        $getTitle = $revision->getPageAsLinkTarget();
        if (self::titleCheck($getTitle)) {
            return true;
        }

        #$titleOBJ = $revision->Title();
        $title = $revision->getId();
        $parent_id = $revision->getId();

        $content = $revision->getContent( SlotRecord::MAIN );
        $new_content = $content->getText();

        
        #$test = $revision->ParserOutput();

        $parent_id = "";
        if ($parent_id == "") {

            $pos = strpos($new_content, "[[Category:Malak]]");
            if ($pos) {
                $newTitle = self::newTitlePre() . $getTitle;
                #$wikiPage->setTitle($newTitle);
            }

        }

        self::pr($newTitle);

    }



EDIT........

Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.

Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.

    public static function onEditFormPreloadText(string &$text, Title &$title ) {
        global $wgOut;

        if ( isset( $_GET["MalakHere"] ) ) {
            return true;
        }

        $pos = strpos($text, "[[Category:Malak]]");
        if ($pos) {
            $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $urlTitle = urlencode($_GET["title"]);

            $newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
            $newTitle = self::newTitlePre() . $title->prefixedText;
            $url = $newURL . "&title=" . $newTitle;

            return $wgOut->redirect($url);
        }

        return true;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-20 11:02:41

仍然没有找到正确的方法,而是想出了适合我需求的工作(黑客)。

使用 oneDitformPreloadtext 代码> malakhere '),编辑了'标题'变更标题的参数,然后使用新页名重定向。在挂钩函数中,如果找到(仅是重定向的原因),则需要检查“ malakhere”参数,然后它将退出该函数,以免创建循环。

    public static function onEditFormPreloadText(string &$text, Title &$title ) {
        global $wgOut;

        if ( isset( $_GET["MalakHere"] ) ) {
            return true;
        }

        $pos = strpos($text, "[[Category:Malak]]");
        if ($pos) {
            $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $urlTitle = urlencode($_GET["title"]);

            $newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
            $newTitle = self::newTitlePre() . $title->prefixedText;
            $url = $newURL . "&title=" . $newTitle;

            return $wgOut->redirect($url);
        }

        return true;
    }

Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.

Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.

    public static function onEditFormPreloadText(string &$text, Title &$title ) {
        global $wgOut;

        if ( isset( $_GET["MalakHere"] ) ) {
            return true;
        }

        $pos = strpos($text, "[[Category:Malak]]");
        if ($pos) {
            $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $urlTitle = urlencode($_GET["title"]);

            $newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
            $newTitle = self::newTitlePre() . $title->prefixedText;
            $url = $newURL . "&title=" . $newTitle;

            return $wgOut->redirect($url);
        }

        return true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文