Commit graph

62 commits

Author SHA1 Message Date
Umherirrender
1145328459 api: Use type-declaration for string arg in ApiBase::__construct
Also for all sub-classes
Remove simple doc-blocks without further information

Change-Id: I981934efe32d44f52e5ab865a9b887be5bd0f41e
2024-10-17 20:09:15 +02:00
Umherirrender
e662614f95 Use explicit nullable type on parameter arguments
Implicitly marking parameter $... as nullable is deprecated in php8.4,
the explicit nullable type must be used instead

Created with autofix from Ide15839e98a6229c22584d1c1c88c690982e1d7a

Break one long line in SpecialPage.php

Bug: T376276
Change-Id: I807257b2ba1ab2744ab74d9572c9c3d3ac2a968e
2024-10-16 20:58:33 +02:00
James D. Forrester
984076f98e Add namespace to includes/api classes
Bug: T353458
Change-Id: I3ea6b08c5018ba03ba45c5766e1f46e12f6b8597
2024-09-25 19:31:14 +00:00
Umherirrender
fc9e42823b rdbms: Create IReadableDatabase::andExpr() / ::orExpr()
Avoid the call to internal constructor of AndExpressionGroup and
OrExpressionGroup by creating a factory function similiar as the
IReadableDatabase::expr function for Expression objects.

This is also a replacement for calls to ISQLPlatform::makeList with
LIST_AND or LIST_OR argument to reduce passing sql as string to the
query builders.

Created two functions to allow the return type to be set for both
expression group to allow further calls of ->and() or ->or() on the
returned object.
Depending on the length of the array argument to makeList() it is
sometimes hard to see if the list gets converted to AND or OR, having
the operator in the function name makes it easier to read, so two
functions are helpful in this case as well.

Bug: T358961
Change-Id: Ica29689cbd0b111b099bb09b20845f85ae4c3376
2024-07-11 15:29:20 +00:00
Umherirrender
723134eade Add explicit parentheses around mixed boolean operator
Mixing different binary boolean operators within an expression
without using parentheses to clarify precedence is not allowed (T358966)

Change-Id: I24ca752d5dac7c948fdbcabf721f6f0aef8a466f
2024-03-23 01:58:59 +01:00
Amir Sarabadani
1f069df2bd api: Only force backlink namespace index when there is one ns only
If there is more than one ns in the conditions, mysql picks a range
lookup depending on the cardinality and usage and it becomes really
slow. There is an example of that in details T351237#9383766

Bug: T351237
Change-Id: Ibee81ab8f295157bbb1e457e355ea6e7e60bef68
2023-12-06 19:20:08 +01:00
Amir Sarabadani
17589adcc4 Migrate another batch to use $db->expr instead of raw SQL
Bug: T210206
Change-Id: I327517fef250c24392565a26bbe7b296dc19508a
2023-10-30 17:56:35 -04:00
Bartosz Dziewoński
e89be77475 Remove allowances for nullable rd_interwiki and rd_fragment
After the other changes in T346290 these fields can never be null.
The only place that needs to handle null values is the migration
script fixInconsistentRedirects.php.

Bug: T346290
Change-Id: I0235c4be93b203f369b29522b54c0110ee8d61e9
2023-10-03 19:29:55 +02:00
Ammar Abdulhamid
cd13bbb727 Get correct main page in API call examples
Remove hardcoded "Main Page" and use whatever the main page of
the wiki is. Many wikis have their main page in a different
title than the default or even in a different namespace entirely.

With the hardcoded title this produces broken/redlink for the doc
examples and makes it overall less useful.

Most typical examples; Mediawiki.org itself, Wikidata.org, etc.

Bug: T235207
Change-Id: Ia9eee76544cad153166dd5a2eb8e8c1bf3a38b74
2023-09-20 17:12:06 +03:00
Bartosz Dziewoński
c03cb71796 Use real type hints for services etc. in api/ApiQuery*.php
Mostly used find-and-replace:

Find:
/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?)[\s\*]+/\s*(private|protected|public) (\$[a-z]\w+;\n)((?=\s*/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?))\n|)
Replace with:
\3 \1 \4

Followed by some manual review to make sure I'm not changing too much,
omitting some changes that looked too complicated and anything that
caused test failures, and some whitespace fixes.

Change-Id: Ie78be1c614985d7c2964156e454cc9266515dc18
2023-08-29 01:02:40 +00:00
Umherirrender
b47e1f27a4 i18n: Split apihelp for redirects/linkshere/transcludedin/fileusage show
Easier to translate
There is no visible change on Special:ApiHelp/query+redirects
There is no visible change on Special:ApiHelp/query+linkshere
There is no visible change on Special:ApiHelp/query+transcludedin
There is no visible change on Special:ApiHelp/query+fileusage

Bug: T285545
Change-Id: Ifaf996219e48e7c0cb072f3d25424f56f2827045
2023-08-05 14:59:42 +02:00
Umherirrender
160411d4cf Simplify WHERE conditions with field = '' OR IS NULL
Syntax supported since 26235c73

Change-Id: I397c76d4aba753a1629383559b95df86514f19ff
2023-07-01 22:17:42 +02:00
Timo Tijhof
678326e6fb rdbms: Throw when makeWhereFrom2d() receives empty data
This semi-internal method has only four callers, each of which
explicitly or implicitly ensures that $data is empty. For example:

* ApiQueryBacklinksprop: The caller returns early return if $titles
  is empty, and then proceeds to build $map from $titles PageSet.
  I've made it more explicit here for added confidence and to satisfy
  static analysis.
* cleanupPreferences.php: The caller explicilty checks $data
  (as $deleteWhere) before calling makeWhereFrom2d().
* ResourceLoader\WikiModule, the caller checks $batch->isEmpty()
  before calling $batch->constructSet().

More importantly, is that not one of these checks whether
makeWhereFrom2d() returns false, and blindly passes it on to
methods like IDatabase::select() or IDatabase::delete(), which does
not permit boolean false as $where/$conds, but also doesn't check
for it, which seems seems likely to wreak havoc at some point through
casting to empty string or empty array at some point.

Given there are very few callers, and given that the current
responsiblity to check the return value is never excercised, throw
an exception instead to make this code appear less fragile.

Follow-up CR from
<https://gerrit.wikimedia.org/r/c/mediawiki/core/+/892988>

Change-Id: Ifdf3ea54271ea856f2f555a5e087b8f521e348b3
2023-05-10 01:55:47 +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
DannyS712
c1db64b808 Make use of ??= in more places
New feature from PHP 7.4

Change-Id: Ifa7a9bc7b2ec415ad7ecb23f4c1776f51f58fd6b
2022-12-17 01:10:13 +00:00
Bartosz Dziewoński
56a3e83d03 Use buildComparison() instead of raw SQL in API prop=linkshere and friends
(prop=fileusage, prop=linkshere, prop=redirects, prop=transcludedin)

Bug: T321422
Change-Id: I7bd032bf10d551c9680ed47be61851c9c682799e
2022-10-22 01:39:32 +02:00
jenkins-bot
43830cde86 Merge "Use array_key_first()/array_key_last() in some places" 2022-10-21 21:57:34 +00:00
Tim Starling
0077c5da15 Use short array destructuring instead of list()
Introduced in PHP 7.1. Because it's shorter and looks nice.

I used regex replacement.

Change-Id: I0555e199d126cd44501f859cb4589f8bd49694da
2022-10-21 15:33:37 +11:00
Tim Starling
1aeccebaf2 Use array_key_first()/array_key_last() in some places
Introduced in PHP 7.3. I used it to replace reset()/end() followed by
key() where the return value of reset() is not captured and internal
pointer iteration is not locally occuring.

I also used it in a couple of places when reset() is absent but
array_key_first() is semantically desired.

Change-Id: I750d3fa71420cbdca5fb00d82ac5ca40821769d4
2022-10-21 15:01:59 +11:00
Reedy
0b5084f868 api: Replace numerous deprecated constants
Change-Id: I34ffcb70efbfa257da8dab6e0790aa0d697caf5b
2022-06-06 00:18:50 +01:00
Ladsgroup
1fdbaa963f Revert "ApiQueryBacklinksprop: Completely remove index hints"
This reverts commit 7da86e4ecf.

Reason for revert: This breaks in pagelinks queries.

Change-Id: If3f4c69eb5fafb83616e37dd2f5b7f9597771a7b
2022-05-24 16:32:26 +00:00
Amir Sarabadani
7da86e4ecf ApiQueryBacklinksprop: Completely remove index hints
These index hints were added to bypass a bug in optimizer which has been
resolved long time ago: T139056#5546891

On top, this is causing all sorts of slow down when the user picks a lot
of namespaces rendering the index useless.

Bug: T306673
Change-Id: I07b023e46005187a5ed428b270f355612d49987a
2022-05-23 22:06:36 +02:00
Amir Sarabadani
70a337d51f ApiQueryBacklinksprop: Make sure the index setting exists
Without it, if user tries to hit redirects, they would get:
PHP Notice: Undefined index: indexes

This would make it to be empty string which won't be used later due to
this condition: !empty( $settings['indexes'] )

Bug: T306673
Change-Id: I17f2d38bf17356ece00ebf527372e7a62f20b1af
2022-05-16 17:21:57 +02:00
Amir Sarabadani
ed58b647c7 ApiQueryBacklinksprop: Force the correct templatelinks index on read new
Currently, in read new wikis, it tries to force tl_backlinks_namespace
index which won't work on new queries leading to lots of slow queries.

Bug: T306673
Change-Id: Ic72bb96bc53c1882bd2113cf66c73d2e46475754
2022-05-16 15:40:36 +02:00
Umherirrender
d79fd02d4b Use injection for LinksMigration on LinkBatch, action, api, special page
Even the service does not long stay in that classes,
it should be injected to avoid global state

Bug: T304780
Change-Id: Ib488037f5a6966ab61042ed3cd889ddc50f1ba8e
2022-05-10 20:19:58 +00:00
Amir Sarabadani
2cfaf2da13 api: Add support for LinksMigration in ApiQueryBacklinksprop
It should be generalized enough to be used in future tables as well.
Note that it removes the useIndex when the table is in mapping even if
it's read old because useIndex is evil and should not be used like this.

Also removing the straight join hint as it would intefere with
linktarget join and I tested the query with the current version of
mariadb we have in production and it works just fine.

Bug: T304780
Change-Id: Iea3e0b309ddd013687008571513afeef6b9b672b
2022-04-20 22:53:33 +02:00
Aryeh Gregor
666ca1bdf3 Use MainConfigNames instead of string literals, #2
This covers all occurrences of /onfig->.*get( '/ in includes/.
Undoubtedly there are still plenty more to go.

Change-Id: I33196c4153437778496f40436bcde399638ac361
2022-04-13 18:55:46 +03:00
Umherirrender
6caf78c2c8 phan: Remove PhanPossiblyUndeclaredVariable suppression
Make phan stricter about conditional variable declaration
Remaining false positive issues are suppressed.
The suppression and the setting change can only be done together

Bug: T259172
Change-Id: I1f200ac37df7448453688bf464a8250c97313e5d
2022-03-30 19:47:15 +00:00
Umherirrender
7aa0884029 phan: Remove PhanTypePossiblyInvalidDimOffset suppression
Make phan stricter about array keys
Remaining false positive issues are suppressed.
The suppression and the setting change can only be done together

Bug: T304887
Depends-On: I3105a5fd4826f8667b5232834defc5ec93be32a1
Depends-On: Ie9610a6e83731468311edb3ed17f80fc509de385
Change-Id: I701f12ab94478c3b8e7fd82110ade74a8e6b04ef
2022-03-28 23:26:49 +02:00
Tim Starling
9c3c0b704b Use array_fill_keys() instead of array_flip() if that reflects the developer's intention
array_fill_keys() was introduced in PHP 5.2.0 and works like
array_flip() except that it does only one thing (copying keys) instead
of two things (copying keys and values). That makes it faster and more
obvious.

When array_flip() calls were paired, I left them as is, because that
pattern is too cute. I couldn't kill something so cute.

Sometimes it was hard to figure out whether the values in array_flip()
result were used. That's the point of this change. If you use
array_fill_keys(), the intention is obvious.

Change-Id: If8d340a8bc816a15afec37e64f00106ae45e10ed
2021-06-15 00:11:10 +00:00
daniel
246c543737 ApiPageSet: replace Title in method signatures
Bug: T278459
Change-Id: Ie817fd476d6236791431b5ba5ce557800ef5222d
2021-05-12 23:17:59 +02:00
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
jenkins-bot
60099b87db Merge "Add PHPDoc documentation to all API query module constructors" 2020-11-01 21:04:39 +00:00
Umherirrender
c85a43561e Improve class property documentation
Reformat existing documentation to match the format

Change-Id: I190b54b5e962f17bab6502dd1b3c02f11dc926d2
2020-10-30 10:38:58 +01:00
Thiemo Kreuz
08cf4c49df Add PHPDoc documentation to all API query module constructors
Change-Id: I2bc140576efc891bb9b6e50e44041d5bb022ac8f
2020-10-27 19:16:41 +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
f5d0ecce94 Run executeGenderCacheFromResultWrapper with titles in prop=linkshere
The gender information are only needed when outputting titles,
in other cases (when only pageids are selected) it results in a
undefined property

Bug: T235316
Follow-Up: I911dcb160a7b169091b9e8f66fb3908d0f2a1ba4
Change-Id: I5c9a501919914afd38343551c755126c98d457e6
2019-10-12 10:40:22 +02:00
Umherirrender
a8525d7201 Fill GenderCache for used pages in action=query&prop=fileusage
Add an utility function
ApiQueryBase::executeGenderCacheFromResultWrapper

GenderCache stops working when there are more than 1000 cache missed
and returning the default value (T200238)
Fill the cache with all needed users avoids this behaviour
and it saves one query per user page.

Change-Id: I911dcb160a7b169091b9e8f66fb3908d0f2a1ba4
2019-09-25 21:41:34 +02:00
Brad Jorsch
c7bccd3a8c API: Apply miser-mode namespace check for generator=redirects
Bug: T224246
Change-Id: Icad65fcc14ad91d302c8285e08d6a7ea299a90c6
2019-06-07 20:36:18 +00:00
Brad Jorsch
4000245c45 API: Handle empty xxnamespace parameter in ApiQueryBacklinksprop
If the xxnamespace parameter is specified but empty, we don't need to do
anything because there will be no results.

Bug: T205810
Change-Id: Iae5d766a5408368cadd01583d18b745661304a7f
2018-10-01 10:18:40 -04:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Bartosz Dziewoński
485f66f174 Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient
Find: /isset\(\s*([^()]+?)\s*\)\s*\?\s*\1\s*:\s*/
Replace with: '\1 ?? '

(Everywhere except includes/PHPVersionCheck.php)
(Then, manually fix some line length and indentation issues)

Then manually reviewed the replacements for cases where confusing
operator precedence would result in incorrect results
(fixing those in I478db046a1cc162c6767003ce45c9b56270f3372).

Change-Id: I33b421c8cb11cdd4ce896488c9ff5313f03a38cf
2018-05-30 18:06:13 -07:00
Umherirrender
c9d303d39e Remove "Created on" from file header comments
It is the job of git and svn to provide this information/metadata.
The form was different, some with short, some with long month name
some with leading zero at the day, some without.
The year is also present in the Copyright clause

Change-Id: If006907b82b9e45f13cfa2e45d41107a95570e1a
2018-01-26 23:12:40 +00:00
Brad Jorsch
9f86261000 API: Account for PHP 7.2 change
PHP 7.2 broke existing functionality in making count( null ) raise a
warning. So add tests for null all over the place, or change tests where
we know the value is null or an array (but not false, empty-string, or
0) to just cast to boolean.

Bug: T182004
Change-Id: Idfe23a07daa9f60eee72f2daf04304be87057a29
2017-12-04 13:46:33 -05:00
Bryan Davis
aaf1e17088 Remove "@author Bryan Davis" and "Brad Jorsch" annotations
Undo traces of a practice we carried over from past projects and
existing examples that is neither universal nor actively encouraged in
the MediaWiki codebase.

Bug: T139301
Change-Id: I5c9c89b72a45a44aa4264a5e57b003c1a86cdf6e
Co-Authored-By: Brad Jorsch <bjorsch@wikimedia.org>
2017-06-26 15:25:46 -06:00
Baha
036b9c4167 Make API documentation links language aware
Links generated by the API are now aware of the user's preferred
language and will show documents in that language if available.

To test, log in to mediawiki.org and set your language preference to 'es',
then on an MediaWiki installation with this patch view the generated
expanded API help at `api.php?action=help&recursivesubmodules=1&modules=main`.
Each link to documentation on mediawiki.org should take you to its
translated /es subpage, if one exists.

Bug: T104518
Change-Id: I339a1f3ae1bce9d759cf251899d57c32b1def91e
2017-04-05 11:08:25 -04:00
Brad Jorsch
47e2bec361 API: Allow finding log events and links to special pages
Log events are sometimes attributed to a special page; it should be
allowed to use rcnamespace or lenamespace to filter for these.

It's also possible for special pages to be the targets of redirects, so
list=allredirects and prop=redirects should find them.

Maybe someday we'll record links to and transclusions of special pages
too (see T19597), so we may as well make it possible to query for those
as well via list=alllinks, list=alltransclusions, list=backlinks,
list=embeddedin, prop=linkshere, prop=transcludedin, prop=links, and
prop=templates.

NS_MEDIA has similar considerations: although we currently "normalize"
page links to the corresponding File and I don't think anything logs the
Media title rather than the File, transclusions and redirects do show
up in those tables.

Bug: T154319
Change-Id: I00348f83855c6c703b6bd6015f6d3bedc5bfd1c5
2017-01-06 13:07:29 -05:00
Brad Jorsch
4e6810e4a2 API: i18n for warnings and errors
API warnings and error messages are currently hard-coded English
strings. This patch changes that.

With a few exceptions, this patch should be compatible with non-updated
extensions:
* The change to ApiBase::$messageMap will blow up anything trying to
  mess with it.
* The changes to the 'ApiCheckCanExecute' hook will cause a wrong
  (probably unparsed) error message to be emitted for extensions not
  already using an ApiMessage. Unless they're currently broken like
  Wikibase.

Bug: T37074
Bug: T47843
Depends-On: Ia2b66b57cd4eaddc30b3ffdd7b97d6ca3e02d898
Depends-On: I2e1bb975bb0045476c03ebe6cdec00259bae22ec
Depends-On: I53987bf87c48f6c00deec17a8e957d24fcc3eaa6
Depends-On: Ibf93a459eb62d30f7c70d20e91ec9faeb80d10ed
Depends-On: I3cf889811f44a15935e454dd42f081164d4a098c
Depends-On: Ieae527de86735ddcba34724730e8730fb277b99b
Depends-On: I535344c29d51521147c2a26c341dae38cec3e931
Change-Id: Iae0e2ce3bd42dd4776a9779664086119ac188412
2016-12-06 10:20:48 -05:00
Brad Jorsch
81912c3ef9 API: Force straight join for prop=linkshere|transcludedin|fileusage
MySQL (or at least 5.5.5-10.0.23-MariaDB) chooses a really bad query plan if it
thinks there will be more matching rows in the linktable than are in page. Use
STRAIGHT_JOIN here to force it to use the intended, fast plan. See T145079 for
details.

Bug: T145079
Change-Id: I1a363450b818a049938145d0feb207c9894d2a49
2016-09-14 10:13:10 -04:00
Brad Jorsch
17b837d725 API: Force indexes for prop=linkshere|transcludedin|fileusage
MySQL's optimizer chokes if we have too many values in "$bl_title IN
(...)" and chooses the wrong index, so specify the correct index to use
for the query.

Bug: T139056
Change-Id: I989dfbe898b75d649c22e198a6fbdd3a2de196e5
2016-06-30 13:53:49 -04:00