jenkins-bot has submitted this change and it was merged.

Change subject: Beginning of Page pages refactoring.
......................................................................


Beginning of Page pages refactoring.

Added ProofreadPagePage and ProofreadPageValue classes.

Change-Id: I52f69ac806929f4cba0f1c9399d2519165d96086
---
M ProofreadPage.php
A includes/page/ProofreadPageContent.php
A includes/page/ProofreadPagePage.php
3 files changed, 433 insertions(+), 2 deletions(-)

Approvals:
  Tpt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ProofreadPage.php b/ProofreadPage.php
index f989e27..ef346b1 100644
--- a/ProofreadPage.php
+++ b/ProofreadPage.php
@@ -40,16 +40,19 @@
 $wgExtensionMessagesFiles['ProofreadPageAlias'] = $dir . 
'ProofreadPage.alias.php';
 
 $wgAutoloadClasses['ProofreadPage'] = $dir . 'ProofreadPage.body.php';
-$wgAutoloadClasses['ProofreadPageDbConnector'] = $dir . 
'includes/page/ProofreadPageDbConnector.php';
 $wgAutoloadClasses['ProofreadPageInit'] = $dir . 
'includes/ProofreadPageInit.php';
 
 $wgAutoloadClasses['EditProofreadIndexPage'] = $dir . 
'includes/index/EditProofreadIndexPage.php';
-$wgAutoloadClasses['EditProofreadPagePage'] = $dir . 
'includes/page/EditProofreadPagePage.php';
 $wgAutoloadClasses['ProofreadIndexEntry'] = $dir . 
'includes/index/ProofreadIndexEntry.php';
 $wgAutoloadClasses['ProofreadIndexValue'] = $dir . 
'includes/index/ProofreadIndexValue.php';
 $wgAutoloadClasses['ProofreadIndexPage'] = $dir . 
'includes/index/ProofreadIndexPage.php';
 $wgAutoloadClasses['ProofreadIndexDbConnector'] = $dir . 
'includes/index/ProofreadIndexDbConnector.php';
 
+$wgAutoloadClasses['ProofreadPageDbConnector'] = $dir . 
'includes/page/ProofreadPageDbConnector.php';
+$wgAutoloadClasses['EditProofreadPagePage'] = $dir . 
'includes/page/EditProofreadPagePage.php';
+$wgAutoloadClasses['ProofreadPagePage'] = 
$dir.'includes/page/ProofreadPagePage.php';
+$wgAutoloadClasses['ProofreadPageValue'] = 
$dir.'includes/page/ProofreadPageValue.php';
+
 $wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'ProofreadPage',
diff --git a/includes/page/ProofreadPageContent.php 
b/includes/page/ProofreadPageContent.php
new file mode 100644
index 0000000..15a60f3
--- /dev/null
+++ b/includes/page/ProofreadPageContent.php
@@ -0,0 +1,258 @@
+<?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
+ */
+
+ /**
+  * A value of a page entry
+  */
+
+class ProofreadPageContent {
+
+       /**
+        * @var string header of the page
+        */
+       private $header;
+
+       /**
+        * @var string body of the page
+        */
+       private $body;
+
+       /**
+        * @var string footer of the page
+        */
+       private $footer;
+
+       /**
+        * @var User|null last proofreader of the page
+        */
+       protected $proofreader;
+
+       /**
+        * @var integer proofreading level of the page
+        */
+       protected $level = 1;
+
+       /**
+        * Constructor
+        * @param $header string
+        * @param $body string
+        * @param $footer string
+        * @param $level integer
+        * @param $proofreader User|string
+        */
+       public function __construct( $header = '', $body = '', $footer = '', 
$level = 1, $proofreader = null ) {
+               $this->setHeader( $header );
+               $this->setBody( $body );
+               $this->setFooter( $footer );
+               $this->setLevel( $level );
+               if ( is_string( $proofreader ) ) {
+                       $this->setProofreaderFromName( $proofreader );
+               } else {
+                       $this->setProofreader( $proofreader );
+               }
+       }
+
+       /**
+        * returns the header of the page
+        * @return string
+        */
+       public function getHeader() {
+               return $this->header;
+       }
+
+       /**
+        * returns the body of the page
+        * @return string
+        */
+       public function getBody(){
+               return $this->body;
+       }
+
+       /**
+        * returns the footer of the page
+        * @return string
+        */
+       public function getFooter() {
+               return $this->footer;
+       }
+
+       /**
+        * returns the proofreading level of the page.
+        * @return integer
+        */
+       public function getProofreadingLevels() {
+               return $this->level;
+       }
+
+       /**
+        * returns last proofreader of the page
+        * @return User
+        */
+       public function getProofreader() {
+               return $this->proofreader;
+       }
+
+       /**
+        * Sets value of the header
+        * @param $header string
+        * @throws MWException
+        */
+       public function setHeader( $header ) {
+               if ( !is_string( $header ) ) {
+                       throw new MWException( 'header must be a string.' );
+               }
+               $this->header = $header;
+       }
+
+       /**
+        * Sets value of the body
+        * @param $body string
+        * @throws MWException
+        */
+       public function setBody( $body ) {
+               if ( !is_string( $body ) ) {
+                       throw new MWException( 'body must be a string.' );
+               }
+               $this->body = $body;
+       }
+
+       /**
+        * Sets value of the footer
+        * @param $footer string
+        * @throws MWException
+        */
+       public function setFooter( $footer ) {
+               if ( !is_string( $footer ) ) {
+                       throw new MWException( 'footer must be a string.' );
+               }
+               $this->footer = $footer;
+       }
+
+       /**
+        * Sets the last proofreader
+        * @param $proofreader User
+        */
+       public static function setProofreader( User $user ) {
+               $this->proofreader = $user;
+       }
+
+       /**
+       * Sets the last proofreader from his name
+       * @param $name string
+       * @throws MWException
+       */
+       public static function setProofreaderFromName( $name ) {
+               if ( $name === '' ) {
+                       $this->proofreader = null;
+               } elseif ( IP::isValid( $name ) ) {
+                       $this->proofreader = User::newFromName( IP::sanitizeIP( 
$name ), false );
+               } else {
+                       $name = User::newFromName( $name );
+                       if ( $name === false ) {
+                               throw new MWException( 'Name is an invalid 
username.' );
+                       } else {
+                               $this->proofreader = $name;
+                       }
+               }
+       }
+
+       /**
+        * Sets level
+        * @param $level integer
+        * @throws MWException
+        */
+       public static function setLevel( $level ) {
+               if ( !is_integer( $level ) || $level < 0 || $level > 4 ) {
+                       throw new MWException( 'level must be an integer 
between 0 and 4.' );
+               }
+               $this->level = $level;
+       }
+
+       /**
+        * Serialize the content of the ProofreadPageValue to wikitext
+        * @return string
+        */
+       public function serialize() {
+               $text = '<noinclude><pagequality level="' . $this->level . '" 
user="';
+               if ( $this->proofreader !== null ) {
+                       $text .= $this->proofreader->getName();
+               }
+               $text .= '" /><div class="pagetext">' . $this->header. "\n\n\n" 
. '</noinclude>';
+               $text .= $this->body;
+               $text .= '<noinclude>' . $this->footer . '</div></noinclude>';
+               return $text;
+       }
+
+       /**
+        * Parse a wikitext to setup the content of the ProofreadPageValue
+        * @param $text string
+        * @throws MWException
+        */
+       public function unserialize( $text ) {
+               if ( !preg_match( 
'/^<noinclude>(.*?)<\/noinclude>(.*?)<noinclude>(.*?)<\/noinclude>$/s', $text, 
$m ) ) {
+                       throw new MWException( 'The serialize value of the page 
is not valid.' );
+               }
+               $header = $m[1];
+               $this->setBody( $m[2] );
+               $this->setFooter( $m[3] );
+
+               //Extract quality
+               if ( preg_match( '/^<pagequality level=\"(0|1|2|3|4)\" 
user=\"(.*?)\" \/>(.*)$/', $header, $m ) ) {
+                       $this->setHeader( $m[3] );
+                       $this->setProofreaderFromName( $m[2] );
+                       $this->setLevel( inval( $m[1] ) );
+               } elseif( preg_match( 
'/^\{\{PageQuality\|(0|1|2|3|4)(|\|(.*?))\}\}(.*)/is', $header, $m ) ){
+                       $this->setHeader( $m[4] );
+                       $this->setProofreaderFromName( $m[3] );
+                       $this->setLevel( inval( $m[1] ) );
+               } else {
+                       $this->setHeader( $header );
+               }
+       }
+
+       /**
+        * Create a new empty ProofreadPageValue
+        * @return ProofreadPageValue
+        */
+       public static function newEmpty() {
+               return new self();
+       }
+
+       /**
+        * Create a new ProofreadPageValue from a ProofreadIndexPage
+        * @param $index ProofreadIndexPage
+        * @return ProofreadPageValue
+        */
+       public static function newFromWikitext( $text ) {
+               $value = new self();
+               try {
+                       $value->unserialize( $text );
+               } catch( MWExeption $e ) {
+                       $value->setBody( $text );
+               }
+               return $value;
+       }
+
+       /**
+        * Returns if the value is empty
+        * @return bool
+        */
+       public function isEmpty() {
+               return $this->body === '';
+       }
+}
diff --git a/includes/page/ProofreadPagePage.php 
b/includes/page/ProofreadPagePage.php
new file mode 100644
index 0000000..509465c
--- /dev/null
+++ b/includes/page/ProofreadPagePage.php
@@ -0,0 +1,170 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+/**
+ * The content of a page page
+ */
+class ProofreadPagePage {
+
+       /**
+        * @var Title
+        */
+       protected $title;
+
+       /**
+        * @var ProofreadPageContent content of the page
+        */
+       protected $content;
+
+       /**
+        * @var ProofreadIndexPage|null index related to the page
+        */
+       protected $index;
+
+       /**
+        * Constructor
+        * @param $title Title Reference to a Title object.
+        * @param $content ProofreadPageContent content of the page. Warning: 
only done for EditProofreadPagePage use.
+        * @param $index ProofreadIndexPage index related to the page.
+        */
+       public function __construct( Title $title, ProofreadPageContent 
$content = null, ProofreadIndexPage $index = null ) {
+               $this->title = $title;
+               $this->content = $content;
+               $this->index = $index;
+       }
+
+       /**
+        * Create a new ProofreadPagePage from a Title object
+        * @param $title Title
+        * @return ProofreadPagePage
+        */
+       public static function newFromTitle( Title $title ) {
+               return new self( $title, null, null );
+       }
+       /**
+        * Returns Title of the index page
+        * @return Title
+        */
+       public function getTitle() {
+               return $this->title;
+       }
+
+       /**
+        * Returns number of the page in the file if it's a multi-page file or 
null
+        * @return integer|null
+        */
+       protected function getPageNumber() {
+               $parts = explode( '/', $this->title->getText() );
+               if ( count( $parts ) === 1 ) {
+                       return null;
+               }
+               $val = $wgContLang->parseFormattedNumber( $parts[count( $parts 
) - 1] );
+               if ( !is_integer( $val ) ) {
+                       return null;
+               }
+               return (int) $val;
+       }
+
+       /**
+        * Return index of the page if it exist or false.
+        * @return ProofreadIndexPage|false
+        */
+       public function getIndex() {
+               if( $this->index !== null ) {
+                       return $this->index;
+               }
+
+               $result = ProofreadIndexDbConnector::getRowsFromTitle( 
$this->title() );
+
+               foreach ( $result as $x ) {
+                       $refTitle = Title::makeTitle( $x->page_namespace, 
$x->page_title );
+                       if ( $refTitle !== null && $refTitle->inNamespace( 
ProofreadPage::getIndexNamespaceId() ) ) {
+                               $this->index = 
ProofreadIndexPage::newFromTitle( $refTitle );
+                               return $this->index;
+                       }
+               }
+
+               $m = explode( '/', $title->getText(), 2 );
+               if ( isset( $m[1] ) ) {
+                       $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[0] );
+                       if ( $imageTitle !== null ) {
+                               $image = wfFindFile( $imageTitle );
+                               // if it is multipage, we use the page order of 
the file
+                               if ( $image !== null && $image->exists() && 
$image->isMultipage() ) {
+                                       $indexTitle = Title::makeTitle( 
ProofreadPage::getIndexNamespaceId(), $image->getTitle()->getText());
+                                       if ( $indexTitle !== null ) {
+                                               $this->index = 
ProofreadIndexPage::newFromTitle( $indexTitle );
+                                               return $this->index;
+                                       }
+                               }
+                       }
+               }
+               $this->index = false;
+               return false;
+       }
+
+       /**
+        * Return content of the page
+        * @return ProofreadPageValue
+        */
+       protected function getContent() {
+               if ( $this->content === null ) {
+                       $rev = Revision::newFromTitle( $this->title );
+                       if ( $rev === null ) {
+                               $this->content = ProofreadPageValue::newEmpty();
+                       } else {
+                               $this->content = 
ProofreadPageValue::newFromWikitext( $rev->getText() );
+                       }
+               }
+               return $this->content;
+       }
+
+       /**
+        * Return content of the page initialised for edition
+        * @return ProofreadPageValue
+        */
+       public function getContentForEdition() {
+               global $wgContLang;
+               $content = $this->getContent();
+               if ( $content->isEmpty() ) {
+                       $index = $this->getIndex();
+                       if ( $index ) {
+                               list( $header, $footer, $css, $editWidth ) = 
$index->getIndexDataForPage();
+                               $content->setHeader( $header );
+                               $content->setFooter( $footer );
+
+                               //Extract text layer
+                               $image = $index->getImage();
+                               $pageNumber = $this->getPageNumber();
+                               if ( $image && $pageNumber !== null && 
$image->exists() ) {
+                                       $text = 
$image->getHandler()->getPageText( $image, $pageNumber );
+                                       if ( $text ) {
+                                               $text = preg_replace( 
"/(\\\\n)/", "\n", $text );
+                                               $text = preg_replace( 
"/(\\\\\d*)/", '', $text );
+                                               $content->setBody( $text );
+                                       }
+                               }
+                       }
+               }
+               return $content;
+       }
+
+}
\ No newline at end of file

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I52f69ac806929f4cba0f1c9399d2519165d96086
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: pagePagesRefactoring
Gerrit-Owner: Rtdwivedi <ellydwivedi2...@gmail.com>
Gerrit-Reviewer: Rtdwivedi <ellydwivedi2...@gmail.com>
Gerrit-Reviewer: Tpt <thoma...@hotmail.fr>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to