<?php

/**
 * @brief		Converter Invision Community Class
 * @author		<a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
 * @copyright	(c) Invision Power Services, Inc.
 * @package		Invision Community
 * @subpackage	Converter
 * @since		26 July 2017
 */

namespace IPS\convert\Software\Forums;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

class _Invisioncommunity extends \IPS\convert\Software
{
	/**
	 * @brief 	Whether the versions of IPS4 match
	 */
	public static $versionMatch = FALSE;

	/**
	 * @brief 	Whether the database has been required
	 */
	public static $dbNeeded = FALSE;

	public function __construct( \IPS\convert\App $app, $needDB=TRUE )
	{
		/* Set filename obscuring flag */
		\IPS\convert\Library::$obscureFilenames = FALSE;

		$return = parent::__construct( $app, $needDB );

		if( $needDB )
		{
			static::$dbNeeded = TRUE;

			$version = $this->db->select( 'app_version', 'core_applications', array( 'app_directory=?', 'core' ) )->first();

			/* We're matching against the human version since the long version can change with patches */
			if( $version == \IPS\Application::load( 'core')->version )
			{
				static::$versionMatch = TRUE;
			}

			/* Get parent sauce */
			$this->parent = $this->app->get__parent()->getSource();
		}

		return $return;
	}

	/**
	 * Software Name
	 *
	 * @return	string
	 */
	public static function softwareName()
	{
		/* Child classes must override this method */
		return 'Invision Community (' . \IPS\Application::load( 'core' )->version . ')';
	}

	/**
	 * Software Key
	 *
	 * @return	string
	 */
	public static function softwareKey()
	{
		/* Child classes must override this method */
		return "invisioncommunity";
	}

	/**
	 * Content we can convert from this software.
	 *
	 * @return	array
	 */
	public static function canConvert()
	{
		if( !static::$versionMatch AND static::$dbNeeded )
		{
			throw new \IPS\convert\Exception( 'convert_invision_mismatch' );
		}

		return array(
			'convertForumsForums'			=> array(
				'table'						=> 'forums_forums',
				'where'						=> NULL,
			),
			'convertForumsTopics'				=> array(
				'table'						=> 'forums_topics',
				'where'						=> NULL,
			),
			'convertForumsPosts'			=> array(
				'table'						=> 'forums_posts',
				'where'						=> NULL,
			),
			'convertForumsTopicsMultimods'	=> array(
				'table'						=> 'forums_topic_mmod',
				'where'						=> NULL,
			),
			'convertForumsRssImports'		=> array(
				'table'						=> 'forums_rss_import',
				'where'						=> NULL,
			),
			'convertForumsRssImported'		=> array(
				'table'						=> 'forums_rss_imported',
				'where'						=> NULL,
			),
			'convertForumsQuestionRatings'	=> array(
				'table'						=> 'forums_question_ratings',
				'where'						=> NULL,
			),
			'convertForumsAnswerRatings'	=> array(
				'table'						=> 'forums_answer_ratings',
				'where'						=> NULL,
			),
			'convertAttachments'			=> array(
				'table'						=> 'core_attachments',
				'where'						=> NULL
			),
		);
	}

	/**
	 * Requires Parent
	 *
	 * @return	boolean
	 */
	public static function requiresParent()
	{
		return TRUE;
	}

	/**
	 * List of conversion methods that require additional information
	 *
	 * @return	array
	 */
	public static function checkConf()
	{
		return array(
			'convertAttachments'
		);
	}
	
	/**
	 * Get More Information
	 *
	 * @param	string	$method	Method name
	 * @return	array
	 */
	public function getMoreInfo( $method )
	{
		$return = array();

		switch( $method )
		{
			case 'convertAttachments':
				\IPS\Member::loggedIn()->language()->words["upload_path"] = \IPS\Member::loggedIn()->language()->addToStack( 'convert_invision_upload_input' );
				\IPS\Member::loggedIn()->language()->words["upload_path_desc"] = \IPS\Member::loggedIn()->language()->addToStack( 'convert_invision_upload_input_desc' );
				$return[ $method ] = array(
					'upload_path'				=> array(
						'field_class'		=> 'IPS\\Helpers\\Form\\Text',
						'field_default'		=> isset( $this->parent->app->_session['more_info']['convertEmoticons']['upload_path'] ) ? $this->parent->app->_session['more_info']['convertEmoticons']['upload_path'] : NULL,
						'field_required'	=> TRUE,
						'field_extra'		=> array(),
						'field_hint'		=> \IPS\Member::loggedIn()->language()->addToStack('convert_invision_upload_path'),
						'field_validation'	=> function( $value ) { if ( !@is_dir( $value ) ) { throw new \DomainException( 'path_invalid' ); } },
					)
				);
				break;
		}

		return ( isset( $return[ $method ] ) ) ? $return[ $method ] : array();
	}

	/**
	 * Possible Parent Conversions
	 *
	 * @return	array
	 */
	public static function parents()
	{
		return array( 'core' => array( 'invisioncommunity' ) );
	}

	/**
	 * Finish - Adds everything it needs to the queues and clears data store
	 *
	 * @return	array		Messages to display
	 */
	public function finish()
	{
		/* Content Rebuilds */
		\IPS\Task::queue( 'core', 'RebuildContainerCounts', array( 'class' => 'IPS\forums\Forum', 'count' => 0 ), 5, array( 'class' ) );
		\IPS\Task::queue( 'core', 'RebuildItemCounts', array( 'class' => 'IPS\forums\Topic' ), 3, array( 'class' ) );
		\IPS\Task::queue( 'convert', 'RebuildFirstPostIds', array( 'app' => $this->app->app_id ), 2, array( 'app' ) );
		\IPS\Task::queue( 'convert', 'DeleteEmptyTopics', array( 'app' => $this->app->app_id ), 4, array( 'app' ) );
		\IPS\Task::queue( 'convert', 'InvisionCommunityRebuildContent', array( 'app' => $this->app->app_id, 'link' => 'forums_posts', 'class' => 'IPS\forums\Topic\Post' ), 2, array( 'app', 'link', 'class' ) );

		/* Caches */
		\IPS\Task::queue( 'convert', 'RebuildTagCache', array( 'app' => $this->app->app_id, 'link' => 'forums_topics', 'class' => 'IPS\forums\Topic' ), 3, array( 'app', 'link', 'class' ) );

		return array( "f_forum_last_post_data", "f_rebuild_posts", "f_recounting_forums", "f_recounting_topics", "f_topic_tags_recount" );
	}

	/**
	 * Convert forums
	 *
	 * @return	void
	 */
	public function convertForumsForums()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'id' );

		foreach( $this->fetch( 'forums_forums', 'id'  ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_forums', 'forums' );

			/* Add name after clearing other columns */
			$row['name'] = $this->parent->getWord( 'forums_forum_' . $row['id'] );
			$row['description'] = $this->parent->getWord( 'forums_forum_' . $row['id'] . '_desc' );

			$libraryClass->convertForumsForum( $row );

			/* Convert Follows */
			foreach( $this->db->select( '*', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_rel_id=?', 'forums', 'forum', $row['id'] ) ) as $follow )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $follow, 'core_follow', 'core' );

				/* Change follow data */
				$follow['follow_rel_id_type'] = 'forums_forums';
				$follow['follow_index_id'] = NULL;

				$libraryClass->convertFollow( $follow );
			}

			$libraryClass->setLastKeyValue( $row['id'] );
		}
	}

	/**
	 * Convert topics
	 *
	 * @return	void
	 */
	public function convertForumsTopics()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'tid' );

		foreach( $this->fetch( 'forums_topics', 'tid' ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_topics', 'forums' );

			$libraryClass->convertForumsTopic( $row );

			/* Convert Follows */
			foreach( $this->db->select( '*', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_rel_id=?', 'forums', 'topic', $row['tid'] ) ) as $follow )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $follow, 'core_follow', 'core' );

				/* Change follow data */
				$follow['follow_rel_id_type'] = 'forums_topics';
				$follow['follow_index_id'] = NULL;

				$libraryClass->convertFollow( $follow );
			}

			/* Convert Ratings */
			foreach( $this->db->select( '*', 'core_ratings', array( 'class=? AND item_id=?', 'IPS\\forums\\Topic', $row['tid'] ) ) as $rating )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $rating, 'core_ratings', 'core' );

				/* Change rating data */
				$rating['item_link'] = 'forums_topics';

				$libraryClass->convertRating( $rating );
			}

			/* Convert Tags */
			foreach( $this->db->select( '*', 'core_tags', array( 'tag_meta_app=? AND tag_meta_area=? AND tag_meta_id=?', 'forums', 'forums', $row['tid'] ) ) as $tag )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $tag, 'core_tags', 'core' );

				$libraryClass->convertTag( $tag );
			}

			$libraryClass->setLastKeyValue( $row['tid'] );
		}
	}

	/**
	 * Convert posts
	 *
	 * @return	void
	 */
	public function convertForumsPosts()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'pid' );

		foreach( $this->fetch( 'forums_posts', 'pid' ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_posts', 'forums' );

			$libraryClass->convertForumsPost( $row );

			/* Convert Edit History */
			foreach( $this->db->select( '*', 'core_edit_history', array( 'class=? AND comment_id=?', 'IPS\\forums\\Topic\\Post', $row['pid'] ) ) as $history )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $history, 'core_edit_history', 'core' );

				$libraryClass->convertEditHistory( $history );
			}

			/* Reputation */
			foreach( $this->db->select( '*', 'core_reputation_index', array( "app=? AND type=? AND type_id=?", 'forums', 'pid', $row['pid'] ) ) AS $rep )
			{
				/* Remove non-standard columns */
				$this->parent->unsetNonStandardColumns( $rep, 'core_reputation_index', 'core' );

				$libraryClass->convertReputation( $rep );
			}

			$libraryClass->setLastKeyValue( $row['pid'] );
		}
	}

	/**
	 * Convert topics mmod
	 *
	 * @return	void
	 */
	public function convertForumsTopicsMultimods()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'mm_id' );

		foreach( $this->fetch( 'forums_topic_mmod', 'mm_id'  ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_topic_mmod', 'forums' );

			/* Add name after clearing other columns */
			$row['mm_name'] = $this->parent->getWord( 'forums_mmod_' . $row['mm_id'] );

			$libraryClass->convertForumsTopicMultimod( $row );
			$libraryClass->setLastKeyValue( $row['mm_id'] );
		}
	}

	/**
	 * Convert rss imports
	 *
	 * @return	void
	 */
	public function convertForumsRssImports()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'rss_import_id' );

		foreach( $this->fetch( 'forums_rss_import', 'rss_import_id'  ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_rss_import', 'forums' );

			$libraryClass->convertForumsRssImport( $row );
			$libraryClass->setLastKeyValue( $row['rss_import_id'] );
		}
	}

	/**
	 * Convert rss imported
	 *
	 * @return	void
	 */
	public function convertForumsRssImported()
	{
		$libraryClass = $this->getLibrary();

		foreach( $this->fetch( 'forums_rss_imported', 'rss_imported_tid'  ) AS $row )
		{
			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'forums_rss_imported', 'forums' );

			$libraryClass->convertForumsRssImported( $row );
		}
	}

	/**
	 * Convert attachments
	 *
	 * @return	void
	 */
	public function convertAttachments()
	{
		$libraryClass = $this->getLibrary();
		$libraryClass::setKey( 'attach_id' );

		foreach( $this->fetch( 'core_attachments', 'attach_id' ) AS $row )
		{
			try
			{
				$attachmentMap = $this->db->select( '*', 'core_attachments_map', array( 'attachment_id=? AND location_key=?', $row['attach_id'], 'forums_Forums' ) )->first();
			}
			catch( \UnderflowException $e )
			{
				$libraryClass->setLastKeyValue( $row['attach_id'] );
				continue;
			}

			/* Remove non-standard columns */
			$this->parent->unsetNonStandardColumns( $row, 'core_attachments' );
			$this->parent->unsetNonStandardColumns( $attachmentMap, 'core_attachments_map' );

			/* Remap rows */
			$name = explode( '/', $row['attach_location'] );
			$row['attach_container'] = isset( $name[1] ) ? $name[0] : NULL;
			$thumbName = explode( '/', $row['attach_thumb_location'] );
			$row['attach_thumb_container'] = isset( $thumbName[1] ) ? $thumbName[0] : NULL;

			$filePath = $this->app->_session['more_info']['convertAttachments']['upload_path'] . '/' . $row['attach_location'];
			$thumbnailPath = $this->app->_session['more_info']['convertAttachments']['upload_path'] . '/' . $row['attach_thumb_location'];

			unset( $row['attach_file'] );

			$libraryClass->convertAttachment( $row, $attachmentMap, $filePath, NULL, $thumbnailPath );		
			$libraryClass->setLastKeyValue( $row['attach_id'] );
		}
	}

	/**
	 * Check if we can redirect the legacy URLs from this software to the new locations
	 *
	 * @return	NULL|\IPS\Http\Url
	 */
	public function checkRedirects()
	{
		$url = \IPS\Request::i()->url();

		if( preg_match( '#/ips4merge/(forum|topic)/([0-9]+)-(.+?)#i', $url->data[ \IPS\Http\Url::COMPONENT_PATH ], $matches ) )
		{
			$oldId	= (int) $matches[2];

			switch( $matches[1] )
			{
				case 'forum':
					$class	= '\IPS\forums\Forum';
					$types	= array( 'forums', 'forums_forums' );
				break;

				case 'topic':
					$class	= '\IPS\forums\Topic';
					$types	= array( 'topics', 'forums_topics' );

					if( \IPS\Request::i()->do == 'findComment' AND \IPS\Request::i()->comment )
					{
						$class	= '\IPS\forums\Topic\Post';
						$types	= array( 'posts', 'forums_posts' );
						$oldId	= \IPS\Request::i()->comment;
					}
				break;
			}
		}

		if( isset( $class ) )
		{
			try
			{
				try
				{
					$data = (string) $this->app->getLink( $oldId, $types );
				}
				catch( \OutOfRangeException $e )
				{
					$data = (string) $this->app->getLink( $oldId, $types, FALSE, TRUE );
				}
				$item = $class::load( $data );

				if( $item instanceof \IPS\Content )
				{
					if( $item->canView() )
					{
						return $item->url();
					}
				}
				elseif( $item instanceof \IPS\Node\Model )
				{
					if( $item->can( 'view' ) )
					{
						return $item->url();
					}
				}
			}
			catch( \Exception $e )
			{
				return NULL;
			}
		}

		return NULL;
	}
}