Commit graph

195 commits

Author SHA1 Message Date
Adam Wight
188d2cbbb0 Remove unchecked exception annotations
Callers should not catch an unchecked exception, so it doesn't belong
in a function signature.  Unchecked exceptions indicate a coding error,
which by definition the code will not be able to handle correctly.

If any of these exceptions were supposed to be in response to an edge
case, user input, or initial conditions, then they should be changed
to a runtime error.  If the exception class cannot be changed, then
the annotation should include a comment explaining its purpose and
prognosis.

Bug: T240672
Change-Id: I2e640b9737cb68090a8e1cb70067d1b74037d647
2024-09-17 22:20:58 +02:00
thiemowmde
dca4931b42 Make use of the ??= and ?? operators where it makes sense
This touches various production classes and maintenance scripts.
The code should do the exact same as before. The main benefit is that
the syntax avoids any repetition.

Change-Id: I5c552125469f4d7fb5b0fe494d198951b05eb35f
2024-08-26 09:26:36 +02:00
thiemowmde
1356804aff site: Streamline Site class using modern PHP features
I believe this makes such code much easier to read and less
error-prone.

Change-Id: I40023b28b934bc75d1217d23ad6ec181312db11e
2024-07-18 15:38:01 +02:00
Ebrahim Byagowi
fab78547ad Add namespace to the root classes of ObjectCache
And deprecated aliases for the the no namespaced classes.

ReplicatedBagOStuff that already is deprecated isn't moved.

Bug: T353458
Change-Id: Ie01962517e5b53e59b9721e9996d4f1ea95abb51
2024-07-10 00:14:54 +03:30
Umherirrender
1951aea6b8 Fix various version mention for class_alias
Versions are changed in 8e940c4f21,
but that makes the version wrong

Follow-Up: I7f85d931d3b79da23e87b4e5692b2e14be8fcaa0
Change-Id: Iae43725b8e0fffc4d44bf57f6227334b41290bd9
2024-07-05 18:39:49 +02:00
Ebrahim Byagowi
a717db8e60 Add namespace and deprecation alias to FormatJson
This patch introduces a namespace declaration for the
MediaWiki\Json to FormatJson and establishes a class
alias marked as deprecated since version 1.43.

Bug: T353458
Change-Id: I5e1311e4eb7a878a7db319b725ae262f40671c32
2024-05-16 16:28:01 +03:30
Ebrahim Byagowi
14b4269d83 Add namespace and deprecation alias to Xml and XmlSelect
This patch introduces a new namespace declaration,
MediaWiki\Xml and adds Xml and XmlSelect to it
and establishes class aliases marked as deprecated
since version 1.43.

Bug: T353458
Change-Id: I45cccd540b6e15f267d3ab588a064fbeb719d921
2024-05-16 15:23:14 +03:30
frankie
73a0a6830d Typo fixes
Two small typo fixes for dev environment and gerrit setup.

Bug: T362742
Change-Id: I92399002019a8df51731f8c927ca606f91211b51
2024-04-23 15:21:03 -04:00
James D. Forrester
8e940c4f21 Standardise all our class alias deprecation comments for ease of grepping
Change-Id: I7f85d931d3b79da23e87b4e5692b2e14be8fcaa0
2024-03-19 20:11:29 +00:00
Daimona Eaytoy
175c0c4abf Replace more instances of deprecated MWException
Bug: T328220
Change-Id: Iba90f7f9b5766bccc05380d040138d74d5e9558a
2024-01-19 23:11:59 +00:00
thiemowmde
9b0f6b568d Replace a few array_key_exists with the ??= syntax
Or just remove it. It's not needed when the structure is already an
array. PHP will happily initialize multi-dimentional arrays when
needed.

Change-Id: I93845e8d6f870d147bd55cfe3827bc94b375d0ba
2024-01-08 10:12:10 +01:00
jenkins-bot
3e07889c70 Merge "Inline once-called short private methods to its caller, part 1" 2023-12-19 00:05:45 +00:00
Amir Sarabadani
a778d3fea7 Inline once-called short private methods to its caller, part 1
This makes the code easier to read to avoid jumps and improves
encapsulation by not allowing the rest of the class having access to the
logic. Of course, if needed then some code can be refactored out again
but for now let's not do YAGNI.

Change-Id: Ic37524e386fc04fd67e33768417ff8425f85b0ca
2023-12-18 23:43:32 +00:00
James D. Forrester
4cf719019a Namespace remaining files in includes/site
Bug: T353458
Change-Id: I38da09f28cd68add721ba7b05f351acb56515d95
2023-12-14 14:57:34 -05:00
Amir Sarabadani
74287cf721 Namespace Site and SiteList
Bug: T353458
Change-Id: I31f7cba42c02701e462eb3ed6bcc3928d438c1ef
2023-12-14 11:23:56 -05:00
Daimona Eaytoy
154f04299c Remove redundant empty() constructs (2)
empty() only makes sense when the expression it checks is possibly
undefined, otherwise it's equivalent to a truthiness check with the
additional downside of suppressing errors when it's not wanted.

Replace it with simple truthiness checks, using strict comparison when
that seems to help with polymorphic variables.

These were caught by a bespoke phan plugin.

Change-Id: I70b629dbf9e47cf3ba48ff439b18f19e839677f4
2023-09-08 23:28:11 +02:00
Tim Starling
95bd40b25c In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.

That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.

In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.

So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.

Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 10:16:08 +10:00
Matěj Suchánek
1c8896a0dd Fix various typos and documentation issues
Change-Id: I2cd4b647c01d84cfe0e1b4d55e155ced8c918b17
2023-08-27 12:05:11 +02:00
Amir Sarabadani
17d04077fd site: Migrate DBSiteStore to use ICP
Bug: T330641
Change-Id: If4bd4c0f3b2b74d3056db9b9521e7e6f1a25f307
2023-08-24 13:34:35 +02:00
Amir Sarabadani
98d6503f65 Mass migrate Database::insert calls to InsertQueryBuilder
Done automatically based on a php parser written on top of ANTLR4

Bug: T335377
Change-Id: Ie8fabc594edab18e55cb1d5bbf573762106e3d71
2023-08-07 19:44:27 +02:00
Amir Sarabadani
c57cdf4276 Migrate last batch of simple Database::select* calls to SQB
Done semi-automatically via a parser written on top of ANTLR4

Bug: T311866
Change-Id: Ib71d6810e480d7176726082347b569e346557de7
2023-08-01 20:38:13 +02:00
Umherirrender
6720650758 site: Use DeleteQueryBuilder in DBSiteStore
Possible after ALL_ROWS support was added (7cf42e26)

Also remove return from clear(), it is not documented on the interface

Bug: T340065
Change-Id: Ibc41c92148c317862e8e3ea9583e2e28cfa0f61f
2023-07-06 22:18:36 +00:00
Umherirrender
fe40101587 Replace IDatabase::delete with DeleteQueryBuilder
Change-Id: Ie0c1c955ca1a7028f75f24563fdeb9f94285af30
2023-06-21 17:50:31 +02:00
Daimona Eaytoy
4d5cd3a84f Replace deprecated MWException
Bug: T328220
Change-Id: I66be7a6dd752d6b9c254beb65f4eb5ace3c89776
2023-06-09 17:21:12 +02:00
Amir Sarabadani
b55c501fe0 Migrate Database::update() to UpdateQueryBuilder
I did this using a script written on top of antlr4 parser so it doesn't
have some clean ups a human would do but it's pretty nice already.

Bug: T330640
Change-Id: I608566700c6d737ee986bf47dda87effc69614d6
2023-06-08 12:34:20 +02:00
Timo Tijhof
90e2ed6ccb site: Simplify SiteList by removing GenericArrayObject indirection
== Background ==

In 2012, commit afe46f1403 (Id7e9b59c7e) added libs/GenericArrayObject
along with an (unused) abstract GenericArrayObjectTest case.

The same code was also added to the Wikibase extension with
change 6347b35a55cd (Ifa7f1dc702).

The code was then factored out from Wikibase into the wmde/Diff
library.

In 2013, GenericArrayObject was removed from wmde/Diff in the commit
at https://github.com/wmde/Diff/commit/d9c2bd5c140e2a783fd42298db6c.

== This change ==

Remove the GenericArrayObject indirection from SiteList as there exist
nothing outside SiteList refering to it in Codesearch Everywhere, and
even in SiteList much of the code in GenericArrayObject is overridden,
unused, or otherwise needlessly indirect.

Change-Id: Ifea09c5de50af1616058d8baa9037db273dfb0e5
2023-06-05 19:39:33 +00:00
Aaron Schulz
ac1819cc32 Remove uses Database::update()/Database::insert() return values
Change-Id: I527ea0900cc76faac9506b2a43a2b1522b35dfbd
2023-05-27 02:19:55 +00:00
James D. Forrester
ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00
Daimona Eaytoy
c5a01d4b2e Replace some usages of MWException
These exceptions are not documented with @throws and they're really not
meant to be caught.

Bug: T86704
Change-Id: I07f32e42c6fd4bc8785bac91547858f15a9fc2a8
2023-01-26 00:10:34 +00:00
Umherirrender
1e62e1af8f site\MediaWikiPageNameNormalizer: Replace deprecated Http::get
Bug: T305813
Change-Id: I138aecd6275b7e9b2a852565e67e083460b8286d
2022-12-14 21:12:30 +00:00
jenkins-bot
530c08f39c Merge "Various doc fixes about false on method arguments/return types" 2022-11-17 14:53:24 +00:00
jenkins-bot
8a6ab5bccd Merge "Remove pre PHP 7.4 serialize()/unserialize()" 2022-11-10 20:10:49 +00:00
Reedy
27ee63f8c9 Remove pre PHP 7.4 serialize()/unserialize()
Depends-On: I3b893152ed278a72b5e74ccf91a3381f319bfcd9
Change-Id: I9f69a9316eaec23103262ec2ffd5b85baaf16f3f
2022-11-10 19:25:01 +00:00
Umherirrender
ea5ea60b31 Various doc fixes about false on method arguments/return types
Doc-only changes

Change-Id: I5177f582ae7ee70c357e9389fed14819faf79463
2022-11-10 19:23:46 +00:00
Lucas Werkmeister
dadd9942f3 site: Hard-deprecate MediaWikiSite::getFileUrl() without $path
All callers available in codesearch pass in a $path (and for all callers
except getFileUrl()’s own test, that path is 'api.php'); omitting the
path hasn’t worked correctly for almost ten years (see change
I5ed7e169c9, commit 779df10df7).

Change-Id: I50819efb66fd72f2b5a1cd83d5cdd3a148742299
2022-11-10 08:02:32 +00:00
Timo Tijhof
c87e4d9f70 site: Switch new code from MWException to RuntimeException
Follows-up I5ed7e169c9753 (779df10d, T319219) which introduced a new
use of MWException, as per T86704.

While at it, also remove a few unhelpful `@throws` annotations that
do nothing other than save an outdated IDE scan of what throw
statements internally exist. These aren't meant to be caught and thus
aren't of use to consumers of these docs as per T321683.

Change-Id: I4ef964342de80eb2c1dec99cd6258727e6cd1a2c
2022-11-08 23:16:24 +00:00
Timo Tijhof
6a0efd6d5e site: Use common terms in docs for script path and article path
Its unfortunate that this class has encoded these unusual terms
in its stable API but we can at least use more common terminology
and document what these things actually are.

Virutally every word in the explaination "a full file path [is] a site
url and relative file path", meant something different from its usual
meaning, namely the full URL to an entry point script under /w/.

Change-Id: I15f1c9d56f61e622734bb3ddc304025ad4bfc169
2022-11-08 23:12:29 +00:00
Timo Tijhof
c8f1699d51 site: Annotate CachingSiteStore as @internal and perform minor cleanup
* Recognise CachingSiteStore as the internal class that it is.

* Clean up the file header and move ingroup annotation, similar to
  other commits under
  <https://gerrit.wikimedia.org/r/q/message:ingroup+owner:Krinkle>.

  The one here was particularly odd as it placed the license inside
  the class doc block rather than the file doc block.

* Remove unused $cacheKey and $cacheTimeout constructor args. They
  were also not covered by any unit tests.

* Avoid use of `empty()` as per
  <https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP>

* Use a more conventional cache key in CachingSiteStore, keeping
  the dynamic part for a later segment and keeping the first segment
  explicitly tied to a single concrete key group. This also improves
  ergonomics of cache statistics e.g. in Grafana and makes the cache
  key easier to locate in codesearch/grep, and more recognisable,
  e.g. starting with the code component ("site").

Change-Id: I541ad62551ab76127d70ef665272fd1d590ab556
2022-11-05 00:37:12 +00:00
Lucas Werkmeister
779df10df7 Fix Site::getPath() + MediaWikiSite::getFileUrl() confusion
Site::getPath() has returned null since change I08ffa6a970 (commit
a00337c3f8), almost ten years ago; I think on the whole it’s safer to
fix the prose documentation and keep the @return and actual returned
value as they are, rather than the other way around.

MediaWikiSite::getPageUrl() compared the $filePath to false instead of
null, so the comparison would always be true; but on closer inspection,
I think this code was supposed to check $path, not $filePath, in the
first place. The only reason this didn’t come up so far is that all
known callers pass in a custom path (specifically, all non-test callers
pass in 'api.php'), so the $path = false default was never used.

To avoid a deprecation warning in PHP 8.1 when passing a null $filePath
into str_replace(), throw in that case instead – the function isn’t
documented to return string|null, and none of the callers seem prepared
to handle a null return as far as I can tell, so throwing seems better
than returning null. Tests that produce a null $filePath need to be
fixed to complete the Site object (compare Wikibase change If20688411e).

Bug: T319219
Change-Id: I5ed7e169c9753486c765fd816d1b21016c5c1def
Depends-On: If20688411e3f0108b0c0ba3b91e5cc264b70a208
2022-11-04 11:10:35 +01:00
Mark A. Hershberger
c8586c88dc site: Consistently return null from Site::getDomain()
Note that null triggers the following for php 8.1:

    PHP Deprecated:  strrev(): Passing null to parameter #1 ($string) of
    type string is deprecated

Got a few of these in WikiMapTest::testMakeForeignLink(), caused by a
single call in DBSiteStore.

Bug: T289926
Change-Id: I0b29963ec0e2e8a38a66e250e81fbc16e42465ab
2022-07-06 19:07:01 +00:00
Aryeh Gregor
c435212260 Get rid of warnings on PHP 8.1
This is mostly about adding return types to methods that implement PHP
interfaces, and not passing null to core functions that want a string.
After this patch, and an update to return types in RemexHtml,
tests/phpunit/integration/ has no more errors than in PHP 8.0.

Bug: T289879
Bug: T289926
Change-Id: Ia424f5cc897070f4188ae126b5bf6a1f552db0e1
2022-06-13 04:42:20 -04:00
Aryeh Gregor
4a52bf553f Use MainConfigNames instead of string literals, #3
This edition brought to you by:

grep -ERIn $(grep -o "'[A-Za-z0-9_]*'" includes/MainConfigNames.php | tr
"\n" '|' | sed 's/|$/\n/') includes/

I only corrected a fraction of the results provided by that command. I'm
submitting the partial patch now so it doesn't bitrot.

Bug: T305805
Change-Id: If1918c0b3d88cdf90403921e4310740e206d6962
2022-04-26 14:31:26 +03:00
jenkins-bot
7949ed99d0 Merge "Implement __serialize/__unserialize for PHP 8.1 support" 2022-02-07 05:10:04 +00:00
Tim Starling
ca71e69fc6 Try not to discard Excimer timeout exceptions
Don't catch and discard exceptions from the RequestTimeout library,
except when the exception is properly handled and the code seems to be
trying to wrap things up.

In most cases the exception is rethrown. Ideally it should instead be
done by narrowing the catch, and this was feasible in a few cases. But
sometimes the exception being caught is an instance of the base class
(notably DateTime::__construct()). Often Exception is the root of the
hierarchy of exceptions being thrown and so is the obvious catch-all.

Notes on specific callers:

* In the case of ResourceLoader::respond(), exceptions were caught for API
  correctness, but processing continued. I added an outer try block for
  timeout handling so that termination would be more prompt.
* In LCStoreCDB the Exception being caught was Cdb\Exception not
  \Exception. I added an alias to avoid confusion.
* In ImageGallery I added a special exception class.
* In Message::__toString() the rationale for catching disappears
  in PHP 7.4.0+, so I added a PHP version check.
* In PoolCounterRedis, let the shutdown function do its thing, but
  rethrow the exception for logging.

Change-Id: I4c3770b9efc76a1ce42ed9f59329c36de04d657c
2022-02-02 16:27:44 +11:00
Reedy
ccf1dcd975 Implement __serialize/__unserialize for PHP 8.1 support
Bug: T299312
Change-Id: I8a758ed52b18d52cf083b645fbd88725cca983e8
2022-02-01 15:06:20 +00:00
Tim Starling
54c735354f Stop using is_resource() where possible
* Enforce the ban on is_resource in phpcs
* In OrderedStreamingForkController, the comment was incorrect. I
  confirmed using a small test script that if the child closes one end
  of a socket pair, the other end will still be open, and is_resource()
  will still return true, and fclose() will not fail. The issue was
  introduced in c82d30d19c, it was not present in the
  CirrusSearch copy of the class.
* Allow is_resource() for debug logging.
* Allow is_resource() for parameter validation where a stream may be
  passed to a function, since there is no alternative.

Bug: T260735
Change-Id: I59a752f7bb4b043ddfb2434b52a02f9221bf54db
2022-01-26 10:03:23 +11:00
Tim Starling
a2d88e6a45 Suppress deprecation warnings from libxml_disable_entity_loader()
As discussed in the task, there is no simple replacement for this
function.

Reduces core PHPUnit error count from 121 to 2.

Bug: T268847
Change-Id: Ia8697e1c3ba369346b39ef50d94ea8b132eb9c80
2022-01-21 14:03:12 +11:00
TChin
47adb6d65a Refactor global variables to use MediaWikiServices instead
Automatically refactors wg prefixed globals to use MediaWikiServices config using Rector. Doesn't include files that set globals or files that fail CI.

Rector Gist: https://gist.github.com/tchin25/7cc54f6d23aedef010b22e4dfbead228

* This patch uses a modified source code rector library for our specific use case and the rector will have different effects without it.

A writeup for future reference is here: https://meta.wikimedia.org/wiki/User:TChin_(WMF)/Using_Rector_On_MediaWiki

Change-Id: I1a691f01cd82e60bf41207d32501edb4b9835e37
2022-01-10 13:55:53 -05:00
Daimona Eaytoy
e0d3b3e8fc Use getWithSetCallback() instead of get() + set()
This allows the backend to do whatever it can to ensure atomicity, and
is also easier to read, since the intent is obvious.

Change-Id: Ibbfecd92a2c6d9a5997ca37ea101e068bd1e8202
2021-11-18 18:58:13 +00:00
Thiemo Kreuz
69242b0876 Various updates and fixes to PHPDoc documentation
Some minor updates I collected in my local dev environment the
past months.

Change-Id: I30d5339bc262d54ba76e2860a3a3e9e5f002fdc3
2021-08-09 09:26:23 +02:00