/**
 * This function appears to be unused...I think it was used in ancient history.
 */
function wfGetUserStatusProfile( $user_id, $num ) {
	global $wgScriptPath;

	$s = new UserStatus();

	$update = $s->getStatusMessages( $user_id, 0, 0, 1, $num );
	$update = $update[0];

	return SportsTeams::getLogo( $update['sport_id'], $update['team_id'], 's' ) .
		"<img src=\"{$wgScriptPath}/extensions/UserStatus/quoteIcon.png\" border=\"0\" style=\"margin-left:5px;\" alt=\"\" />
		{$update['text']}
		<img src=\"{$wgScriptPath}/extensions/UserStatus/endQuoteIcon.png\" border=\"0\" alt=\"\" />
		<span class=\"user-status-date\">" . 
			wfMsg( 'userstatus-ago', UserStatus::getTimeAgo( $update['timestamp'] ) ) .
		'</span>';
}
	/**
	 * Show the special page
	 *
	 * @param $par Mixed: parameter passed to the special page or null
	 */
	public function execute( $par ) {
		global $wgRequest, $wgOut, $wgUser, $wgScriptPath;

		$messages_show = 25;
		$output = '';
		$user_name = $wgRequest->getVal( 'user', $par );
		$page = $wgRequest->getInt( 'page', 1 );

		/**
		 * Redirect Non-logged in users to Login Page
		 * It will automatically return them to their Status page
		 */
		if( $wgUser->getID() == 0 && $user_name == '' ) {
			$wgOut->setPageTitle( wfMsg( 'userstatus-woops' ) );
			$login = SpecialPage::getTitleFor( 'Userlogin' );
			$wgOut->redirect( $login->getFullURL( 'returnto=Special:UserStatus' ) );
			return false;
		}

		/**
		 * If no user is set in the URL, we assume its the current user
		 */
		if( !$user_name ) {
			$user_name = $wgUser->getName();
		}
		$user_id = User::idFromName( $user_name );
		$user = Title::makeTitle( NS_USER, $user_name );

		/**
		 * Error message for username that does not exist (from URL)
		 */
		if( $user_id == 0 ) {
			$wgOut->setPageTitle( wfMsg( 'userstatus-woops' ) );
			$wgOut->addHTML( wfMsg( 'userstatus-no-user' ) );
			return false;
		}

		/**
		 * Config for the page
		 */
		$per_page = $messages_show;

		$stats = new UserStats( $user_id, $user_name );
		$stats_data = $stats->getUserStats();
		$total = $stats_data['user_status_count'];

		$s = new UserStatus();
		$messages = $s->getStatusMessages( $user_id, 0, 0, $messages_show, $page );

		if ( !( $wgUser->getName() == $user_name ) ) {
			$wgOut->setPageTitle( wfMsg( 'userstatus-user-thoughts', $user_name ) );
		} else {
			$wgOut->setPageTitle( wfMsg( 'userstatus-your-thoughts' ) );
		}

		$output .= '<div class="gift-links">';
		if ( !( $wgUser->getName() == $user_name ) ) {
			$output .= "<a href=\"{$user->getFullURL()}\">" .
				wfMsg( 'userstatus-back-user-profile', $user_name ) . '</a>';
		} else {
			$output .= '<a href="' . $wgUser->getUserPage()->getFullURL() . '">' .
				wfMsg( 'userstatus-back-your-profile' ) . '</a>';
		}
		$output .= '</div>';

		if( $page == 1 ) {
			$start = 1;
		} else {
			$start = ( $page - 1 ) * $per_page + 1;
		}

		$end = $start + ( count( $messages ) ) - 1;
		wfDebug( "total = {$total}" );

		if( $total ) {
			$output .= '<div class="user-page-message-top">
				<span class="user-page-message-count" style="font-size: 11px; color: #666666;">' .
					wfMsgExt( 'userstatus-showing-thoughts', 'parsemag', $start, $end, $total ) .
				'</span>
			</div>';
		}

		/**
		 * Build next/prev navigation
		 */
		$numofpages = $total / $per_page;

		if( $numofpages > 1 ) {
			$output .= '<div class="page-nav">';
			if( $page > 1 ) {
				$output .= '<a href="' . $this->getTitle()->getFullURL( array(
					'user' => $user_name, 'page' => ( $page - 1 ) ) ) . '">' .
					wfMsg( 'userstatus-prev' ) . '</a> ';
			}

			if( ( $total % $per_page ) != 0 ) {
				$numofpages++;
			}
			if( $numofpages >= 9 && $page < $total ) {
				$numofpages = 9 + $page;
				if( $numofpages >= ( $total / $per_page ) ) {
					$numofpages = ( $total / $per_page ) + 1;
				}
			}

			for( $i = 1; $i <= $numofpages; $i++ ) {
				if( $i == $page ) {
					$output .= ( $i . ' ' );
				} else {
					$output .= '<a href="' . $this->getTitle()->getFullURL(
						array( 'user' => $user_name, 'page' => $i ) ) .
						"\">$i</a> ";
				}
			}

			if( ( $total - ( $per_page * $page ) ) > 0 ) {
				$output .= ' <a href="' . $this->getTitle()->getFullURL( array(
					'user' => $user_name, 'page' => ( $page + 1 ) ) ) . '">' .
					wfMsg( 'userstatus-next' ) . '</a>';
			}
			$output .= '</div><p>';
		}

		// Add CSS & JS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.userStatus' );
			$wgOut->addModuleScripts( 'ext.userStatus' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/UserStatus/UserStatus.css' );
			$wgOut->addScriptFile( $wgScriptPath . '/extensions/UserStatus/UserStatus.js' );
		}

		$output .= '<div class="user-status-container">';
		$thought_link = SpecialPage::getTitleFor( 'ViewThought' );
		if( $messages ) {
			foreach ( $messages as $message ) {
				$user = Title::makeTitle( NS_USER, $message['user_name'] );
				$avatar = new wAvatar( $message['user_id'], 'm' );

				$network_link = '<a href="' . SportsTeams::getNetworkURL( $message['sport_id'], $message['team_id'] ) . '">' .
					wfMsg( 'userstatus-all-team-updates', SportsTeams::getNetworkName( $message['sport_id'], $message['team_id'] ) ) .
				'</a>';

				$delete_link = '';
				if( $wgUser->getName() == $message['user_name'] ) {
					$delete_link = "<span class=\"user-board-red\">
						<a href=\"javascript:void(0);\" onclick=\"javascript:delete_message({$message['id']})\">" .
						wfMsg( 'userstatus-delete-thought-text' ) ."</a>
					</span>";
				}

				$message_text = preg_replace_callback(
					'/(<a[^>]*>)(.*?)(<\/a>)/i',
					array( 'UserStatus', 'cutLinkText' ),
					$message['text']
				);
				$vote_count = wfMsgExt( 'userstatus-num-agree', 'parsemag', $message['plus_count'] );

				$vote_link = '';
				if( $wgUser->isLoggedIn() && $wgUser->getName() != $message['user_name'] ) {
					if( !$message['voted'] ) {
						$vote_link = "<a href=\"javascript:void(0);\" onclick=\"vote_status({$message['id']},1)\">[" .
							wfMsg( 'userstatus-_agree' ) . "]</a>";
					} else {
						$vote_link = $vote_count;
					}
				}

				$view_thought_link = '<a href="' . $thought_link->getFullURL( "id={$message['id']}" ) . '">[' .
					wfMsg( 'userstatus-see-who-agrees' ) . ']</a>';

				$output .= '<div class="user-status-row">

					<div class="user-status-logo">

						<a href="' . SportsTeams::getNetworkURL( $message['sport_id'], $message['team_id'] ) . '">' .
							SportsTeams::getLogo( $message['sport_id'], $message['team_id'], 'm' ) .
						"</a>

					</div>

					<div class=\"user-status-message\">

						{$message_text}

						<div class=\"user-status-date\">" . 
							wfMsg( 'userstatus-ago', UserStatus::getTimeAgo( $message['timestamp'] ) ) .
							"<span class=\"user-status-vote\" id=\"user-status-vote-{$message['id']}\">
								{$vote_link}
							</span>
							{$view_thought_link}
							<span class=\"user-status-links\">
								{$delete_link}
							</span>
						</div>

					</div>

					<div class=\"cleared\"></div>

				</div>";
			}
		} else {
			$output .= '<p>' . wfMsg( 'userstatus-no-updates' ) . '</p>';
		}

		$output .= '</div>';

		$wgOut->addHTML( $output );
	}
	/**
	 * Show the special page
	 *
	 * @param $par Mixed: parameter passed to the special page or null
	 */
	public function execute( $par ) {
		global $wgRequest, $wgOut, $wgUser, $wgScriptPath;

		$messages_show = 25;
		$output = '';
		$us_id = $wgRequest->getInt( 'id', $par );
		$page = $wgRequest->getInt( 'page', 1 );

		// No ID? Show an error message then.
		if( !$us_id || !is_numeric( $us_id ) ) {
			$wgOut->addHTML( wfMsg( 'userstatus-invalid-link' ) );
			return false;
		}

		/**
		 * Config for the page
		 */
		$per_page = $messages_show;

		$s = new UserStatus();
		$message = $s->getStatusMessage( $us_id );
		$user_name = $message['user_name'];
		$user = Title::makeTitle( NS_USER, $user_name );

		// Different page title, depending on whose status updates we're
		// viewing
		if ( !( $wgUser->getName() == $user_name ) ) {
			$wgOut->setPageTitle( wfMsg( 'userstatus-user-thoughts', $user_name ) );
		} else {
			$wgOut->setPageTitle( wfMsg( 'userstatus-your-thoughts' ) );
		}

		// Add CSS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.userStatus.viewThought' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/UserStatus/ViewThought.css' );
		}

		$output .= "<div class=\"view-thought-links\">
			<a href=\"{$user->getFullURL()}\">" .
				wfMsg( 'userstatus-user-profile', $user_name ) .
			'</a>
		</div>';
		$output .= '<div class="user-status-container">';
		$output .= '<div class="user-status-row">

				<div class="user-status-logo">

					<a href="' . SportsTeams::getNetworkURL( $message['sport_id'], $message['team_id'] ) . '">' .
						SportsTeams::getLogo( $message['sport_id'], $message['team_id'], 'm' ) .
					"</a>

				</div>

				<div class=\"user-status-message\">

					{$message['text']}

					<div class=\"user-status-date\">" .
						wfMsg( 'userstatus-ago', UserStatus::getTimeAgo( $message['timestamp'] ) ) .
					'</div>

				</div>

				<div class="cleared"></div>

		</div>
		</div>';

		$output .= '<div class="who-agrees">';
		$output .= '<h1>' . wfMsg( 'userstatus-who-agrees' ) . '</h1>';
		$voters = $s->getStatusVoters( $us_id );
		// Get the people who agree with this status update, if any
		if( $voters ) {
			foreach ( $voters as $voter ) {
				$user = Title::makeTitle( NS_USER, $voter['user_name'] );
				$avatar = new wAvatar( $voter['user_id'], 'm' );

				$output .= "<div class=\"who-agrees-row\">
					<a href=\"{$user->getFullURL()}\">{$avatar->getAvatarURL()}</a>
					<a href=\"{$user->getFullURL()}\">{$voter['user_name']}</a>
				</div>";
			}
		} else {
			$output .= '<p>' . wfMsg( 'userstatus-nobody-agrees' ) . '</p>';
		}

		$output .= '</div>';

		$wgOut->addHTML( $output );
	}
	/**
	 * Show the special page
	 *
	 * @param $par Mixed: parameter passed to the special page or null
	 */
	public function execute( $par ) {
		global $wgRequest, $wgOut, $wgUser, $wgScriptPath;

		$messages_show = 25;
		$updates_show = 25; // just an arbitrary value to stop PHP from complaining on 12 August 2011 --ashley
		$output = '';
		$sport_id = $wgRequest->getInt( 'sport_id' );
		$team_id = $wgRequest->getInt( 'team_id' );
		$page = $wgRequest->getInt( 'page', 1 );

		if ( $team_id ) {
			$team = SportsTeams::getTeam( $team_id );
			$network_name = $team['name'];
		} elseif ( $sport_id ) {
			$sport = SportsTeams::getSport( $sport_id );
			$network_name = $sport['name'];
		} else {
			// No sports ID nor team ID...bail out or we'll get a database
			// error...
			$wgOut->setPageTitle( wfMsg( 'userstatus-woops' ) );
			$out = '<div class="relationship-request-message">' .
				wfMsg( 'userstatus-invalid-link' ) . '</div>';
			$out .= '<div class="relationship-request-buttons">';
			$out .= '<input type="button" class="site-button" value="' .
				wfMsg( 'mainpage' ) .
				"\" onclick=\"window.location='" .
				Title::newMainPage()->escapeFullURL() . "'\"/>";
			/* removed because I was too lazy to port the error message over :P
			if ( $wgUser->isLoggedIn() ) {
				$out .= ' <input type="button" class="site-button" value="' .
					wfMsg( 'st_network_your_profile' ) .
					"\" onclick=\"window.location='" .
					Title::makeTitle( NS_USER, $wgUser->getName() )->escapeFullURL() . "'\"/>";
			}
			*/
			$out .= '</div>';
			$wgOut->addHTML( $out );
			return true;
		}

		$wgOut->setPageTitle( wfMsg( 'userstatus-network-thoughts', $network_name ) );

		/**
		 * Config for the page
		 */
		$per_page = $messages_show;

		$s = new UserStatus();
		$total = $s->getNetworkUpdatesCount( $sport_id, $team_id );
		$messages = $s->getStatusMessages(
			0,
			$sport_id,
			$team_id,
			$messages_show,
			$page
		);

		$output .= '<div class="gift-links">';
		$output .= '<a href="' .
			SportsTeams::getNetworkURL( $sport_id, $team_id ) . '">' .
				wfMsg( 'userstatus-back-to-network' ) . '</a>';
		$output .= '</div>';

		if( $page == 1 ) {
			$start = 1;
		} else {
			$start = ( $page - 1 ) * $per_page + 1;
		}
		$end = $start + ( count( $messages ) ) - 1;

		if( $total ) {
			$output .= '<div class="user-page-message-top">
			<span class="user-page-message-count" style="font-size: 11px; color: #666666;">' .
				wfMsgExt( 'userstatus-showing-thoughts', 'parsemag', $start, $end, $total ) .
			'</span>
		</div>';
		}

		/**
		 * Build next/prev navigation
		 */
		$numofpages = $total / $per_page;

		if( $numofpages > 1 ) {
			$output .= '<div class="page-nav">';
			if( $page > 1 ) {
				$output .= '<a href="' .
					SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
					'&page=' . ( $page - 1 ) . '">' . wfMsg( 'userstatus-prev' ) .
					'</a> ';
			}

			if( ( $total % $per_page ) != 0 ) {
				$numofpages++;
			}
			if( $numofpages >= 9 && $page < $total ) {
				$numofpages = 9 + $page;
				if( $numofpages >= ( $total / $per_page ) ) {
					$numofpages = ( $total / $per_page ) + 1;
				}
			}

			for( $i = 1; $i <= $numofpages; $i++ ) {
				if( $i == $page ) {
					$output .= ( $i . ' ');
				} else {
					$output .= '<a href="' .
						SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
						"&page=$i\">$i</a> ";
				}
			}

			if( ( $total - ( $per_page * $page ) ) > 0 ) {
				$output .= ' <a href="' .
					SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
					'&page=' . ( $page + 1 ) . '">' . wfMsg( 'userstatus-next' ) .
					'</a>';
			}
			$output .= '</div><p>';
		}

		// Add CSS & JS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.userStatus' );
			$wgOut->addModuleScripts( 'ext.userStatus' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/UserStatus/UserStatus.css' );
			$wgOut->addScriptFile( $wgScriptPath . '/extensions/UserStatus/UserStatus.js' );
		}

		// Registered users who are not blocked can add status updates when the
		// database is not locked
		if( $wgUser->isLoggedIn() && !$wgUser->isBlocked() && !wfReadOnly() ) {
			$output .= "<script>
				var __sport_id__ = {$sport_id};
				var __team_id__ = {$team_id};
				var __updates_show__ = \"{$updates_show}\";
				var __redirect_url__ = \"" . str_replace( '&amp;', '&', SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) ) . "\";
			</script>";

			$output .= "<div class=\"user-status-form\">
			<span class=\"user-name-top\">{$wgUser->getName()}</span> <input type=\"text\" name=\"user_status_text\" id=\"user_status_text\" size=\"40\"/>
			<input type=\"button\" value=\"" . wfMsg( 'userstatus-btn-add' ) . '" class="site-button" onclick="add_status()" />
			</div>';
		}

		$output .= '<div class="user-status-container">';
		if( $messages ) {
			foreach ( $messages as $message ) {
				$user = Title::makeTitle( NS_USER, $message['user_name'] );
				$avatar = new wAvatar( $message['user_id'], 'm' );

				$messages_link = '<a href="' .
					UserStatus::getUserUpdatesURL( $message['user_name'] ) . '">' .
					wfMsg( 'userstatus-view-all-updates', $message['user_name'] ) .
					'</a>';
				$delete_link = '';
				// Allow the owner of the status update and privileged users to
				// delete it
				if(
					$wgUser->getName() == $message['user_name'] ||
					$wgUser->isAllowed( 'delete-status-updates' )
				)
				{
					$delete_link = "<span class=\"user-board-red\">
							<a href=\"javascript:void(0);\" onclick=\"javascript:delete_message({$message['id']})\">" .
						wfMsg( 'userstatus-delete' ) . '</a>
					</span>';
				}

				$message_text = preg_replace_callback(
					'/(<a[^>]*>)(.*?)(<\/a>)/i',
					array( 'UserStatus', 'cutLinkText' ),
					$message['text']
				);

				$output .= "<div class=\"user-status-row\">
					<a href=\"{$user->getFullURL()}\">{$avatar->getAvatarURL()}</a>
					<a href=\"{$user->getFullURL()}\"><b>{$message['user_name']}</b></a> {$message_text}
					<span class=\"user-status-date\">" .
						wfMsg( 'userstatus-ago', UserStatus::getTimeAgo( $message['timestamp'] ) ) .
					'</span>
				</div>';
			}
		} else {
			$output .= '<p>' . wfMsg( 'userstatus-no-updates' ) . '</p>';
		}

		$output .= '</div>';

		$wgOut->addHTML( $output );
	}