MediaWiki  master
CollationFactory.php
Go to the documentation of this file.
1 <?php
2 
23 
24 use Collation;
28 use MWException;
29 use Wikimedia\ObjectFactory;
30 
40  public const CONSTRUCTOR_OPTIONS = [
41  'CategoryCollation',
42  ];
43 
44  private const CORE_COLLATIONS = [
45  'uppercase' => [
46  'class' => \UppercaseCollation::class,
47  'services' => [
48  'LanguageFactory',
49  ]
50  ],
51  'numeric' => [
52  'class' => \NumericUppercaseCollation::class,
53  'services' => [
54  'LanguageFactory',
55  'ContentLanguage',
56  ]
57  ],
58  'identity' => [
59  'class' => \IdentityCollation::class,
60  'services' => [
61  'ContentLanguage',
62  ]
63  ],
64  'uca-default' => [
65  'class' => \IcuCollation::class,
66  'services' => [
67  'LanguageFactory',
68  ],
69  'args' => [
70  'root',
71  ]
72  ],
73  'uca-default-u-kn' => [
74  'class' => \IcuCollation::class,
75  'services' => [
76  'LanguageFactory',
77  ],
78  'args' => [
79  'root-u-kn',
80  ]
81  ],
82  'xx-uca-ckb' => [
83  'class' => \CollationCkb::class,
84  'services' => [
85  'LanguageFactory',
86  ]
87  ],
88  'uppercase-ab' => [
89  'class' => \AbkhazUppercaseCollation::class,
90  'services' => [
91  'LanguageFactory',
92  ]
93  ],
94  'uppercase-ba' => [
95  'class' => \BashkirUppercaseCollation::class,
96  'services' => [
97  'LanguageFactory',
98  ]
99  ],
100  ];
101 
103  private $options;
104 
106  private $objectFactory;
107 
109  private $hookRunner;
110 
116  public function __construct(
118  ObjectFactory $objectFactory,
119  HookContainer $hookContainer
120  ) {
121  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
122  $this->options = $options;
123  $this->objectFactory = $objectFactory;
124  $this->hookRunner = new HookRunner( $hookContainer );
125  }
126 
130  public function getCategoryCollation(): Collation {
131  return $this->makeCollation( $this->options->get( 'CategoryCollation' ) );
132  }
133 
139  public function makeCollation( string $collationName ): Collation {
140  if ( isset( self::CORE_COLLATIONS[$collationName] ) ) {
141  return $this->instantiateCollation( self::CORE_COLLATIONS[$collationName] );
142  }
143 
144  if ( preg_match( '/^uca-([A-Za-z@=-]+)$/', $collationName, $match ) ) {
145  return $this->instantiateCollation( [
146  'class' => \IcuCollation::class,
147  'services' => [
148  'LanguageFactory',
149  ],
150  'args' => [
151  $match[1],
152  ]
153  ] );
154  }
155 
156  // Provide a mechanism for extensions to hook in.
157  $collationObject = null;
158  $this->hookRunner->onCollation__factory( $collationName, $collationObject );
159 
160  if ( !$collationObject instanceof Collation ) {
161  throw new MWException( __METHOD__ . ": unknown collation type \"$collationName\"" );
162  }
163 
164  return $collationObject;
165  }
166 
171  private function instantiateCollation( $spec ): Collation {
172  return $this->objectFactory->createObject(
173  $spec,
174  [
175  'assertClass' => Collation::class
176  ]
177  );
178  }
179 
180 }
MediaWiki\Collation\CollationFactory\$objectFactory
ObjectFactory $objectFactory
Definition: CollationFactory.php:106
MediaWiki\Collation\CollationFactory\CORE_COLLATIONS
const CORE_COLLATIONS
Definition: CollationFactory.php:44
Collation
Definition: Collation.php:30
MWException
MediaWiki exception.
Definition: MWException.php:29
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Collation\CollationFactory\$options
ServiceOptions $options
Definition: CollationFactory.php:103
MediaWiki\Collation\CollationFactory\getCategoryCollation
getCategoryCollation()
Definition: CollationFactory.php:130
MediaWiki\Collation\CollationFactory
Common factory to construct collation classes.
Definition: CollationFactory.php:36
MediaWiki\Collation\CollationFactory\$hookRunner
HookRunner $hookRunner
Definition: CollationFactory.php:109
MediaWiki\Collation\CollationFactory\__construct
__construct(ServiceOptions $options, ObjectFactory $objectFactory, HookContainer $hookContainer)
Definition: CollationFactory.php:116
MediaWiki\Collation
Definition: CollationFactory.php:22
MediaWiki\Collation\CollationFactory\makeCollation
makeCollation(string $collationName)
Definition: CollationFactory.php:139
MediaWiki\Collation\CollationFactory\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: CollationFactory.php:40
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:557
MediaWiki\Collation\CollationFactory\instantiateCollation
instantiateCollation( $spec)
Definition: CollationFactory.php:171
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71