r78530 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78529‎ | r78530 | r78531 >
Date:11:29, 17 December 2010
Author:questpc
Status:deferred
Tags:
Comment:
Use ResourceLoader for MW 1.17+ in QPoll extension
Modified paths:
  • /trunk/extensions/QPoll/qp_question.php (modified) (history)
  • /trunk/extensions/QPoll/qp_user.css (modified) (history)
  • /trunk/extensions/QPoll/qp_user.php (modified) (history)

Diff [purge]

Index: trunk/extensions/QPoll/qp_user.php
@@ -37,9 +37,7 @@
3838 die( "This file is part of the QPoll extension. It is not a valid entry point.\n" );
3939 }
4040
41 -define( 'QP_CSS_ERROR_COLOR1' , "LightYellow" );
4241 define( 'QP_CSS_ERROR_COLOR2', "#D700D7" );
43 -define( 'QP_CSS_ERROR_STYLE', 'background-color: ' . QP_CSS_ERROR_COLOR1 . ';' );
4442
4543 define( 'QP_ERROR_MISSED_TITLE', 1 );
4644 define( 'QP_ERROR_INVALID_ADDRESS', 2 );
@@ -144,9 +142,9 @@
145143 self::$ScriptPath = $wgScriptPath . '/extensions' . ( ( $top_dir == 'extensions' ) ? '' : '/' . $top_dir );
146144 $wgExtensionMessagesFiles['QPoll'] = self::$ExtDir . '/qp_i18n.php';
147145 $wgAutoloadClasses['PollResults'] = self::$ExtDir . '/qp_results.php';
148 - $wgAutoloadClasses['qp_Question'] = self::$ExtDir . '/qp_question.php';
 146+ $wgAutoloadClasses['qp_Question'] =
149147 $wgAutoloadClasses['qp_QuestionStats'] = self::$ExtDir . '/qp_question.php';
150 - $wgAutoloadClasses['qp_PollStore'] = self::$ExtDir . '/qp_pollstore.php';
 148+ $wgAutoloadClasses['qp_PollStore'] =
151149 $wgAutoloadClasses['qp_QuestionData'] = self::$ExtDir . '/qp_pollstore.php';
152150 $wgAutoloadClasses['qp_QueryPage'] = self::$ExtDir . '/qp_results.php';
153151 // TODO: Use the new technique for i18n of special page aliases
@@ -154,10 +152,11 @@
155153 // TODO: Use the new technique for i18n of magic words
156154 // instantiating fake instance for PHP < 5.2.3, which does not support 'Class::method' type of callbacks
157155 $qp_Setup = new qp_Setup;
158 - $wgHooks['LanguageGetMagic'][] = $qp_Setup;
159 - $wgHooks['MediaWikiPerformAction'][] = $qp_Setup;
160 - $wgHooks['ParserFirstCallInit'][] = $qp_Setup;
161 - $wgHooks['LoadAllMessages'][] = $qp_Setup;
 156+ $wgHooks['LanguageGetMagic'][] =
 157+ $wgHooks['MediaWikiPerformAction'][] =
 158+ $wgHooks['ParserFirstCallInit'][] =
 159+ $wgHooks['LoadAllMessages'][] =
 160+ $wgHooks['ResourceLoaderRegisterModules'][] = $qp_Setup;
162161 }
163162
164163 static function onLoadAllMessages() {
@@ -236,21 +235,24 @@
237236 * Register the extension with the WikiText parser.
238237 */
239238 static function onParserFirstCallInit() {
 239+ global $wgOut;
 240+ if ( class_exists( 'ResourceLoader' ) ) {
 241+ # MW 1.17+
 242+ // $wgOut->addModules( 'jquery' );
 243+ $wgOut->addModules( 'ext.qpoll' );
 244+ } else {
 245+ # MW < 1.17
 246+ global $wgJsMimeType, $wgContLang;
 247+ # Ouput the style and the script to the header once for all.
 248+ $head = '<script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/qp_user.js"></script>' . "\n";
 249+ $wgOut->addScript( $head );
 250+ $wgOut->addExtensionStyle( self::$ScriptPath . '/qp_user.css' );
 251+ if ( $wgContLang->isRTL() ) {
 252+ $wgOut->addExtensionStyle( self::$ScriptPath . '/qp_user_rtl.css' );
 253+ }
 254+ }
240255 global $wgParser;
241 - global $wgExtensionCredits;
242256 global $wgQPollFunctionsHook;
243 - global $wgContLang;
244 - global $wgJsMimeType, $wgOut;
245 - # Ouput the style and the script to the header once for all.
246 - $head = '<style type="text/css">' . "\n";
247 - $head .= '.qpoll .fatalerror { border: 1px solid gray; padding: 4px; ' . QP_CSS_ERROR_STYLE . ' }' . "\n";
248 - $head .= '</style>' . "\n";
249 - $head .= '<script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/qp_user.js"></script>' . "\n";
250 - $wgOut->addScript( $head );
251 - $wgOut->addExtensionStyle( self::$ScriptPath . '/qp_user.css' );
252 - if ( $wgContLang->isRTL() ) {
253 - $wgOut->addExtensionStyle( self::$ScriptPath . '/qp_user_rtl.css' );
254 - }
255257 # setup tag hook
256258 $wgParser->setHook( 'qpoll', array( 'qp_Setup', 'renderPoll' ) );
257259 $wgQPollFunctionsHook = new qp_FunctionsHook();
@@ -260,6 +262,28 @@
261263 }
262264
263265 /**
 266+ * MW 1.17+ ResourceLoader module hook (JS,CSS)
 267+ */
 268+ static function onResourceLoaderRegisterModules( $resourceLoader ) {
 269+ global $wgExtensionAssetsPath;
 270+ $localpath = dirname( __FILE__ );
 271+ $remotepath = "$wgExtensionAssetsPath/QPoll";
 272+ $resourceLoader->register(
 273+ array(
 274+ 'ext.qpoll' => new ResourceLoaderFileModule(
 275+ array(
 276+ 'scripts' => 'qp_user.js',
 277+ 'styles' => 'qp_user.css'
 278+ ),
 279+ $localpath,
 280+ $remotepath
 281+ )
 282+ )
 283+ );
 284+ return true;
 285+ }
 286+
 287+ /**
264288 * Call the poll parser on an input text.
265289 *
266290 * @param $input Text between <qpoll> and </qpoll> tags, in QPoll syntax.
@@ -1121,6 +1145,19 @@
11221146 return $tag_open . $tag_val . $tag_close;
11231147 }
11241148
 1149+ /**
 1150+ * add one or more of CSS class names to tag class attribute
 1151+ */
 1152+ static function addClass( &$tag, $className ) {
 1153+ if ( !isset( $tag['class'] ) ) {
 1154+ $tag['class'] = $className;
 1155+ return;
 1156+ }
 1157+ if ( array_search( $className, explode( ' ', $tag['class'] ) ) === false ) {
 1158+ $tag['class'] .= " $className";
 1159+ }
 1160+ }
 1161+
11251162 # creates one "htmlobject" row of the table
11261163 # elements of $row can be either a string/number value of cell or an array( "count"=>colspannum, "attribute"=>value, 0=>html_inside_tag )
11271164 # attribute maps can be like this: ("name"=>0, "count"=>colspan" )
Index: trunk/extensions/QPoll/qp_user.css
@@ -1,3 +1,5 @@
 2+.qpoll .error { background-color: LightYellow; }
 3+.qpoll .fatalerror { border: 1px solid gray; padding: 4px; background-color: LightYellow; }
24 .qpoll .settings input.numerical { width:2em; }
35 .qpoll .header {font-weight: bold;}
46 *>.qpoll .header .questionId {text-indent: -1.5em; } /* *> prevent ie6 to interprate it. */
Index: trunk/extensions/QPoll/qp_question.php
@@ -180,7 +180,7 @@
181181 $spanState->cellsLeft = $this->mCategorySpans[ $spanState->id ]['count'];
182182 if ( $spanState->cellsLeft < 2 ) {
183183 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_too_few_spans' ), 'error' ) . $text;
184 - $row[ $catId ][ 'style' ] = QP_CSS_ERROR_STYLE;
 184+ QP_Renderer::addClass( $row[ $catId ], 'error' );
185185 }
186186 $spanState->isDrawing = $spanState->cellsLeft != 1 && $spanState->cellsLeft != count( $this->mCategories );
187187 # hightlight only spans of count != 1 and count != count(categories)
@@ -301,12 +301,12 @@
302302 if ( $this->categoriesStyle != '' ) {
303303 qp_Renderer::applyAttrsToRow( $spansRow, array( 'style'=>$this->categoriesStyle ) );
304304 }
305 - $this->addRow( $spansRow, array( 'class'=>'spans'), 'th', array( 'count'=>$this->spanType, 'name'=>0 ) );
 305+ $this->addRow( $spansRow, array( 'class'=>'spans' ), 'th', array( 'count'=>$this->spanType, 'name'=>0 ) );
306306 }
307307 if ( $this->categoriesStyle != '' ) {
308308 qp_Renderer::applyAttrsToRow( $catRow, array( 'style'=>$this->categoriesStyle ) );
309309 }
310 - $this->addRow( $catRow, array( 'class'=>'categories'), 'th', array( 'name'=>0 ) );
 310+ $this->addRow( $catRow, array( 'class'=>'categories' ), 'th', array( 'name'=>0 ) );
311311 foreach ( $this->mProposalText as $proposalId => $text ) {
312312 $row = Array();
313313 $rawClass = 'proposal';
@@ -320,7 +320,7 @@
321321 $this->renderSpan( $name, $catDesc, $text, $rawClass, $spanState );
322322 break;
323323 }
324 - $row[ $catId ][ 'class' ] = $spanState->className;
 324+ QP_Renderer::addClass( $row[ $catId ], $spanState->className );
325325 if ( $this->showResults['type'] != 0 ) {
326326 # there ars some stat in row (not necessarily all cells, because size of question table changes dynamically)
327327 $row[ $catId ][ 0 ] = $this->{'addShowResults' . $this->showResults['type']}( $proposalId, $catId );
@@ -576,12 +576,12 @@
577577 if ( $this->categoriesStyle != '' ) {
578578 qp_Renderer::applyAttrsToRow( $spansRow, array( 'style'=>$this->categoriesStyle ) );
579579 }
580 - $this->addRow( $spansRow, array( 'class'=>'spans'), 'th', array( 'count'=>$this->spanType, 'name'=>0 ) );
 580+ $this->addRow( $spansRow, array( 'class'=>'spans' ), 'th', array( 'count'=>$this->spanType, 'name'=>0 ) );
581581 }
582582 if ( $this->categoriesStyle != '' ) {
583583 qp_Renderer::applyAttrsToRow( $catRow, array( 'style'=>$this->categoriesStyle ) );
584584 }
585 - $this->addRow( $catRow, array( 'class'=>'categories'), 'th', array( 'name'=>0 ) );
 585+ $this->addRow( $catRow, array( 'class'=>'categories' ), 'th', array( 'name'=>0 ) );
586586 }
587587
588588 function singleChoiceParseBody() {
@@ -633,7 +633,7 @@
634634 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_unanswered_span' ), 'NA' ) . $text;
635635 # highlight current span to indicate an error
636636 for ( $i = $catId, $j = $this->mCategorySpans[ $spanState->id ]['count']; $j > 0; $i--, $j-- ) {
637 - $row[$i][ 'style' ] = QP_CSS_ERROR_STYLE;
 637+ QP_Renderer::addClass( $row[$i], 'error' );
638638 }
639639 $rawClass = 'proposalerror';
640640 }
@@ -653,7 +653,7 @@
654654 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_non_unique_choice' ), 'NA' ) . $text;
655655 $rawClass = 'proposalerror';
656656 unset( $inp[ 'checked' ] );
657 - $row[ $catId ][ 'style' ] = QP_CSS_ERROR_STYLE;
 657+ QP_Renderer::addClass( $row[ $catId ], 'error' );
658658 }
659659 } else {
660660 $spanState->wasChecked = true;
@@ -664,7 +664,7 @@
665665 $this->mProposalCategoryId[ $proposalId ][] = $catId;
666666 $this->mProposalCategoryText[ $proposalId ][] = '';
667667 }
668 - $row[ $catId ][ 'class' ] = $spanState->className;
 668+ QP_Renderer::addClass( $row[ $catId ], $spanState->className );
669669 if ( $this->mSubType == 'unique' ) {
670670 # unique (question,category,proposal) "coordinate" for javascript
671671 $inp[ 'id' ] = 'uq' . $this->mQuestionId . 'c' . $catId . 'p' . $proposalId;
@@ -673,7 +673,7 @@
674674 # if there was no previous errors, hightlight the whole row
675675 if ( $this->getState() == '' ) {
676676 foreach( $row as &$cell ) {
677 - $cell[ 'style' ] = QP_CSS_ERROR_STYLE;
 677+ QP_Renderer::addClass( $cell, 'error' );
678678 }
679679 }
680680 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_unique' ), 'error' ) . $text;
@@ -695,7 +695,7 @@
696696 if( trim( $text ) == '' ) {
697697 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_proposal_text_empty' ), 'error' );
698698 foreach( $row as &$cell ) {
699 - $cell[ 'style' ] = QP_CSS_ERROR_STYLE;
 699+ QP_Renderer::addClass( $cell, 'error' );
700700 }
701701 $rawClass = 'proposalerror';
702702 }
@@ -704,7 +704,7 @@
705705 # if there was no previous errors, hightlight the whole row
706706 if ( $this->getState() == '' ) {
707707 foreach( $row as &$cell ) {
708 - $cell[ 'style' ] = QP_CSS_ERROR_STYLE;
 708+ QP_Renderer::addClass( $cell, 'error' );
709709 }
710710 }
711711 $text = $this->bodyErrorMessage( wfMsg( 'qp_error_no_answer' ), 'NA' ) . $text;
@@ -851,7 +851,7 @@
852852 } catch( Exception $e ) {
853853 if ( $e->getMessage() == 'qp_error' ) {
854854 foreach( $row as &$cell ) {
855 - $cell[ 'style' ] = QP_CSS_ERROR_STYLE;
 855+ QP_Renderer::addClass( $cell, 'error' );
856856 }
857857 $rawClass = 'proposalerror';
858858 } else {

Status & tagging log