Legoktm has uploaded a new change for review.

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

Change subject: Add Collation to MediaWikiServices
......................................................................

Add Collation to MediaWikiServices

Also add a Collation::getName() function to return the name of the
currently configured collation type to avoid usage of
$wgCategoryCollation.

Change-Id: I6fa8ad9a5ae76d44801bf70e8236b51814edda39
---
M includes/CategoryViewer.php
M includes/MediaWikiServices.php
M includes/MovePage.php
M includes/ServiceWiring.php
M includes/api/ApiQueryCategoryMembers.php
M includes/collation/Collation.php
M includes/collation/CollationCkb.php
M includes/collation/CollationEt.php
M includes/collation/IcuCollation.php
M includes/collation/IdentityCollation.php
M includes/collation/NumericUppercaseCollation.php
M includes/collation/UppercaseCollation.php
M includes/deferred/LinksUpdate.php
M maintenance/updateCollation.php
M tests/phpunit/includes/MediaWikiServicesTest.php
15 files changed, 82 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/38/313338/1

diff --git a/includes/CategoryViewer.php b/includes/CategoryViewer.php
index c858dd7..0b622e9 100644
--- a/includes/CategoryViewer.php
+++ b/includes/CategoryViewer.php
@@ -98,7 +98,7 @@
                $this->limit = $context->getConfig()->get( 
'CategoryPagingLimit' );
                $this->cat = Category::newFromTitle( $title );
                $this->query = $query;
-               $this->collation = Collation::singleton();
+               $this->collation = 
MediaWikiServices::getInstance()->getCollation();
                unset( $this->query['title'] );
        }
 
diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php
index b16044e..f12695e 100644
--- a/includes/MediaWikiServices.php
+++ b/includes/MediaWikiServices.php
@@ -1,6 +1,7 @@
 <?php
 namespace MediaWiki;
 
+use Collation;
 use Config;
 use ConfigFactory;
 use EventRelayerGroup;
@@ -442,6 +443,14 @@
        }
 
        /**
+        * @since 1.28
+        * @return Collation
+        */
+       public function getCollation() {
+               return $this->getService( 'Collation' );
+       }
+
+       /**
         * @since 1.27
         * @return StatsdDataFactory
         */
diff --git a/includes/MovePage.php b/includes/MovePage.php
index 5f1dd3f..b532f82 100644
--- a/includes/MovePage.php
+++ b/includes/MovePage.php
@@ -228,8 +228,6 @@
         * @return Status
         */
        public function move( User $user, $reason, $createRedirect ) {
-               global $wgCategoryCollation;
-
                Hooks::run( 'TitleMove', [ $this->oldTitle, $this->newTitle, 
$user ] );
 
                // If it is a file, move it first.
@@ -276,14 +274,15 @@
                } else {
                        $type = 'page';
                }
+               $collation = MediaWikiServices::getInstance()->getCollation();
                foreach ( $prefixes as $prefixRow ) {
                        $prefix = $prefixRow->cl_sortkey_prefix;
                        $catTo = $prefixRow->cl_to;
                        $dbw->update( 'categorylinks',
                                [
-                                       'cl_sortkey' => 
Collation::singleton()->getSortKey(
+                                       'cl_sortkey' => $collation->getSortKey(
                                                        
$this->newTitle->getCategorySortkey( $prefix ) ),
-                                       'cl_collation' => $wgCategoryCollation,
+                                       'cl_collation' => $collation->getName(),
                                        'cl_type' => $type,
                                        'cl_timestamp=cl_timestamp' ],
                                [
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
index 6044911..7414295 100644
--- a/includes/ServiceWiring.php
+++ b/includes/ServiceWiring.php
@@ -103,6 +103,12 @@
                );
        },
 
+       'Collation' => function( MediaWikiServices $services ) {
+               return Collation::factory(
+                       $services->getMainConfig()->get( 'CategoryCollation' )
+               );
+       },
+
        'StatsdDataFactory' => function( MediaWikiServices $services ) {
                return new BufferingStatsdDataFactory(
                        rtrim( $services->getMainConfig()->get( 
'StatsdMetricPrefix' ), '.' )
diff --git a/includes/api/ApiQueryCategoryMembers.php 
b/includes/api/ApiQueryCategoryMembers.php
index 4865ad5..257e5b8 100644
--- a/includes/api/ApiQueryCategoryMembers.php
+++ b/includes/api/ApiQueryCategoryMembers.php
@@ -24,6 +24,8 @@
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * A query module to enumerate pages that belong to a category.
  *
@@ -150,7 +152,8 @@
                                $this->addWhereRange( 'cl_from', $dir, null, 
null );
                        } else {
                                if ( $params['startsortkeyprefix'] !== null ) {
-                                       $startsortkey = 
Collation::singleton()->getSortKey( $params['startsortkeyprefix'] );
+                                       $collation = 
MediaWikiServices::getInstance()->getCollation();
+                                       $startsortkey = $collation->getSortKey( 
$params['startsortkeyprefix'] );
                                } elseif ( $params['starthexsortkey'] !== null 
) {
                                        if ( !$this->validateHexSortkey( 
$params['starthexsortkey'] ) ) {
                                                $this->dieUsage( 'The 
starthexsortkey provided is not valid', 'bad_starthexsortkey' );
@@ -160,7 +163,8 @@
                                        $startsortkey = $params['startsortkey'];
                                }
                                if ( $params['endsortkeyprefix'] !== null ) {
-                                       $endsortkey = 
Collation::singleton()->getSortKey( $params['endsortkeyprefix'] );
+                                       $collation = 
MediaWikiServices::getInstance()->getCollation();
+                                       $endsortkey = $collation->getSortKey( 
$params['endsortkeyprefix'] );
                                } elseif ( $params['endhexsortkey'] !== null ) {
                                        if ( !$this->validateHexSortkey( 
$params['endhexsortkey'] ) ) {
                                                $this->dieUsage( 'The 
endhexsortkey provided is not valid', 'bad_endhexsortkey' );
diff --git a/includes/collation/Collation.php b/includes/collation/Collation.php
index 881c8c2..581501c 100644
--- a/includes/collation/Collation.php
+++ b/includes/collation/Collation.php
@@ -20,23 +20,22 @@
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * @since 1.16.3
  * @author Tim Starling
  */
 abstract class Collation {
-       private static $instance;
-
        /**
+        * @deprecated since 1.28 use MediaWikiServices instead
+        *
         * @since 1.16.3
         * @return Collation
         */
        public static function singleton() {
-               if ( !self::$instance ) {
-                       global $wgCategoryCollation;
-                       self::$instance = self::factory( $wgCategoryCollation );
-               }
-               return self::$instance;
+               wfDeprecated( __METHOD__, '1.28' );
+               return MediaWikiServices::getInstance()->getCollation();
        }
 
        /**
@@ -54,9 +53,9 @@
                        case 'identity':
                                return new IdentityCollation;
                        case 'uca-default':
-                               return new IcuCollation( 'root' );
+                               return new IcuCollation( 'root', $collationName 
);
                        case 'uca-default-u-kn':
-                               return new IcuCollation( 'root-u-kn' );
+                               return new IcuCollation( 'root-u-kn', 
$collationName );
                        case 'xx-uca-ckb':
                                return new CollationCkb;
                        case 'xx-uca-et':
@@ -64,7 +63,7 @@
                        default:
                                $match = [];
                                if ( preg_match( '/^uca-([a-z@=-]+)$/', 
$collationName, $match ) ) {
-                                       return new IcuCollation( $match[1] );
+                                       return new IcuCollation( $match[1], 
$collationName );
                                }
 
                                # Provide a mechanism for extensions to hook in.
@@ -81,6 +80,19 @@
        }
 
        /**
+        * Returns the name of the collation, falls back
+        * to class name for subclasses that haven't updated
+        * to implement this
+        *
+        * @since 1.28
+        * @return string
+        */
+       public function getName() {
+               wfWarn( __METHOD__ . ': does not implement 
Collation::getName()' );
+               return get_class( $this );
+       }
+
+       /**
         * Given a string, convert it to a (hopefully short) key that can be 
used
         * for efficient sorting.  A binary sort according to the sortkeys
         * corresponds to a logical sort of the corresponding strings.  Current
diff --git a/includes/collation/CollationCkb.php 
b/includes/collation/CollationCkb.php
index 01a4f7f..e670195 100644
--- a/includes/collation/CollationCkb.php
+++ b/includes/collation/CollationCkb.php
@@ -28,7 +28,7 @@
 class CollationCkb extends IcuCollation {
        public function __construct() {
                // This will set $locale and collators, which affect the actual 
sorting order
-               parent::__construct( 'fa' );
+               parent::__construct( 'fa', 'xx-uca-ckb' );
                // Override the 'fa' language set by parent constructor, which 
affects #getFirstLetterData()
                $this->digitTransformLanguage = Language::factory( 'ckb' );
        }
diff --git a/includes/collation/CollationEt.php 
b/includes/collation/CollationEt.php
index 5dc9fa2..c17fbf3 100644
--- a/includes/collation/CollationEt.php
+++ b/includes/collation/CollationEt.php
@@ -30,7 +30,7 @@
  */
 class CollationEt extends IcuCollation {
        public function __construct() {
-               parent::__construct( 'et' );
+               parent::__construct( 'et', 'xx-uca-et' );
        }
 
        private static function mangle( $string ) {
diff --git a/includes/collation/IcuCollation.php 
b/includes/collation/IcuCollation.php
index 9c0b96e..253df08 100644
--- a/includes/collation/IcuCollation.php
+++ b/includes/collation/IcuCollation.php
@@ -42,6 +42,9 @@
        /** @var array */
        private $firstLetterData;
 
+       /** @var string */
+       private $collationName;
+
        /**
         * Unified CJK blocks.
         *
@@ -182,12 +185,12 @@
         */
        const RECORD_LENGTH = 14;
 
-       public function __construct( $locale ) {
+       public function __construct( $locale, $collationName ) {
                if ( !extension_loaded( 'intl' ) ) {
                        throw new MWException( 'An ICU collation was requested, 
' .
                                'but the intl extension is not available.' );
                }
-
+               $this->collationName = $collationName;
                $this->locale = $locale;
                // Drop everything after the '@' in locale's name
                $localeParts = explode( '@', $locale );
@@ -211,6 +214,13 @@
                }
        }
 
+       /**
+        * @return string
+        */
+       public function getName() {
+               return $this->collationName;
+       }
+
        public function getSortKey( $string ) {
                return $this->mainCollator->getSortKey( $string );
        }
diff --git a/includes/collation/IdentityCollation.php 
b/includes/collation/IdentityCollation.php
index 46e7f38..5c51fe2 100644
--- a/includes/collation/IdentityCollation.php
+++ b/includes/collation/IdentityCollation.php
@@ -28,6 +28,10 @@
  */
 class IdentityCollation extends Collation {
 
+       public function getName() {
+               return 'identity';
+       }
+
        public function getSortKey( $string ) {
                return $string;
        }
diff --git a/includes/collation/NumericUppercaseCollation.php 
b/includes/collation/NumericUppercaseCollation.php
index 4bf2f73..40655ca 100644
--- a/includes/collation/NumericUppercaseCollation.php
+++ b/includes/collation/NumericUppercaseCollation.php
@@ -27,6 +27,10 @@
  * @since 1.28
  */
 class NumericUppercaseCollation extends UppercaseCollation {
+       public function getName() {
+               return 'numeric';
+       }
+
        public function getSortKey( $string ) {
                $sortkey = parent::getSortKey( $string );
 
diff --git a/includes/collation/UppercaseCollation.php 
b/includes/collation/UppercaseCollation.php
index 92a4c3b..a9241ae 100644
--- a/includes/collation/UppercaseCollation.php
+++ b/includes/collation/UppercaseCollation.php
@@ -30,6 +30,10 @@
                $this->lang = Language::factory( 'en' );
        }
 
+       public function getName() {
+               return 'uppercase';
+       }
+
        public function getSortKey( $string ) {
                return $this->lang->uc( $string );
        }
diff --git a/includes/deferred/LinksUpdate.php 
b/includes/deferred/LinksUpdate.php
index 8954304..e3b9afa 100644
--- a/includes/deferred/LinksUpdate.php
+++ b/includes/deferred/LinksUpdate.php
@@ -524,7 +524,7 @@
         * @return array
         */
        private function getCategoryInsertions( $existing = [] ) {
-               global $wgContLang, $wgCategoryCollation;
+               global $wgContLang;
                $diffs = array_diff_assoc( $this->mCategories, $existing );
                $arr = [];
                foreach ( $diffs as $name => $prefix ) {
@@ -543,7 +543,8 @@
                        # things are forced to sort as '*' or something, they'll
                        # sort properly in the category rather than in page_id
                        # order or such.
-                       $sortkey = Collation::singleton()->getSortKey(
+                       $collation = 
MediaWikiServices::getInstance()->getCollation();
+                       $sortkey = $collation->getSortKey(
                                $this->mTitle->getCategorySortkey( $prefix ) );
 
                        $arr[] = [
@@ -552,7 +553,7 @@
                                'cl_sortkey' => $sortkey,
                                'cl_timestamp' => $this->getDB()->timestamp(),
                                'cl_sortkey_prefix' => $prefix,
-                               'cl_collation' => $wgCategoryCollation,
+                               'cl_collation' => $collation->getName(),
                                'cl_type' => $type,
                        ];
                }
diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php
index e754e3c..8477e23 100644
--- a/maintenance/updateCollation.php
+++ b/maintenance/updateCollation.php
@@ -26,6 +26,8 @@
 
 require_once __DIR__ . '/Maintenance.php';
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Maintenance script that will find all rows in the categorylinks table
  * whose collation is out-of-date.
@@ -67,8 +69,6 @@
        }
 
        public function execute() {
-               global $wgCategoryCollation;
-
                $dbw = $this->getDB( DB_MASTER );
                $dbr = $this->getDB( DB_REPLICA );
                $force = $this->getOption( 'force' );
@@ -78,8 +78,8 @@
                        $collationName = $this->getOption( 'target-collation' );
                        $collation = Collation::factory( $collationName );
                } else {
-                       $collationName = $wgCategoryCollation;
-                       $collation = Collation::singleton();
+                       $collation = 
MediaWikiServices::getInstance()->getCollation();
+                       $collationName = $collation->getName();
                }
 
                // Collation sanity check: in some cases the constructor will 
work,
diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php 
b/tests/phpunit/includes/MediaWikiServicesTest.php
index a05e39d..7eef94c 100644
--- a/tests/phpunit/includes/MediaWikiServicesTest.php
+++ b/tests/phpunit/includes/MediaWikiServicesTest.php
@@ -304,6 +304,7 @@
                        'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
                        'StatsdDataFactory' => [ 'StatsdDataFactory', 
StatsdDataFactory::class ],
                        'InterwikiLookup' => [ 'InterwikiLookup', 
InterwikiLookup::class ],
+                       'Collation' => [ 'Collation', Collation::class ],
                        'EventRelayerGroup' => [ 'EventRelayerGroup', 
EventRelayerGroup::class ],
                        'SearchEngineFactory' => [ 'SearchEngineFactory', 
SearchEngineFactory::class ],
                        'SearchEngineConfig' => [ 'SearchEngineConfig', 
SearchEngineConfig::class ],

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6fa8ad9a5ae76d44801bf70e8236b51814edda39
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

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

Reply via email to