Extension:HTTP302Found/Version 1.0

From MediaWiki.org
Jump to navigation Jump to search
<?php
/* MediaWiki extension to force an external HTTP 302 redirect instead of internal redirects
 * Copyright (C) 2009  Ian Weller <ian@ianweller.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

$wgExtensionCredits["other"][] = array(
        'path' => __FILE__,
        'name' => "HTTP 302 Found",
        'description' => "Forces an external HTTP 302 redirect instead of internal redirects",    
        'version' => "1.0",         
        'author' => "Ian Weller",         
        'url' => "http://www.mediawiki.org/wiki/Extension:HTTP302Found",
);

$wgHooks['InitializeArticleMaybeRedirect'][] = 'http302_redirect_hook';
$wgHooks['ArticlePageDataBefore'][] = 'http302_redirect_notification_hook';

// This function causes the actual redirect
function http302_redirect_hook($title, $request, &$ignoreRedirect, &$target, &$article)
{
        if (!$ignoreRedirect && $article->isRedirect()) {
                if (($target = $article->followRedirect()) instanceof Title) {
                        $target = $target->getFullURL()."?rd=".$title->mUrlform;
                }
        }
        return true;
}

// This function handles the "rd" GET variable and forces the "Redirected from ..." message
function http302_redirect_notification_hook(&$article, &$fields)
{
        $title = Title::newFromText($_GET["rd"]);
        $article->setRedirectedFrom($title);
        return true;
}