Rtdwivedi has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/71965


Change subject: Separated the rendering and parsing part from 
ProofreadPage.body.php.
......................................................................

Separated the rendering and parsing part from ProofreadPage.body.php.

Change-Id: I9d51105c2948675794abe7cabce96e880fdf476b
---
M ProofreadPage.body.php
M ProofreadPage.php
A includes/ProofreadPageParser.php
A includes/ProofreadPageRenderer.php
M includes/page/EditProofreadPagePage.php
5 files changed, 516 insertions(+), 458 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage 
refs/changes/65/71965/1

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 23a0557..2105b7d 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -119,19 +119,6 @@
        }
 
        /**
-        * Set up our custom parser hooks when initializing parser.
-        *
-        * @param Parser $parser
-        * @return boolean hook return value
-        */
-       public static function onParserFirstCallInit( $parser ) {
-               $parser->setHook( 'pagelist', array( __CLASS__, 
'renderPageList' ) );
-               $parser->setHook( 'pages', array( __CLASS__, 'renderPages' ) );
-               $parser->setHook( 'pagequality', array( __CLASS__, 
'pageQuality' ) );
-               return true;
-       }
-
-       /**
         * Query the database to find if the current page is referred in an 
Index page.
         * @param $title Title
         */
@@ -477,410 +464,6 @@
        }
 
        /**
-        * Parser hook for index pages
-        * Display a list of coloured links to pages
-        * @param $input
-        * @param $args array
-        * @param $parser Parser
-        * @return string
-        */
-       public static function renderPageList( $input, $args, $parser ) {
-               global $wgContLang;
-
-               $title = $parser->getTitle();
-               if ( !$title->inNamespace( self::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 ) = self::pageNumber( $i, 
$args );
-
-                       if ( $mode == 'highroman' || $mode == 'roman' ) {
-                               $view = '&#160;' . $view;
-                       }
-
-                       $n = strlen( $count ) - mb_strlen( $view );
-                       if ( $n && ( $mode == 'normal' || $mode == 'empty' ) ) {
-                               $txt = '<span style="visibility:hidden;">';
-                               $pad = $wgContLang->formatNum( 0, true );
-                               for ( $j = 0; $j < $n; $j++ ) {
-                                       $txt = $txt . $pad;
-                               }
-                               $view = $txt . '</span>' . $view;
-                       }
-                       $title = self::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 ) {
-               global $wgContLang;
-
-               $pageNamespaceId = self::getPageNamespaceId();
-
-               // abort if this is nested <pages> call
-               if ( isset( $parser->proofreadRenderingPages ) && 
$parser->proofreadRenderingPages ) {
-                       return '';
-               }
-
-               $index = array_key_exists( 'index', $args ) ? $args['index'] : 
null;
-               $from = array_key_exists( 'from', $args ) ? $args['from'] : 
null;
-               $to = array_key_exists( 'to', $args ) ? $args['to'] : null;
-               $include = array_key_exists( 'include', $args ) ? 
$args['include'] : null;
-               $exclude = array_key_exists( 'exclude', $args ) ? 
$args['exclude'] : null;
-               $step = array_key_exists( 'step', $args ) ? $args['step'] : 
null;
-               $header = array_key_exists( 'header', $args ) ? $args['header'] 
: null;
-               $tosection = array_key_exists( 'tosection', $args ) ? 
$args['tosection'] : null;
-               $fromsection = array_key_exists( 'fromsection', $args ) ? 
$args['fromsection'] : null;
-               $onlysection = array_key_exists( 'onlysection', $args ) ? 
$args['onlysection'] : null;
-
-               // abort if the tag is on an index page
-               if ( $parser->getTitle()->inNamespace( 
self::getIndexNamespaceId() ) ) {
-                       return '';
-               }
-               // abort too if the tag is in the page namespace
-               if ( $parser->getTitle()->inNamespace( $pageNamespaceId ) ) {
-                       return '';
-               }
-               // ignore fromsection and tosection arguments if onlysection is 
specified
-               if ( $onlysection !== null ) {
-                       $fromsection = null;
-                       $tosection = null;
-               }
-
-               if( !$index ) {
-                       return '<strong class="error">' . wfMessage( 
'proofreadpage_index_expected' )->inContentLanguage()->escaped() . '</strong>';
-               }
-               $index_title = Title::makeTitleSafe( 
self::getIndexNamespaceId(), $index );
-               if( !$index_title || !$index_title->exists() ) {
-                       return '<strong class="error">' . wfMessage( 
'proofreadpage_nosuch_index' )->inContentLanguage()->escaped() . '</strong>';
-               }
-               $indexPage = ProofreadIndexPage::newFromTitle( $index_title );
-
-               $parser->getOutput()->addTemplate( $index_title, 
$index_title->getArticleID(), $index_title->getLatestRevID() );
-
-               $out = '';
-
-               list( $links, $params ) = $indexPage->getPages();
-
-               if( $from || $to || $include ) {
-                       $pages = array();
-
-                       if( empty( $links ) ) {
-                               $from = ( $from === null ) ? null : 
$wgContLang->parseFormattedNumber( $from );
-                               $to = ( $to === null ) ? null : 
$wgContLang->parseFormattedNumber( $to );
-                               $step = ( $step === null ) ? null : 
$wgContLang->parseFormattedNumber( $step );
-
-                               $imageTitle = Title::makeTitleSafe( NS_IMAGE, 
$index );
-                               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>';
-                               }
-                               $count = $image->pageCount();
-
-                               if( !$step ) {
-                                       $step = 1;
-                               }
-                               if( !is_numeric( $step ) || $step < 1 ) {
-                                       return '<strong class="error">' . 
wfMessage( 'proofreadpage_number_expected' )->inContentLanguage()->escaped() . 
'</strong>';
-                               }
-
-                               $pagenums = array();
-
-                               //add page selected with $include in pagenums
-                               if( $include ) {
-                                       $list = self::parse_num_list( $include 
);
-                                       if( $list  == null ) {
-                                               return '<strong class="error">' 
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() 
. '</strong>';
-                                       }
-                                       $pagenums = $list;
-                               }
-
-                               //ad pages selected with from and to in pagenums
-                               if( $from || $to ) {
-                                       if( !$from ) {
-                                               $from = 1;
-                                       }
-                                       if( !$to ) {
-                                               $to = $count;
-                                       }
-                                       if( !is_numeric( $from ) || 
!is_numeric( $to )  || !is_numeric( $step ) ) {
-                                               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; $i++ ) {
-                                               $pagenums[$i] = $i;
-                                       }
-                               }
-
-                               //remove excluded pages form $pagenums
-                               if( $exclude ) {
-                                       $excluded = self::parse_num_list( 
$exclude );
-                                       if( $excluded  == null ) {
-                                               return '<strong class="error">' 
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() 
. '</strong>';
-                                       }
-                                       $pagenums = array_diff( $pagenums, 
$excluded );
-                               }
-
-                               if( count($pagenums)/$step > 1000 ) {
-                                       return '<strong class="error">' . 
wfMessage( 'proofreadpage_interval_too_large' )->inContentLanguage()->escaped() 
. '</strong>';
-                               }
-
-                               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>';
-                               }
-
-                               //Create the list of pages to translude. the 
step system start with the smaller pagenum
-                               $mod = reset( $pagenums ) % $step;
-                               foreach( $pagenums as $num ) {
-                                       if( $step == 1 || $num % $step == $mod 
) {
-                                               list( $pagenum, $links, $mode ) 
= self::pageNumber( $num, $params );
-                                               $pages[] = array( 
self::getPageTitle( $index, $num ), $pagenum );
-                                       }
-                               }
-
-                               list( $from_page, $from_pagenum ) = reset( 
$pages );
-                               list( $to_page, $to_pagenum ) = end( $pages );
-
-                       } else {
-                               if( $from ) {
-                                       $adding = false;
-                               } else {
-                                       $adding = true;
-                                       $from_pagenum = $links[0][1];
-                               }
-
-                               $from_page = Title::makeTitleSafe( 
$pageNamespaceId, $from );
-                               $to_page = Title::makeTitleSafe( 
$pageNamespaceId, $to );
-                               for( $i = 0; $i < count( $links ); $i++ ) {
-                                       $link = $links[$i][0];
-                                       $pagenum = $links[$i][1];
-                                       if( $from_page !== null && 
$from_page->equals( $link ) ) {
-                                               $adding = true;
-                                               $from_pagenum = $pagenum;
-                                       }
-                                       if( $adding ) {
-                                               $pages[] = array( $link, 
$pagenum );
-                                       }
-                                       if( $to_page !== null && 
$to_page->equals( $link ) ) {
-                                               $adding = false;
-                                               $to_pagenum = $pagenum;
-                                       }
-                               }
-                               if( !$to ) {
-                                       $to_pagenum = $links[count( $links[1] ) 
- 1][1];
-                               }
-                       }
-                       // find which pages have quality0
-                       $q0_pages = array();
-                       if( !empty( $pages ) ) {
-                               $pp = array();
-                               foreach( $pages as $item ) {
-                                       list( $page, $pagenum ) = $item;
-                                       $pp[] = $page->getDBkey();
-                               }
-                               $cat = str_replace( ' ' , '_' , wfMessage( 
'proofreadpage_quality0_category' )->inContentLanguage()->escaped() );
-                               $res = 
ProofreadPageDbConnector::getPagesNameInCategory( $pp, $cat );
-
-                               if( $res ) {
-                                       foreach ( $res as $o ) {
-                                               $q0_pages[] = $o->page_title;
-                                       }
-                               }
-                       }
-
-                       // write the output
-                       foreach( $pages as $item ) {
-                               list( $page, $pagenum ) = $item;
-                               if( in_array( $page->getDBKey(), $q0_pages ) ) {
-                                       $is_q0 = true;
-                               } else {
-                                       $is_q0 = false;
-                               }
-                               $text = $page->getPrefixedText();
-                               if( !$is_q0 ) {
-                                       $out .= 
'<span>{{:MediaWiki:Proofreadpage_pagenum_template|page=' . $text . 
"|num=$pagenum}}</span>";
-                               }
-                               if( $from_page !== null && $page->equals( 
$from_page ) && $fromsection !== null ) {
-                                       $ts = '';
-                                       // Check if it is single page 
transclusion
-                                       if ( $to_page !== null && 
$page->equals( $to_page ) && $tosection !== null ) {
-                                               $ts = $tosection;
-                                       }
-                                       $out .= '{{#lst:' . $text . '|' . 
$fromsection . '|' . $ts .'}}';
-                               } elseif( $to_page !== null && $page->equals( 
$to_page ) && $tosection !== null ) {
-                                       $out .= '{{#lst:' . $text . '||' . 
$tosection . '}}';
-                               } elseif ( $onlysection !== null ) {
-                                       $out .= '{{#lst:' . $text . '|' . 
$onlysection . '}}';
-                               } else {
-                                       $out .= '{{:' . $text . '}}';
-                               }
-                               if( !$is_q0 ) {
-                                       $out.= "&#32;";
-                               }
-                       }
-               } else {
-                       /* table of Contents */
-                       $header = 'toc';
-                       if( $links == null ) {
-                               $firstpage = self::getPageTitle( $index, 1 );
-                       } else {
-                               $firstpage = $links[0][0];
-                       }
-                       if ( $firstpage !== null ) {
-                               $parser->getOutput()->addTemplate(
-                                       $firstpage,
-                                       $firstpage->getArticleID(),
-                                       $firstpage->getLatestRevID()
-                               );
-                       }
-               }
-
-               if( $header ) {
-                       if( $header == 'toc') {
-                               $parser->getOutput()->is_toc = true;
-                       }
-                       $indexLinks = $indexPage->getLinksToMainNamespace();
-                       $pageTitle = $parser->getTitle();
-                       $h_out = '{{:MediaWiki:Proofreadpage_header_template';
-                       $h_out .= "|value=$header";
-                       // find next and previous pages in list
-                       for( $i = 0; $i < count( $indexLinks ); $i++ ) {
-                               if( $pageTitle->equals( $indexLinks[$i][0] ) ) {
-                                       $current = '[[' . 
$indexLinks[$i][0]->getFullText() . '|' . $indexLinks[$i][1] . ']]';
-                                       break;
-                               }
-                       }
-                       if( $i > 1 ) {
-                               $prev = '[[' . $indexLinks[$i - 
1][0]->getFullText() . '|' . $indexLinks[$i - 1][1] . ']]';
-                       }
-                       if( $i + 1 < count( $indexLinks ) ) {
-                               $next = '[[' . $indexLinks[$i + 
1][0]->getFullText() . '|' . $indexLinks[$i + 1][1] . ']]';
-                       }
-                       if( isset( $args['current'] ) ) {
-                               $current = $args['current'];
-                       }
-                       if( isset( $args['prev'] ) ) {
-                               $prev = $args['prev'];
-                       }
-                       if( isset( $args['next'] ) ) {
-                               $next = $args['next'];
-                       }
-                       if( isset( $current ) ) {
-                               $h_out .= "|current=$current";
-                       }
-                       if( isset( $prev ) ) {
-                               $h_out .= "|prev=$prev";
-                       }
-                       if( isset( $next ) ) {
-                               $h_out .= "|next=$next";
-                       }
-                       if( isset( $from_pagenum ) ) {
-                               $h_out .= "|from=$from_pagenum";
-                       }
-                       if( isset( $to_pagenum ) ) {
-                               $h_out .= "|to=$to_pagenum";
-                       }
-                       $attributes = $indexPage->getIndexEntriesForHeader();
-                       foreach( $attributes as $attribute ) {
-                               $key = strtolower( $attribute->getKey() );
-                               if( array_key_exists( $key, $args ) ) {
-                                       $val = $args[$key];
-                               } else {
-                                       $val = $attribute->getStringValue();
-                               }
-                               $h_out .= "|$key=$val";
-                       }
-                       $h_out .= '}}';
-                       $out = $h_out . $out ;
-               }
-
-               // 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;
-               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
-        */
-       private static function parse_num_list($input) {
-               $input = str_replace(array(' ', '\t', '\n'), '', $input);
-               $list = explode( ',', $input );
-               $nums = array();
-               foreach( $list as $item ) {
-                       if( is_numeric( $item ) ) {
-                               $nums[$item] = $item;
-                       } else {
-                               $interval = explode( '-', $item );
-                               if( count( $interval ) != 2
-                                       || !is_numeric( $interval[0] )
-                                       || !is_numeric( $interval[1] )
-                                       || $interval[1] < $interval[0]
-                               ) {
-                                       return null;
-                               }
-                               for( $i = $interval[0]; $i <= $interval[1]; $i 
+= 1 ) {
-                                       $nums[$i] = $i;
-                               }
-                       }
-               }
-               return $nums;
-       }
-
-       /**
         * Set is_toc flag (true if page is a table of contents)
         * @param $outputPage OutputPage
         * @param $parserOutput ParserOutput
@@ -895,46 +478,18 @@
                return true;
        }
 
+
        /**
-        * Try to parse a page.
-        * Return quality status of the page and username of the proofreader
-        * Return -1 if the page cannot be parsed
+        * Set up our custom parser hooks when initializing parser.
+        *
+        * @param Parser $parser
+        * @return boolean hook return value
         */
-       public static function parsePage( $text, $title ) {
-               global $wgUser;
-
-               $username = $wgUser->getName();
-               $page_regexp = 
"/^<noinclude>(.*?)<\/noinclude>(.*?)<noinclude>(.*?)<\/noinclude>$/s";
-               if( !preg_match( $page_regexp, $text, $m ) ) {
-                       self::loadIndex( $title );
-                       if ( $title->prpIndexPage !== null ) {
-                               list( $header, $footer, $css, $editWidth ) = 
$title->prpIndexPage->getIndexDataForPage();
-                       } else {
-                               $header = '';
-                               $footer = '';
-                       }
-                       $new_text = "<noinclude><pagequality level=\"1\" 
user=\"$username\" /><div class=\"pagetext\">"
-                               
."$header\n\n\n</noinclude>$text<noinclude>\n$footer</div></noinclude>";
-                       return array( -1, null, $new_text );
-               }
-
-               $header = $m[1];
-               $body = $m[2];
-               $footer = $m[3];
-
-               $header_regexp = "/^<pagequality level=\"(0|1|2|3|4)\" 
user=\"(.*?)\" \/>/";
-               if( preg_match( $header_regexp, $header, $m2 ) ) {
-                       return array( intval($m2[1]), $m2[2], null );
-               }
-
-               $old_header_regexp = 
"/^\{\{PageQuality\|(0|1|2|3|4)(|\|(.*?))\}\}/is";
-               if( preg_match( $old_header_regexp, $header, $m3 ) ) {
-                       return array( intval($m3[1]), $m3[3], null );
-               }
-
-               $new_text = "<noinclude><pagequality level=\"1\" 
user=\"$username\" />"
-                       . 
"$header\n\n\n</noinclude>$body<noinclude>\n$footer</noinclude>";
-               return array( -1, null, $new_text );
+       public static function onParserFirstCallInit( $parser ) {
+               $parser->setHook( 'pagelist', array( 'ProofreadPageRenderer', 
'renderPageList' ) );
+               $parser->setHook( 'pages', array( 'ProofreadPageRenderer', 
'renderPages' ) );
+               $parser->setHook( 'pagequality', array( __CLASS__, 
'pageQuality' ) );
+               return true;
        }
 
        /**
diff --git a/ProofreadPage.php b/ProofreadPage.php
index b004a03..6822600 100644
--- a/ProofreadPage.php
+++ b/ProofreadPage.php
@@ -41,6 +41,8 @@
 
 $wgAutoloadClasses['ProofreadPage'] = $dir . 'ProofreadPage.body.php';
 $wgAutoloadClasses['ProofreadPageInit'] = $dir . 
'includes/ProofreadPageInit.php';
+$wgAutoloadClasses['ProofreadPageRenderer'] = $dir . 
'includes/ProofreadPageRenderer.php';
+$wgAutoloadClasses['ProofreadPageParser'] = $dir . 
'includes/ProofreadPageParser.php';
 
 $wgAutoloadClasses['EditProofreadIndexPage'] = $dir . 
'includes/index/EditProofreadIndexPage.php';
 $wgAutoloadClasses['ProofreadIndexEntry'] = $dir . 
'includes/index/ProofreadIndexEntry.php';
@@ -150,7 +152,7 @@
 $wgHooks['SpecialMovepageAfterMove'][] = 
'ProofreadPage::onSpecialMovepageAfterMove';
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'ProofreadIndexDbConnector::onLoadExtensionSchemaUpdates';
 $wgHooks['EditPage::importFormData'][] = 
'EditProofreadPagePage::onEditPageImportFormData';
-$wgHooks['OutputPageParserOutput'][] = 
'ProofreadPage::onOutputPageParserOutput';
+$wgHooks['OutputPageParserOutput'][] = 
'ProofreadPageParser::onOutputPageParserOutput';
 $wgHooks['wgQueryPages'][] = 'ProofreadPage::onwgQueryPages';
 $wgHooks['GetPreferences'][] = 'ProofreadPage::onGetPreferences';
 $wgHooks['LinksUpdateConstructed'][] = 
'ProofreadPage::onLinksUpdateConstructed';
diff --git a/includes/ProofreadPageParser.php b/includes/ProofreadPageParser.php
new file mode 100644
index 0000000..734d09c
--- /dev/null
+++ b/includes/ProofreadPageParser.php
@@ -0,0 +1,92 @@
+<?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
+ *
+ */
+class ProofreadPageParser {
+       /**
+        * 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
+        */
+       public static function parseNumList($input) {
+               $input = str_replace(array(' ', '\t', '\n'), '', $input);
+               $list = explode( ',', $input );
+               $nums = array();
+               foreach( $list as $item ) {
+                       if( is_numeric( $item ) ) {
+                               $nums[$item] = $item;
+                       } else {
+                               $interval = explode( '-', $item );
+                               if( count( $interval ) != 2
+                                       || !is_numeric( $interval[0] )
+                                       || !is_numeric( $interval[1] )
+                                       || $interval[1] < $interval[0]
+                               ) {
+                                       return null;
+                               }
+                               for( $i = $interval[0]; $i <= $interval[1]; $i 
+= 1 ) {
+                                       $nums[$i] = $i;
+                               }
+                       }
+               }
+               return $nums;
+       }
+
+       /**
+        * Try to parse a page.
+        * Return quality status of the page and username of the proofreader
+        * Return -1 if the page cannot be parsed
+        */
+       public static function parsePage( $text, $title ) {
+               global $wgUser;
+
+               $username = $wgUser->getName();
+               $page_regexp = 
"/^<noinclude>(.*?)<\/noinclude>(.*?)<noinclude>(.*?)<\/noinclude>$/s";
+               if( !preg_match( $page_regexp, $text, $m ) ) {
+                       self::loadIndex( $title );
+                       if ( $title->prpIndexPage !== null ) {
+                               list( $header, $footer, $css, $editWidth ) = 
$title->prpIndexPage->getIndexDataForPage();
+                       } else {
+                               $header = '';
+                               $footer = '';
+                       }
+                       $new_text = "<noinclude><pagequality level=\"1\" 
user=\"$username\" /><div class=\"pagetext\">"
+                               
."$header\n\n\n</noinclude>$text<noinclude>\n$footer</div></noinclude>";
+                       return array( -1, null, $new_text );
+               }
+
+               $header = $m[1];
+               $body = $m[2];
+               $footer = $m[3];
+
+               $header_regexp = "/^<pagequality level=\"(0|1|2|3|4)\" 
user=\"(.*?)\" \/>/";
+               if( preg_match( $header_regexp, $header, $m2 ) ) {
+                       return array( intval($m2[1]), $m2[2], null );
+               }
+
+               $old_header_regexp = 
"/^\{\{PageQuality\|(0|1|2|3|4)(|\|(.*?))\}\}/is";
+               if( preg_match( $old_header_regexp, $header, $m3 ) ) {
+                       return array( intval($m3[1]), $m3[3], null );
+               }
+
+               $new_text = "<noinclude><pagequality level=\"1\" 
user=\"$username\" />"
+                       . 
"$header\n\n\n</noinclude>$body<noinclude>\n$footer</noinclude>";
+               return array( -1, null, $new_text );
+       }
+}
\ No newline at end of file
diff --git a/includes/ProofreadPageRenderer.php 
b/includes/ProofreadPageRenderer.php
new file mode 100644
index 0000000..51058ba
--- /dev/null
+++ b/includes/ProofreadPageRenderer.php
@@ -0,0 +1,394 @@
+<?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
+ *
+ */
+class ProofreadPageRenderer {
+
+       /**
+        * Parser hook for index pages
+        * Display a list of coloured links to pages
+        * @param $input
+        * @param $args array
+        * @param $parser Parser
+        * @return string
+        */
+       public static function renderPageList( $input, $args, $parser ) {
+               global $wgContLang;
+
+               $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 = '&#160;' . $view;
+                       }
+
+                       $n = strlen( $count ) - mb_strlen( $view );
+                       if ( $n && ( $mode == 'normal' || $mode == 'empty' ) ) {
+                               $txt = '<span style="visibility:hidden;">';
+                               $pad = $wgContLang->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 ) {
+               global $wgContLang;
+
+               $pageNamespaceId = ProofreadPage::getPageNamespaceId();
+
+               // abort if this is nested <pages> call
+               if ( isset( $parser->proofreadRenderingPages ) && 
$parser->proofreadRenderingPages ) {
+                       return '';
+               }
+
+               $index = array_key_exists( 'index', $args ) ? $args['index'] : 
null;
+               $from = array_key_exists( 'from', $args ) ? $args['from'] : 
null;
+               $to = array_key_exists( 'to', $args ) ? $args['to'] : null;
+               $include = array_key_exists( 'include', $args ) ? 
$args['include'] : null;
+               $exclude = array_key_exists( 'exclude', $args ) ? 
$args['exclude'] : null;
+               $step = array_key_exists( 'step', $args ) ? $args['step'] : 
null;
+               $header = array_key_exists( 'header', $args ) ? $args['header'] 
: null;
+               $tosection = array_key_exists( 'tosection', $args ) ? 
$args['tosection'] : null;
+               $fromsection = array_key_exists( 'fromsection', $args ) ? 
$args['fromsection'] : null;
+               $onlysection = array_key_exists( 'onlysection', $args ) ? 
$args['onlysection'] : null;
+
+               // abort if the tag is on an index page
+               if ( $parser->getTitle()->inNamespace( 
ProofreadPage::getIndexNamespaceId() ) ) {
+                       return '';
+               }
+               // abort too if the tag is in the page namespace
+               if ( $parser->getTitle()->inNamespace( $pageNamespaceId ) ) {
+                       return '';
+               }
+               // ignore fromsection and tosection arguments if onlysection is 
specified
+               if ( $onlysection !== null ) {
+                       $fromsection = null;
+                       $tosection = null;
+               }
+
+               if( !$index ) {
+                       return '<strong class="error">' . wfMessage( 
'proofreadpage_index_expected' )->inContentLanguage()->escaped() . '</strong>';
+               }
+               $index_title = Title::makeTitleSafe( 
ProofreadPage::getIndexNamespaceId(), $index );
+               if( !$index_title || !$index_title->exists() ) {
+                       return '<strong class="error">' . wfMessage( 
'proofreadpage_nosuch_index' )->inContentLanguage()->escaped() . '</strong>';
+               }
+               $indexPage = ProofreadIndexPage::newFromTitle( $index_title );
+
+               $parser->getOutput()->addTemplate( $index_title, 
$index_title->getArticleID(), $index_title->getLatestRevID() );
+
+               $out = '';
+
+               list( $links, $params ) = $indexPage->getPages();
+
+               if( $from || $to || $include ) {
+                       $pages = array();
+
+                       if( empty( $links ) ) {
+                               $from = ( $from === null ) ? null : 
$wgContLang->parseFormattedNumber( $from );
+                               $to = ( $to === null ) ? null : 
$wgContLang->parseFormattedNumber( $to );
+                               $step = ( $step === null ) ? null : 
$wgContLang->parseFormattedNumber( $step );
+
+                               $imageTitle = Title::makeTitleSafe( NS_IMAGE, 
$index );
+                               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>';
+                               }
+                               $count = $image->pageCount();
+
+                               if( !$step ) {
+                                       $step = 1;
+                               }
+                               if( !is_numeric( $step ) || $step < 1 ) {
+                                       return '<strong class="error">' . 
wfMessage( 'proofreadpage_number_expected' )->inContentLanguage()->escaped() . 
'</strong>';
+                               }
+
+                               $pagenums = array();
+
+                               //add page selected with $include in pagenums
+                               if( $include ) {
+                                       $list = 
ProofreadPageParser::parseNumList( $include );
+                                       if( $list  == null ) {
+                                               return '<strong class="error">' 
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() 
. '</strong>';
+                                       }
+                                       $pagenums = $list;
+                               }
+
+                               //ad pages selected with from and to in pagenums
+                               if( $from || $to ) {
+                                       if( !$from ) {
+                                               $from = 1;
+                                       }
+                                       if( !$to ) {
+                                               $to = $count;
+                                       }
+                                       if( !is_numeric( $from ) || 
!is_numeric( $to )  || !is_numeric( $step ) ) {
+                                               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; $i++ ) {
+                                               $pagenums[$i] = $i;
+                                       }
+                               }
+
+                               //remove excluded pages form $pagenums
+                               if( $exclude ) {
+                                       $excluded = 
ProofreadPageParser::parseNumList( $exclude );
+                                       if( $excluded  == null ) {
+                                               return '<strong class="error">' 
. wfMessage( 'proofreadpage_invalid_interval' )->inContentLanguage()->escaped() 
. '</strong>';
+                                       }
+                                       $pagenums = array_diff( $pagenums, 
$excluded );
+                               }
+
+                               if( count($pagenums)/$step > 1000 ) {
+                                       return '<strong class="error">' . 
wfMessage( 'proofreadpage_interval_too_large' )->inContentLanguage()->escaped() 
. '</strong>';
+                               }
+
+                               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>';
+                               }
+
+                               //Create the list of pages to translude. the 
step system start with the smaller pagenum
+                               $mod = reset( $pagenums ) % $step;
+                               foreach( $pagenums as $num ) {
+                                       if( $step == 1 || $num % $step == $mod 
) {
+                                               list( $pagenum, $links, $mode ) 
= ProofreadPage::pageNumber( $num, $params );
+                                               $pages[] = array( 
ProofreadPage::getPageTitle( $index, $num ), $pagenum );
+                                       }
+                               }
+
+                               list( $from_page, $from_pagenum ) = reset( 
$pages );
+                               list( $to_page, $to_pagenum ) = end( $pages );
+
+                       } else {
+                               if( $from ) {
+                                       $adding = false;
+                               } else {
+                                       $adding = true;
+                                       $from_pagenum = $links[0][1];
+                               }
+
+                               $from_page = Title::makeTitleSafe( 
$pageNamespaceId, $from );
+                               $to_page = Title::makeTitleSafe( 
$pageNamespaceId, $to );
+                               for( $i = 0; $i < count( $links ); $i++ ) {
+                                       $link = $links[$i][0];
+                                       $pagenum = $links[$i][1];
+                                       if( $from_page !== null && 
$from_page->equals( $link ) ) {
+                                               $adding = true;
+                                               $from_pagenum = $pagenum;
+                                       }
+                                       if( $adding ) {
+                                               $pages[] = array( $link, 
$pagenum );
+                                       }
+                                       if( $to_page !== null && 
$to_page->equals( $link ) ) {
+                                               $adding = false;
+                                               $to_pagenum = $pagenum;
+                                       }
+                               }
+                               if( !$to ) {
+                                       $to_pagenum = $links[count( $links[1] ) 
- 1][1];
+                               }
+                       }
+                       // find which pages have quality0
+                       $q0_pages = array();
+                       if( !empty( $pages ) ) {
+                               $pp = array();
+                               foreach( $pages as $item ) {
+                                       list( $page, $pagenum ) = $item;
+                                       $pp[] = $page->getDBkey();
+                               }
+                               $cat = str_replace( ' ' , '_' , wfMessage( 
'proofreadpage_quality0_category' )->inContentLanguage()->escaped() );
+                               $res = 
ProofreadPageDbConnector::getPagesNameInCategory( $pp, $cat );
+
+                               if( $res ) {
+                                       foreach ( $res as $o ) {
+                                               $q0_pages[] = $o->page_title;
+                                       }
+                               }
+                       }
+
+                       // write the output
+                       foreach( $pages as $item ) {
+                               list( $page, $pagenum ) = $item;
+                               if( in_array( $page->getDBKey(), $q0_pages ) ) {
+                                       $is_q0 = true;
+                               } else {
+                                       $is_q0 = false;
+                               }
+                               $text = $page->getPrefixedText();
+                               if( !$is_q0 ) {
+                                       $out .= 
'<span>{{:MediaWiki:Proofreadpage_pagenum_template|page=' . $text . 
"|num=$pagenum}}</span>";
+                               }
+                               if( $from_page !== null && $page->equals( 
$from_page ) && $fromsection !== null ) {
+                                       $ts = '';
+                                       // Check if it is single page 
transclusion
+                                       if ( $to_page !== null && 
$page->equals( $to_page ) && $tosection !== null ) {
+                                               $ts = $tosection;
+                                       }
+                                       $out .= '{{#lst:' . $text . '|' . 
$fromsection . '|' . $ts .'}}';
+                               } elseif( $to_page !== null && $page->equals( 
$to_page ) && $tosection !== null ) {
+                                       $out .= '{{#lst:' . $text . '||' . 
$tosection . '}}';
+                               } elseif ( $onlysection !== null ) {
+                                       $out .= '{{#lst:' . $text . '|' . 
$onlysection . '}}';
+                               } else {
+                                       $out .= '{{:' . $text . '}}';
+                               }
+                               if( !$is_q0 ) {
+                                       $out.= "&#32;";
+                               }
+                       }
+               } else {
+                       /* table of Contents */
+                       $header = 'toc';
+                       if( $links == null ) {
+                               $firstpage = ProofreadPage::getPageTitle( 
$index, 1 );
+                       } else {
+                               $firstpage = $links[0][0];
+                       }
+                       if ( $firstpage !== null ) {
+                               $parser->getOutput()->addTemplate(
+                                       $firstpage,
+                                       $firstpage->getArticleID(),
+                                       $firstpage->getLatestRevID()
+                               );
+                       }
+               }
+
+               if( $header ) {
+                       if( $header == 'toc') {
+                               $parser->getOutput()->is_toc = true;
+                       }
+                       $indexLinks = $indexPage->getLinksToMainNamespace();
+                       $pageTitle = $parser->getTitle();
+                       $h_out = '{{:MediaWiki:Proofreadpage_header_template';
+                       $h_out .= "|value=$header";
+                       // find next and previous pages in list
+                       for( $i = 0; $i < count( $indexLinks ); $i++ ) {
+                               if( $pageTitle->equals( $indexLinks[$i][0] ) ) {
+                                       $current = '[[' . 
$indexLinks[$i][0]->getFullText() . '|' . $indexLinks[$i][1] . ']]';
+                                       break;
+                               }
+                       }
+                       if( $i > 1 ) {
+                               $prev = '[[' . $indexLinks[$i - 
1][0]->getFullText() . '|' . $indexLinks[$i - 1][1] . ']]';
+                       }
+                       if( $i + 1 < count( $indexLinks ) ) {
+                               $next = '[[' . $indexLinks[$i + 
1][0]->getFullText() . '|' . $indexLinks[$i + 1][1] . ']]';
+                       }
+                       if( isset( $args['current'] ) ) {
+                               $current = $args['current'];
+                       }
+                       if( isset( $args['prev'] ) ) {
+                               $prev = $args['prev'];
+                       }
+                       if( isset( $args['next'] ) ) {
+                               $next = $args['next'];
+                       }
+                       if( isset( $current ) ) {
+                               $h_out .= "|current=$current";
+                       }
+                       if( isset( $prev ) ) {
+                               $h_out .= "|prev=$prev";
+                       }
+                       if( isset( $next ) ) {
+                               $h_out .= "|next=$next";
+                       }
+                       if( isset( $from_pagenum ) ) {
+                               $h_out .= "|from=$from_pagenum";
+                       }
+                       if( isset( $to_pagenum ) ) {
+                               $h_out .= "|to=$to_pagenum";
+                       }
+                       $attributes = $indexPage->getIndexEntriesForHeader();
+                       foreach( $attributes as $attribute ) {
+                               $key = strtolower( $attribute->getKey() );
+                               if( array_key_exists( $key, $args ) ) {
+                                       $val = $args[$key];
+                               } else {
+                                       $val = $attribute->getStringValue();
+                               }
+                               $h_out .= "|$key=$val";
+                       }
+                       $h_out .= '}}';
+                       $out = $h_out . $out ;
+               }
+
+               // 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;
+               return $out;
+       }
+
+}
\ No newline at end of file
diff --git a/includes/page/EditProofreadPagePage.php 
b/includes/page/EditProofreadPagePage.php
index 4aa3d8b..15ea032 100644
--- a/includes/page/EditProofreadPagePage.php
+++ b/includes/page/EditProofreadPagePage.php
@@ -113,7 +113,7 @@
 
                $text = $editpage->textbox1;
                // parse the page
-               list( $q, $username, $ptext ) = ProofreadPage::parsePage( 
$text, $title );
+               list( $q, $username, $ptext ) = ProofreadPageParser::parsePage( 
$text, $title );
                if( $q == -1 ) {
                        $editpage->textbox1 = $ptext;
                        $q = 1;
@@ -123,7 +123,7 @@
                $rev = Revision::newFromTitle( $title, false, 
Revision::READ_LATEST );
                if( $rev ) {
                        $old_text = $rev->getText();
-                       list( $old_q, $old_username, $old_ptext ) = 
ProofreadPage::parsePage( $old_text, $title );
+                       list( $old_q, $old_username, $old_ptext ) = 
ProofreadPageParser::parsePage( $old_text, $title );
                        if( $old_q != -1 ) {
                                // check usernames
                                if( ( $old_q != $q ) && !$wgUser->isAllowed( 
'pagequality' ) ) {
@@ -197,4 +197,19 @@
 
                return true;
        }
+
+       /**
+        * Set is_toc flag (true if page is a table of contents)
+        * @param $outputPage OutputPage
+        * @param $parserOutput ParserOutput
+        * @return bool
+        */
+       public static function onOutputPageParserOutput( $outputPage, 
$parserOutput ) {
+               if( isset( $parserOutput->is_toc ) ) {
+                       $outputPage->is_toc = $parserOutput->is_toc;
+               } else {
+                       $outputPage->is_toc = false;
+               }
+               return true;
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/71965
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9d51105c2948675794abe7cabce96e880fdf476b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: pagePagesRefactoring
Gerrit-Owner: Rtdwivedi <ellydwivedi2...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to