r99359 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99358‎ | r99359 | r99360 >
Date:16:52, 9 October 2011
Author:johnduhart
Status:ok
Tags:
Comment:
Cleanup and changes to use Contexts where possible
Modified paths:
  • /trunk/extensions/Video/SpecialAddVideo.php (modified) (history)
  • /trunk/extensions/Video/SpecialNewVideos.php (modified) (history)
  • /trunk/extensions/Video/VideoClass.php (modified) (history)
  • /trunk/extensions/Video/VideoGallery.php (modified) (history)
  • /trunk/extensions/Video/VideoGalleryPopulate.php (modified) (history)
  • /trunk/extensions/Video/VideoHooks.php (modified) (history)
  • /trunk/extensions/Video/VideoPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Video/VideoClass.php
@@ -69,17 +69,24 @@
7070 var $dataLoaded;
7171
7272 /**
 73+ * @var IContextSource
 74+ */
 75+ protected $context;
 76+
 77+ /**
7378 * Constructor -- create a new Video object from the given Title and set
7479 * some member variables
7580 *
7681 * @param $title Object: Title object associated with the Video
 82+ * @param $context IContextSource nearest context object
7783 */
78 - public function __construct( $title ) {
 84+ public function __construct( $title, IContextSource $context ) {
7985 if( !is_object( $title ) ) {
8086 throw new MWException( 'Video constructor given bogus title.' );
8187 }
8288 $this->title =& $title;
8389 $this->name = $title->getDBkey();
 90+ $this->context = $context;
8491 $this->height = 400;
8592 $this->width = 400;
8693 $this->ratio = 1;
@@ -90,11 +97,13 @@
9198 * Create a Video object from a video name
9299 *
93100 * @param $name Mixed: name of the video, used to create a title object using Title::makeTitleSafe
 101+ * @param $context IContextSource nearest context object
 102+ * @return Video|null returns a Video object on success, null if the title is invalid
94103 */
95 - public static function newFromName( $name ) {
 104+ public static function newFromName( $name, IContextSource $context ) {
96105 $title = Title::makeTitleSafe( NS_VIDEO, $name );
97106 if ( is_object( $title ) ) {
98 - return new Video( $title );
 107+ return new Video( $title, $context );
99108 } else {
100109 return null;
101110 }
@@ -108,9 +117,8 @@
109118 * @param $categories String: pipe-separated list of categories
110119 * @param $watch Boolean: add the new video page to the user's watchlist?
111120 */
112 - public function addVideo( $url, $type, $categories, $watch = '' ) {
113 - global $wgUser, $wgContLang;
114 -
 121+ public function addVideo( $url, $type, $categories, $watch = false ) {
 122+ $user = $this->context->getUser();
115123 $dbw = wfGetDB( DB_MASTER );
116124
117125 $now = $dbw->timestamp();
@@ -128,8 +136,8 @@
129137 'video_name' => $this->getName(),
130138 'video_url' => $url,
131139 'video_type' => $type,
132 - 'video_user_id' => $wgUser->getID(),
133 - 'video_user_name' => $wgUser->getName(),
 140+ 'video_user_id' => $user->getID(),
 141+ 'video_user_name' => $user->getName(),
134142 'video_timestamp' => $now
135143 ),
136144 __METHOD__,
@@ -172,8 +180,8 @@
173181 array( /* SET */
174182 'video_url'=> $url,
175183 'video_type' => $type,
176 - 'video_user_id' => $wgUser->getID(),
177 - 'video_user_name' => $wgUser->getName(),
 184+ 'video_user_id' => $user->getID(),
 185+ 'video_user_name' => $user->getName(),
178186 'video_timestamp' => $now
179187 ),
180188 array( /* WHERE */
@@ -185,7 +193,7 @@
186194
187195 $descTitle = $this->getTitle();
188196 $article = new Article( $descTitle );
189 - $watch = $watch || $wgUser->isWatched( $descTitle );
 197+ $watch = $watch || $user->isWatched( $descTitle );
190198
191199 // Get the localized category name
192200 $videoCategoryName = wfMsgForContent( 'video-category-name' );
@@ -202,7 +210,7 @@
203211 foreach( $categories_array as $ctg ) {
204212 $ctg = trim( $ctg );
205213 if( $ctg ) {
206 - $catName = $wgContLang->getNsText( NS_CATEGORY );
 214+ $catName = $this->context->getLang()->getNsText( NS_CATEGORY );
207215 $tag = "[[{$catName}:{$ctg}]]";
208216 if( strpos( $categoryWikiText, $tag ) === false ) {
209217 $categoryWikiText .= "\n{$tag}";
@@ -222,7 +230,7 @@
223231 }
224232
225233 if( $watch ) {
226 - $wgUser->addWatch( $descTitle );
 234+ $user->addWatch( $descTitle );
227235 }
228236
229237 // Add the log entry
Index: trunk/extensions/Video/VideoGalleryPopulate.php
@@ -70,7 +70,7 @@
7171 $gallery->setShowFilename( true );
7272
7373 foreach ( $res as $row ) {
74 - $video = Video::newFromName( $row->page_title );
 74+ $video = Video::newFromName( $row->page_title, RequestContext::getMain() );
7575 $gallery->add( $video );
7676 }
7777
Index: trunk/extensions/Video/VideoPage.php
@@ -16,31 +16,30 @@
1717 * Called on every video page view.
1818 */
1919 public function view() {
20 - global $wgOut, $wgUser, $wgRequest;
 20+ $this->video = new Video( $this->getTitle(), $this->getContext() );
 21+ $out = $this->getContext()->getOutput();
2122
22 - $this->video = new Video( $this->getTitle() );
23 -
2423 $videoLinksHTML = '<br />' . Xml::element( 'h2',
2524 array( 'id' => 'filelinks' ), wfMsg( 'video-links' ) ) . "\n";
26 - $sk = $wgUser->getSkin();
 25+ $sk = $this->getContext()->getSkin();
2726
2827 // No need to display noarticletext, we use our own message
2928 if ( $this->getID() ) {
3029 parent::view();
3130 } else {
3231 // Just need to set the right headers
33 - $wgOut->setArticleFlag( true );
34 - $wgOut->setRobotPolicy( 'index,follow' );
35 - $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
 32+ $out->setArticleFlag( true );
 33+ $out->setRobotPolicy( 'index,follow' );
 34+ $out->setPageTitle( $this->mTitle->getPrefixedText() );
3635 }
3736
3837 if( $this->video->exists() ) {
3938 // Display flash video
40 - $wgOut->addHTML( $this->video->getEmbedCode() );
 39+ $out->addHTML( $this->video->getEmbedCode() );
4140
4241 // Force embed this code to have width of 300
4342 $this->video->setWidth( 300 );
44 - $wgOut->addHTML( $this->getEmbedThisTag() );
 43+ $out->addHTML( $this->getEmbedThisTag() );
4544
4645 $this->videoHistory();
4746
@@ -55,7 +54,7 @@
5655 array(),
5756 array( 'wpTitle' => $this->video->getName() )
5857 );
59 - $wgOut->addHTML( wfMsgWikiHtml( 'video-novideo', $link ) );
 58+ $out->addHTML( wfMsgWikiHtml( 'video-novideo', $link ) );
6059
6160 //$wgOut->addHTML( $videoLinksHTML );
6261 //$this->videoLinks();
@@ -293,7 +292,7 @@
294293 //$oldver = wfTimestampNow() . "!{$name}";
295294
296295 // Record upload and update metadata cache
297 - $video = Video::newFromName( $name );
 296+ $video = Video::newFromName( $name, $this->getContext() );
298297 $video->addVideo( $url, $type, '' );
299298
300299 $wgOut->setPageTitle( wfMsgHtml( 'actioncomplete' ) );
@@ -444,7 +443,7 @@
445444 * Add a page in the video namespace
446445 */
447446 function addVideo( $title, $sortkey, $pageLength ) {
448 - $video = new Video( $title );
 447+ $video = new Video( $title, $this->context );
449448 if( $this->flip ) {
450449 $this->videogallery->insert( $video );
451450 } else {
Index: trunk/extensions/Video/SpecialNewVideos.php
@@ -22,19 +22,23 @@
2323 * @param $par Mixed: parameter passed to the page or null
2424 */
2525 public function execute( $par ) {
26 - global $wgUser, $wgOut, $wgLang, $wgRequest, $wgGroupPermissions;
 26+ global $wgGroupPermissions;
2727
28 - $wgOut->setPageTitle( wfMsgHtml( 'newvideos' ) );
 28+ $out = $this->getOutput();
 29+ $request = $this->getRequest();
 30+ $lang = $this->getLang();
 31+
 32+ $out->setPageTitle( wfMsgHtml( 'newvideos' ) );
2933
30 - $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
 34+ $wpIlMatch = $request->getText( 'wpIlMatch' );
3135 $dbr = wfGetDB( DB_SLAVE );
32 - $sk = $wgUser->getSkin();
 36+ $sk = $this->getSkin();
3337 $shownav = !$this->including();
34 - $hidebots = $wgRequest->getBool( 'hidebots', 1 );
 38+ $hidebots = $request->getBool( 'hidebots', 1 );
3539
3640 $hidebotsql = '';
3741 if( $hidebots ) {
38 - /**
 42+ /*
3943 * Make a list of group names which have the 'bot' flag
4044 * set.
4145 */
@@ -49,7 +53,7 @@
5054 if( $botconds ) {
5155 $isbotmember = $dbr->makeList( $botconds, LIST_OR );
5256
53 - /**
 57+ /*
5458 * This join, in conjunction with WHERE ug_group
5559 * IS NULL, returns only those rows from IMAGE
5660 * where the uploading user is not a member of
@@ -102,10 +106,10 @@
103107 }
104108
105109 $invertSort = false;
106 - if( $until = $wgRequest->getVal( 'until' ) ) {
 110+ if( $until = $request->getVal( 'until' ) ) {
107111 $where[] = 'video_timestamp < ' . $dbr->timestamp( $until );
108112 }
109 - if( $from = $wgRequest->getVal( 'from' ) ) {
 113+ if( $from = $request->getVal( 'from' ) ) {
110114 $where[] = 'video_timestamp >= ' . $dbr->timestamp( $from );
111115 $invertSort = true;
112116 }
@@ -123,9 +127,7 @@
124128 $sql.= ' LIMIT ' . ( $limit + 1 );
125129 $res = $dbr->query( $sql, __METHOD__ );
126130
127 - /**
128 - * We have to flip things around to get the last N after a certain date
129 - */
 131+ // We have to flip things around to get the last N after a certain date
130132 $videos = array();
131133 foreach( $res as $s ) {
132134 if( $invertSort ) {
@@ -150,13 +152,13 @@
151153 $ut = $s->video_user_name;
152154
153155 $nt = Title::newFromText( $name, NS_VIDEO );
154 - $vid = new Video( $nt );
 156+ $vid = new Video( $nt, $this->getContext() );
155157 $ul = $sk->makeLinkObj( Title::makeTitle( NS_USER, $ut ), $ut );
156158
157159 $gallery->add(
158160 $vid,
159161 "$ul<br />\n<i>" .
160 - $wgLang->timeanddate( $s->video_timestamp, true ) .
 162+ $lang->timeanddate( $s->video_timestamp, true ) .
161163 "</i><br />\n"
162164 );
163165
@@ -168,17 +170,17 @@
169171 }
170172
171173 $bydate = wfMsg( 'bydate' );
172 - $lt = $wgLang->formatNum( min( $shownVideos, $limit ) );
 174+ $lt = $lang->formatNum( min( $shownVideos, $limit ) );
173175 if( $shownav ) {
174176 $text = wfMsgExt( 'imagelisttext', 'parse', $lt, $bydate );
175 - $wgOut->addHTML( $text . "\n" );
 177+ $out->addHTML( $text . "\n" );
176178 }
177179
178180 $sub = wfMsg( 'ilsubmit' );
179181 $titleObj = SpecialPage::getTitleFor( 'NewVideos' );
180182 $action = $titleObj->escapeLocalURL( $hidebots ? '' : 'hidebots=0' );
181183 if( $shownav ) {
182 - $wgOut->addHTML(
 184+ $out->addHTML(
183185 "<form id=\"imagesearch\" method=\"post\" action=\"{$action}\">" .
184186 Xml::input( 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
185187 Xml::submitButton( $sub, array( 'name' => 'wpIlSubmit' ) ) .
@@ -186,9 +188,7 @@
187189 );
188190 }
189191
190 - /**
191 - * Paging controls...
192 - */
 192+ // Paging controls...
193193
194194 # If we change bot visibility, this needs to be carried along.
195195 if( !$hidebots ) {
@@ -197,8 +197,8 @@
198198 $botpar = array();
199199 }
200200 $now = wfTimestampNow();
201 - $date = $wgLang->date( $now, true );
202 - $time = $wgLang->time( $now, true );
 201+ $date = $lang->date( $now, true );
 202+ $time = $lang->time( $now, true );
203203 $query = array_merge(
204204 array( 'from' => $now ),
205205 $botpar,
@@ -227,7 +227,7 @@
228228 );
229229
230230 $opts = array( 'parsemag', 'escapenoentities' );
231 - $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
 231+ $prevLink = wfMsgExt( 'pager-newer-n', $opts, $lang->formatNum( $limit ) );
232232 if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
233233 $query = array_merge(
234234 array( 'from' => $firstTimestamp ),
@@ -242,7 +242,7 @@
243243 );
244244 }
245245
246 - $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
 246+ $nextLink = wfMsgExt( 'pager-older-n', $opts, $lang->formatNum( $limit ) );
247247 if( $shownVideos > $limit && $lastTimestamp ) {
248248 $query = array_merge(
249249 array( 'until' => $lastTimestamp ),
@@ -263,16 +263,16 @@
264264 '</p>';
265265
266266 if( $shownav ) {
267 - $wgOut->addHTML( $prevnext );
 267+ $out->addHTML( $prevnext );
268268 }
269269
270270 if( count( $videos ) ) {
271 - $wgOut->addHTML( $gallery->toHTML() );
 271+ $out->addHTML( $gallery->toHTML() );
272272 if( $shownav ) {
273 - $wgOut->addHTML( $prevnext );
 273+ $out->addHTML( $prevnext );
274274 }
275275 } else {
276 - $wgOut->addWikiMsg( 'video-no-videos' );
 276+ $out->addWikiMsg( 'video-no-videos' );
277277 }
278278 }
279279 }
\ No newline at end of file
Index: trunk/extensions/Video/VideoHooks.php
@@ -38,7 +38,7 @@
3939 $name = $matches[2];
4040 $params = explode( '|', $name );
4141 $video_name = $params[0];
42 - $video = Video::newFromName( $video_name );
 42+ $video = Video::newFromName( $video_name, RequestContext::getMain() );
4343 $x = 1;
4444
4545 foreach( $params as $param ) {
@@ -84,7 +84,7 @@
8585 if ( $title->getNamespace() == NS_VIDEO ) {
8686 if( $wgRequest->getVal( 'action' ) == 'edit' ) {
8787 $addTitle = SpecialPage::getTitleFor( 'AddVideo' );
88 - $video = Video::newFromName( $title->getText() );
 88+ $video = Video::newFromName( $title->getText(), $article->getContext() );
8989 if( !$video->exists() ) {
9090 global $wgOut;
9191 $wgOut->redirect(
@@ -146,7 +146,7 @@
147147 }
148148
149149 $output = '';
150 - $video = Video::newFromName( $video_name );
 150+ $video = Video::newFromName( $video_name, RequestContext::getMain() );
151151 if( $video->exists() ) {
152152 $video->setWidth( $width );
153153 $video->setHeight( $height );
@@ -162,7 +162,7 @@
163163 /**
164164 * Injects Video Gallery into Category pages
165165 *
166 - * @param $cat Object: CategoryPage object
 166+ * @param $cat CategoryPage object
167167 * @return Boolean: false
168168 */
169169 public static function categoryPageWithVideo( &$cat ) {
@@ -176,7 +176,7 @@
177177 // here to prevent an E_NOTICE about an undefined variable...
178178 $until = $wgRequest->getVal( 'until' );
179179
180 - $viewer = new CategoryWithVideoViewer( $cat->mTitle, $from, $until );
 180+ $viewer = new CategoryWithVideoViewer( $cat->mTitle, $cat->getContext(), $from, $until );
181181 $wgOut->addHTML( $viewer->getHTML() );
182182 }
183183
@@ -187,7 +187,7 @@
188188 * Called on video deletion; this is the main logic for deleting videos.
189189 * There is no logic related to video deletion on the VideoPage class.
190190 *
191 - * @param $articleObj Object: instance of Article or its subclass
 191+ * @param $articleObj Article: instance of Article or its subclass
192192 * @param $user Object: current User object ($wgUser)
193193 * @param $reason String: reason for the deletion [unused]
194194 * @param $error String: error message, if any [unused]
@@ -197,7 +197,7 @@
198198 if ( $articleObj->getTitle()->getNamespace() == NS_VIDEO ) {
199199 global $wgRequest;
200200
201 - $videoObj = new Video( $articleObj->getTitle() );
 201+ $videoObj = new Video( $articleObj->getTitle(), $articleObj->getContext() );
202202 $videoName = $videoObj->getName();
203203 $oldVideo = $wgRequest->getVal( 'wpOldVideo', false );
204204 $where = array(
Index: trunk/extensions/Video/SpecialAddVideo.php
@@ -1,4 +1,10 @@
22 <?php
 3+/**
 4+ * Special:AddVideo - special page for adding Videos
 5+ *
 6+ * @ingroup extensions
 7+ * @file
 8+ */
39
410 class AddVideo extends SpecialPage {
511
@@ -20,15 +26,17 @@
2127 * Show the special page
2228 *
2329 * @param $par Mixed: parameter passed to the page or null
 30+ * @return bool|null
2431 */
2532 public function execute( $par ) {
26 - global $wgRequest, $wgOut, $wgUser, $wgScriptPath;
 33+ global $wgExtensionAssetsPath;
2734
 35+ $out = $this->getRequest();
2836 // Add CSS
2937 if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
30 - $wgOut->addModuleStyles( 'ext.video' );
 38+ $out->addModuleStyles( 'ext.video' );
3139 } else {
32 - $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/Video/Video.css' );
 40+ $out->addExtensionStyle( $wgExtensionAssetsPath . '/Video/Video.css' );
3341 }
3442
3543 // If the user doesn't have the required 'addvideo' permission, display an error
@@ -43,8 +51,8 @@
4452 }
4553
4654 // If user is blocked, s/he doesn't need to access this page
47 - if( $wgUser->isBlocked() ) {
48 - $wgOut->blockedPage( false );
 55+ if( $this->getUser()->isBlocked() ) {
 56+ throw new UserBlockedError( $this->getUser()->mBlock );
4957 return false;
5058 }
5159
@@ -63,6 +71,12 @@
6472 $form->show();
6573 }
6674
 75+ /**
 76+ * Extracts the URL and provider type from a raw string
 77+ *
 78+ * @param string $value Value form the Video input
 79+ * @return array Element 0 is the URL, 1 is the provider
 80+ */
6781 protected function getUrlAndProvider( $value ) {
6882 $url = $value;
6983 if ( !Video::isURL( $url ) ) {
@@ -72,6 +86,16 @@
7387 return array( $url, Video::getProviderByURL( $url ) );
7488 }
7589
 90+ /**
 91+ * Custom validator for the Video field
 92+ *
 93+ * Checks to see if the string given is a valid URL and corresponds
 94+ * to a supported provider.
 95+ *
 96+ * @param $value Array
 97+ * @param $allData Array
 98+ * @return bool|String
 99+ */
76100 public function validateVideoField( $value, $allData ) {
77101 list( , $provider ) = $this->getUrlAndProvider( $value );
78102
@@ -82,8 +106,18 @@
83107 return true;
84108 }
85109
 110+ /**
 111+ * Custom validator for the Title field
 112+ *
 113+ * Just checks that it's a valid title name and that it doesn't already
 114+ * exist (unless it's an overwrite)
 115+ *
 116+ * @param $value Array
 117+ * @param $allData Array
 118+ * @return bool|String
 119+ */
86120 public function validateTitleField( $value, $allData ) {
87 - $video = Video::newFromName( $value );
 121+ $video = Video::newFromName( $value, $this->getContext() );
88122
89123 if ( $video === null || !( $video instanceof Video ) ) {
90124 return wfMsg( 'badtitle' );
@@ -99,6 +133,12 @@
100134 return true;
101135 }
102136
 137+ /**
 138+ * Actually inserts the Video into the DB if validation passes
 139+ *
 140+ * @param $data Array
 141+ * @return bool
 142+ */
103143 public function submit( array $data ) {
104144 list( $url, $provider ) = $this->getUrlAndProvider( $data['Video'] );
105145
@@ -109,6 +149,11 @@
110150 return true;
111151 }
112152
 153+ /**
 154+ * Fields for HTMLForm
 155+ *
 156+ * @return array
 157+ */
113158 protected function getFormFields() {
114159 $fields = array(
115160 'Title' => array(
Index: trunk/extensions/Video/VideoGallery.php
@@ -70,7 +70,8 @@
7171 $html = $pout->getText();
7272 */
7373
74 - $vg->add( new Video( $nt ), $html );
 74+ // Gah, there should be a better way to get context here
 75+ $vg->add( new Video( $nt, RequestContext::getMain() ), $html );
7576
7677 }
7778 return $vg->toHTML();

Status & tagging log