Rtdwivedi has uploaded a new change for review.

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


Change subject: 1. Separated index functions from pages. 2. Separated setting 
up editing system from the main class ProofreadPage. Change-Id: 
I0b841a32f2ea00b742d8e06e71984bc0d6edce42
......................................................................

1. Separated index functions from pages.
2. Separated setting up editing system from the main class ProofreadPage.
Change-Id: I0b841a32f2ea00b742d8e06e71984bc0d6edce42
---
M ProofreadPage.body.php
A ProofreadPage.index.php
M ProofreadPage.php
A includes/ProofreadPage.index.php
M includes/ProofreadPageDbConnector.php
A includes/PrrofreadEditor.php
M includes/index/ProofreadIndexDbConnector.php
A includes/index/ProofreadPage.index.php
8 files changed, 387 insertions(+), 285 deletions(-)


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

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 6e709f6..710979b 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -24,11 +24,6 @@
 */
 
 class ProofreadPage {
-       /**
-        * Parser object for index pages
-        * @var Parser
-        */
-       private static $index_parser = null;
 
        /**
         * Returns id of Page namespace.
@@ -104,23 +99,6 @@
                return true;
        }
 
-       /**
-        * Set up our custom edition system.
-        *
-        * @param Article $article  being edited
-        * @param User $user User performing the edit
-        * @return boolean hook return value
-        */
-       public static function onCustomEditor( $article, $user ) {
-               global $request;
-               if ( $article->getTitle()->inNamespace( 
self::getIndexNamespaceId() ) ) { //TODO ExternalEditor case
-                       $editor = new EditProofreadIndexPage( $article );
-                       $editor->edit();
-                       return false;
-               } else {
-                       return true;
-               }
-       }
 
        /**
         * Set up our custom parser hooks when initializing parser.
@@ -135,50 +113,6 @@
                return true;
        }
 
-       /**
-        * Query the database to find if the current page is referred in an 
Index page.
-        * @param $title Title
-        */
-       private static function load_index( $title ) {
-               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
-
-               $title->pr_index_title = null;
-               $result = ProofreadIndexDbConnector::getRowsFromTitle( $title );
-
-               foreach ( $result as $x ) {
-                       $ref_title = Title::makeTitle( $x->page_namespace, 
$x->page_title );
-                       if ( $ref_title->inNamespace( 
self::getIndexNamespaceId() ) ) {
-                               $title->pr_index_title = 
$ref_title->getPrefixedText();
-                               break;
-                       }
-               }
-
-               if ( $title->pr_index_title ) {
-                       return;
-               }
-
-               $imageTitle = null;
-               /* check if we are a page of a multipage file */
-               if ( preg_match( "/^$page_namespace:(.*?)(\/(.*?)|)$/", 
$title->getPrefixedText(), $m ) ) {
-                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
-               }
-               if ( !$imageTitle ) {
-                       return;
-               }
-
-               $image = wfFindFile( $imageTitle );
-
-               // if it is multipage, we use the page order of the file
-               if ( $image && $image->exists() && $image->isMultipage() ) {
-                       $name = $image->getTitle()->getText();
-                       $index_name = "$index_namespace:$name";
-
-                       if ( !$title->pr_index_title ) {
-                               // there is no index, or the page is not listed 
in the index : use canonical index
-                               $title->pr_index_title = $index_name;
-                       }
-               }
-       }
 
        /**
         * return the URLs of the index, previous and next pages.
@@ -225,7 +159,7 @@
                }
 
                // if the index page exists, find current page number, previous 
and next pages
-               list( $links, $params, $attributes ) = self::parse_index( 
$index_title );
+               list( $links, $params, $attributes ) = 
ProofreadPageIndex::parse_index( $index_title );
 
                if( $links == null ) {
                        list( $pagenum, $links, $mode ) = self::pageNumber( 
$pagenr, $params );
@@ -263,92 +197,7 @@
                return array( $index_title, $prev_title, $next_title, $header, 
$footer, $css, $edit_width );
        }
 
-       /**
-        * Read metadata from an index page.
-        * Depending on whether the index uses pagelist,
-        * it will return either a list of links or a list
-        * of parameters to pagelist, and a list of attributes.
-        * @param $index_title Title
-        * @return array
-        */
-       private static function parse_index( $index_title ) {
-               $err = array( false, false, array() );
-               if ( !$index_title ) {
-                       return $err;
-               }
-               if ( !$index_title->exists() ) {
-                       return $err;
-               }
 
-               $rev = Revision::newFromTitle( $index_title );
-               $text = $rev->getText();
-               return self::parse_index_text( $text );
-       }
-
-       /**
-        * @param $text string
-        * @return array
-        */
-       private static function parse_index_text( $text ) {
-               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
-               //check if it is using pagelist
-               preg_match_all( "/<pagelist([^<]*?)\/>/is", $text, $m, 
PREG_PATTERN_ORDER );
-               if( $m[1] ) {
-                       $params_s = '';
-                       for( $k = 0; $k < count( $m[1] ); $k++ ) {
-                               $params_s = $params_s . $m[1][$k];
-                       }
-                       $params = Sanitizer::decodeTagAttributes( $params_s );
-                       $links = null;
-               } else {
-                       $params = null;
-                       $tag_pattern = 
"/\[\[$page_namespace:(.*?)(\|(.*?)|)\]\]/i";
-                       preg_match_all( $tag_pattern, $text, $links, 
PREG_PATTERN_ORDER );
-               }
-
-               // read attributes
-               $attributes = array();
-               $config = ProofreadIndexPage::getDataConfig();
-               foreach( $config as $var_name => $property ) {
-                       $var = strtolower( $var_name );
-                       if( ( isset( $property['header'] ) && 
$property['header'] ) || in_array( $var, array( 'header', 'footer', 'css', 
'width' ) ) ) {
-                               if( isset( $property['hidden'] ) && 
$property['hidden'] ) {
-                                       if( isset( $property['default'] ) ) {
-                                               $attributes[$var] = 
$property['default'];
-                                       } else {
-                                               $attributes[$var] = '';
-                                       }
-                               } else {
-                                       $tag_pattern = "/\n\|" . $var_name . 
"=(.*?)\n(\||\}\})/is";
-                                       //$var = 'proofreadPage' . $var_name;
-
-                                       if( preg_match( $tag_pattern, $text, 
$matches ) ) {
-                                               $attributes[$var] = $matches[1];
-                                       } else {
-                                               $attributes[$var] = '';
-                                       }
-                               }
-                       }
-               }
-               return array( $links, $params, $attributes );
-       }
-
-       /**
-        * Return the ordered list of links to ns-0 from an index page
-        */
-       private static function parse_index_links( $index_title ) {
-               // Instanciate a new parser object to avoid side effects of 
$parser->replaceVariables
-               if( is_null( self::$index_parser ) ) {
-                       self::$index_parser = new Parser;
-               }
-               $rev = Revision::newFromTitle( $index_title );
-               $text = $rev->getText();
-               $options = new ParserOptions();
-               $rtext = self::$index_parser->preprocess( $text, $index_title, 
$options );
-               $text_links_pattern = "/\[\[\s*([^:\|]*?)\s*(\|(.*?)|)\]\]/i";
-               preg_match_all( $text_links_pattern, $rtext, $text_links, 
PREG_PATTERN_ORDER );
-               return $text_links;
-       }
 
        /**
         * Append javascript variables and code to the page.
@@ -399,8 +248,10 @@
        private static function preparePage( $out, $m, $isEdit ) {
                global $wgUser, $wgExtensionAssetsPath, $wgContLang;
 
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+
                if ( !isset( $out->getTitle()->pr_index_title ) ) {
-                       self::load_index( $out->getTitle() );
+                       ProofreadPageIndex::load_index( $out->getTitle(), 
$page_namespace, $index_namespace );
                }
 
                $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
@@ -782,7 +633,7 @@
 
                $out = '';
 
-               list( $links, $params, $attributes ) = self::parse_index( 
$index_title );
+               list( $links, $params, $attributes ) = 
ProofreadPageIndex::parse_index( $index_title );
 
                if( $from || $to || $include ) {
                        $pages = array();
@@ -968,7 +819,7 @@
                        if( $header == 'toc') {
                                $parser->getOutput()->is_toc = true;
                        }
-                       $text_links = self::parse_index_links( $index_title );
+                       $text_links = ProofreadPageIndex::parse_index_links( 
$index_title );
                        $h_out = '{{:MediaWiki:Proofreadpage_header_template';
                        $h_out .= "|value=$header";
                        // find next and previous pages in list
@@ -1080,10 +931,11 @@
        private static function parse_page( $text, $title ) {
                global $wgUser;
 
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
                $username = $wgUser->getName();
                $page_regexp = 
"/^<noinclude>(.*?)<\/noinclude>(.*?)<noinclude>(.*?)<\/noinclude>$/s";
                if( !preg_match( $page_regexp, $text, $m ) ) {
-                       self::load_index( $title );
+                       ProofreadPageIndex::load_index( $title, 
$page_namespace, $index_namespace );
                        list( $index_title, $prev_title, $next_title, $header, 
$footer, $css, $edit_width ) = self::navigation( $title );
                        $new_text = "<noinclude><pagequality level=\"1\" 
user=\"$username\" /><div class=\"pagetext\">"
                                
."$header\n\n\n</noinclude>$text<noinclude>\n$footer</div></noinclude>";
@@ -1168,7 +1020,7 @@
                // check that pages listed on an index are unique.
                if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
                        $text = $editpage->textbox1;
-                       list( $links, $params, $attributes ) = 
self::parse_index_text( $text );
+                       list( $links, $params, $attributes ) = 
ProofreadPageIndex::parse_index_text( $text );
                        if( $links != null && count( $links[1] ) != count( 
array_unique( $links[1] ) ) ) {
                                $wgOut->showErrorPage( 
'proofreadpage_indexdupe', 'proofreadpage_indexdupetext' );
                                return false;
@@ -1226,32 +1078,6 @@
        }
 
        /**
-        * Remove index data from pr_index table.
-        * @param $pageId Integer: page identifier
-        */
-       private static function removeIndexData( $pageId ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'pr_index', array( 'pr_page_id' => $pageId ), 
__METHOD__ );
-       }
-
-       /**
-        * Updates index data for an index referencing the specified page.
-        * @param $title Title: page title object
-        * @param $deleted Boolean: indicates whether the page was deleted
-        */
-       private static function updateIndexOfPage( $title, $deleted = false ) {
-               self::load_index( $title );
-               if ( $title->pr_index_title ) {
-                       $index_title = Title::newFromText( 
$title->pr_index_title );
-                       $index_title->invalidateCache();
-                       $index = new Article( $index_title );
-                       if ( $index ) {
-                               self::update_pr_index( $index, $deleted ? 
$title->getDBKey() : null );
-                       }
-               }
-       }
-
-       /**
         * if I delete a page, I need to update the index table
         * if I delete an index page too...
         *
@@ -1260,14 +1086,15 @@
         */
        public static function onArticleDelete( $article ) {
                $title = $article->getTitle();
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
 
                // Process Index removal.
                if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
-                       self::removeIndexData( $article->getId() );
+                       ProofreadIndexDbConnector::removeIndexData( 
$article->getId() );
 
                // Process Page removal.
                } elseif ( $title->inNamespace( self::getPageNamespaceId() ) ) {
-                       self::updateIndexOfPage( $title, true );
+                       ProofreadPageIndex::updateIndexOfPage( $title, 
$page_namespace, $index_namespace, true );
                }
 
                return true;
@@ -1279,16 +1106,17 @@
         * @return bool
         */
        public static function onArticleUndelete( $title, $create ) {
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
                // Process Index restoration.
                if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
                        $index = new Article( $title );
                        if ( $index ) {
-                               self::update_pr_index( $index );
+                               ProofreadPageIndex::update_pr_index( $index );
                        }
 
                // Process Page restoration.
                } elseif ( $title->inNamespace( self::getPageNamespaceId() ) ) {
-                       self::updateIndexOfPage( $title );
+                       ProofreadPageIndex::updateIndexOfPage( $title, 
$page_namespace, $index_namespace );
                }
 
                return true;
@@ -1300,10 +1128,11 @@
         */
        public static function onArticleSaveComplete( $article ) {
                $title = $article->getTitle();
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
 
                // if it's an index, update pr_index table
                if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
-                       self::update_pr_index( $article );
+                       ProofreadPageIndex::update_pr_index( $article );
                        return true;
                }
 
@@ -1314,7 +1143,7 @@
 
                /* check if there is an index */
                if ( !isset( $title->pr_index_title ) ) {
-                       self::load_index( $title );
+                       ProofreadPageIndex::load_index( $title, 
$page_namespace, $index_namespace );
                }
                if( ! $title->pr_index_title ) {
                        return true;
@@ -1378,8 +1207,10 @@
         * @return bool
         */
        public static function onSpecialMovepageAfterMove( $form, $ot, $nt ) {
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+
                if ( $ot->inNamespace( self::getPageNamespaceId() ) ) {
-                       self::updateIndexOfPage( $ot );
+                       ProofreadPageIndex::updateIndexOfPage( $ot, 
$page_namespace, $index_namespace );
                } elseif ( $ot->inNamespace( self::getIndexNamespaceId() )
                          && !$nt->inNamespace( self::getIndexNamespaceId() ) ) 
{
                        // The page is moved out of the Index namespace.
@@ -1389,111 +1220,24 @@
                        // There is no page under the old title or it is a 
redirect.
                        $article = new Article( $nt );
                        if( $article ) {
-                               self::removeIndexData( $article->getId() );
+                               ProofreadIndexDbConnector::removeIndexData( 
$article->getId() );
                        }
                }
 
                if ( $nt->inNamespace( self::getPageNamespaceId() ) ) {
-                       self::load_index( $nt );
+                       ProofreadPageIndex::load_index( $nt, $page_namespace, 
$index_namespace );
                        if( $nt->pr_index_title !== null
                           && ( !isset( $ot->pr_index_title ) || ( 
$nt->pr_index_title != $ot->pr_index_title ) ) ) {
-                               self::updateIndexOfPage( $nt );
+                               ProofreadPageIndex::updateIndexOfPage( $nt );
                        }
                } elseif ( $nt->inNamespace( self::getIndexNamespaceId() ) ) {
                        // Update index data.
                        $article = new Article( $nt );
                        if( $article ) {
-                               self::update_pr_index( $article );
+                               ProofreadPageIndex::update_pr_index( $article );
                        }
                }
                return true;
-       }
-
-       /**
-        * When an index page is created or purged, recompute pr_index values
-        * @param $article Article
-        * @return bool
-        */
-       public static function onArticlePurge( $article ) {
-               $title = $article->getTitle();
-               if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
-                       self::update_pr_index( $article );
-                       return true;
-               }
-               return true;
-       }
- 
-       /**
-        * Update the pr_index entry of an article
-        * @param $index Article
-        * @param $deletedpage null|string
-        */
-       private static function update_pr_index( $index, $deletedpage = null ) {
-               //list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
-               $page_ns_index = self::getPageNamespaceId();
-               if ( $page_ns_index == null ) {
-                       return;
-               }
-
-               $index_title = $index->getTitle();
-               $index_id = $index->getID();
-               $dbr = wfGetDB( DB_SLAVE );
-
-               $n = 0;
-
-               // read the list of pages
-               $pages = array();
-               list( $links, $params, $attributes ) = self::parse_index( 
$index_title );
-               if( $links == null ) {
-                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, 
$index_title->getText() );
-                       if ( $imageTitle ) {
-                               $image = wfFindFile( $imageTitle );
-                               if ( $image && $image->isMultipage() && 
$image->pageCount() ) {
-                                       $n = $image->pageCount();
-                                       for ( $i = 1; $i <= $n; $i++ ) {
-                                               $page = 
$index_title->getDBKey() . '/' . $i;
-                                               if( $page != $deletedpage ) {
-                                                       array_push( $pages, 
$page );
-                                               }
-                                       }
-                               }
-                       }
-               } else {
-                       $n = count( $links[1] );
-                       for ( $i = 0; $i < $n; $i++ ) {
-                               $page = str_replace( ' ' , '_' , $links[1][$i] 
);
-                               if( $page != $deletedpage ) {
-                                       array_push( $pages, $page );
-                               }
-                       }
-               }
-
-               if( !count( $pages ) ) {
-                       return;
-               }
-
-               $row = ProofreadPageDbConnector::getCountOfPages( 
$page_ns_index, $pages );
-               if( $row ) {
-                       $total = $row->count;
-               } else{
-                       return;
-               }
-
-               // proofreading status of pages
-               $queryArr = array(
-                       'tables' => array( 'page', 'categorylinks' ),
-                       'fields' => array( 'COUNT(page_id) AS count' ),
-                       'conds' => array( 'cl_to' => '', 'page_namespace' => 
$page_ns_index, 'page_title' => $pages ),
-                       'joins' => array( 'categorylinks' => array( 'LEFT 
JOIN', 'cl_from=page_id' ) )
-               );
-
-               $n0 = ProofreadPageDbConnector::queryCount( $dbr, $queryArr, 
'proofreadpage_quality0_category' );
-               $n2 = ProofreadPageDbConnector::queryCount( $dbr, $queryArr, 
'proofreadpage_quality2_category' );
-               $n3 = ProofreadPageDbConnector::queryCount( $dbr, $queryArr, 
'proofreadpage_quality3_category' );
-               $n4 = ProofreadPageDbConnector::queryCount( $dbr, $queryArr, 
'proofreadpage_quality4_category' );
-               $n1 = $total - $n0 - $n2 - $n3 - $n4;
-
-               $replace = ProofreadPageDbConnector::replaceIndex( $n, $n0, 
$n1, $n2, $n3, $n4, $index_id );
        }
 
        /**
@@ -1635,4 +1379,4 @@
                }
                return true;
        }
-}
+}
\ No newline at end of file
diff --git a/ProofreadPage.index.php b/ProofreadPage.index.php
new file mode 100644
index 0000000..5ef850f
--- /dev/null
+++ b/ProofreadPage.index.php
@@ -0,0 +1,24 @@
+<?php
+class ProofreadPageIndex{
+       /**
+        * Read metadata from an index page.
+        * Depending on whether the index uses pagelist,
+        * it will return either a list of links or a list
+        * of parameters to pagelist, and a list of attributes.
+        * @param $index_title Title
+        * @return array
+        */
+       public static function parse_index( $index_title ) {
+               $err = array( false, false, array() );
+               if ( !$index_title ) {
+                       return $err;
+               }
+               if ( !$index_title->exists() ) {
+                       return $err;
+               }
+
+               $rev = Revision::newFromTitle( $index_title );
+               $text = $rev->getText();
+               return self::parse_index_text( $text );
+       }
+}
\ No newline at end of file
diff --git a/ProofreadPage.php b/ProofreadPage.php
index 91e566e..1eafe69 100644
--- a/ProofreadPage.php
+++ b/ProofreadPage.php
@@ -43,6 +43,8 @@
 $wgAutoloadClasses['ProofreadIndexDbConnector'] = $dir . 
'includes/index/ProofreadIndexDbConnector.php';
 $wgAutoloadClasses['ProofreadPageDbConnector'] = $dir . 
'includes/ProofreadPageDbConnector.php';
 $wgAutoloadClasses['ProofreadPageInit'] = $dir . 
'includes/ProofreadPageInit.php';
+$wgAutoloadClasses['ProofreadPageIndex'] = $dir . 
'includes/index/ProofreadPage.index.php';
+$wgAutoloadClasses['ProofreadEditor'] = $dir . 'includes/ProofreadEditor.php';
 
 
 $wgAutoloadClasses['EditProofreadIndexPage'] = $dir . 
'includes/index/EditProofreadIndexPage.php';
@@ -142,8 +144,8 @@
 $wgHooks['ArticleSaveComplete'][] = 'ProofreadPage::onArticleSaveComplete';
 $wgHooks['ArticleDelete'][] = 'ProofreadPage::onArticleDelete';
 $wgHooks['ArticleUndelete'][] = 'ProofreadPage::onArticleUndelete';
-$wgHooks['EditFormPreloadText'][] = 'ProofreadPage::onEditFormPreloadText';
-$wgHooks['ArticlePurge'][] = 'ProofreadPage::onArticlePurge';
+$wgHooks['EditFormPreloadText'][] = 'ProofreadEditor::onEditFormPreloadText';
+$wgHooks['ArticlePurge'][] = 'ProofreadPageIndex::onArticlePurge';
 $wgHooks['SpecialMovepageAfterMove'][] = 
'ProofreadPage::onSpecialMovepageAfterMove';
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'ProofreadIndexDbConnector::onLoadExtensionSchemaUpdates';
 $wgHooks['EditPage::importFormData'][] = 
'ProofreadPage::onEditPageImportFormData';
diff --git a/includes/ProofreadPage.index.php b/includes/ProofreadPage.index.php
new file mode 100644
index 0000000..5ef850f
--- /dev/null
+++ b/includes/ProofreadPage.index.php
@@ -0,0 +1,24 @@
+<?php
+class ProofreadPageIndex{
+       /**
+        * Read metadata from an index page.
+        * Depending on whether the index uses pagelist,
+        * it will return either a list of links or a list
+        * of parameters to pagelist, and a list of attributes.
+        * @param $index_title Title
+        * @return array
+        */
+       public static function parse_index( $index_title ) {
+               $err = array( false, false, array() );
+               if ( !$index_title ) {
+                       return $err;
+               }
+               if ( !$index_title->exists() ) {
+                       return $err;
+               }
+
+               $rev = Revision::newFromTitle( $index_title );
+               $text = $rev->getText();
+               return self::parse_index_text( $text );
+       }
+}
\ No newline at end of file
diff --git a/includes/ProofreadPageDbConnector.php 
b/includes/ProofreadPageDbConnector.php
index 0566388..012467a 100644
--- a/includes/ProofreadPageDbConnector.php
+++ b/includes/ProofreadPageDbConnector.php
@@ -128,7 +128,7 @@
         * @param $cat string
         * @return int
         */
-       public static function queryCount( $dbr, $query, $cat ) {
+       public static function queryCount( $query, $cat ) {
                $dbr = wfGetDB( DB_SLAVE );
                $query['conds']['cl_to'] = str_replace( ' ' , '_' , wfMessage( 
$cat )->inContentLanguage()->text() );
                $res = $dbr->select( $query['tables'], $query['fields'], 
$query['conds'], __METHOD__, array(), $query['joins'] );
diff --git a/includes/PrrofreadEditor.php b/includes/PrrofreadEditor.php
new file mode 100644
index 0000000..5995619
--- /dev/null
+++ b/includes/PrrofreadEditor.php
@@ -0,0 +1,50 @@
+<?php
+
+class ProofreadEditor{
+       /**
+        * Preload text layer from multipage formats
+        * @param $textbox1
+        * @param $mTitle Title
+        * @return bool
+        */
+       public static function onEditFormPreloadText( $textbox1, $mTitle ) {
+               global $wgContLang;
+
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+               if ( preg_match( "/^$page_namespace:(.*?)\/(.*?)$/", 
$mTitle->getPrefixedText(), $m ) ) {
+                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
+                       if ( !$imageTitle ) {
+                               return true;
+                       }
+
+                       $image = wfFindFile( $imageTitle );
+                       if ( $image && $image->exists() ) {
+                               $text = $image->getHandler()->getPageText( 
$image, $wgContLang->parseFormattedNumber( $m[2] ) );
+                               if ( $text ) {
+                                       $text = preg_replace( "/(\\\\n)/", 
"\n", $text );
+                                       $text = preg_replace( "/(\\\\\d*)/", 
'', $text );
+                                       $textbox1 = $text;
+                               }
+                       }
+               }
+               return true;
+       }
+
+       /**
+        * Set up our custom edition system.
+        *
+        * @param Article $article  being edited
+        * @param User $user User performing the edit
+        * @return boolean hook return value
+        */
+       public static function onCustomEditor( $article, $user ) {
+               global $request;
+               if ( $article->getTitle()->inNamespace( 
ProofreadPage::getIndexNamespaceId() ) ) { //TODO ExternalEditor case
+                       $editor = new EditProofreadIndexPage( $article );
+                       $editor->edit();
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+}
\ No newline at end of file
diff --git a/includes/index/ProofreadIndexDbConnector.php 
b/includes/index/ProofreadIndexDbConnector.php
index b35d3c8..0c550ae 100644
--- a/includes/index/ProofreadIndexDbConnector.php
+++ b/includes/index/ProofreadIndexDbConnector.php
@@ -37,4 +37,13 @@
                );
                return $result;
        }
+
+       /**
+        * Remove index data from pr_index table.
+        * @param $pageId Integer: page identifier
+        */
+       public static function removeIndexData( $pageId ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'pr_index', array( 'pr_page_id' => $pageId ), 
__METHOD__ );
+       }
 }
\ No newline at end of file
diff --git a/includes/index/ProofreadPage.index.php 
b/includes/index/ProofreadPage.index.php
new file mode 100644
index 0000000..d5ca682
--- /dev/null
+++ b/includes/index/ProofreadPage.index.php
@@ -0,0 +1,249 @@
+<?php
+class ProofreadPageIndex{
+
+       /**
+        * Parser object for index pages
+        * @var Parser
+        */
+       private static $index_parser = null;
+
+       /**
+        * Read metadata from an index page.
+        * Depending on whether the index uses pagelist,
+        * it will return either a list of links or a list
+        * of parameters to pagelist, and a list of attributes.
+        * @param $index_title Title
+        * @return array
+        */
+       public static function parse_index( $index_title ) {
+               $err = array( false, false, array() );
+               if ( !$index_title ) {
+                       return $err;
+               }
+               if ( !$index_title->exists() ) {
+                       return $err;
+               }
+
+               $rev = Revision::newFromTitle( $index_title );
+               $text = $rev->getText();
+               return self::parse_index_text( $text );
+       }
+
+       /**
+        * @param $text string
+        * @return array
+        */
+       public static function parse_index_text( $text ) {
+               list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+               //check if it is using pagelist
+               preg_match_all( "/<pagelist([^<]*?)\/>/is", $text, $m, 
PREG_PATTERN_ORDER );
+               if( $m[1] ) {
+                       $params_s = '';
+                       for( $k = 0; $k < count( $m[1] ); $k++ ) {
+                               $params_s = $params_s . $m[1][$k];
+                       }
+                       $params = Sanitizer::decodeTagAttributes( $params_s );
+                       $links = null;
+               } else {
+                       $params = null;
+                       $tag_pattern = 
"/\[\[$page_namespace:(.*?)(\|(.*?)|)\]\]/i";
+                       preg_match_all( $tag_pattern, $text, $links, 
PREG_PATTERN_ORDER );
+               }
+
+               // read attributes
+               $attributes = array();
+               $config = ProofreadIndexPage::getDataConfig();
+               foreach( $config as $var_name => $property ) {
+                       $var = strtolower( $var_name );
+                       if( ( isset( $property['header'] ) && 
$property['header'] ) || in_array( $var, array( 'header', 'footer', 'css', 
'width' ) ) ) {
+                               if( isset( $property['hidden'] ) && 
$property['hidden'] ) {
+                                       if( isset( $property['default'] ) ) {
+                                               $attributes[$var] = 
$property['default'];
+                                       } else {
+                                               $attributes[$var] = '';
+                                       }
+                               } else {
+                                       $tag_pattern = "/\n\|" . $var_name . 
"=(.*?)\n(\||\}\})/is";
+                                       //$var = 'proofreadPage' . $var_name;
+
+                                       if( preg_match( $tag_pattern, $text, 
$matches ) ) {
+                                               $attributes[$var] = $matches[1];
+                                       } else {
+                                               $attributes[$var] = '';
+                                       }
+                               }
+                       }
+               }
+               return array( $links, $params, $attributes );
+       }
+
+       /**
+        * Return the ordered list of links to ns-0 from an index page
+        */
+       public static function parse_index_links( $index_title ) {
+               // Instanciate a new parser object to avoid side effects of 
$parser->replaceVariables
+               if( is_null( self::$index_parser ) ) {
+                       self::$index_parser = new Parser;
+               }
+               $rev = Revision::newFromTitle( $index_title );
+               $text = $rev->getText();
+               $options = new ParserOptions();
+               $rtext = self::$index_parser->preprocess( $text, $index_title, 
$options );
+               $text_links_pattern = "/\[\[\s*([^:\|]*?)\s*(\|(.*?)|)\]\]/i";
+               preg_match_all( $text_links_pattern, $rtext, $text_links, 
PREG_PATTERN_ORDER );
+               return $text_links;
+       }
+
+       /**
+        * Updates index data for an index referencing the specified page.
+        * @param $title Title: page title object
+        * @param $page_namespace 
+        * @param $index_namespace,
+        * @param $deleted Boolean: indicates whether the page was deleted
+        */
+       private static function updateIndexOfPage( $title, $page_namespace, 
$index_namespace, $deleted = false ) {
+               self::load_index( $title, $page_namespace, $index_namespace );
+               if ( $title->pr_index_title ) {
+                       $index_title = Title::newFromText( 
$title->pr_index_title );
+                       $index_title->invalidateCache();
+                       $index = new Article( $index_title );
+                       if ( $index ) {
+                               self::update_pr_index( $index, $deleted ? 
$title->getDBKey() : null );
+                       }
+               }
+       }
+
+       /**
+        * Query the database to find if the current page is referred in an 
Index page.
+        * @param $page_namespace 
+        * @param $index_namespace,
+        * @param $title Title
+        */
+       public static function load_index( $title, $page_namespace, 
$index_namespace ) {
+//             list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+
+               $title->pr_index_title = null;
+               $result = ProofreadIndexDbConnector::getRowsFromTitle( $title );
+
+               foreach ( $result as $x ) {
+                       $ref_title = Title::makeTitle( $x->page_namespace, 
$x->page_title );
+                       if ( $ref_title->inNamespace( 
self::getIndexNamespaceId() ) ) {
+                               $title->pr_index_title = 
$ref_title->getPrefixedText();
+                               break;
+                       }
+               }
+
+               if ( $title->pr_index_title ) {
+                       return;
+               }
+
+               $imageTitle = null;
+               /* check if we are a page of a multipage file */
+               if ( preg_match( "/^$page_namespace:(.*?)(\/(.*?)|)$/", 
$title->getPrefixedText(), $m ) ) {
+                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
+               }
+               if ( !$imageTitle ) {
+                       return;
+               }
+
+               $image = wfFindFile( $imageTitle );
+
+               // if it is multipage, we use the page order of the file
+               if ( $image && $image->exists() && $image->isMultipage() ) {
+                       $name = $image->getTitle()->getText();
+                       $index_name = "$index_namespace:$name";
+
+                       if ( !$title->pr_index_title ) {
+                               // there is no index, or the page is not listed 
in the index : use canonical index
+                               $title->pr_index_title = $index_name;
+                       }
+               }
+       }
+
+       /**
+        * Update the pr_index entry of an article
+        * @param $index Article
+        * @param $deletedpage null|string
+        */
+       public static function update_pr_index( $index, $deletedpage = null ) {
+               //list( $page_namespace, $index_namespace ) = 
self::getPageAndIndexNamespace();
+               $page_ns_index = ProofreadPage::getPageNamespaceId();
+               if ( $page_ns_index == null ) {
+                       return;
+               }
+
+               $index_title = $index->getTitle();
+               $index_id = $index->getID();
+               $dbr = wfGetDB( DB_SLAVE );
+
+               $n = 0;
+
+               // read the list of pages
+               $pages = array();
+               list( $links, $params, $attributes ) = 
ProofreadPageIndex::parse_index( $index_title );
+               if( $links == null ) {
+                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, 
$index_title->getText() );
+                       if ( $imageTitle ) {
+                               $image = wfFindFile( $imageTitle );
+                               if ( $image && $image->isMultipage() && 
$image->pageCount() ) {
+                                       $n = $image->pageCount();
+                                       for ( $i = 1; $i <= $n; $i++ ) {
+                                               $page = 
$index_title->getDBKey() . '/' . $i;
+                                               if( $page != $deletedpage ) {
+                                                       array_push( $pages, 
$page );
+                                               }
+                                       }
+                               }
+                       }
+               } else {
+                       $n = count( $links[1] );
+                       for ( $i = 0; $i < $n; $i++ ) {
+                               $page = str_replace( ' ' , '_' , $links[1][$i] 
);
+                               if( $page != $deletedpage ) {
+                                       array_push( $pages, $page );
+                               }
+                       }
+               }
+
+               if( !count( $pages ) ) {
+                       return;
+               }
+
+               $row = ProofreadPageDbConnector::getCountOfPages( 
$page_ns_index, $pages );
+               if( $row ) {
+                       $total = $row->count;
+               } else{
+                       return;
+               }
+
+               // proofreading status of pages
+               $queryArr = array(
+                       'tables' => array( 'page', 'categorylinks' ),
+                       'fields' => array( 'COUNT(page_id) AS count' ),
+                       'conds' => array( 'cl_to' => '', 'page_namespace' => 
$page_ns_index, 'page_title' => $pages ),
+                       'joins' => array( 'categorylinks' => array( 'LEFT 
JOIN', 'cl_from=page_id' ) )
+               );
+
+               $n0 = ProofreadPageDbConnector::queryCount( $queryArr, 
'proofreadpage_quality0_category' );
+               $n2 = ProofreadPageDbConnector::queryCount( $queryArr, 
'proofreadpage_quality2_category' );
+               $n3 = ProofreadPageDbConnector::queryCount( $queryArr, 
'proofreadpage_quality3_category' );
+               $n4 = ProofreadPageDbConnector::queryCount( $queryArr, 
'proofreadpage_quality4_category' );
+               $n1 = $total - $n0 - $n2 - $n3 - $n4;
+
+               $replace = ProofreadPageDbConnector::replaceIndex( $n, $n0, 
$n1, $n2, $n3, $n4, $index_id );
+       }
+
+       /**
+        * When an index page is created or purged, recompute pr_index values
+        * @param $article Article
+        * @return bool
+        */
+       public static function onArticlePurge( $article ) {
+               $title = $article->getTitle();
+               if ( $title->inNamespace( self::getIndexNamespaceId() ) ) {
+                       ProofreadPageIndex::update_pr_index( $article );
+                       return true;
+               }
+               return true;
+       }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b841a32f2ea00b742d8e06e71984bc0d6edce42
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: master
Gerrit-Owner: Rtdwivedi <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to