<commit_before><?php

namespace MediaWiki\Extension\Notifications\Special;

use EchoDataOutputFormatter;
use EchoSeenTime;
use Html;
use MediaWiki\Extension\Notifications\OOUI\LabelIconWidget;
use MWEchoEventLogging;
use OOUI;
use SpecialPage;

class SpecialNotifications extends SpecialPage {

	/**
	 * Number of notification records to display per page/load
	 */
	private const DISPLAY_NUM = 20;

	public function __construct() {
		parent::__construct( 'Notifications' );
	}

	/**
	 * @param string|null $par
	 */
	public function execute( $par ) {
		$this->setHeaders();

		$out = $this->getOutput();
		$out->setPageTitle( $this->msg( 'echo-specialpage' )->text() );

		$this->addHelpLink( 'Help:Notifications/Special:Notifications' );

		$out->addJsConfigVars( 'wgNotificationsSpecialPageLinks', [
			'preferences' => SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-echo' )->getLinkURL(),
		] );

		$user = $this->getUser();
		if ( !$user->isRegistered() ) {
			// Redirect to login page and inform user of the need to login
			$this->requireLogin( 'echo-notification-loginrequired' );
			return;
		}

		$out->addSubtitle( $this->buildSubtitle() );

		$out->enableOOUI();

		$pager = new NotificationPager( $this->getContext() );
		$pager->setOffset( $this->getRequest()->getVal( 'offset' ) );
		$pager->setLimit( $this->getRequest()->getInt( 'limit', self::DISPLAY_NUM ) );
		$notifications = $pager->getNotifications();

		$noJSDiv = new OOUI\Tag();
		$noJSDiv->addClasses( [ 'mw-echo-special-nojs' ] );

		// If there are no notifications, display a message saying so
		if ( !$notifications ) {
			// Wrap this with nojs so it is still hidden if JS is loading
			$noJSDiv->appendContent(
				new OOUI\LabelWidget( [ 'label' => $this->msg( 'echo-none' )->text() ] )
			);
			$out->addHTML( $noJSDiv );
			$out->addModules( [ 'ext.echo.special' ] );
			return;
		}

		$notif = [];
		foreach ( $notifications as $notification ) {
			$output = EchoDataOutputFormatter::formatOutput( $notification, 'special', $user, $this->getLanguage() );
			if ( $output ) {
				$notif[] = $output;
			}
		}

		// Add the notifications to the page (interspersed with date headers)
		$dateHeader = '';
		$anyUnread = false;
		$echoSeenTime = EchoSeenTime::newFromUser( $user );
		$seenTime = $echoSeenTime->getTime();
		$notifArray = [];
		foreach ( $notif as $row ) {
			if ( !$row['*'] ) {
				continue;
			}

			$classes = [ 'mw-echo-notification' ];

			if ( $seenTime !== null && $row['timestamp']['mw'] > $seenTime ) {
				$classes[] = 'mw-echo-notification-unseen';
			}

			// Output the date header if it has not been displayed
			if ( $dateHeader !== $row['timestamp']['date'] ) {
				$dateHeader = $row['timestamp']['date'];
			}
			$notifArray[ $dateHeader ] = [
				'unread' => [],
				'notices' => []
			];

			// Collect unread IDs
			if ( !isset( $row['read'] ) ) {
				$classes[] = 'mw-echo-notification-unread';
				$anyUnread = true;
				$notifArray[ $dateHeader ][ 'unread' ][] = $row['id'];
			}

			$li = new OOUI\Tag( 'li' );
			$li
				->addClasses( $classes )
				->setAttributes( [
					'data-notification-category' => $row['category'],
					'data-notification-event' => $row['id'],
					'data-notification-type' => $row['type']
				] )
				->appendContent( new OOUI\HtmlSnippet( $row['*'] ) );

			// Store
			$notifArray[ $dateHeader ][ 'notices' ][] = $li;
		}

		$markAllAsReadFormWrapper = '';
		// Ensure there are some unread notifications
		if ( $anyUnread ) {
			$markReadSpecialPage = new SpecialNotificationsMarkRead();
			$markReadSpecialPage->setContext( $this->getContext() );

			$markAllAsReadText = $this->msg( 'echo-mark-all-as-read' )->text();
			$markAllAsReadLabelIcon = new LabelIconWidget( [
				'label' => $markAllAsReadText,
				'icon' => 'checkAll',
			] );

			$markAllAsReadForm = $markReadSpecialPage->getMinimalForm(
				[ 'ALL' ],
				$markAllAsReadText,
				true,
				$markAllAsReadLabelIcon->toString()
			);

			// First submission attempt
			$formHtml = $markAllAsReadForm->prepareForm()->getHTML( false );

			$markAllAsReadFormWrapper = new OOUI\Tag();
			$markAllAsReadFormWrapper
				->addClasses( [ 'mw-echo-special-markAllReadButton' ] )
				->appendContent( new OOUI\HtmlSnippet( $formHtml ) );
		}

		// Build the list
		$notices = new OOUI\Tag( 'ul' );
		$notices->addClasses( [ 'mw-echo-special-notifications' ] );

		$markReadSpecialPage = new SpecialNotificationsMarkRead();
		$markReadSpecialPage->setContext( $this->getContext() );
		foreach ( $notifArray as $section => $data ) {
			$heading = ( new OOUI\Tag( 'li' ) )->addClasses( [ 'mw-echo-date-section' ] );

			$dateTitle = new OOUI\LabelWidget( [
				'classes' => [ 'mw-echo-date-section-text' ],
				'label' => $section
			] );

			$heading->appendContent( $dateTitle );

			// Mark all read button
			if ( $data[ 'unread' ] !== [] ) {
				// tell the UI to show 'unread' notifications only (instead of 'all')
				$out->addJsConfigVars( 'wgEchoReadState', 'unread' );

				$markReadSectionText = $this->msg( 'echo-specialpage-section-markread' )->text();
				$markAsReadLabelIcon = new LabelIconWidget( [
					'label' => $markReadSectionText,
					'icon' => 'checkAll',
				] );

				// There are unread notices. Add the 'mark section as read' button
				$markSectionAsReadForm = $markReadSpecialPage->getMinimalForm(
					$data[ 'unread' ],
					$markReadSectionText,
					true,
					$markAsReadLabelIcon->toString()
				);

				// First submission attempt<commit_msg>Fix no-JS Special:Notifications only displaying one notification per day<commit_after><?php

namespace MediaWiki\Extension\Notifications\Special;

use EchoDataOutputFormatter;
use EchoSeenTime;
use Html;
use MediaWiki\Extension\Notifications\OOUI\LabelIconWidget;
use MWEchoEventLogging;
use OOUI;
use SpecialPage;

class SpecialNotifications extends SpecialPage {

	/**
	 * Number of notification records to display per page/load
	 */
	private const DISPLAY_NUM = 20;

	public function __construct() {
		parent::__construct( 'Notifications' );
	}

	/**
	 * @param string|null $par
	 */
	public function execute( $par ) {
		$this->setHeaders();

		$out = $this->getOutput();
		$out->setPageTitle( $this->msg( 'echo-specialpage' )->text() );

		$this->addHelpLink( 'Help:Notifications/Special:Notifications' );

		$out->addJsConfigVars( 'wgNotificationsSpecialPageLinks', [
			'preferences' => SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-echo' )->getLinkURL(),
		] );

		$user = $this->getUser();
		if ( !$user->isRegistered() ) {
			// Redirect to login page and inform user of the need to login
			$this->requireLogin( 'echo-notification-loginrequired' );
			return;
		}

		$out->addSubtitle( $this->buildSubtitle() );

		$out->enableOOUI();

		$pager = new NotificationPager( $this->getContext() );
		$pager->setOffset( $this->getRequest()->getVal( 'offset' ) );
		$pager->setLimit( $this->getRequest()->getInt( 'limit', self::DISPLAY_NUM ) );
		$notifications = $pager->getNotifications();

		$noJSDiv = new OOUI\Tag();
		$noJSDiv->addClasses( [ 'mw-echo-special-nojs' ] );

		// If there are no notifications, display a message saying so
		if ( !$notifications ) {
			// Wrap this with nojs so it is still hidden if JS is loading
			$noJSDiv->appendContent(
				new OOUI\LabelWidget( [ 'label' => $this->msg( 'echo-none' )->text() ] )
			);
			$out->addHTML( $noJSDiv );
			$out->addModules( [ 'ext.echo.special' ] );
			return;
		}

		$notif = [];
		foreach ( $notifications as $notification ) {
			$output = EchoDataOutputFormatter::formatOutput( $notification, 'special', $user, $this->getLanguage() );
			if ( $output ) {
				$notif[] = $output;
			}
		}

		// Add the notifications to the page (interspersed with date headers)
		$dateHeader = '';
		$anyUnread = false;
		$echoSeenTime = EchoSeenTime::newFromUser( $user );
		$seenTime = $echoSeenTime->getTime();
		$notifArray = [];
		foreach ( $notif as $row ) {
			if ( !$row['*'] ) {
				continue;
			}

			$classes = [ 'mw-echo-notification' ];

			if ( $seenTime !== null && $row['timestamp']['mw'] > $seenTime ) {
				$classes[] = 'mw-echo-notification-unseen';
			}

			// Output the date header if it has not been displayed
			if ( $dateHeader !== $row['timestamp']['date'] ) {
				$dateHeader = $row['timestamp']['date'];
			}
			// Collect unread IDs
			if ( !isset( $row['read'] ) ) {
				$classes[] = 'mw-echo-notification-unread';
				$anyUnread = true;
				$notifArray[ $dateHeader ][ 'unread' ][] = $row['id'];
			}

			$li = new OOUI\Tag( 'li' );
			$li
				->addClasses( $classes )
				->setAttributes( [
					'data-notification-category' => $row['category'],
					'data-notification-event' => $row['id'],
					'data-notification-type' => $row['type']
				] )
				->appendContent( new OOUI\HtmlSnippet( $row['*'] ) );

			// Store
			$notifArray[ $dateHeader ][ 'notices' ][] = $li;
		}

		$markAllAsReadFormWrapper = '';
		// Ensure there are some unread notifications
		if ( $anyUnread ) {
			$markReadSpecialPage = new SpecialNotificationsMarkRead();
			$markReadSpecialPage->setContext( $this->getContext() );

			$markAllAsReadText = $this->msg( 'echo-mark-all-as-read' )->text();
			$markAllAsReadLabelIcon = new LabelIconWidget( [
				'label' => $markAllAsReadText,
				'icon' => 'checkAll',
			] );

			$markAllAsReadForm = $markReadSpecialPage->getMinimalForm(
				[ 'ALL' ],
				$markAllAsReadText,
				true,
				$markAllAsReadLabelIcon->toString()
			);

			// First submission attempt
			$formHtml = $markAllAsReadForm->prepareForm()->getHTML( false );

			$markAllAsReadFormWrapper = new OOUI\Tag();
			$markAllAsReadFormWrapper
				->addClasses( [ 'mw-echo-special-markAllReadButton' ] )
				->appendContent( new OOUI\HtmlSnippet( $formHtml ) );
		}

		// Build the list
		$notices = new OOUI\Tag( 'ul' );
		$notices->addClasses( [ 'mw-echo-special-notifications' ] );

		$markReadSpecialPage = new SpecialNotificationsMarkRead();
		$markReadSpecialPage->setContext( $this->getContext() );
		foreach ( $notifArray as $section => $data ) {
			$heading = ( new OOUI\Tag( 'li' ) )->addClasses( [ 'mw-echo-date-section' ] );

			$dateTitle = new OOUI\LabelWidget( [
				'classes' => [ 'mw-echo-date-section-text' ],
				'label' => $section
			] );

			$heading->appendContent( $dateTitle );

			// Mark all read button
			if ( isset( $data[ 'unread' ] ) ) {
				// tell the UI to show 'unread' notifications only (instead of 'all')
				$out->addJsConfigVars( 'wgEchoReadState', 'unread' );

				$markReadSectionText = $this->msg( 'echo-specialpage-section-markread' )->text();
				$markAsReadLabelIcon = new LabelIconWidget( [
					'label' => $markReadSectionText,
					'icon' => 'checkAll',
				] );

				// There are unread notices. Add the 'mark section as read' button
				$markSectionAsReadForm = $markReadSpecialPage->getMinimalForm(
					$data[ 'unread' ],
					$markReadSectionText,
					true,
					$markAsReadLabelIcon->toString()
				);

				// First submission attempt
{'fraction_non_alphanumeric': 0.17088262056414924, 'fraction_numerical': 0.0003639672429481347, 'mean_word_length': 3.6434305027460923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5095573', 'n_tokens_mistral': 4311, 'n_tokens_neox': 3675, 'n_words': 762}