Commit graph

1692 commits

Author SHA1 Message Date
Ori Livneh
b5ccf229c3 languages: Apply a small optimization to Language::ucfirst()
We don't need to scan the whole string for multi-byte characters here,
since we only care about the first character in the string. By only
checking the byte-width of the first character we avoid a potentially
expensive call to mb_strlen.

Change-Id: I1e8dd6e3f3c5a8d795085edd1b128bc38ec67d78
2020-05-04 23:41:25 +00:00
Timo Tijhof
75ccdc6147 languages: Move default $wgNamespaceAliases to MessagesEn.php
These are not configuration but business logic, similar to the
canonical names that are in NamespaceInfo.php, these must always
exist and cannot be altered or unset.

They were previously unconditionally assigned during all requests
in Setup.php and passed down as "site configuration".

Changes:

* Move them to MessagesEn.php where they can be cached and
  processed the same way as other core-provided aliases.

  Document and confirm with tests that this is a mergeable
  attribute that follows the language chain.

* Remove the duplicated code in a few places that was reading
  this variable + Language::getNamespaceAliases(), to instead
  just call the latter and move the logic there, centralised,
  and tested.

  In doing so I noticed that these were applied in an
  inconsistent order. Sometimes the config won, sometimes not.
  There's no obvious right or wrong way here, but I've chosen
  to standardise on the way that Language::getNamespaceIds() did
  it, which is that config wins. This because that method seems
  to be most widely used of the three (it decides how URLs and
  titles are parsed), and thus the one I least want to change
  the behaviour of.

* Document that $wgNamespaceAliases may only be used to
  define (extra) aliases, it is and never was a way to access
  the complete list of aliases.

Bug: T189966
Change-Id: Ibb14181aba8c1b509264ed40523e9ab4000fd71a
2020-03-14 19:27:40 +00:00
ArtBaltai
31283f34bf Reduce usage of the Language class
reduce/deprecate visibility of some members of the Language class

Bug: T243913
Change-Id: I6bad608455ceaa46f895f00dcc6380cec6d32680
2020-03-03 01:38:27 +03:00
jenkins-bot
0f357294ca Merge "Remove LanguageConverter dependencies on Title and use LinkTarget" 2020-02-12 22:44:12 +00:00
ArtBaltai
3bf4b42490 Remove LanguageConverter dependencies on Title and use LinkTarget
Replace usage of Title in LanguageConverter with LinkTarget which
is more light weighted and provides just the props needed in Language.

Bug: T226834
Change-Id: I02a386bd9898e83c773cbd3d738d347d08f52c11
2020-02-12 18:37:11 +03:00
Timo Tijhof
f5895c2c82 language: Clean up file headers and class-level docs
This follows-up d83fcce5cb, which did something similar for
includes/profiler/.

* Ensure presence of license header.

* Merge any file-level descriptions with the class block,
  where it gets seen in generated docs about that class.

* Add any missing `@ingroup` tags to class blocks.

* Remove remaining `@ingroup` from file blocks.
  These clutter the Doxygen pages with duplicate entries.

* Fix some misspelled words from 61e0908fa2 and f136c2953c.

Change-Id: I5d21ec159766b799ba519da951d4f0716bae5f9f
2020-02-12 02:15:44 +00:00
Peter Ovchyn
61e0908fa2 languages: Introduce LanguageConverterFactory
Done:
* Replace LanguageConverter::newConverter by LanguageConverterFactory::getLanguageConverter
* Remove LanguageConverter::newConverter from all subclasses
* Add LanguageConverterFactory integration tests which covers all languages by their code.
* Caching of LanguageConverters in factory
* Make all tests running (hope that's would be enough)
* Uncomment  the deprecated functions.
* Rename FakeConverter to TrivialLanguageConverter
* Create ILanguageConverter to have shared ancestor
* Make the LanguageConverter class abstract.
* Create table with mapping between lang code and converter instead of using name convention
* ILanguageConverter @internal
* Clean up code

Change-Id: I0e4d77de0f44e18c19956a1ffd69d30e63cf51bf
Bug: T226833, T243332
2020-02-03 11:38:03 +02:00
jenkins-bot
a9aec68a21 Merge "Rewrite Language::firstChar() using mbstring" 2020-01-17 11:01:22 +00:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
Umherirrender
1324072833 Fix some string $key to int $key in Language class
All used as array index

Change-Id: Ie5fd7a9d7b01592b593dfd4c6d67a5cc34c08667
2019-12-29 16:59:38 +01:00
Umherirrender
d6089d9f97 Fix doc for Language::formatNumNoSeparators
Follow up changes to Language::formatNum in
I6d538ce3ca7fd2d495c2bafbab7cc279da69db1c

Change-Id: I1fcb339b127d93e1d6441cb000ee0661c98da5a7
2019-12-27 15:39:33 +01:00
jenkins-bot
b64d9bed33 Merge "Fix doc of Language::sprintfDate" 2019-12-15 19:04:40 +00:00
Daimona Eaytoy
25ba3b3794 Document more scalar types for BagOStuff::makeKey and Language::formatNum
These were spotted by phan's stricter type checks. All make*Key methods
can also take integers, and that's pretty common across core and
extensions.
Language::formatNum can also take numbers (and tests cover this case);
in this case, move the cast to the top of the method, so that we're
passing a string to strtr, too.

The methods touched in this patch are the main source of false positives
in both core and extensions.

Change-Id: I6d538ce3ca7fd2d495c2bafbab7cc279da69db1c
2019-12-15 00:02:04 +00:00
Umherirrender
be9ab59542 Fix doc of Language::sprintfDate
The init value for $ttl as output varibale on the caller side is
typically null, so allow that in the doc

Change-Id: I1bc274288542fd542377c52bd61e19250473500b
2019-12-13 21:30:21 +01:00
Max Semenik
f61b9d1295 Rewrite Language::firstChar() using mbstring
Change-Id: I6e7843c766196b9457b60df5dd0f5d9ad73afead
2019-12-09 21:29:31 +03:00
Umherirrender
844c6db2b6 Set method visibility on languages classes
Change-Id: I5db62d8243ce3abd7c442de10d6143de6dd1aad3
2019-12-09 16:37:38 +00:00
jenkins-bot
94b12c7ba8 Merge "Set visibility on php magic functions __destruct/sleep/wakeup/get/call" 2019-12-09 16:36:13 +00:00
jenkins-bot
b878bb2bdd Merge "language: Fix misleading Language::formatNum() documentation" 2019-12-06 13:05:40 +00:00
Thiemo Kreuz
c78aa36307 language: Fix misleading Language::formatNum() documentation
Change-Id: I403e79cd1ebfea7742cdf4bb85a248d133f90202
2019-12-06 13:38:46 +01:00
Umherirrender
eb2373dcd1 Set visibility on php magic functions __destruct/sleep/wakeup/get/call
All the magic functions needs public visibility to be callable from php
internals like garbage collector

Change-Id: I1baf04bf8ff787da880d46e4a6daa77f5a6de73f
2019-12-05 18:52:55 +01:00
Max Semenik
40704cc6d0 Language: replace static variables with constants
The successor of $durationIntervals has lower visibility because
it's not really needed outside of its class.

Change-Id: I314df8a528dc1cfb6155b131b9929d7808257fb3
2019-12-05 11:13:17 +03:00
jenkins-bot
af4b49e67c Merge "Improve param docs" 2019-11-28 19:36:24 +00:00
Umherirrender
c7ad21c25f Improve param docs
Change-Id: I746a69f6ed01c3ff000da125457df62b02d13b34
2019-11-28 19:08:59 +01:00
jenkins-bot
f7627d5e64 Merge "Remove unused Language::getUserToggle" 2019-11-23 13:05:42 +00:00
jenkins-bot
c9e872cf20 Merge "Remove unused Language::addMagicWordsByLang" 2019-11-23 13:02:12 +00:00
jenkins-bot
3239ea2884 Merge "Remove unused Language::initContLang" 2019-11-23 13:01:36 +00:00
jenkins-bot
2ff79e1636 Merge "Remove Language::getParsedTitle" 2019-11-18 23:53:36 +00:00
jenkins-bot
d784e03e8e Merge "Inline callbacks in language class" 2019-11-18 23:53:05 +00:00
Umherirrender
04df904085 Remove unused Language::initContLang
Change-Id: I8ab66d3fd597e7515ceb6eea4c9dc1df8485b846
2019-11-18 19:38:46 +01:00
Umherirrender
0328bbd482 Remove unused Language::getUserToggle
Change-Id: I096e9c90a63a0cc824fdebdb35b988ac94c12030
2019-11-18 19:34:10 +01:00
Umherirrender
1d368562c9 Remove unused Language::addMagicWordsByLang
Using alias file is the modern way

Change-Id: Iac85fcd620c1fe5fdd6046426e988da60f1980b7
2019-11-18 19:32:13 +01:00
Umherirrender
ccb6b21daf Inline callbacks in language class
Change-Id: Ic2b78a7bedd1cefcdcfab24a086a5d550df61967
2019-11-18 15:46:29 +00:00
Umherirrender
0fc0b995bf Remove Language::getParsedTitle
It is calling a non existing method getParsedTitle of the
LanguageConverter

Change-Id: I5b7a1b4a06c8c223128431697743eeebdf85bdd9
2019-11-18 16:43:47 +01:00
Umherirrender
8847aff551 Set method visibility on languages classes
Change-Id: I9dd10bbf81d277865301eccde73da17418df1238
2019-11-17 00:22:43 +01:00
Umherirrender
32ecaffca1 Add public visibility to some language function
This only adds public for some function used outside of languages
classes.
This does not fixed all functions, just one first shot

Change-Id: I5c0ee93a0867c847b084d2ce459dbad9cddaf9fb
2019-10-31 19:03:55 +01:00
Aryeh Gregor
27a8523cad Drop MediaWikiServices::resetLanguageServices()
There is no reason this should be necessary. Resetting services should
do the trick, together with Language::$mLangObjCache while it exists.
Resetting only a subset of services is fragile and shouldn't be done,
because one of the reset services might later be injected into a service
that's not reset, resulting in bugs.

Change-Id: I8c72b466ba7337649f9256ee6c078c76163f9785
2019-10-29 15:28:18 +02:00
jenkins-bot
c20a0de09a Merge "Cache nonexistence of grammar transformations" 2019-10-29 10:20:06 +00:00
Aryeh Gregor
537bdc2d7d Deprecate Language::getMessage*For()
These call the deprecated Language::getLocalisationCache() method and
thereby indirectly access MediaWikiServices. Callers should be changed
to inject a LocalisationCache and use that directly.

There are only a few callers in code search, so it didn't seem worth
adding convenience methods to LocalisationCache. The one caller that was
already using DI was MessageCache, and I injected LocalisationCache
there.

Bug: T201405
Change-Id: I01919fba5685fc5e0a31f739714f125a22de8939
2019-10-29 11:52:07 +02:00
Aryeh Gregor
251f4811be Cache nonexistence of grammar transformations
Previously every call to Language::getGrammarTransformations() for a
language without grammar transformations (i.e., almost all) would look
for a transformations file using is_readable(). This is not going to be
especially expensive, but it's presumably still more expensive than
caching the result.

The change is not actually testable right now without resorting to
TestingAccessWrapper or somehow mocking the file access, but I threw in
some basic tests for the method anyway. If later we decide how best to
mock the file access, they can be updated to check that the cache is
working properly.

Change-Id: I76e0fb7dbb1602df4f353722aacbd5adfff3c053
2019-10-29 09:12:21 +00:00
jenkins-bot
8904293650 Merge "LanguageFactory to replace Language::factory()" 2019-10-28 15:03:22 +00:00
Aryeh Gregor
e787246ce8 LanguageFactory to replace Language::factory()
Languages with variants no longer can override the parent's constructor
(which is now used for injecting services). Instead, they need to
override Language::newConverter().

Bug: T201405
Change-Id: I923400d61763cf1db88cb0c3f684c9c10e58032d
2019-10-23 10:48:23 -07:00
Fomafix
262946570a Use $this->localisationCache instead of $this->getLocalisationCache()
This change is a follow-up to 043d88f68.

Change-Id: I29e9232bfeeff8879e500f6a184078e7fafc6858
2019-10-22 21:16:16 +02:00
Daimona Eaytoy
fbf5f8178f language: mark sprintfDate as unsafe
It uses several msg()->text(), and is thus unsafe. taint-check 3.0.0 will
see it on its own, but let's mark it now.

Change-Id: I7b02e110bbf9dbd5317f717b3a421d21cb3b2ab0
2019-10-20 13:05:08 +00:00
Aaron Schulz
0dec11bc1e language: optimize uc(), lc(), and (uc|lc)first() and enforce return type
In some cases these would previously not return strings if passed things like
integers or floats which should be harmless. Note that ord() on an integer or
float internally casts the value to a string to get the first octet code.

Bug: T229266
Change-Id: I0dca34dde39bc1d3ace9b4a3196af334357b0653
2019-10-16 17:46:15 +00:00
Aryeh Gregor
f23c4570e4 Make grammar transform cache an instance member
Normally there shouldn't be more than one Language object in existence
at a time because of $mLangObjCache, so there's no need for a
MapCacheLRU for grammar transformations. Just make it an instance member
of Language.

If someone directly called the Language constructor instead of
factory(), or meddled with $mLangObjCache, with one of the three
languages that have transforms defined, and called
getGrammarTransformations() on both distinct objects, this change could
result in duplication of an array of about 50 elements. I think the risk
is acceptable.

The change should be covered acceptably by existing tests for LanguageHe
and LanguageRu.  (There's not much to test.)

Bug: T201405
Change-Id: I483bafbbb7d109b670596f16381def9e3bd26d89
2019-10-08 22:24:22 +00:00
Aryeh Gregor
8c4f59db64 New LanguageFallback service
This replaces the static Language methods getFallbackFor(),
getFallbacksFor(), and getFallbacksIncludingSiteLanguage(). There is
100% unit and integration test coverage for the new class.

One deliberate functional change: I changed one place where we threw
MWException to InvalidArgumentException.

Bug: T201405
Depends-On: Ie7a89f6ed7d52a0bc01672019ff92e7ee105a1f3
Change-Id: I49222eb55f1feec5b1dcd40f364cffe0c8801855
2019-10-08 15:11:39 -07:00
Aryeh Gregor
6d80b6c082 Split some Language methods to LanguageNameUtils
These are static methods that have to do with processing language names
and codes. I didn't include fallback behavior, because that would mean a
circular dependency with LocalisationCache.

In the new class, I renamed AS_AUTONYMS to AUTONYMS, and added a class
constant DEFINED for 'mw' to match the existing SUPPORTED and ALL. I
also renamed fetchLanguageName(s) to getLanguageName(s).

There is 100% test coverage for the code in the new class.

This was previously committed as 2e52f48c2e and reverted because it
depended on e4468a1d6b, which had to be reverted for performance
issues. There should be no changes other than rebasing.

Bug: T201405
Change-Id: Ifa346c8a92bf1eb57dc5e79458b32b7b26f1ee8a
2019-10-07 15:20:52 -07:00
Aryeh Gregor
043d88f680 Make LocalisationCache a service
This removes Language::$dataCache without deprecation, because 1) I
don't know of a way to properly simulate it in the new paradigm, and 2)
I found no direct access to the member outside of the Language and
LanguageTest classes.

An earlier version of this patch (e4468a1d6b) had to be reverted
because of a massive slowdown on test runs. Based on some local testing,
this should fix the problem. Running all tests in languages is slowed
down by only around 20% instead of a factor of five, and memory usage is
actually reduced greatly (~350 MB -> ~200 MB). The slowdown is still not
great, but I assume it's par for the course for converting things to
services and is acceptable. If not, I can try to optimize further.

Bug: T231220
Bug: T231198
Bug: T231200
Bug: T201405
Change-Id: Ieadbd820379a006d8ad2d2e4a1e96241e172ec5a
2019-10-07 13:18:47 -07:00
Daimona Eaytoy
b5cbb5ab3f Upgrade phan config to 0.7.1
This allows us to remove many suppressions for phan false positives.

Bug: T231636
Depends-On: I82a279e1f7b0fdefd3bb712e46c7d0665429d065
Change-Id: I5c251e9584a1ae9fb1577afcafb5001e0dcd41c7
2019-09-04 08:20:53 +00:00
Daimona Eaytoy
c659bc6308 Unsuppress another phan issue (part 7)
Bug: T231636
Depends-On: I2cd24e73726394e3200a570c45d5e86b6849bfa9
Depends-On: I4fa3e6aad872434ca397325ed7a83f94973661d0
Change-Id: Ie6233561de78457cae5e4e44e220feec2d1272d8
2019-09-03 17:19:21 +00:00