Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
60.00% covered (warning)
60.00%
36 / 60
EntitySchema\Presentation\AutocommentFormatter
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
29.40
60.00% covered (warning)
60.00%
36 / 60
 formatAutocomment
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
9 / 9
 parseAutocomment
0.00% covered (danger)
0.00%
0 / 1
23.61
52.94% covered (warning)
52.94%
27 / 51
<?php
namespace EntitySchema\Presentation;
use RequestContext;
use User;
use EntitySchema\DataAccess\MediaWikiRevisionSchemaInserter;
use EntitySchema\DataAccess\MediaWikiRevisionSchemaUpdater;
use Language;
/**
 * @license GPL-2.0-or-later
 */
class AutocommentFormatter {
    /**
     * @param bool   $pre  Whether any text appears in the summary before this autocomment.
     *                     If true, we insert the autocomment-prefix before the autocomment
     *                     (outside the two <span>s) to separate it from that.
     * @param string $auto The autocomment content (without the surrounding comment marks)
     * @param bool   $post Whether any text appears in the summary after this autocomment.
     *                     If true, we append the colon-separator after the autocomment
     *                     (still inside the two <span>s) to separate it from that.
     *
     * @return string|null
     */
    public function formatAutocomment( $pre, $auto, $post ) {
        $comment = $this->parseAutocomment( $auto );
        if ( $comment === null ) {
            return null;
        }
        if ( $post ) {
            $comment .= wfMessage( 'colon-separator' )->escaped();
        }
        $comment = '<span dir="auto"><span class="autocomment">' . $comment . '</span></span>';
        if ( $pre ) {
            $comment = wfMessage( 'autocomment-prefix' )->escaped() . $comment;
        }
        return $comment;
    }
    private function parseAutocomment( $auto ) {
        $commentParts = explode( ':', $auto, 2 );
        $context = RequestContext::getMain();
        switch ( $commentParts[0] ) {
            case MediaWikiRevisionSchemaInserter::AUTOCOMMENT_NEWSCHEMA:
                $comment = wfMessage( 'entityschema-summary-newschema-nolabel' );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UPDATED_SCHEMATEXT:
                $comment = wfMessage( 'entityschema-summary-update-schema-text' );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UPDATED_NAMEBADGE:
                $languageName = Language::fetchLanguageName(
                    $commentParts[1],
                    $context->getLanguage()->getCode()
                );
                $comment = wfMessage( 'entityschema-summary-update-schema-namebadge' )
                    ->params( $languageName );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UPDATED_LABEL:
                $languageName = Language::fetchLanguageName(
                    $commentParts[1],
                    $context->getLanguage()->getCode()
                );
                $comment = wfMessage( 'entityschema-summary-update-schema-label' )
                    ->params( $languageName );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UPDATED_DESCRIPTION:
                $languageName = Language::fetchLanguageName(
                    $commentParts[1],
                    $context->getLanguage()->getCode()
                );
                $comment = wfMessage( 'entityschema-summary-update-schema-description' )
                    ->params( $languageName );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UPDATED_ALIASES:
                $languageName = Language::fetchLanguageName(
                    $commentParts[1],
                    $context->getLanguage()->getCode()
                );
                $comment = wfMessage( 'entityschema-summary-update-schema-aliases' )
                    ->params( $languageName );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_RESTORE:
                list( $revId, $username ) = explode( ':', $commentParts[1], 2 );
                $user = User::newFromName( $username ) ?: $username;
                $comment = wfMessage( 'entityschema-summary-restore-autocomment' )
                    ->params( $revId, $user );
                break;
            case MediaWikiRevisionSchemaUpdater::AUTOCOMMENT_UNDO:
                list( $revId, $username ) = explode( ':', $commentParts[1], 2 );
                $user = User::newFromName( $username ) ?: $username;
                $comment = wfMessage( 'entityschema-summary-undo-autocomment' )
                    ->params( $revId, $user );
                break;
            default:
                return null;
        }
        return $comment->escaped();
    }
}