Index: trunk/extensions/Survey/Survey.php |
— | — | @@ -59,6 +59,7 @@ |
60 | 60 | $wgAutoloadClasses['SurveyDBClass'] = dirname( __FILE__ ) . '/includes/SurveyDBClass.php'; |
61 | 61 | $wgAutoloadClasses['SurveyQuestion'] = dirname( __FILE__ ) . '/includes/SurveyQuestion.php'; |
62 | 62 | $wgAutoloadClasses['SurveySubmission'] = dirname( __FILE__ ) . '/includes/SurveySubmission.php'; |
| 63 | +$wgAutoloadClasses['SurveyTag'] = dirname( __FILE__ ) . '/includes/SurveyTag.php'; |
63 | 64 | |
64 | 65 | $wgAutoloadClasses['SpecialSurvey'] = dirname( __FILE__ ) . '/specials/SpecialSurvey.php'; |
65 | 66 | $wgAutoloadClasses['SpecialSurveyPage'] = dirname( __FILE__ ) . '/specials/SpecialSurveyPage.php'; |
— | — | @@ -84,6 +85,7 @@ |
85 | 86 | |
86 | 87 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'SurveyHooks::onSchemaUpdate'; |
87 | 88 | $wgHooks['UnitTestsList'][] = 'SurveyHooks::registerUnitTests'; |
| 89 | +$wgHooks['ParserFirstCallInit'][] = 'SurveyHooks::onParserFirstCallInit'; |
88 | 90 | |
89 | 91 | $wgAvailableRights[] = 'surveyadmin'; |
90 | 92 | $wgAvailableRights[] = 'surveysubmit'; |
— | — | @@ -133,7 +135,7 @@ |
134 | 136 | 'messages' => $egSurveyMessages['ext.survey.special'] |
135 | 137 | ); |
136 | 138 | |
137 | | -$wgResourceModules['jquery.survey'] = $moduleTemplate + array( |
| 139 | +$wgResourceModules['ext.survey.jquery'] = $moduleTemplate + array( |
138 | 140 | 'scripts' => array( |
139 | 141 | 'jquery.survey.js' |
140 | 142 | ), |
Index: trunk/extensions/Survey/Survey.i18n.php |
— | — | @@ -20,23 +20,26 @@ |
21 | 21 | $messages['en'] = array( |
22 | 22 | 'survey-desc' => 'Survey tool for MediaWiki', |
23 | 23 | |
| 24 | + // Rights |
24 | 25 | 'right-surveyadmin' => 'Manage surveys', |
25 | 26 | 'right-surveysubmit' => 'Participate in surveys', |
26 | 27 | |
| 28 | + // Special page names |
27 | 29 | 'special-survey' => 'Survey admin', |
28 | 30 | 'special-surveys' => 'Surveys admin', |
29 | 31 | 'special-surveystats' => 'Survey statistics', |
30 | 32 | 'special-takesurvey' => 'Take survey', |
31 | 33 | |
| 34 | + // API errors |
32 | 35 | 'survey-err-id-xor-name' => 'You need to provide either the id or the name of the survey to submit', |
33 | 36 | 'survey-err-survey-name-unknown' => 'There is no survey with the name "$1"', |
34 | 37 | 'survey-err-duplicate-name' => 'There already is a survey with name "$1"', |
35 | 38 | |
| 39 | + // Special:Surveys |
36 | 40 | 'surveys-special-addnew' => 'Add a new survey', |
37 | 41 | 'surveys-special-namedoc' => 'Enter the name for the new survey.', |
38 | 42 | 'surveys-special-newname' => 'New survey name:', |
39 | 43 | 'surveys-special-add' => 'Add survey', |
40 | | - |
41 | 44 | 'surveys-special-existing' => 'Existing surveys', |
42 | 45 | 'surveys-special-name' => 'Name', |
43 | 46 | 'surveys-special-status' => 'Status', |
— | — | @@ -46,4 +49,7 @@ |
47 | 50 | 'surveys-special-disabled' => 'Disabled', |
48 | 51 | 'surveys-special-confirm-delete' => 'Are you sure you want to delete this survey?', |
49 | 52 | 'surveys-special-delete-failed' => 'Failed to delete survey.', |
| 53 | + |
| 54 | + // Special:TakeSurvey |
| 55 | + 'surveys-takesurvey-loading' => 'Loading survey.' |
50 | 56 | ); |
Index: trunk/extensions/Survey/specials/SpecialTakeSurvey.php |
— | — | @@ -32,6 +32,13 @@ |
33 | 33 | public function execute( $subPage ) { |
34 | 34 | parent::execute( $subPage ); |
35 | 35 | |
| 36 | + $this->getOutput()->addWikiText( Xml::element( |
| 37 | + 'survey', |
| 38 | + array( |
| 39 | + 'name' => $subPage |
| 40 | + ), |
| 41 | + wfMsg( 'surveys-takesurvey-loading' ) |
| 42 | + ) ); |
36 | 43 | } |
37 | 44 | |
38 | 45 | } |
Index: trunk/extensions/Survey/Survey.hooks.php |
— | — | @@ -14,6 +14,35 @@ |
15 | 15 | final class SurveyHooks { |
16 | 16 | |
17 | 17 | /** |
| 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 | + /** |
18 | 47 | * Schema update to set up the needed database tables. |
19 | 48 | * |
20 | 49 | * @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 |
1 | 101 | + native |
Index: trunk/extensions/Survey/includes/SurveyCompat.php |
— | — | @@ -21,26 +21,43 @@ |
22 | 22 | * |
23 | 23 | * @since 0.1 |
24 | 24 | * |
25 | | - * @param OuputPage $out |
26 | | - * @param array $modules |
| 25 | + * @param OuputPage|ParserOutput $out |
| 26 | + * @param array|string $modules |
27 | 27 | */ |
28 | | - public static function addResourceModules( OuputPage $out, array $modules ) { |
| 28 | + public static function addResourceModules( $out, $modules ) { |
29 | 29 | global $egSurveyScriptPath, $egSurveyMessages; |
30 | | - static $addedJsMessages = false; |
| 30 | + static $addedJsMessages = false, $addedBaseJs = false; |
31 | 31 | |
| 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 | + |
32 | 43 | foreach ( $modules as $module ) { |
33 | 44 | switch ( $module ) { |
34 | 45 | case 'ext.survey.special': |
35 | 46 | $out->addHeadItem( |
36 | | - $module, |
37 | | - Html::linkedScript( $egSurveyScriptPath . 'ext.survey.special.survey.js' ) |
| 47 | + Html::linkedScript( $egSurveyScriptPath . '/ext.survey.special.survey.js' ), |
| 48 | + $module |
38 | 49 | ); |
39 | 50 | break; |
| 51 | + case 'ext.survey.jquery': |
| 52 | + $out->addHeadItem( |
| 53 | + Html::linkedScript( $egSurveyScriptPath . '/jquery.survey.js' ), |
| 54 | + $module |
| 55 | + ); |
| 56 | + break; |
40 | 57 | } |
41 | 58 | |
42 | 59 | if ( array_key_exists( $module, $egSurveyMessages ) ) { |
43 | 60 | if ( !$addedJsMessages ) { |
44 | | - $out->addInlineScript( 'var wgSurveyMessages = [];' ); |
| 61 | + $out->addHeadItem( Html::inlineScript( 'var wgSurveyMessages = [];' ), uniqid() ); |
45 | 62 | |
46 | 63 | $addedJsMessages = true; |
47 | 64 | } |
— | — | @@ -51,7 +68,9 @@ |
52 | 69 | $messages[$msg] = wfMsgNoTrans( $msg ); |
53 | 70 | } |
54 | 71 | |
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() ); |
56 | 75 | } |
57 | 76 | } |
58 | 77 | } |