Commit graph

257 commits

Author SHA1 Message Date
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
Umherirrender
c7ad21c25f Improve param docs
Change-Id: I746a69f6ed01c3ff000da125457df62b02d13b34
2019-11-28 19:08:59 +01:00
Daimona Eaytoy
d3252fc870 Fix new phan errors, part 5
Some of the errors are suppressed because they're phan false positives.
The idea behind this is that they'll be fixed in a future version of
phan, and we'll just have to remove the suppressions.

Note: I'm disabling UnusedPluginSuppression so that we can start suppressing
issues even if they're still disabled. The sniff should be re-enabled
as soon as we upgrade phan.

Bug: T231636
Change-Id: I0f7fa06a9e03fbb86c7a5eb6e50a850bb258a7f7
2019-10-18 20:45:53 +00:00
Umherirrender
268346e562 phan: Enable PhanTypeMismatchArgument issue
Bug: T231636
Depends-On: I5de4f8f32a47c3f41c990ffe2ebd091fc23d1a58
Change-Id: I34d65fe3ff1916f2af675f0b1f19641b0cdfadc0
2019-09-19 20:11:42 +02:00
Daimona Eaytoy
9881ea93be Better grouping for code adding elements to arrays
Change-Id: I7cc8c810f0dcf1ee84b9d5f14967ec2568e3e0be
Follow-up: I50377746f01749b058c39fd8229f9d566224cc43
2019-09-03 19:38:57 +02:00
Daimona Eaytoy
e2e543f7c2 Unsuppress more phan issues (part 5)
Bug: T231636
Depends-On: I6e5fba7bd273219b1206559420b5bdb78734aa84
Change-Id: I50377746f01749b058c39fd8229f9d566224cc43
2019-09-01 09:48:31 +00:00
Tim Starling
51e837f68f Don't try to store File objects to the upload session
File objects can contain closures which can't be serialized.

Instead, add makeWarningsSerializable(), which converts the warnings
to a serializable array. Make ApiUpload::transformWarnings() act on this
serializable array instead. For consistency, ApiUpload::getApiWarnings()
also needs to convert the result of checkWarnings() before transforming
it.

Bug: T228749
Change-Id: I8236aaf3683f93a03a5505803f4638e022cf6d85
2019-07-26 16:15:30 +10:00
Hashar
723db87d9d Revert "Always validate uploads over api"
The verification is broken with chunken uploads and ultimately
cause large files to no more be uploadable.

This reverts commit 38ec6d8a34.

Bug: T223448
Change-Id: If414a8f751a3e1488a2ab099abd8b598c973c1f4
2019-05-17 14:42:44 +00:00
Umherirrender
38ec6d8a34 Always validate uploads over api
filesize and title are validated in UploadBase::verifyUpload with more
accurate error message

Using stashed async with a long title can cause null errors later on

Bug: T208539
Change-Id: I545435e2baa222ae1544673011c5527874d1d2cb
2019-05-09 21:20:10 +02:00
Umherirrender
fcaa4c0fdd Fix api error for apierror-upload-missingresult
The message key was wrong

Change-Id: I2cc4f9e1523bef6164eb9ff4b79655b007665fae
2019-03-26 22:44:05 +01:00
Thalia
520dbe677a Check whether a user is blocked from upload in ApiUpload
Partially blocked users are not blocked from uploading, so check
explicitly whether a user is blocked from uploading, rather than
simply whether they have a block.

Bug: T217255
Change-Id: I21089a90202941ec3bc5bdbf9c0d2e7cab00b365
2019-03-20 22:19:02 +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
3124a990a2 Use ::class to resolve class names in includes files
This helps to find renamed or misspelled classes earlier.
Phan will check the class names

Change-Id: I07a925c2a9404b0865e8a8703864ded9d14aa769
2018-01-27 20:34:29 +01: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
Kunal Mehta
0cb2a09b6d Always put type information before variable name for @var tags
Like other tags, @var should have the type information before the
variable name.

<https://docs.phpdoc.org/references/phpdoc/tags/var.html>

Change-Id: I9eca6957b1990fa8cc687103dc02ee38af5f9086
2017-09-04 11:06:51 -07: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
93d16b8ce9 ApiUpload: Raise an error on attempts to async-upload a POSTed file
"Upload from stash" and "Upload from POST" use the same code path via
ApiUpload::performUpload(). It looks like when a31d0f8edd implemented
"async upload from stash", it was overlooked that someone might attempt
to do an unsupported "async upload from POST".

Bug: T156365
Change-Id: I6d207270af490cfc59d994a22a6406df45a9f6a2
2017-01-26 12:26:38 -05:00
Erik Bernhardson
e5b8bf4942 Un-blacklist PhanUndeclaredVariable
Undeclared variables are a very common error type that we want to catch
as often as possible. To avoid needing to refactor a variety of global
level code (mostly in old-style maintenance scripts) this ignores
undeclared variables in global scope. This is still a good improvement
over what was happening previously.

Change-Id: I50b41d571724244552074b9408abbdf6160aca59
2017-01-18 13:07:39 -08:00
WMDE-Fisch
4b89b32f27 Fix var name in ApiUpload
Found via phan.

Change-Id: I38b35db6b83d0728bcc8e0990f646b7d331cab13
2017-01-04 12:54:45 +01:00
Brad Jorsch
a90bbf1a48 Add ILocalizedException interface
We already throw around some exceptions that are localized
(ErrorPageError and its subclasses, MalformedTitleException), but
there's no standard way to recognize them. Let's change that.

Then let's use them in the API to be able to have internationalized
errors when such exceptions are caught, instead of wrapping the
English-language version.

Change-Id: Iac7c90f92a889f8de9dae373547c07b884addaea
2016-12-08 13:53:24 -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
Matthias Mullie
4e50971b32 Add API warnings when upload is same as older versions
Bug: T141822
Change-Id: I115d84d865c59200dbb60bd962c093185c9afafe
2016-09-06 18:43:00 +00:00
Bartosz Dziewoński
13d2035cbb ApiUpload: Better handle unreasonably large metadata in 'imageinfo'
Bug: T143993
Change-Id: I1fcdbca9981dd034572eeb32070d574cf97a132f
2016-08-26 16:53:52 +02:00
Bartosz Dziewoński
0afc3cf016 ApiUpload: Fix fatal in dieStatusWithCode()
If $extraData was null, but $moreExtraData was given, the following
fatal would occur:

  Fatal error: Unsupported operand types in
  /var/www/html/w/includes/api/ApiUpload.php on line 408

Follow-up to c9b5b3e988.

Change-Id: I613eed1f7429247fe46afa454d36f518f6a81ebe
2016-08-22 21:16:21 +02:00
Bartosz Dziewoński
959daa2ca4 Check for warnings for assembled file after a chunked upload
Bug: T130564
Change-Id: Iebc84f030c45c634dc29b02cbd720f33abf50f2b
2016-08-19 01:33:10 +00:00
Bartosz Dziewoński
ec41a9c559 UploadBase: Stop mLocalFile doubling as stashed file
"I've a great idea", they said. "You know what would be cool? If I
made this boring getter, getLocalFile(), return something completely
different after the file was stashed. This will be a nice surprise for
someone in the future to discover", they added gleefully.

I am pretty sure everything still works, but I never could get async
upload publishing to work locally, so I'd appreciate some testing.

Change-Id: I11dcf2ed89e4f1dd8ddf081af521da005efdbf39
2016-08-19 03:00:30 +02:00
Bartosz Dziewoński
c9b5b3e988 Run 'UploadStashFile' hook for chunked uploads too
In UploadFromChunks, doStashFile() only stashes the current chunk,
and stashing the whole file after all chunks are uploaded is done
in concatenateChunks().

Also more custom code on top of custom code to handle ApiMessage
errors, because action=upload is special.

Follow-up to 2ee27d9a4d.

Change-Id: I968280353f15bbca9b1059631fa2cfd7c3cd2f1d
2016-08-17 17:53:08 +02:00
Bartosz Dziewoński
09f2cba006 Do not call the 'UploadStashFile' hook for partially uploaded files
Consumers of this hook don't expect partial files, and even if they did,
they have no way to tell if they've been given a partial file.

Also document a pre-existing restriction that verifyUpload() must be
called first (for non-partial files). ApiUpload previously called this
function for partial files too, causing T143161.

Follow-up to 2ee27d9a4d.

Bug: T143161
Change-Id: I107cb957e046545ea72834d72233cc1ed2867e42
2016-08-17 01:34:52 +02:00
Bartosz Dziewoński
2ee27d9a4d Introduce UploadStashFile hook, improve API handling of stash errors
UploadBase:
* Introduce a new method, tryStashFile(), as a replacement for the
  now-soft-deprecated stashFile(). The method runs the new hook and
  returns a Status object, with an error (if the hook returned an
  error) or a value (if it didn't).
* Introduce a new hook, UploadStashFile, allowing extensions to
  prevent a file from being stashed. Note that code in extensions
  which has not been updated for MediaWiki 1.28 may still call
  stashFile() directly, and therefore not call this hook. For
  important checks (not just for UI), extension authors should use
  UploadVerifyFile or UploadVerifyUpload hooks.
* Extract common code of tryStashFile() and stashFile() to
  a new protected method doStashFile().

SpecialUpload:
* Use tryStashFile() when stashing a file after a warning or
  "recoverable error" was encountered.

ApiUpload:
* Refactor stashing code so that error handling only happens in one
  place, not four different ones. Use Status objects rather than
  exception throwing/catching for control flow.
* Simplify the error messages slightly (error codes are unchanged).
  Produce better ones by always using handleStashException().
  'stashfailed' is now always at root (not nested inside 'warnings'),
  behaving the same as 'filekey' does on success.
* Use tryStashFile() when stashing. Handle errors so as to allow
  custom API results passed via ApiMessage to be preserved.

Some API result changes for different requests are shown below.

  api.php?action=upload&format=json&filename=good.png&file=...&stash=1

    Before:
      {
        "error": {
          "code": "stashfilestorage",
          "info": "Could not store upload in the stash: Stashing temporary file failed: UploadStashFileException Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\".",
          "*": "See http://localhost:3080/w/api.php for API usage"
        }
      }

    After:
      {
        "error": {
          "code": "stashfilestorage",
          "info": "Could not store upload in the stash: Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\".",
          "*": "See http://localhost:3080/w/api.php for API usage"
        }
      }

  api.php?action=upload&format=json&filename=[bad].png&file=...

    Before:
      {
        "upload": {
          "result": "Warning",
          "warnings": {
            "badfilename": "-bad-.png",
            "stashfailed": "Stashing temporary file failed: UploadStashFileException Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\"."
          }
        }
      }

    After:
      {
        "upload": {
          "result": "Warning",
          "stashfailed": "Could not store upload in the stash: Error storing file in '/tmp/phpB32SRT': Could not create directory \"mwstore://local-backend/local-temp/3/3a\"."
          "warnings": {
            "badfilename": "-bad-.png",
          }
        }
      }

Bug: T140521
Change-Id: I2f574b355cd33b2e9fa7ff8e1793503b257cce65
2016-08-09 18:10:18 +02:00
jenkins-bot
b3307c8a5a Merge "ApiUpload: Better handle ApiMessage errors from UploadVerifyFile hook" 2016-07-11 19:34:49 +00:00
jenkins-bot
96d9fb9731 Merge "Introduce new hook UploadVerifyUpload to allow preventing file uploads" 2016-07-07 02:27:44 +00:00
Brad Jorsch
5f01cbb3ad Allow Message::newFromSpecifier to handle ApiMessages
Instead of constructing a new Message from the Message as
a MessageSpecifier, just clone the existing Message which will preserve
subclass data.

Also, make use of this to simplify the logic in ApiBase::parseMsg().

Change-Id: I9545acb8da752c0c21e16d8b1d37d8802fcb329d
2016-06-28 12:15:27 -04:00
Bartosz Dziewoński
931796f88b ApiUpload: Better handle ApiMessage errors from UploadVerifyFile hook
The extra data is actually understood and output now.

Bug: T137961
Change-Id: Ifac8995a4d16d11840cee814177fc2808bc2072c
2016-06-23 04:39:02 +00:00
Bartosz Dziewoński
8f2acfcde4 Introduce new hook UploadVerifyUpload to allow preventing file uploads
The new hook runs at the beginning of UploadBase::performUpload().
Unlike the existing UploadVerifyFile hook, the new one provides the
information about the file description page being created, and the
user who is performing the upload. It also does not run for uploads
to stash.

The action=upload API and Special:Upload have been changed to treat
errors from UploadBase::performUpload() as recoverable, which means
that they will attempt to stash the file (previously they would require
the user to upload it again). This is mostly intended for errors from
the new hook, but I think it makes sense for the existing ones, which
are mostly transient filesystem erorrs.

Bug: T89302
Change-Id: Ie68801b307de8456e1753ba54a29c34c8063bc36
2016-06-22 23:38:50 +02:00
Matthias Mullie
ed6a2d5df1 Check if user is blocked during upload process
Further down, this was already somewhat being checked.
On L112, `verifyTitlePermissions` is called, which will fail
if the user is blocked. However:

* This was not being checked during stashed uploads
* Block just "happens" to be part of that check: the intent is
  actually to verify the file title. The error is treated as
  recoverable (the title can be changed), but it isn't (the
  user can't unblock himself in this process)

Bug: T111228
Change-Id: I9cbf250a0b92c3daa3a0843f2257cc049abd3923
2016-05-03 13:23:44 +00:00
jenkins-bot
8cb8af442a Merge "Use english messages for background use of Status::getWikiText" 2016-04-13 14:03:56 +00:00
umherirrender
932c37e3cb Use english messages for background use of Status::getWikiText
Status::getWikiText is used for internal logging, api error messages and
maintenance scripts. All this places are usually in english, so pass an
english language to getWikiText.

Change-Id: I3010fca8eb5740a3a851c55a8b12e171714c78f7
2016-04-12 20:01:44 +02:00
Mark Holmquist
785d472559 Add preference for watching uploaded files
Adds a preference in the Watchlist section for watching uploaded files
for an account. Also works from API-based upload methods, so
UploadWizard and other tools should work fine.

Bug: T33313
Change-Id: If962e667de12b35904b2d1b2d9e99c26b588ec2a
2016-04-08 15:21:17 -05:00
Siebrand Mazeland
5b119a0e44 Replace uses of join() by implode()
All of core uses implode() consistently now.

Change-Id: Iba50898c64c43f356d1caf8869f484e90d9ff651
2016-03-08 18:24:16 +00:00
Siebrand Mazeland
bc0ae710e3 Use single quotes in API where possible
Change-Id: I972e296f4820f78f5dfcecc27bc4912ca84a3178
2016-03-08 17:27:00 +01:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Bartosz Dziewoński
05c2c65ab9 Allow users to tag file uploads
Using either action=upload API or Special:Upload. (No user interface
is provided for the latter, this is meant to be used by on-wiki
scripts/gadgets enhancing the upload process.)

Modelled after how ae3ab9eef0
implemented tagging of regular edits.

Bug: T121876
Change-Id: Ia3e0dbd895b2f8bc66985b24db35f112b6f9a22d
2016-02-16 03:51:29 +01:00
Bartosz Dziewoński
1ebc9128d0 Remove putrid remains of $wgAllowAsyncCopyUploads
None of this works and it's been long begging for a mercy kill.
All it does is waste contributor time on updating deprecations
in the dead code. I imagine we wouldn't reuse much of this
code if we're ever going to reimplement it.

Bug: T119336
Change-Id: Ibd26a4bea621857aac77823017e9be9b7dc52cca
2016-01-22 00:18:25 +00:00
Reedy
32ff1f4c47 Remove various unused variables
Change-Id: I4b1b20b4126735cb32a80e473fe48d523bcb24d1
2015-11-07 21:22:17 +00:00
jenkins-bot
0183ae1453 Merge "Allow passing detailed permission errors data to API" 2015-11-04 22:38:47 +00:00
Brad Jorsch
05ff357a42 API: Log all deprecated parameter uses to api-feature-usage.log
Some were being logged, and some weren't. Let's log them all
automatically when PARAM_DEPRECATED is processed, instead of requiring
each module to manually log them.

Bug: T117569
Change-Id: Ia38aeeccd0b9857b12b28914f509284483fbcca8
2015-11-03 12:23:00 -05:00
Bartosz Dziewoński
92c29b8891 Allow passing detailed permission errors data to API
Using the new system introduced in
1c57794e37 (see T47843).

This change allows Title::getUserPermissionsErrors() to include
MessageSpecifiers instead of string message keys in its return value.
This doesn't seem to have any bad effects, and should work seamlessly as
long as callers aren't trying to do anything stupid and just pass the
value to PermissionsError or OutputPage::showPermissionsErrorPage()
or wfMessage() or some such.

If the callers *are* trying something stupid, nothing worse than
duplicated or otherwise less-than-perfect error messages (in code
which tries to handle some message keys specially) should happen.
(I fixed wfMergeErrorArrays(), but who knows what else lurks in all
this code.) Any problems should only affect new-style errors using
MessageSpecifier, though.

Since MessageSpecifiers tend to be stringable, we probably won't get
fatals, but might get incorrect checks. Should we try to log this
happening somehow?

Goes with I42a0c5b0ea7e61088dd609b764dd7d1396c60cd5 in TitleBlacklist.

Bug: T115258
Change-Id: I1334ba21a2862973a9d8ff5be2c9bec06a82698b
2015-11-02 17:11:50 +01:00
jenkins-bot
376879ee06 Merge "ApiBase::PARAM_DFLT => null is the default anyway" 2015-10-26 19:02:33 +00:00
csteipp
59b627b0b7 SECURITY: API: Improve validation in chunked uploading
This fixes a few shortcomings in the chunked uploader:
* Raises an error if offset + chunksize > filesize.
* Enforces a minimum chunk size for non-final chunks.
* Refuses additional chunks after seeing a final chunk.
* Status of a chunked upload in progress is now available with
  'checkstatus'.

Bug: T91203
Bug: T91205
Change-Id: I2262db1bc8460616b069c564475d2e4148001768
2015-10-16 14:10:44 -07:00
csteipp
c804391572 SECURITY: Throttle uploads
Add throttle check in ApiUpload and SpecialUpload.

Bug: T91850
Change-Id: If33cc99f304aab2486507c7500b4abb06b6b5d70
2015-10-16 11:23:18 -07:00