Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #12611 from Wikia/IRIS-3983
Browse files Browse the repository at this point in the history
IRIS-3983 Redirect all Forum URLs to Discussions on migrated site
  • Loading branch information
tortila authored and James Sutterfield committed Apr 26, 2017
1 parent 8d48897 commit bdc8e60
Show file tree
Hide file tree
Showing 7 changed files with 865 additions and 26 deletions.
27 changes: 21 additions & 6 deletions extensions/wikia/Discussions/Discussions.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$wgAutoloadClasses['DiscussionsVarTogglerException'] = $dir . 'DiscussionsVarToggler.class.php';
$wgAutoloadClasses['ThreadCreator'] = $dir . 'api/ThreadCreator.class.php';
$wgAutoloadClasses['DiscussionsActivator'] = $dir . 'api/DiscussionsActivator.class.php';
$wgAutoloadClasses['LegacyRedirect'] = $dir . 'api/LegacyRedirect.class.php';
$wgAutoloadClasses['StaffWelcomePoster'] = $dir . 'maintenance/StaffWelcomePoster.class.php';

// register special page
Expand All @@ -29,7 +30,25 @@
// is enabled and Forums are disabled.
if ( !empty( $wgEnableDiscussions ) && empty( $wgEnableForumExt ) ) {
$wgAutoloadClasses['SpecialForumRedirectController'] = $dir . 'controllers/SpecialForumRedirectController.class.php';
$wgHooks['ArticleViewHeader'][] = 'SpecialForumRedirectController::onArticleViewHeader';
$wgHooks['BeforePageHistory'][] = 'SpecialForumRedirectController::onBeforePageHistory';
$wgSpecialPages['Forum'] = 'SpecialForumRedirectController';

// Make sure we recognize the Forum namespaces so we can redirect them if requested
$wgExtensionNamespacesFiles['Discussions'] = $dir . '../Forum/Forum.namespaces.php';
wfLoadExtensionNamespaces( 'Forum',
[
NS_WIKIA_FORUM_BOARD,
NS_WIKIA_FORUM_BOARD_THREAD,
NS_WIKIA_FORUM_TOPIC_BOARD
]
);

$app->registerNamespaceController(
NS_WIKIA_FORUM_BOARD,
'SpecialForumRedirectController',
'redirectBoardToCategory'
);
}

// message files
Expand All @@ -48,17 +67,13 @@
$wgHooks['WikiaSkinTopScripts'][] = 'addDiscussionJsVariable';

/**
* MW1.19 - ResourceLoaderStartUpModule class adds more variables
* @param array $vars JS variables to be added at the bottom of the page
* @param OutputPage $out
* @param $scripts
*
* @return bool return true - it's a hook
*/
function addDiscussionJsVariable(Array &$vars, &$scripts) {
wfProfileIn(__METHOD__);

$vars['wgDiscussionsApiUrl'] = F::app()->wg->DiscussionsApiUrl;

wfProfileOut(__METHOD__);
return true;
}

80 changes: 80 additions & 0 deletions extensions/wikia/Discussions/api/LegacyRedirect.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use Swagger\Client\ApiException;
use Swagger\Client\Discussion\Api\LegacyRedirectsApi;
use Wikia\DependencyInjection\Injector;
use Wikia\Service\Swagger\ApiProvider;

class LegacyRedirect {

const SERVICE_NAME = 'discussion';
const TIMEOUT = 5;
const SITE_NAME_MAX_LENGTH = 256;

private $siteId;

private $legacyRedirectApi;
private $logger;

public function __construct( int $siteId ) {
$this->siteId = $siteId;

$this->legacyRedirectApi = $this->getLegacyRedirectApi();
$this->logger = Wikia\Logger\WikiaLogger::instance();
}

/**
* Return a path to redirect the client to based on a board ID
*
* @param int $boardId
* @return string
*/
public function getBoardRedirect( $boardId ) {
try {
$response = $this->legacyRedirectApi->getForumRedirect( $this->siteId, $boardId );
return $response->getPath();
} catch ( ApiException $e ) {
$this->logAndThrowError( $e );
}

return '';
}

/**
* Return a path to redirect the client to based on a thread ID
*
* @param int $threadId
* @return string
*/
public function getThreadRedirect( $threadId ) {
try {
$response = $this->legacyRedirectApi->getThreadRedirect( $this->siteId, $threadId );
return $response->getPath();
} catch ( ApiException $e ) {
$this->logAndThrowError( $e );
}

return '';
}

private function getLegacyRedirectApi() {
/** @var ApiProvider $apiProvider */
$apiProvider = Injector::getInjector()->get( ApiProvider::class );
/** @var LegacyRedirectsApi $api */
$api = $apiProvider->getApi( self::SERVICE_NAME, LegacyRedirectsApi::class );
$api->getApiClient()->getConfig()->setCurlTimeout( self::TIMEOUT );

return $api;
}

private function logAndThrowError( Exception $e ) {
$this->logger->error(
'DISCUSSIONS Retrieving legacy Forum redirect caused an error',
[
'siteId' => $this->siteId,
'error' => $e->getMessage()
]
);
throw new ErrorPageError( 'unknown-error', 'discussions-activate-error' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,147 @@ class SpecialForumRedirectController extends WikiaSpecialPageController {

const DISCUSSIONS_LINK = '/d/f';

private $legacyRedirect;

public function __construct() {
parent::__construct( 'Forum', '', false );

$this->legacyRedirect = new LegacyRedirect( F::App()->wg->CityId );
}

public function index() {
$this->response->redirect( self::DISCUSSIONS_LINK );
$this->redirectForumToDiscussions();
}

/**
* Redirect requests for Special:Forum to discussions, e.g. from:
*
* http://garth.wikia.com/wiki/Special:Forum
*
* to:
*
* http://garth.wikia.com/d/f?sort=latest
*/
public function redirectForumToDiscussions() {
$discussionUrl = $this->getDiscussionUrl();
$this->response->redirect( $discussionUrl );
}

/**
* Redirect requests for the board page to discussions, e.g, from:
*
* http://garth.wikia.com/wiki/Board:Some_Board_Name
*
* to:
*
* http://garth.wikia.com/d/f?catId=2016136434548737693
*
* This is handled by registering a namespace controller (deprecated currently, but how wall
* works) e.g. this line in Discussions.setup.php:
*
* $app->registerNamespaceController(
* NS_WIKIA_FORUM_BOARD,
* 'SpecialForumRedirectController',
* 'redirectBoardToCategory',
* );
*
*/
public function redirectBoardToCategory() {
// No template for this, we're only interested in setting the redirect.
$this->skipRendering();

$boardId = $this->getBoardId();
$categoryUrl = $this->legacyRedirect->getBoardRedirect( $boardId );

$this->response->redirect( $categoryUrl );
}

/**
* Redirect requests for a thread to discussions, e.g. from:
*
* http://garth.garth.wikia-dev.us/wiki/Thread:2892
*
* to:
*
* http://garth.wikia.com/d/p/2914856863801542335
*
* @param Title $thread
*/
public static function redirectThreadToPost( Title $thread ) {
$threadId = $thread->getArticleID();

$legacyRedirect = new LegacyRedirect( F::App()->wg->CityId );
$postUrl = $legacyRedirect->getThreadRedirect( $threadId );

F::app()->wg->Out->redirect( $postUrl );
}

private function getDiscussionUrl() {
return self::DISCUSSIONS_LINK;
}

private function getBoardId() {
return $this->wg->Title->getArticleID();
}

/**
* @param Article $article
*
* @return bool
*/
public static function onBeforePageHistory( &$article ) {
self::redirectPage( $article );
return true;
}

/**
* @param Article $article
* @param bool $outputDone
* @param bool $useParserCache
*
* @return bool
*/
public static function onArticleViewHeader( &$article, &$outputDone, &$useParserCache ) {
self::redirectPage( $article );
return true;
}

private static function redirectPage( &$article ) {
$mainTitle = self::getMainTitle( $article );
if ( empty( $mainTitle ) ) {
return;
}

// Redirect to discussions
self::redirectThreadToPost( $mainTitle );
}

/**
* Some of this logic has been borrowed from WallHooksHelper::onArticleViewHeader
*
* @param Article $article
* @return null|Title
*/
private static function getMainTitle( Article $article ) {
$title = $article->getTitle();

if ( $title->getNamespace() != NS_USER_WALL_MESSAGE || $title->getText() <= 0 ) {
return null;
}

$mainTitle = Title::newFromId( $title->getText() );

if ( empty( $mainTitle ) ) {
return null;
}

$dbKey = $mainTitle->getDBkey();

$helper = new WallHelper();
if ( !$helper->isDbkeyFromWall( $dbKey ) ) {
return null;
}

return $mainTitle;
}
}
}
4 changes: 2 additions & 2 deletions extensions/wikia/Forum/Forum.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
include ( $dir . '/Forum.namespace.setup.php' );

// add this namespace to list of wall namespaces
$app->registerNamespaceControler( NS_WIKIA_FORUM_BOARD, 'ForumController', 'board', true );
$app->registerNamespaceControler( NS_WIKIA_FORUM_TOPIC_BOARD, 'ForumController', 'board', true );
$app->registerNamespaceController( NS_WIKIA_FORUM_BOARD, 'ForumController', 'board', true );
$app->registerNamespaceController( NS_WIKIA_FORUM_TOPIC_BOARD, 'ForumController', 'board', true );

JSMessages::registerPackage( 'Forum', [
'back',
Expand Down
Loading

0 comments on commit bdc8e60

Please sign in to comment.