Tpt has uploaded a new change for review. https://gerrit.wikimedia.org/r/113165
Change subject: Improve tags parser code ...................................................................... Improve tags parser code Removes a duplicate file Change-Id: Ib44682281a7fb2d473c07e7cec7852b4755b15d8 --- M includes/Parser/PagelistTagParser.php M includes/Parser/PagesTagParser.php M includes/Parser/ParserEntryPoint.php M includes/Parser/TagParser.php D includes/parser/ParserEntryPoint.php 5 files changed, 38 insertions(+), 61 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage refs/changes/65/113165/1 diff --git a/includes/Parser/PagelistTagParser.php b/includes/Parser/PagelistTagParser.php index a3939e3..1c9de85 100644 --- a/includes/Parser/PagelistTagParser.php +++ b/includes/Parser/PagelistTagParser.php @@ -17,17 +17,17 @@ */ public function render( $input, array $args ) { $title = $this->parser->getTitle(); - if ( !$title->inNamespace( ProofreadPage::getIndexNamespaceId() ) ) { + if ( !$title->inNamespace( $this->context->getIndexNamespaceId() ) ) { return ''; } $imageTitle = Title::makeTitleSafe( NS_IMAGE, $title->getText() ); if ( !$imageTitle ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_nosuch_file' ); } $image = wfFindFile( $imageTitle ); if ( !( $image && $image->isMultipage() && $image->pageCount() ) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_nosuch_file' ); } $return = ''; @@ -39,10 +39,10 @@ $to = array_key_exists( 'to', $args ) ? $args['to'] : $count; if( !is_numeric( $from ) || !is_numeric( $to ) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_number_expected' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_number_expected' ); } if( ( $from > $to ) || ( $from < 1 ) || ( $to < 1 ) || ( $to > $count ) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_invalid_interval' ); } for ( $i = $from; $i < $to + 1; $i++ ) { diff --git a/includes/Parser/PagesTagParser.php b/includes/Parser/PagesTagParser.php index cd6efff..a527f9b 100644 --- a/includes/Parser/PagesTagParser.php +++ b/includes/Parser/PagesTagParser.php @@ -18,7 +18,7 @@ * @see TagParser::render */ public function render( $input, array $args ) { - $pageNamespaceId = ProofreadPage::getPageNamespaceId(); + $pageNamespaceId = $this->context->getPageNamespaceId(); // abort if this is nested <pages> call if ( isset( $this->parser->proofreadRenderingPages ) && $this->parser->proofreadRenderingPages ) { @@ -37,7 +37,7 @@ $onlysection = array_key_exists( 'onlysection', $args ) ? $args['onlysection'] : null; // abort if the tag is on an index page - if ( $this->parser->getTitle()->inNamespace( ProofreadPage::getIndexNamespaceId() ) ) { + if ( $this->parser->getTitle()->inNamespace( $this->context->getIndexNamespaceId() ) ) { return ''; } // abort too if the tag is in the page namespace @@ -51,11 +51,11 @@ } if( !$index ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_index_expected' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_index_expected' ); } - $index_title = Title::makeTitleSafe( ProofreadPage::getIndexNamespaceId(), $index ); + $index_title = Title::makeTitleSafe( $this->context->getIndexNamespaceId(), $index ); if( !$index_title || !$index_title->exists() ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_nosuch_index' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_nosuch_index' ); } $indexPage = ProofreadIndexPage::newFromTitle( $index_title ); @@ -76,11 +76,11 @@ $imageTitle = Title::makeTitleSafe( NS_IMAGE, $index ); if ( !$imageTitle ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_nosuch_file' ); } $image = wfFindFile( $imageTitle ); if ( !( $image && $image->isMultipage() && $image->pageCount() ) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_nosuch_file' ); } $count = $image->pageCount(); @@ -88,7 +88,7 @@ $step = 1; } if( !is_numeric( $step ) || $step < 1 ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_number_expected' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_number_expected' ); } $pagenums = array(); @@ -97,7 +97,7 @@ if( $include ) { $list = $this->parseNumList( $include ); if( $list == null ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_invalid_interval' ); } $pagenums = $list; } @@ -111,10 +111,10 @@ $to = $count; } if( !is_numeric( $from ) || !is_numeric( $to ) || !is_numeric( $step ) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_number_expected' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_number_expected' ); } if( ($from > $to) || ($from < 1) || ($to < 1 ) || ($to > $count) ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_invalid_interval' ); } for( $i = $from; $i <= $to; $i++ ) { @@ -126,18 +126,18 @@ if( $exclude ) { $excluded = $this->parseNumList( $exclude ); if( $excluded == null ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_invalid_interval' ); } $pagenums = array_diff( $pagenums, $excluded ); } if( count($pagenums)/$step > 1000 ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_interval_too_large' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_interval_too_large' ); } ksort( $pagenums ); //we must sort the array even if the numerical keys are in a good order. if( reset( $pagenums ) > $count ) { - return '<strong class="error">' . wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() . '</strong>'; + return $this->formatError( 'proofreadpage_invalid_interval' ); } //Create the list of pages to translude. the step system start with the smaller pagenum diff --git a/includes/Parser/ParserEntryPoint.php b/includes/Parser/ParserEntryPoint.php index f6bf48c..c634b09 100644 --- a/includes/Parser/ParserEntryPoint.php +++ b/includes/Parser/ParserEntryPoint.php @@ -3,6 +3,7 @@ namespace ProofreadPage\Parser; use Parser; +use ProofreadPage\Context; /** * @licence GNU GPL v2+ @@ -20,7 +21,7 @@ * @return string */ public static function renderPagelistTag( $input, array $args, Parser $parser ) { - $tagParser = new PagelistTagParser( $parser ); + $tagParser = new PagelistTagParser( $parser, Context::getDefaultContext() ); return $tagParser->render( $input, $args ); } @@ -33,7 +34,7 @@ * @return string */ public static function renderPagesTag( $input, array $args, Parser $parser ) { - $tagParser = new PagesTagParser( $parser ); + $tagParser = new PagesTagParser( $parser, Context::getDefaultContext() ); return $tagParser->render( $input, $args ); } } diff --git a/includes/Parser/TagParser.php b/includes/Parser/TagParser.php index 67bfd99..e780709 100644 --- a/includes/Parser/TagParser.php +++ b/includes/Parser/TagParser.php @@ -3,6 +3,7 @@ namespace ProofreadPage\Parser; use Parser; +use ProofreadPage\Context; /** * @licence GNU GPL v2+ @@ -17,10 +18,16 @@ protected $parser; /** + * @var Context + */ + protected $context; + + /** * @param Parser $parser the current parser */ - public function __construct( Parser $parser ) { + public function __construct( Parser $parser, Context $context ) { $this->parser = $parser; + $this->context = $context; } /** @@ -31,4 +38,12 @@ * @return string */ public abstract function render( $input, array $args ); + + /** + * @param string $errorMsg + * @return string + */ + protected function formatError( $errorMsg ) { + return '<strong class="error">' . wfMessage( $errorMsg )->inContentLanguage()->escaped() . '</strong>'; + } } diff --git a/includes/parser/ParserEntryPoint.php b/includes/parser/ParserEntryPoint.php deleted file mode 100644 index f6bf48c..0000000 --- a/includes/parser/ParserEntryPoint.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace ProofreadPage\Parser; - -use Parser; - -/** - * @licence GNU GPL v2+ - * - * Entry point for parser hooks - */ -class ParserEntryPoint { - - /** - * Parser hook for <pagelist> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @param Parser $parser the current parser - * @return string - */ - public static function renderPagelistTag( $input, array $args, Parser $parser ) { - $tagParser = new PagelistTagParser( $parser ); - return $tagParser->render( $input, $args ); - } - - /** - * Parser hook for <pages> tag - * - * @param string $input the content between opening and closing tags - * @param array $args tags arguments - * @param Parser $parser the current parser - * @return string - */ - public static function renderPagesTag( $input, array $args, Parser $parser ) { - $tagParser = new PagesTagParser( $parser ); - return $tagParser->render( $input, $args ); - } -} -- To view, visit https://gerrit.wikimedia.org/r/113165 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib44682281a7fb2d473c07e7cec7852b4755b15d8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ProofreadPage Gerrit-Branch: master Gerrit-Owner: Tpt <thoma...@hotmail.fr> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits