Topic on Project:Support desk

Block Video Uploads for Non Admins - Extension Troubleshooting

2
Kimmywingz (talkcontribs)

Hello,

I am working on a custom extension that would only allow admin users to upload video files, the extension is enabled and appears under Special:Version. The permission also appears under the correct category under Special:ListGroupRights.

However, when I attempt to upload a video (mp4) under Special:Upload, it goes through.

I just enabled error reporting / showExceptionDetails / debugToolbar / ShowDebug / DevelopmentWarnings and added print statements to my extension file, but I am not seeing any indicators that it's trying to work but is failing.

I am hoping somebody could point me in the right direction on how to debug this more efficiently or offer insight into the problem. Thank you for your time and expertise.


In the PHP file:

namespace MediaWiki\Extension\VideoPermissions;

use MediaWiki\Hook\UploadVerifyUploadHook;

use User;

use UploadBase;

class PermissionCheck implements UploadVerifyUploadHook {

public function onUploadVerifyUpload (UploadBase $upload, User $user, ?array $props, $comment, $pageText, &$error) {

$file = $upload->getLocalFile();

if ($file->getMediaType() !== MEDIATYPE_VIDEO) {

return;

} else {

$title = $upload->getTitle();

if ($user->definitelyCan('videopermission-allow', $title)) {

} else {

$error = 'videopermission-no-perm-to-upload';

return false;

}}}


in the extension.json file:

"AvailableRights": ["videopermissions-allow"],

"GroupPermissions": {"sysop": {

"videopermissions-allow": true

}},

"Hooks": {"UploadVerifyUpload": "main"

},

"HookHandlers": {"main": {

"class": "\\MediaWiki\\Extension\\VideoPermissions\\PermissionCheck"

}},

Bawolff (talkcontribs)

So, first of all, print() (and echo) won't work in mediawiki due to output buffering. Always use var_dump() if you want to output something for debugging purposes to the page (or if you want to output to the debug log, use the debug logging system. You can use wfDebug() as a one off, or add a LoggerFactory as a dependency if you want to do it more properly for longer lived code). I personally tend to use var_dump() for one off debugging instead of debug log system, as i find it easier, but its a matter of personal preference.

One thing to note is that in workflows where a POST request is made, processing happens, and then the user is redirected to a different page, the debug messages will be associated with the redirect not the target. I believe that upload processing is one such example of this. In these cases wgDebugToolbar and var_dump wont work (a hack sometimes used is to follow var_dump() immediately by die() to stop processing). In such a case i reccomend setting $wgDebugLogFile to some file and then using wfDebug() for debugging. That way you wont miss the debug messages on redirects.


Based on your description it sounds like permissions are being setup correctly, but the hook is not firing. My suggestion would be to verify if that is true, by putting wfDebug("HERE"); as the first line of your onUploadVerifyUpload function and checking the debug log file to see if it is called at all. If it is being called then i would suggest recording the values of various conditions variables to ensure they match what you think they should be.

Another thing to check - i'm not sure at what point this hook is called. Determining media type is part of file verification. Possibly this hook happens before the file object knows the file is a video. If so, you might have better luck using the $props array passed to the hook. I believe it should also have media type information.

Reply to "Block Video Uploads for Non Admins - Extension Troubleshooting"