<?php
/**
 * Comments of the Day parser hook -- shows the five newest comments that have
 * been sent within the last 24 hours.
 *
 * @file
 * @ingroup Extensions
 * @date 5 August 2011
 */
if ( !defined( 'MEDIAWIKI' ) ) {
	die();
}

$wgHooks['ParserFirstCallInit'][] = 'wfCommentsListAll';

/**
 * Register the new <commentsAll /> parser hook with the Parser.
 *
 * @param $parser Parser: instance of Parser (not necessarily $wgParser)
 * @return Boolean: true
 */
function wfCommentsListAll( &$parser ) {
	$parser->setHook( 'commentsAll', 'getcommentsAll' );
	return true;
}

/**
 * Get comments all -- 
 *
 * @return String: HTML
 */
function getcommentsAll( $input, $args, $parser ) {
	global $wgMemc, $wgUploadPath;

	$oneDay = 60 * 60 * 24 * 50000;

	// Try memcached first
	$key = wfMemcKey( 'comments-all', 'standalone-hook' );
	$data = $wgMemc->get( $key );

	if( $data ) { // success, got it from memcached!
		$commentsAll = $data;
		//$dbr = wfGetDB( DB_SLAVE );
	} elseif ( !$data || $args['nocache'] ) { // just query the DB
		$dbr = wfGetDB( DB_SLAVE );

		$res = $dbr->select(
			array( 'Comments', 'page' ),
			array(
				'Comment_Username', 'comment_ip', 'comment_text',
				'comment_date', 'Comment_user_id', 'CommentID',
				'IFNULL(Comment_Plus_Count - Comment_Minus_Count,0) AS Comment_Score',
				'Comment_Plus_Count AS CommentVotePlus',
				'Comment_Minus_Count AS CommentVoteMinus',
				'Comment_Parent_ID', 'page_title', 'page_namespace'
			),
			array(
				'comment_page_id = page_id',
				'UNIX_TIMESTAMP(comment_date) > ' . ( time() - ( $oneDay ) )
			),
			__METHOD__,
			array( 'ORDER BY' => '(comment_date) DESC')
		);

		$commentsAll = array();
		foreach ( $res as $row ) {
			$commentsAll[] = array(
				'username' => $row->Comment_Username,
				'userid' => $row->Comment_user_id,
				'score' => $row->CommentVotePlus,
				'text' => $row->comment_text,
				'id' => $row->CommentID,
				'pagens' => $row->page_namespace,
				'pagetitle' => $row->page_title,
				'date' => $row->comment_date
			);
		}

		$wgMemc->set( $key, $commentsAll, $oneDay );
	}

	$comments = '';

	foreach ( $commentsAll as $commentsAllTemp ) {
		$title2 = Title::makeTitle(
			$commentsAllTemp['pagens'],
			$commentsAllTemp['pagetitle']
		);

		if( $commentsAllTemp['userid'] != 0 ) {
			$title = Title::makeTitle( NS_USER, $commentsAllTemp['username'] );
			$commentPoster_Display = $commentsAllTemp['username'];
			$commentPoster = '<a href="' . $title->getFullURL() .
				'" title="' . $title->getText() . '" rel="nofollow">' .
				$commentsAllTemp['username'] . '</a>';
			//avatar = new wAvatar( $commentsAllTemp['userid'], 's' );
			$commentIcon = 'extensions/Comments/images/default_s.gif';
		} else {
			$title = Title::makeTitle( NS_USER, $commentsAllTemp['username'] );
			$commentPoster_Display = $commentsAllTemp['username'];
			$commentPoster = '<a href="' . $title->getFullURL() .
				'" title="' . $title->getText() . '" rel="nofollow">' .
				$commentsAllTemp['username'] . '</a>';
			//avatar = new wAvatar( $commentsAllTemp['userid'], 's' );
			$commentIcon = 'extensions/Comments/resources/images/default_s.gif';

            if ( filter_var($commentPoster_Display, FILTER_VALIDATE_IP) !== false ){
                // Wert ist eine IP-Adresse
                $commentPoster = "Anonym";
            }
			/*
			$commentPoster_Display = wfMsg( 'comment-anon-name' );
			$commentPoster = wfMsg( 'comment-anon-name' );
			$commentIcon = '{$IP}/extensions/Comments/images/default_s.gif';
			*/
		}

		// $comment_text = substr( $commentsAllTemp['text'], 0,  550 - strlen( $commentPoster_Display ) );
		$comment_text = $commentsAllTemp['text'];
		if( $comment_text != $commentsAllTemp['text'] ) {
			$comment_text .= wfMsg( 'ellipsis' );
		}
		

		$comments .= '<div style="width:90%;">';
		$comments .= '<div style="float:left;width:220px;">';
		$comments .= '<div style="float:left;width:20px;"><img src='. $commentIcon . ' alt="" align="middle" border="0"/></div>';
		$comments .= '<div style="float:left;width:100px;"><span style="font-size: 70%">' . $commentsAllTemp['date'] . '</span> - </div>';
		$comments .= '<div style="float:left;width:100px;"><span class="cod-poster">' . $commentPoster . ': </span></div></div>';
		$comments .= '<div style="float:left;width:60%;">';
		$comments .= '<div style="float:left;"><span class="cod-comment"><a href="' . 
						$title2->getFullURL() . '#comment-' . $commentsAllTemp['id'] . 
						'" title="' . $title2->getText() . '">' . '<span>' . $comment_text . '</span></a></div>';
		$comments .= '</div><div style="clear:both;"></div></div><hr>';
		
//		$comments .= '<div class="cod">';
//		$sign = '';
//		/*if ( $commentsAllTemp['score'] > 0 ) {
//			$sign = '+';
//		} elseif ( $commentsAllTemp['score'] < 0 ) {
//			$sign = '-'; // this *really* shouldn't be happening...
//		}*/
//		$comments .= '<div><span class="cod-score">' . $sign .
//			'</span> <img src='. $commentIcon .
//			'" alt="" align="middle" style="margin-bottom:8px; margin-right: 10px;" border="0"/>
//			<span class="cod-poster"><span style="font-size: 70%">' . $commentsAllTemp['date'] . '</span> - ' . $commentPoster . ': </span>';
//		$comments .= '<span class="cod-comment"><a href="' .
//			$title2->getFullURL() . '#comment-' . $commentsAllTemp['id'] .
//			'" title="' . $title2->getText() . '">' . '<span>' . $comment_text . '</span></div>' .
//			'</a></span>';
//		$comments .= '</div>';
	}

	$output = '';
	if ( !empty( $comments ) ) {
		$output .= $comments;
	} else {
		$output .= wfMsg( 'comments-no-comments-of-day' );
	}

	return $output;
}