Tpt has uploaded a new change for review.
https://gerrit.wikimedia.org/r/97295
Change subject: Explode ProofreadPageRenderer into smaller not static classes
......................................................................
Explode ProofreadPageRenderer into smaller not static classes
Change-Id: I92819deffa9c9579ae26e58fe67c737dc1c888f8
---
M ProofreadPage.body.php
M ProofreadPage.php
A includes/parser/ProofreadPagelistTagParser.php
R includes/parser/ProofreadPagesTagParser.php
A includes/parser/ProofreadParser.php
A includes/parser/ProofreadTagParser.php
6 files changed, 215 insertions(+), 95 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage
refs/changes/95/97295/1
diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 1b72a4e..9554bdd 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -139,9 +139,9 @@
* @param Parser $parser
* @return boolean hook return value
*/
- public static function onParserFirstCallInit( $parser ) {
- $parser->setHook( 'pagelist', array( 'ProofreadPageRenderer',
'renderPageList' ) );
- $parser->setHook( 'pages', array( 'ProofreadPageRenderer',
'renderPages' ) );
+ public static function onParserFirstCallInit( Parser $parser ) {
+ $parser->setHook( 'pagelist', array( 'ProofreadParser',
'renderPagelistTag' ) );
+ $parser->setHook( 'pages', array( 'ProofreadParser',
'renderPagesTag' ) );
return true;
}
diff --git a/ProofreadPage.php b/ProofreadPage.php
index f32e9a1..0657619 100644
--- a/ProofreadPage.php
+++ b/ProofreadPage.php
@@ -44,7 +44,6 @@
$wgAutoloadClasses['ProofreadPage'] = $dir . 'ProofreadPage.body.php';
$wgAutoloadClasses['ProofreadPageInit'] = $dir .
'includes/ProofreadPageInit.php';
-$wgAutoloadClasses['ProofreadPageRenderer'] = $dir .
'includes/ProofreadPageRenderer.php';
$wgAutoloadClasses['EditProofreadIndexPage'] = $dir .
'includes/index/EditProofreadIndexPage.php';
$wgAutoloadClasses['ProofreadIndexEntry'] = $dir .
'includes/index/ProofreadIndexEntry.php';
@@ -62,6 +61,11 @@
$wgAutoloadClasses['ProofreadPageSubmitAction'] = $dir .
'includes/page/ProofreadPageSubmitAction.php';
$wgAutoloadClasses['ProofreadPageViewAction'] = $dir .
'includes/page/ProofreadPageViewAction.php';
+$wgAutoloadClasses['ProofreadParser'] = $dir .
'includes/parser/ProofreadParser.php';
+$wgAutoloadClasses['ProofreadTagParser'] = $dir .
'includes/parser/ProofreadTagParser.php';
+$wgAutoloadClasses['ProofreadPagelistTagParser'] = $dir .
'includes/parser/ProofreadPagelistTagParser.php';
+$wgAutoloadClasses['ProofreadPagesTagParser'] = $dir .
'includes/parser/ProofreadPagesTagParser.php';
+
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
'name' => 'ProofreadPage',
diff --git a/includes/parser/ProofreadPagelistTagParser.php
b/includes/parser/ProofreadPagelistTagParser.php
new file mode 100644
index 0000000..577de45
--- /dev/null
+++ b/includes/parser/ProofreadPagelistTagParser.php
@@ -0,0 +1,88 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+/**
+ * Parser for the <pagelist> tag
+ */
+class ProofreadPagelistTagParser extends ProofreadTagParser {
+
+ /**
+ * @see ProofreadParser::render
+ */
+ public function render( $input, array $args ) {
+ $title = $this->parser->getTitle();
+ if ( !$title->inNamespace( ProofreadPage::getIndexNamespaceId()
) ) {
+ return '';
+ }
+ $imageTitle = Title::makeTitleSafe( NS_IMAGE, $title->getText()
);
+ if ( !$imageTitle ) {
+ return '<strong class="error">' . wfMessage(
'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>';
+ }
+
+ $image = wfFindFile( $imageTitle );
+ if ( !( $image && $image->isMultipage() && $image->pageCount()
) ) {
+ return '<strong class="error">' . wfMessage(
'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>';
+ }
+
+ $return = '';
+
+ $name = $imageTitle->getDBkey();
+ $count = $image->pageCount();
+
+ $from = array_key_exists( 'from', $args ) ? $args['from'] : 1;
+ $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>';
+ }
+ if( ( $from > $to ) || ( $from < 1 ) || ( $to < 1 ) || ( $to >
$count ) ) {
+ return '<strong class="error">' . wfMessage(
'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() .
'</strong>';
+ }
+
+ for ( $i = $from; $i < $to + 1; $i++ ) {
+ list( $view, $links, $mode ) =
ProofreadPage::pageNumber( $i, $args );
+
+ if ( $mode == 'highroman' || $mode == 'roman' ) {
+ $view = ' ' . $view;
+ }
+
+ $n = strlen( $count ) - mb_strlen( $view );
+ $language = $this->parser->getTargetLanguage();
+ if ( $n && ( $mode == 'normal' || $mode == 'empty' ) ) {
+ $txt = '<span style="visibility:hidden;">';
+ $pad = $language->formatNum( 0, true );
+ for ( $j = 0; $j < $n; $j++ ) {
+ $txt = $txt . $pad;
+ }
+ $view = $txt . '</span>' . $view;
+ }
+ $title = ProofreadPage::getPageTitle( $name, $i );
+
+ if ( !$links || !$title ) {
+ $return .= $view . ' ';
+ } else {
+ $return .= '[[' . $title->getPrefixedText() .
'|' . $view . ']] ';
+ }
+ }
+ $return = $this->parser->recursiveTagParse( $return );
+ return $return;
+ }
+}
\ No newline at end of file
diff --git a/includes/ProofreadPageRenderer.php
b/includes/parser/ProofreadPagesTagParser.php
similarity index 75%
rename from includes/ProofreadPageRenderer.php
rename to includes/parser/ProofreadPagesTagParser.php
index 690c8bd..bd756fc 100644
--- a/includes/ProofreadPageRenderer.php
+++ b/includes/parser/ProofreadPagesTagParser.php
@@ -19,88 +19,19 @@
* @ingroup ProofreadPage
*/
-class ProofreadPageRenderer {
+/**
+ * Parser for the <pages> tag
+ */
+class ProofreadPagesTagParser extends ProofreadTagParser {
/**
- * Parser hook for index pages
- * Display a list of coloured links to pages
- * @param $input
- * @param $args array
- * @param $parser Parser
- * @return string
+ * @see ProofreadParser::render
*/
- public static function renderPageList( $input, $args, $parser ) {
- $title = $parser->getTitle();
- if ( !$title->inNamespace( ProofreadPage::getIndexNamespaceId()
) ) {
- return '';
- }
- $imageTitle = Title::makeTitleSafe( NS_IMAGE, $title->getText()
);
- if ( !$imageTitle ) {
- return '<strong class="error">' . wfMessage(
'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>';
- }
-
- $image = wfFindFile( $imageTitle );
- if ( !( $image && $image->isMultipage() && $image->pageCount()
) ) {
- return '<strong class="error">' . wfMessage(
'proofreadpage_nosuch_file' )->inContentLanguage()->escaped() . '</strong>';
- }
-
- $return = '';
-
- $name = $imageTitle->getDBkey();
- $count = $image->pageCount();
-
- $from = array_key_exists( 'from', $args ) ? $args['from'] : 1;
- $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>';
- }
- if( ( $from > $to ) || ( $from < 1 ) || ( $to < 1 ) || ( $to >
$count ) ) {
- return '<strong class="error">' . wfMessage(
'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() .
'</strong>';
- }
-
- for ( $i = $from; $i < $to + 1; $i++ ) {
- list( $view, $links, $mode ) =
ProofreadPage::pageNumber( $i, $args );
-
- if ( $mode == 'highroman' || $mode == 'roman' ) {
- $view = ' ' . $view;
- }
-
- $n = strlen( $count ) - mb_strlen( $view );
- $language = $parser->getTargetLanguage();
- if ( $n && ( $mode == 'normal' || $mode == 'empty' ) ) {
- $txt = '<span style="visibility:hidden;">';
- $pad = $language->formatNum( 0, true );
- for ( $j = 0; $j < $n; $j++ ) {
- $txt = $txt . $pad;
- }
- $view = $txt . '</span>' . $view;
- }
- $title = ProofreadPage::getPageTitle( $name, $i );
-
- if ( !$links || !$title ) {
- $return .= $view . ' ';
- } else {
- $return .= '[[' . $title->getPrefixedText() .
'|' . $view . ']] ';
- }
- }
- $return = $parser->recursiveTagParse( $return );
- return $return;
- }
-
- /**
- * Parser hook that includes a list of pages.
- * parameters : index, from, to, header
- * @param $input
- * @param $args array
- * @param $parser Parser
- * @return string
- */
- public static function renderPages( $input, $args, $parser ) {
+ public function render( $input, array $args ) {
$pageNamespaceId = ProofreadPage::getPageNamespaceId();
// abort if this is nested <pages> call
- if ( isset( $parser->proofreadRenderingPages ) &&
$parser->proofreadRenderingPages ) {
+ if ( isset( $this->parser->proofreadRenderingPages ) &&
$this->parser->proofreadRenderingPages ) {
return '';
}
@@ -116,11 +47,11 @@
$onlysection = array_key_exists( 'onlysection', $args ) ?
$args['onlysection'] : null;
// abort if the tag is on an index page
- if ( $parser->getTitle()->inNamespace(
ProofreadPage::getIndexNamespaceId() ) ) {
+ if ( $this->parser->getTitle()->inNamespace(
ProofreadPage::getIndexNamespaceId() ) ) {
return '';
}
// abort too if the tag is in the page namespace
- if ( $parser->getTitle()->inNamespace( $pageNamespaceId ) ) {
+ if ( $this->parser->getTitle()->inNamespace( $pageNamespaceId )
) {
return '';
}
// ignore fromsection and tosection arguments if onlysection is
specified
@@ -138,10 +69,10 @@
}
$indexPage = ProofreadIndexPage::newFromTitle( $index_title );
- $parser->getOutput()->addTemplate( $index_title,
$index_title->getArticleID(), $index_title->getLatestRevID() );
+ $this->parser->getOutput()->addTemplate( $index_title,
$index_title->getArticleID(), $index_title->getLatestRevID() );
$out = '';
- $language = $parser->getTargetLanguage();
+ $language = $this->parser->getTargetLanguage();
list( $links, $params ) = $indexPage->getPages();
@@ -174,7 +105,7 @@
//add page selected with $include in pagenums
if( $include ) {
- $list = self::parseNumList( $include );
+ $list = $this->parseNumList( $include );
if( $list == null ) {
return '<strong class="error">'
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped()
. '</strong>';
}
@@ -203,7 +134,7 @@
//remove excluded pages form $pagenums
if( $exclude ) {
- $excluded = self::parseNumList(
$exclude );
+ $excluded = $this->parseNumList(
$exclude );
if( $excluded == null ) {
return '<strong class="error">'
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped()
. '</strong>';
}
@@ -317,7 +248,7 @@
$firstpage = $links[0][0];
}
if ( $firstpage !== null ) {
- $parser->getOutput()->addTemplate(
+ $this->parser->getOutput()->addTemplate(
$firstpage,
$firstpage->getArticleID(),
$firstpage->getLatestRevID()
@@ -327,10 +258,10 @@
if( $header ) {
if( $header == 'toc') {
- $parser->getOutput()->is_toc = true;
+ $this->parser->getOutput()->is_toc = true;
}
$indexLinks = $indexPage->getLinksToMainNamespace();
- $pageTitle = $parser->getTitle();
+ $pageTitle = $this->parser->getTitle();
$h_out = '{{:MediaWiki:Proofreadpage_header_template';
$h_out .= "|value=$header";
// find next and previous pages in list
@@ -386,20 +317,20 @@
// wrap the output in a div, to prevent the parser from
inserting pararaphs
$out = "<div>\n$out\n</div>";
- $parser->proofreadRenderingPages = true;
- $out = $parser->recursiveTagParse( $out );
- $parser->proofreadRenderingPages = false;
+ $this->parser->proofreadRenderingPages = true;
+ $out = $this->parser->recursiveTagParse( $out );
+ $this->parser->proofreadRenderingPages = false;
return $out;
}
/**
* Parse a comma-separated list of pages. A dash indicates an interval
of pages
* example: 1-10,23,38
- * Return an array of pages, or null if the input does not comply to
the syntax
+ *
* @param $input string
- * @return array|null
+ * @return array|null an array of pages, or null if the input does not
comply to the syntax
*/
- public static function parseNumList($input) {
+ public function parseNumList($input) {
$input = str_replace(array(' ', '\t', '\n'), '', $input);
$list = explode( ',', $input );
$nums = array();
diff --git a/includes/parser/ProofreadParser.php
b/includes/parser/ProofreadParser.php
new file mode 100644
index 0000000..b156e33
--- /dev/null
+++ b/includes/parser/ProofreadParser.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+class ProofreadParser {
+
+ /**
+ * Parser hook for <pagelist> tag
+ *
+ * @param $input string the content between opening and closing tags
+ * @param $args array tags arguments
+ * @param $parser Parser the current parser
+ * @return string
+ */
+ public static function renderPagelistTag( $input, array $args, Parser
$parser ) {
+ $tagParser = new ProofreadPagelistTagParser( $parser );
+ return $tagParser->render( $input, $args );
+ }
+
+ /**
+ * Parser hook for <pages> tag
+ *
+ * @param $input string the content between opening and closing tags
+ * @param $args array tags arguments
+ * @param $parser Parser the current parser
+ * @return string
+ */
+ public static function renderPagesTag( $input, array $args, Parser
$parser ) {
+ $tagParser = new ProofreadPagesTagParser( $parser );
+ return $tagParser->render( $input, $args );
+ }
+
+}
\ No newline at end of file
diff --git a/includes/parser/ProofreadTagParser.php
b/includes/parser/ProofreadTagParser.php
new file mode 100644
index 0000000..1f2f360
--- /dev/null
+++ b/includes/parser/ProofreadTagParser.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+/**
+ * Abstract structure for a tag parser
+ */
+abstract class ProofreadTagParser {
+
+ /**
+ * @var Parser
+ */
+ protected $parser;
+
+ /**
+ * @param Parser $parser the current parser
+ */
+ public function __construct( Parser $parser ) {
+ $this->parser = $parser;
+ }
+
+ /**
+ * Render a <pagelist> tag
+ *
+ * @param $input string the content between opening and closing tags
+ * @param $args array tags arguments
+ * @return string
+ */
+ public abstract function render( $input, array $args );
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/97295
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I92819deffa9c9579ae26e58fe67c737dc1c888f8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: master
Gerrit-Owner: Tpt <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits