mediawiki: is there a way to automatically create redirect pages that redirect to the current page?

戏子无情 提交于 2019-12-08 03:54:44

问题


My hobby is writing up stuff on a personal wiki site: http://comp-arch.net. Currently using mediawiki (although I often regret having chosen it, since I need per page access control.)

Often I create pages that define several terms or concepts on the same page. E.g. http://semipublic.comp-arch.net/wiki/Invalidate_before_writing_versus_write_through_is_the_invalidate.

Oftentimes such "A versus B" pages provide the only definitions of A and B. Or at least the only definitions that I have so far gotten around to writing.
Sometimes I will define many more that two topics on the same page.

If I create such an "A vs B" or other paging containing multiple definitions D1, D2, ... DN, I would like to automatically create redirect pages, so that I can say [[A]] or [[B]] or [[D1]] .. [[DN]] in other pages.

At the moment the only way I know of to create such pages is manually. It's hard to keep up.

Furthermore, at the time I create such a page, I would like to provide some page text - typicaly a category.


Here;s another example: variant page names. I often find that I want to create several variants of a page name, all linking to the same place. For example [[multithreading]], [[multithreading (MT)]], [[MT (multithreading)]], [[MT]]

Please don;t tell me to use piped links. That's NOT what I want!


TWiki has plugins such as

  • TOPICCREATE automatically create topics or attach files at topic save time

More than that, I remember a twiki plugin, whose name I cannot remember or google up, that included the text of certain subpages within your current opage. You could then edit all of these pages together, and save - and the text would be extracted and distributed as needed. (By the way, if you can remember the name of tghat package, please remind me. It had certain problems, particularly wrt file locking (IIRC it only locked the top file for editing, bot the sub-topics, so you could lose stuff.))

But this last, in combination with parameterized templtes, would be almost everything I need.

Q: does mediawiki have something similar? I can't find it.


I suppose that I can / could should wrote my own robot to perform such actions.


回答1:


It's possible to do this, although I don't know whether such extensions exist already. If you're not averse to a bit of PHP coding, you could write your own using the ArticleSave and/or ArticleSaveComplete hooks.

Here's an example of an ArticleSaveComplete hook that will create redirects to the page being saved from all section titles on the page:

$wgHooks['ArticleSaveComplete'][] = 'createRedirectsFromSectionTitles';
function createRedirectsFromSectionTitles( &$page, &$user, $text ) {
    // do nothing for pages outside the main namespace:
    $title = $page->getTitle();
    if ( $title->getNamespace() != 0 ) return true;

    // extract section titles:
    // XXX: this is a very quick and dirty implementation;
    // it would be better to call the parser
    preg_match_all( '/^(=+)\s*(.*?)\s*\1\s*$/m', $text, $matches );

    // create a redirect for each title, unless they exist already:
    // (invalid titles and titles outside ns 0 are also skipped)
    foreach ( $matches[2] as $section ) {
        $nt = Title::newFromText( $section );
        if ( !$nt || $nt->getNamespace() != 0 || $nt->exists() ) continue;

        $redirPage = WikiPage::factory( $nt );
        if ( !$redirPage ) continue;  // can't happen; check anyway

        // initialize some variables that we can reuse:
        if ( !isset( $redirPrefix ) ) {
            $redirPrefix = MagicWord::get( 'redirect' )->getSynonym( 0 );
            $redirPrefix .= '[[' . $title->getPrefixedText() . '#';
        }
        if ( !isset( $reason ) ) {
            $reason = wfMsgForContent( 'editsummary-auto-redir-to-section' );
        }

        // create the page (if we can; errors are ignored):
        $redirText = $redirPrefix . $section . "]]\n";
        $flags = EDIT_NEW | EDIT_MINOR | EDIT_DEFER_UPDATES;
        $redirPage->doEdit( $redirText, $reason, $flags, false, $user );
    }
    return true;
}

Note: Much of this code is based on bits and pieces of the pagemove redirect creating code from Title.php and the double redirect fixer code, as well as the documentation for WikiPage::doEdit(). I have not actually tested this code, but I think it has at least a decent chance of working as is. Note that you'll need to create the MediaWiki:editsummary-auto-redir-to-section page on your wiki to set a meaningful edit summary for the redirect edits.



来源:https://stackoverflow.com/questions/9730912/mediawiki-is-there-a-way-to-automatically-create-redirect-pages-that-redirect-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!