Tobias Gritschacher has uploaded a new change for review.

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


Change subject: Use Summary class and ChangeOps for SpecialPages
......................................................................

Use Summary class and ChangeOps for SpecialPages

Bug: 52390
Change-Id: Ida39b1a1e6de2268d0387689007e0ceb618ed464
---
M repo/includes/specials/SpecialModifyEntity.php
M repo/includes/specials/SpecialNewEntity.php
M repo/includes/specials/SpecialSetAliases.php
M repo/includes/specials/SpecialSetDescription.php
M repo/includes/specials/SpecialSetEntity.php
M repo/includes/specials/SpecialSetLabel.php
M repo/includes/specials/SpecialSetSiteLink.php
7 files changed, 74 insertions(+), 98 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/84/78084/4

diff --git a/repo/includes/specials/SpecialModifyEntity.php 
b/repo/includes/specials/SpecialModifyEntity.php
index 3c1992d..f0c6d3c 100644
--- a/repo/includes/specials/SpecialModifyEntity.php
+++ b/repo/includes/specials/SpecialModifyEntity.php
@@ -1,6 +1,6 @@
 <?php
 
-use Wikibase\Autocomment;
+use Wikibase\Summary;
 use Wikibase\Utils;
 
 /**
@@ -84,7 +84,7 @@
                        // TODO: need conflict detection??
                        $editEntity = new \Wikibase\EditEntity( 
$this->entityContent, $this->getUser(), false, $this->getContext() );
                        $editEntity->attemptSave(
-                               $summary,
+                               $summary->toString(),
                                EDIT_UPDATE,
                                $this->getRequest()->getVal( 'wpEditToken' )
                        );
@@ -248,33 +248,12 @@
         *
         * @since 0.4
         *
-        * @return string|boolean The summary or false
+        * @return Summary|boolean The summary or false
         */
        abstract protected function modifyEntity();
 
-       /**
-        * Returning the summary for editing.
-        *
-        * @since 0.4
-        *
-        * @param string $key
-        * @param string $value
-        * @param string $i18n The i18n key of the summary
-        *
-        * @return string The summary
-        */
-       protected function getSummary( $key, $value, $i18n ) {
-               list( $counts, $summary, $lang ) = 
Autocomment::formatAutoSummary(
-                       array( $value ),
-                       $this->getLanguage()
-               );
-
-               $comment = Autocomment::formatAutoComment(
-                       $i18n,
-                       array( $counts, $key )
-               );
-
-               return AutoComment::formatTotalSummary( $comment, $summary, 
$lang );
+       protected function getSummary( $module = null ) {
+               return new Summary( $module );
        }
 
        /**
diff --git a/repo/includes/specials/SpecialNewEntity.php 
b/repo/includes/specials/SpecialNewEntity.php
index 43bbdb1..a63ee4f 100644
--- a/repo/includes/specials/SpecialNewEntity.php
+++ b/repo/includes/specials/SpecialNewEntity.php
@@ -1,7 +1,7 @@
 <?php
 
 use Wikibase\EntityContent;
-use Wikibase\Autocomment;
+use Wikibase\Summary;
 use Wikibase\Utils;
 
 /**
@@ -95,17 +95,12 @@
                                $status = $this->modifyEntity( $entityContent );
 
                                if ( $status->isGood() ) {
-                                       list( $counts, $summary, $lang ) = 
Autocomment::formatAutoSummary(
-                                               array( $this->label, 
$this->description ),
-                                               $this->getLanguage()
-                                       );
-                                       $comment = 
Autocomment::formatAutoComment(
-                                               'wbeditentity-create',
-                                               array( $counts, 
$this->getLanguage()->getCode() )
-                                       );
+                                       $summary = new Summary( 'wbeditentity', 
'create' );
+                                       $summary->setLanguage( 
$this->getLanguage()->getCode() );
+                                       $summary->addAutoSummaryArgs( array( 
$this->label, $this->description ) );
                                        $editEntity = new \Wikibase\EditEntity( 
$entityContent, $this->getUser(), false, $this->getContext() );
                                        $editEntity->attemptSave(
-                                               
AutoComment::formatTotalSummary( $comment, $summary, $lang ),
+                                               $summary->toString(),
                                                EDIT_AUTOSUMMARY|EDIT_NEW,
                                                $this->getRequest()->getVal( 
'token' )
                                        );
diff --git a/repo/includes/specials/SpecialSetAliases.php 
b/repo/includes/specials/SpecialSetAliases.php
index 4d20b64..8f7376b 100644
--- a/repo/includes/specials/SpecialSetAliases.php
+++ b/repo/includes/specials/SpecialSetAliases.php
@@ -1,5 +1,7 @@
 <?php
 
+use Wikibase\ChangeOpAliases;
+
 /**
  * Special page for setting the aliases of a Wikibase entity.
  *
@@ -26,6 +28,7 @@
  * @licence GNU GPL v2+
  * @author Bene* < [email protected] >
  */
+
 class SpecialSetAliases extends SpecialSetEntity {
 
        /**
@@ -70,13 +73,19 @@
         * @param \Wikibase\EntityContent $entityContent
         * @param string $language
         * @param string $value
-        * @param string &$summary The summary for this edit will be saved here.
         *
-        * @return Status
+        * @return Summary
         */
-       protected function setValue( $entityContent, $language, $value, 
&$summary ) {
-               $entityContent->getEntity()->setAliases( $language, explode( 
'|', $value ) );
-               $summary = $this->getSummary( $language, $value, 
'wbsetaliases-set' );
-               return \Status::newGood();
+       protected function setValue( $entityContent, $language, $value ) {
+               $summary = $this->getSummary( 'wbsetaliases' );
+               $entity = $entityContent->getEntity();
+               if ( $value === '' ) {
+                       $changeOp = new ChangeOpAliases( $language, 
$entity->getAliases( $language ), 'remove' );
+               } else {
+                       $changeOp = new ChangeOpAliases( $language, explode( 
'|', $value ), 'set' );
+               }
+               $changeOp->apply( $entity, $summary );
+
+               return $summary;
        }
 }
\ No newline at end of file
diff --git a/repo/includes/specials/SpecialSetDescription.php 
b/repo/includes/specials/SpecialSetDescription.php
index cb99ee8..7408c55 100644
--- a/repo/includes/specials/SpecialSetDescription.php
+++ b/repo/includes/specials/SpecialSetDescription.php
@@ -1,5 +1,7 @@
 <?php
 
+use Wikibase\ChangeOpDescription;
+
 /**
  * Special page for setting the description of a Wikibase entity.
  *
@@ -70,20 +72,15 @@
         * @param \Wikibase\EntityContent $entityContent
         * @param string $language
         * @param string $value
-        * @param string &$summary The summary for this edit will be saved here.
         *
-        * @return Status
+        * @return Summary
         */
-       protected function setValue( $entityContent, $language, $value, 
&$summary ) {
-               if( $value === '' ) {
-                       $entityContent->getEntity()->removeDescription( 
$language );
-                       $i18n = 'wbsetdescription-remove';
-               }
-               else {
-                       $entityContent->getEntity()->setDescription( $language, 
$value );
-                       $i18n = 'wbsetdescription-set';
-               }
-               $summary = $this->getSummary( $language, $value, $i18n );
-               return \Status::newGood();
+       protected function setValue( $entityContent, $language, $value ) {
+               $value = $value === '' ? null : $value;
+               $summary = $this->getSummary( 'wbsetdescription' );
+               $changeOp = new ChangeOpDescription( $language, $value );
+               $changeOp->apply( $entityContent->getEntity(), $summary );
+
+               return $summary;
        }
 }
\ No newline at end of file
diff --git a/repo/includes/specials/SpecialSetEntity.php 
b/repo/includes/specials/SpecialSetEntity.php
index f169af1..45028f0 100644
--- a/repo/includes/specials/SpecialSetEntity.php
+++ b/repo/includes/specials/SpecialSetEntity.php
@@ -1,6 +1,7 @@
 <?php
 
-use Wikibase\Autocomment;
+use Wikibase\Summary;
+use Wikibase\ChangeOpException;
 
 /**
  * Abstract special page for setting a value of a Wikibase entity.
@@ -96,7 +97,7 @@
         *
         * @since 0.4
         *
-        * @return string|boolean The summary or false
+        * @return Summary|bool
         */
        protected function modifyEntity() {
                $request = $this->getRequest();
@@ -119,10 +120,10 @@
                        return false;
                }
 
-               $status = $this->setValue( $this->entityContent, 
$this->language, $this->value, $summary );
-
-               if ( !$status->isGood() ) {
-                       $this->showErrorHTML( $status->getHTML() );
+               try {
+                       $summary = $this->setValue( $this->entityContent, 
$this->language, $this->value );
+               } catch ( ChangeOpException $e ) {
+                       $this->showErrorHTML( $e->getMessage() );
                        return false;
                }
 
@@ -249,10 +250,9 @@
         * @param \Wikibase\EntityContent $entityContent
         * @param string $language
         * @param string $value
-        * @param string &$summary The summary for this edit will be saved here.
         *
-        * @return Status
+        * @return Summary
         */
-       abstract protected function setValue( $entityContent, $language, 
$value, &$summary );
+       abstract protected function setValue( $entityContent, $language, $value 
);
 
 }
diff --git a/repo/includes/specials/SpecialSetLabel.php 
b/repo/includes/specials/SpecialSetLabel.php
index 8074065..0c9e017 100644
--- a/repo/includes/specials/SpecialSetLabel.php
+++ b/repo/includes/specials/SpecialSetLabel.php
@@ -1,5 +1,7 @@
 <?php
 
+use Wikibase\ChangeOpLabel;
+
 /**
  * Special page for setting the label of a Wikibase entity.
  *
@@ -26,6 +28,7 @@
  * @licence GNU GPL v2+
  * @author Bene* < [email protected] >
  */
+
 class SpecialSetLabel extends SpecialSetEntity {
 
        /**
@@ -70,20 +73,15 @@
         * @param \Wikibase\EntityContent $entityContent
         * @param string $language
         * @param string $value
-        * @param string &$summary The summary for this edit will be saved here.
         *
-        * @return Status
+        * @return Summary
         */
-       protected function setValue( $entityContent, $language, $value, 
&$summary ) {
-               if( $value === '' ) {
-                       $entityContent->getEntity()->removeLabel( $language );
-                       $i18n = 'wbsetlabel-remove';
-               }
-               else {
-                       $entityContent->getEntity()->setLabel( $language, 
$value );
-                       $i18n = 'wbsetlabel-set';
-               }
-               $summary = $this->getSummary( $language, $value, $i18n );
-               return \Status::newGood();
+       protected function setValue( $entityContent, $language, $value ) {
+               $value = $value === '' ? null : $value;
+               $summary = $this->getSummary( 'wbsetlabel' );
+               $changeOp = new ChangeOpLabel( $language, $value );
+               $changeOp->apply( $entityContent->getEntity(), $summary );
+
+               return $summary;
        }
 }
\ No newline at end of file
diff --git a/repo/includes/specials/SpecialSetSiteLink.php 
b/repo/includes/specials/SpecialSetSiteLink.php
index 0e6638b..60478c2 100644
--- a/repo/includes/specials/SpecialSetSiteLink.php
+++ b/repo/includes/specials/SpecialSetSiteLink.php
@@ -4,6 +4,8 @@
 use Wikibase\EntityContent;
 use Wikibase\SiteLink;
 use Wikibase\Utils;
+use Wikibase\ChangeOpSiteLink;
+use Wikibase\ChangeOpException;
 
 /**
  * Special page for setting the sitepage of a Wikibase entity.
@@ -117,7 +119,12 @@
                        return false;
                }
 
-               $status = $this->setSiteLink( $this->entityContent, 
$this->site, $this->page, $summary );
+               try {
+                       $status = $this->setSiteLink( $this->entityContent, 
$this->site, $this->page, $summary );
+               } catch ( ChangeOpException $e ) {
+                       $this->showErrorHTML( $e->getMessage() );
+                       return false;
+               }
 
                if ( !$status->isGood() ) {
                        $this->showErrorHTML( $status->getHTML() );
@@ -248,11 +255,11 @@
         * @param EntityContent $entityContent
         * @param string $siteId
         * @param string $pageName
-        * @param string &$summary The summary for this edit will be saved here.
+        * @param Summary &$summary The summary for this edit will be saved 
here.
         *
         * @return Status
         */
-       protected function setSiteLink( $entityContent, $siteId, $pageName, 
&$summary ) {
+       protected function setSiteLink( EntityContent $entityContent, $siteId, 
$pageName, &$summary ) {
                $status = \Status::newGood();
                $site = \Sites::singleton()->getSite( $siteId );
 
@@ -261,37 +268,28 @@
                        return $status;
                }
 
-               /**
-                * @var Item $item
-                */
                $item = $entityContent->getItem();
+               $summary = $this->getSummary( 'wbsetsitelink' );
 
-               // Empty page means remove site link
                if ( $pageName === '' ) {
-                       try {
-                               $item->getSimpleSiteLink( $siteId );
-                       }
-                       catch( \OutOfBoundsException $ex ) {
+                       $pageName = null;
+
+                       if ( !$item->hasLinkToSite( $siteId ) ) {
                                $status->fatal( 
'wikibase-setsitelink-remove-failed' );
                                return $status;
                        }
-                       $item->removeSiteLink( $siteId );
-                       $i18n = 'wbsetsitelink-remove';
-               }
-               else {
-                       // Try to normalize the page name
+               } else {
                        $pageName = $site->normalizePageName( $pageName );
+
                        if ( $pageName === false ) {
                                $status->fatal( 
'wikibase-error-ui-no-external-page' );
                                return $status;
                        }
-                       $siteLink = new SimpleSiteLink( $siteId, $pageName );
-                       $item->addSimpleSiteLink( $siteLink );
-                       $i18n = 'wbsetsitelink-set';
                }
 
-               // $summary is passed by reference ( &$summary )
-               $summary = $this->getSummary( $siteId, $pageName, $i18n );
+               $changeOp = new ChangeOpSiteLink( $siteId, $pageName );
+
+               $changeOp->apply( $item, $summary );
 
                return $status;
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ida39b1a1e6de2268d0387689007e0ceb618ed464
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to