Pergunta

OK so its fairly well documented how to hide the index.php?title= bit from urls, but what I would like is to hide it for all the action type links, similar to how the extension ShortUrls is supposed to work (It doesn't on my site and I don't know how to fix it). I don't have access to .htaccess and was thinking of doing it by adding some javascript to the common.js page, or by modifying the ShortLinks extension but the documentation on those hooks isn't very good and anything I tried wasn't helping.

Foi útil?

Solução

Basically, you need to add an entry to $wgActionPaths for each action that you want to use a short URL for.

For example, if you want the normal view URL of the page Foobar to be /wiki/Foobar, and the edit and history URLs to be, say, /wiki/edit/Foobar and /wiki/history/Foobar, you'd add the following lines to your LocalSettings.php:

$wgArticlePath = '/wiki/$1';
$wgActionPaths['edit'] = '/wiki/edit/$1';
$wgActionPaths['history'] = '/wiki/history/$1';

Of course, you'll also need to configure your web server to rewrite any requests for those short URLs back into something MediaWiki will understand, e.g. using mod_rewrite on Apache. The documentation page I linked to above has some helpful examples.


More generally, you can transform MediaWiki-generated URLs in arbitrary ways using a GetLocalURL or GetLocalURL::Internal hook. (The main difference between the two hooks is that the GetLocalURL hook is also called for interwiki URLs.) These hooks are called from the Title::GetLocalURL(), which you may want to take a look at to see how they work.

For example, here's how you could transform diff URLs into the format /wiki/diff/revA/revB/Page_name:

function prettyDiffURLs( $title, &$url, $query ) {
    if ( preg_match( '/^diff=(\w+)&oldid=(\w+)$/', $query, $matches ) ) {
        $dbkey = wfUrlencode( $title->getPrefixedDBkey() );
        $url = "/wiki/diff/$matches[1]/$matches[2]/$dbkey";
    }
    return true;
}
$wgHooks['GetLocalURL::Internal'][] = 'prettyDiffURLs';

(Warning: I believe this code should work, but I have not tested it!)

Outras dicas

My eventual answer (thanks to the above answer) is:

function pretty_diff($title, &$url, $query)
{
        if ( preg_match('/diff=(\w+)&oldid=(\w+)/', $query, $matches))
        {
                $dbkey = wfUrlencode($title->getPrefixedDBkey());
                $url = "/wiki/$dbkey?$matches[0]";
        }
        return true;
}
$wgHooks['GetLocalURL::Internal'][] = 'pretty_diff';

but I eventually replaced it with a javascript function to potentially cover more links

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top