r99335 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99334‎ | r99335 | r99336 >
Date:05:44, 9 October 2011
Author:johnduhart
Status:deferred
Tags:
Comment:
Converting SpecialAddVideo to use HTMLForm
Modified paths:
  • /trunk/extensions/Video/SpecialAddVideo.php (modified) (history)
  • /trunk/extensions/Video/VideoHooks.php (modified) (history)
  • /trunk/extensions/Video/VideoPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Video/VideoPage.php
@@ -53,7 +53,7 @@
5454 $title,
5555 wfMsgHtml( 'video-novideo-linktext' ),
5656 array(),
57 - array( 'wpDestName' => $this->video->getName() )
 57+ array( 'wpTitle' => $this->video->getName() )
5858 );
5959 $wgOut->addHTML( wfMsgWikiHtml( 'video-novideo', $link ) );
6060
@@ -221,7 +221,10 @@
222222 SpecialPage::getTitleFor( 'AddVideo' ),
223223 wfMsg( 'uploadnewversion-linktext' ),
224224 array(),
225 - array( 'wpDestName' => $this->video->getName() )
 225+ array(
 226+ 'wpTitle' => $this->video->getName(),
 227+ 'wpForReUpload' => 1,
 228+ )
226229 );
227230 $wgOut->addHTML( "<li>{$ulink}</li>" );
228231 }
Index: trunk/extensions/Video/VideoHooks.php
@@ -90,7 +90,7 @@
9191 $wgOut->redirect(
9292 // escapeFullURL() converts & to &amp; which causes the
9393 // destName parameter not to work correctly
94 - $addTitle->getFullURL( 'destName=' . $video->getName() )
 94+ $addTitle->getFullURL( 'wpTitle=' . $video->getName() )
9595 );
9696 }
9797 }
Index: trunk/extensions/Video/SpecialAddVideo.php
@@ -3,6 +3,13 @@
44 class AddVideo extends SpecialPage {
55
66 /**
 7+ * New video object created when the title field is validated
 8+ *
 9+ * @var Video
 10+ */
 11+ protected $video;
 12+
 13+ /**
714 * Constructor -- set up the new special page
815 */
916 public function __construct() {
@@ -25,15 +32,14 @@
2633 }
2734
2835 // If the user doesn't have the required 'addvideo' permission, display an error
29 - if( !$wgUser->isAllowed( 'addvideo' ) ) {
30 - $wgOut->permissionRequired( 'addvideo' );
 36+ if( !$this->userCanExecute( $this->getUser() ) ) {
 37+ $this->displayRestrictionError();
3138 return;
3239 }
3340
3441 // Show a message if the database is in read-only mode
3542 if ( wfReadOnly() ) {
36 - $wgOut->readOnlyPage();
37 - return;
 43+ throw new ReadOnlyError;
3844 }
3945
4046 // If user is blocked, s/he doesn't need to access this page
@@ -42,126 +48,91 @@
4349 return false;
4450 }
4551
46 - $categories = $wgRequest->getVal( 'wpCategories' );
 52+ $this->setHeaders();
4753
48 - // wpDestName URL parameter is set in VideoPage.php; when viewing a
49 - // video page of a video that does not yet exist, there is a link to
50 - // Special:AddVideo and the wpDestName parameter will be set to the
51 - // name of the video
52 - $destination = $wgRequest->getVal( 'destName' );
53 - if( !$destination ) {
54 - $destination = $wgRequest->getVal( 'wpDestName' );
 54+ $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
 55+ $form->setIntro( wfMsgExt( 'video-addvideo-instructions', 'parse' ) );
 56+ $form->setWrapperLegend( wfMsg( 'video-addvideo-title' ) );
 57+ $form->setSubmitText( wfMsg( 'video-addvideo-button' ) );
 58+ $form->setSubmitCallback( array( $this, 'submit' ) );
 59+
 60+ if ( $this->getRequest()->getCheck( 'forReUpload' ) ) {
 61+ $form->addHiddenField( 'forReUpload', true );
5562 }
5663
57 - // Posted items
58 - $video_code = $wgRequest->getVal( 'wpVideo' );
59 - $title = str_replace( '#', '', $wgRequest->getVal( 'wpTitle' ) );
 64+ $form->show();
 65+ }
6066
61 - $pageTitle = wfMsg( 'video-addvideo-title' );
62 - if( $destination ) {
63 - $pageTitle = wfMsg( 'video-addvideo-dest', str_replace( '_', ' ', $destination ) );
 67+ protected function getUrlAndProvider( $value ) {
 68+ $url = $value;
 69+ if ( !Video::isURL( $url ) ) {
 70+ $url = Video::getURLfromEmbedCode( $value );
6471 }
6572
66 - $wgOut->setPageTitle( $pageTitle );
 73+ return array( $url, Video::getProviderByURL( $url ) );
 74+ }
6775
68 - if( $destination ) {
69 - $title = $destination;
 76+ public function validateVideoField( $value, $allData ) {
 77+ list( , $provider ) = $this->getUrlAndProvider( $value );
 78+
 79+ if ( $provider == 'unknown' ) {
 80+ return wfMsg( 'video-addvideo-invalidcode' );
7081 }
7182
72 - $output = '<div class="add-video-container">
73 - <form name="videoadd" action="" method="post">';
 83+ return true;
 84+ }
7485
75 - $output .= '<p class="addvideo-subtitle">' .
76 - wfMsgExt( 'video-addvideo-instructions', 'parse' ) . '</p>';
77 - $output .= '<table border="0" cellpadding="3" cellspacing="5">';
78 - $output .= '<tr>';
 86+ public function validateTitleField( $value, $allData ) {
 87+ $video = Video::newFromName( $value );
7988
80 - // If we're not adding a new version of a pre-existing video, allow the
81 - // user to supply the video's title, obviously...
82 - if( !$destination ) {
83 - $output .= '<td><label for="wpTitle">' .
84 - wfMsgHtml( 'video-addvideo-title-label' ) .
85 - '</label></td><td>';
 89+ if ( $video === null || !( $video instanceof Video ) ) {
 90+ return wfMsg( 'badtitle' );
 91+ }
8692
87 - $output .= Xml::element( 'input',
88 - array(
89 - 'type' => 'text',
90 - 'name' => 'wpTitle',
91 - 'size' => '30',
92 - 'value' => $wgRequest->getVal( 'wpTitle' ),
93 - )
94 - );
95 - $output .= '</td></tr>';
 93+ // TODO: Check to see if this is a new version
 94+ if ( $video->exists() && !$this->getRequest()->getCheck( 'forReUpload' ) ) {
 95+ return wfMsgHtml( 'video-addvideo-exists' );
9696 }
9797
98 - $watchChecked = $wgUser->getOption( 'watchdefault' )
99 - ? ' checked="checked"'
100 - : '';
 98+ $this->video = $video;
10199
102 - $output .= '<tr>
103 - <td valign="top">' . wfMsg( 'video-addvideo-embed-label' ) . '</td>
104 - <td><textarea rows="5" cols="65" name="wpVideo" id="wpVideo">' .
105 - $wgRequest->getVal( 'wpVideo' ) .
106 - '</textarea></td>
107 - </tr>
108 - <tr>
109 - <td></td>
110 - <td>
111 - <input type="checkbox" name="wpWatchthis" id="wpWatchthis"' . $watchChecked . ' value="true" />
112 - <label for="wpWatchthis">' . wfMsgHtml( 'watchthisupload' ) . '</label>
 100+ return true;
 101+ }
113102
114 - </td>
115 - </tr>';
 103+ public function submit( array $data ) {
 104+ list( $url, $provider ) = $this->getUrlAndProvider( $data['Video'] );
116105
117 - $output .= '<tr>
118 - <td></td>
119 - <td>';
120 - $output .= Xml::element( 'input',
121 - array(
122 - 'type' => 'button',
123 - 'value' => wfMsg( 'video-addvideo-button' ),
124 - 'onclick' => 'document.videoadd.submit();',
125 - )
126 - );
127 - $output .= '</td>
128 - </tr>
129 - </table>
130 - </form>
131 - </div>';
 106+ $this->video->addVideo( $url, $provider, false, $data['Watch'] );
132107
133 - if( $wgRequest->wasPosted() ) {
134 - $video = Video::newFromName( $title );
 108+ $this->getOutput()->redirect( $this->video->getTitle()->getFullURL() );
135109
136 - // Page title for Video has already been taken
137 - if( ( $video instanceof Video && $video->exists() ) && !$destination ) {
138 - $error = '<div class="video-error">' .
139 - wfMsgHtml( 'video-addvideo-exists' ) . '</div>';
140 - $wgOut->addHTML( $error );
141 - } else {
142 - // Get URL based on user input
143 - // It could be a straight URL to the page or the embed code
144 - if ( $video->isURL( $video_code ) ) {
145 - $url = $video_code;
146 - } else {
147 - $urlFromEmbed = $video->getURLfromEmbedCode( $video_code );
148 - if ( $video->isURL( $urlFromEmbed ) ) {
149 - $url = $urlFromEmbed;
150 - }
151 - }
152 - $provider = $video->getProviderByURL( $url );
153 - if( !$url || $provider == 'unknown' ) {
154 - $error = '<div class="video-error">' .
155 - wfMsg( 'video-addvideo-invalidcode' ) . '</div>';
156 - $wgOut->addHTML( $error );
157 - } else {
158 - $video->addVideo(
159 - $url, $provider, $categories,
160 - $wgRequest->getVal( 'wpWatchthis' )
161 - );
162 - $wgOut->redirect( $video->title->getFullURL() );
163 - }
164 - }
165 - }
166 - $wgOut->addHTML( $output );
 110+ return true;
167111 }
 112+
 113+ protected function getFormFields() {
 114+ $fields = array(
 115+ 'Title' => array(
 116+ 'type' => 'text',
 117+ 'label-message' => 'video-addvideo-title-label',
 118+ 'size' => '30',
 119+ 'required' => true,
 120+ 'validation-callback' => array( $this, 'validateTitleField' ),
 121+ ),
 122+ 'Video' => array(
 123+ 'type' => 'textarea',
 124+ 'label-message' => 'video-addvideo-embed-label',
 125+ 'rows' => '5',
 126+ 'cols' => '65',
 127+ 'required' => true,
 128+ 'validation-callback' => array( $this, 'validateVideoField' ),
 129+ ),
 130+ 'Watch' => array(
 131+ 'type' => 'check',
 132+ 'label-message' => 'watchthisupload',
 133+ 'default' => $this->getUser()->getOption( 'watchdefault' ),
 134+ ),
 135+ );
 136+
 137+ return $fields;
 138+ }
168139 }
\ No newline at end of file

Status & tagging log