r95509 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95508‎ | r95509 | r95510 >
Date:20:04, 25 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added survey tag
Modified paths:
  • /trunk/extensions/Survey/Survey.hooks.php (modified) (history)
  • /trunk/extensions/Survey/Survey.i18n.php (modified) (history)
  • /trunk/extensions/Survey/Survey.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyCompat.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (added) (history)
  • /trunk/extensions/Survey/specials/SpecialTakeSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.php
@@ -59,6 +59,7 @@
6060 $wgAutoloadClasses['SurveyDBClass'] = dirname( __FILE__ ) . '/includes/SurveyDBClass.php';
6161 $wgAutoloadClasses['SurveyQuestion'] = dirname( __FILE__ ) . '/includes/SurveyQuestion.php';
6262 $wgAutoloadClasses['SurveySubmission'] = dirname( __FILE__ ) . '/includes/SurveySubmission.php';
 63+$wgAutoloadClasses['SurveyTag'] = dirname( __FILE__ ) . '/includes/SurveyTag.php';
6364
6465 $wgAutoloadClasses['SpecialSurvey'] = dirname( __FILE__ ) . '/specials/SpecialSurvey.php';
6566 $wgAutoloadClasses['SpecialSurveyPage'] = dirname( __FILE__ ) . '/specials/SpecialSurveyPage.php';
@@ -84,6 +85,7 @@
8586
8687 $wgHooks['LoadExtensionSchemaUpdates'][] = 'SurveyHooks::onSchemaUpdate';
8788 $wgHooks['UnitTestsList'][] = 'SurveyHooks::registerUnitTests';
 89+$wgHooks['ParserFirstCallInit'][] = 'SurveyHooks::onParserFirstCallInit';
8890
8991 $wgAvailableRights[] = 'surveyadmin';
9092 $wgAvailableRights[] = 'surveysubmit';
@@ -133,7 +135,7 @@
134136 'messages' => $egSurveyMessages['ext.survey.special']
135137 );
136138
137 -$wgResourceModules['jquery.survey'] = $moduleTemplate + array(
 139+$wgResourceModules['ext.survey.jquery'] = $moduleTemplate + array(
138140 'scripts' => array(
139141 'jquery.survey.js'
140142 ),
Index: trunk/extensions/Survey/Survey.i18n.php
@@ -20,23 +20,26 @@
2121 $messages['en'] = array(
2222 'survey-desc' => 'Survey tool for MediaWiki',
2323
 24+ // Rights
2425 'right-surveyadmin' => 'Manage surveys',
2526 'right-surveysubmit' => 'Participate in surveys',
2627
 28+ // Special page names
2729 'special-survey' => 'Survey admin',
2830 'special-surveys' => 'Surveys admin',
2931 'special-surveystats' => 'Survey statistics',
3032 'special-takesurvey' => 'Take survey',
3133
 34+ // API errors
3235 'survey-err-id-xor-name' => 'You need to provide either the id or the name of the survey to submit',
3336 'survey-err-survey-name-unknown' => 'There is no survey with the name "$1"',
3437 'survey-err-duplicate-name' => 'There already is a survey with name "$1"',
3538
 39+ // Special:Surveys
3640 'surveys-special-addnew' => 'Add a new survey',
3741 'surveys-special-namedoc' => 'Enter the name for the new survey.',
3842 'surveys-special-newname' => 'New survey name:',
3943 'surveys-special-add' => 'Add survey',
40 -
4144 'surveys-special-existing' => 'Existing surveys',
4245 'surveys-special-name' => 'Name',
4346 'surveys-special-status' => 'Status',
@@ -46,4 +49,7 @@
4750 'surveys-special-disabled' => 'Disabled',
4851 'surveys-special-confirm-delete' => 'Are you sure you want to delete this survey?',
4952 'surveys-special-delete-failed' => 'Failed to delete survey.',
 53+
 54+ // Special:TakeSurvey
 55+ 'surveys-takesurvey-loading' => 'Loading survey.'
5056 );
Index: trunk/extensions/Survey/specials/SpecialTakeSurvey.php
@@ -32,6 +32,13 @@
3333 public function execute( $subPage ) {
3434 parent::execute( $subPage );
3535
 36+ $this->getOutput()->addWikiText( Xml::element(
 37+ 'survey',
 38+ array(
 39+ 'name' => $subPage
 40+ ),
 41+ wfMsg( 'surveys-takesurvey-loading' )
 42+ ) );
3643 }
3744
3845 }
Index: trunk/extensions/Survey/Survey.hooks.php
@@ -14,6 +14,35 @@
1515 final class SurveyHooks {
1616
1717 /**
 18+ * Register the survey tag extension when the parser initializes.
 19+ *
 20+ * @since 0.1
 21+ *
 22+ * @param Parser $parser
 23+ *
 24+ * @return true
 25+ */
 26+ public static function onParserFirstCallInit( Parser &$parser ) {
 27+ $parser->setHook( 'survey', __CLASS__ . '::onSurveyRender' );
 28+ return true;
 29+ }
 30+
 31+ /**
 32+ * Render the survey tag.
 33+ *
 34+ * @since 0.1
 35+ *
 36+ * @param mixed $input
 37+ * @param array $args
 38+ * @param Parser $parser
 39+ * @param PPFrame $frame
 40+ */
 41+ public static function onSurveyRender( $input, array $args, Parser $parser, PPFrame $frame ) {
 42+ $tag = new SurveyTag( $args, $input );
 43+ return $tag->render( $parser );
 44+ }
 45+
 46+ /**
1847 * Schema update to set up the needed database tables.
1948 *
2049 * @since 0.1
Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -0,0 +1,99 @@
 2+<?php
 3+
 4+/**
 5+ * Class to render survey tags.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SurveyTag.php
 10+ * @ingroup Survey
 11+ *
 12+ * @licence GNU GPL v3+
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class SurveyTag {
 16+
 17+ /**
 18+ * List of survey parameters.
 19+ *
 20+ * @since 0.1
 21+ *
 22+ * @var array
 23+ */
 24+ protected $parameters;
 25+
 26+ protected $contents;
 27+
 28+ /**
 29+ * Constructor.
 30+ *
 31+ * @since 0.1
 32+ *
 33+ * @param array $args
 34+ * @param string|null $contents
 35+ */
 36+ public function __construct( array $args, $contents = null ) {
 37+ $this->parameters = $args;
 38+
 39+ $args = filter_var_array( $args, $this->getSurveyParameters() );
 40+
 41+ if ( is_array( $args ) ) {
 42+ $this->parameters = array();
 43+
 44+ foreach ( $args as $name => $value ) {
 45+ if ( !is_null( $value ) && $value !== false ) {
 46+ $this->parameters['survey-data-' . $name] = $value;
 47+ }
 48+ }
 49+ }
 50+ else {
 51+ throw new MWException( 'Invalid parameters for survey tag.' );
 52+ }
 53+ }
 54+
 55+ /**
 56+ * Renrder the survey div.
 57+ *
 58+ * @since 0.1
 59+ *
 60+ * @param Parser $parser
 61+ *
 62+ * @return string
 63+ */
 64+ public function render( Parser $parser ) {
 65+ static $loadedJs = false;
 66+
 67+ if ( !$loadedJs ) {
 68+ // For backward compatibility with MW < 1.17.
 69+ if ( is_callable( array( $parser, 'addModules' ) ) ) {
 70+ $parser->addModules( 'ext.survey.jquery' );
 71+ }
 72+ else {
 73+ SurveyCompat::addResourceModules( $parser->getOutput(), 'ext.survey.jquery' );
 74+ }
 75+ }
 76+
 77+ return Html::element(
 78+ 'span',
 79+ $this->parameters,
 80+ $this->contents
 81+ );
 82+ }
 83+
 84+ /**
 85+ *
 86+ *
 87+ * @since 0.1
 88+ *
 89+ * @param array $args
 90+ *
 91+ * @return array
 92+ */
 93+ protected function getSurveyParameters() {
 94+ return array(
 95+ 'id' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 1 ) ),
 96+ 'name' => array(),
 97+ );
 98+ }
 99+
 100+}
Property changes on: trunk/extensions/Survey/includes/SurveyTag.php
___________________________________________________________________
Added: svn:eol-style
1101 + native
Index: trunk/extensions/Survey/includes/SurveyCompat.php
@@ -21,26 +21,43 @@
2222 *
2323 * @since 0.1
2424 *
25 - * @param OuputPage $out
26 - * @param array $modules
 25+ * @param OuputPage|ParserOutput $out
 26+ * @param array|string $modules
2727 */
28 - public static function addResourceModules( OuputPage $out, array $modules ) {
 28+ public static function addResourceModules( $out, $modules ) {
2929 global $egSurveyScriptPath, $egSurveyMessages;
30 - static $addedJsMessages = false;
 30+ static $addedJsMessages = false, $addedBaseJs = false;
3131
 32+ $modules = (array)$modules;
 33+
 34+ if ( !$addedBaseJs ) {
 35+ $out->addHeadItem(
 36+ Html::linkedScript( $egSurveyScriptPath . '/ext.survey.js' ),
 37+ 'ext.survey'
 38+ );
 39+
 40+ $addedBaseJs = true;
 41+ }
 42+
3243 foreach ( $modules as $module ) {
3344 switch ( $module ) {
3445 case 'ext.survey.special':
3546 $out->addHeadItem(
36 - $module,
37 - Html::linkedScript( $egSurveyScriptPath . 'ext.survey.special.survey.js' )
 47+ Html::linkedScript( $egSurveyScriptPath . '/ext.survey.special.survey.js' ),
 48+ $module
3849 );
3950 break;
 51+ case 'ext.survey.jquery':
 52+ $out->addHeadItem(
 53+ Html::linkedScript( $egSurveyScriptPath . '/jquery.survey.js' ),
 54+ $module
 55+ );
 56+ break;
4057 }
4158
4259 if ( array_key_exists( $module, $egSurveyMessages ) ) {
4360 if ( !$addedJsMessages ) {
44 - $out->addInlineScript( 'var wgSurveyMessages = [];' );
 61+ $out->addHeadItem( Html::inlineScript( 'var wgSurveyMessages = [];' ), uniqid() );
4562
4663 $addedJsMessages = true;
4764 }
@@ -51,7 +68,9 @@
5269 $messages[$msg] = wfMsgNoTrans( $msg );
5370 }
5471
55 - $out->addInlineScript( 'Array.push.apply( wgSurveyMessages, ' . FormatJson::encode( $messages ) . ');' );
 72+ $out->addHeadItem( Html::inlineScript(
 73+ 'Array.push.apply( wgSurveyMessages, ' . FormatJson::encode( $messages ) . ');'
 74+ ), uniqid() );
5675 }
5776 }
5877 }

Status & tagging log