This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
SpecialAddVideo.php
182 lines (154 loc) · 4.37 KB
/
SpecialAddVideo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Special:AddVideo - special page for adding Videos
*
* @ingroup extensions
* @file
*/
class AddVideo extends SpecialPage {
/**
* New video object created when the title field is validated
*
* @var Video
*/
protected $video;
/**
* Constructor -- set up the new special page
*/
public function __construct() {
parent::__construct( 'AddVideo' /*class*/, 'addvideo' /*restriction*/);
}
/**
* Show the special page
*
* @param $par Mixed: parameter passed to the page or null
* @return bool|null
*/
public function execute( $par ) {
global $wgExtensionAssetsPath;
$out = $this->getOutput();
// Add CSS
if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
$out->addModuleStyles( 'ext.video' );
} else {
$out->addExtensionStyle( $wgExtensionAssetsPath . '/Video/Video.css' );
}
// If the user doesn't have the required 'addvideo' permission, display an error
if( !$this->userCanExecute( $this->getUser() ) ) {
$this->displayRestrictionError();
return;
}
// Show a message if the database is in read-only mode
if ( wfReadOnly() ) {
throw new ReadOnlyError;
}
// If user is blocked, s/he doesn't need to access this page
if( $this->getUser()->isBlocked() ) {
throw new UserBlockedError( $this->getUser()->mBlock );
}
$this->setHeaders();
$form = new HTMLForm( $this->getFormFields(), $this->getContext() );
$form->setIntro( wfMsgExt( 'video-addvideo-instructions', 'parse' ) );
$form->setWrapperLegend( wfMsg( 'video-addvideo-title' ) );
$form->setSubmitText( wfMsg( 'video-addvideo-button' ) );
$form->setSubmitCallback( array( $this, 'submit' ) );
if ( $this->getRequest()->getCheck( 'forReUpload' ) ) {
$form->addHiddenField( 'forReUpload', true );
}
$form->show();
}
/**
* Extracts the URL and provider type from a raw string
*
* @param string $value Value form the Video input
* @return array Element 0 is the URL, 1 is the provider
*/
protected function getUrlAndProvider( $value ) {
$url = $value;
if ( !Video::isURL( $url ) ) {
$url = Video::getURLfromEmbedCode( $value );
}
return array( $url, Video::getProviderByURL( $url ) );
}
/**
* Custom validator for the Video field
*
* Checks to see if the string given is a valid URL and corresponds
* to a supported provider.
*
* @param $value Array
* @param $allData Array
* @return bool|String
*/
public function validateVideoField( $value, $allData ) {
list( , $provider ) = $this->getUrlAndProvider( $value );
if ( $provider == 'unknown' ) {
return wfMsg( 'video-addvideo-invalidcode' );
}
return true;
}
/**
* Custom validator for the Title field
*
* Just checks that it's a valid title name and that it doesn't already
* exist (unless it's an overwrite)
*
* @param $value Array
* @param $allData Array
* @return bool|String
*/
public function validateTitleField( $value, $allData ) {
$video = Video::newFromName( $value, $this->getContext() );
if ( $video === null || !( $video instanceof Video ) ) {
return wfMsg( 'badtitle' );
}
// TODO: Check to see if this is a new version
if ( $video->exists() && !$this->getRequest()->getCheck( 'forReUpload' ) ) {
return wfMsgHtml( 'video-addvideo-exists' );
}
$this->video = $video;
return true;
}
/**
* Actually inserts the Video into the DB if validation passes
*
* @param $data Array
* @return bool
*/
public function submit( array $data ) {
list( $url, $provider ) = $this->getUrlAndProvider( $data['Video'] );
$this->video->addVideo( $url, $provider, false, $data['Watch'] );
$this->getOutput()->redirect( $this->video->getTitle()->getFullURL() );
return true;
}
/**
* Fields for HTMLForm
*
* @return array
*/
protected function getFormFields() {
$fields = array(
'Title' => array(
'type' => 'text',
'label-message' => 'video-addvideo-title-label',
'size' => '30',
'required' => true,
'validation-callback' => array( $this, 'validateTitleField' ),
),
'Video' => array(
'type' => 'textarea',
'label-message' => 'video-addvideo-embed-label',
'rows' => '5',
'cols' => '65',
'required' => true,
'validation-callback' => array( $this, 'validateVideoField' ),
),
'Watch' => array(
'type' => 'check',
'label-message' => 'watchthisupload',
'default' => $this->getUser()->getGlobalPreference( 'watchdefault' ),
),
);
return $fields;
}
}