r95635 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95634‎ | r95635 | r95636 >
Date:19:58, 28 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey admin and rem some compat code
Modified paths:
  • /trunk/extensions/Survey/INSTALL (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/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.special.survey.js (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.special.surveys.js (added) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurveyPage.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurveys.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.php
@@ -24,8 +24,8 @@
2525 die( 'Not an entry point.' );
2626 }
2727
28 -if ( version_compare( $wgVersion, '1.16', '<' ) ) {
29 - die( '<b>Error:</b> Survey requires MediaWiki 1.16 or above.' );
 28+if ( version_compare( $wgVersion, '1.17', '<' ) ) {
 29+ die( '<b>Error:</b> Survey requires MediaWiki 1.17 or above.' );
3030 }
3131
3232 define( 'Survey_VERSION', '0.1 alpha' );
@@ -108,7 +108,7 @@
109109 $egSurveyScriptPath .= '/Survey/resources';
110110
111111 $egSurveyMessages = array(
112 - 'ext.survey.special' => array(
 112+ 'ext.survey.special.surveys' => array(
113113 'surveys-special-confirm-delete'
114114 ),
115115 'jquery.survey' => array(
@@ -127,12 +127,12 @@
128128 ),
129129 );
130130
131 -$wgResourceModules['ext.survey.special'] = $moduleTemplate + array(
 131+$wgResourceModules['ext.survey.special.surveys'] = $moduleTemplate + array(
132132 'scripts' => array(
133 - 'ext.survey.special.survey.js'
 133+ 'ext.survey.special.surveys.js'
134134 ),
135135 'dependencies' => array( 'ext.survey' ),
136 - 'messages' => $egSurveyMessages['ext.survey.special']
 136+ 'messages' => $egSurveyMessages['ext.survey.special.surveys']
137137 );
138138
139139 $wgResourceModules['ext.survey.jquery'] = $moduleTemplate + array(
Index: trunk/extensions/Survey/Survey.i18n.php
@@ -52,5 +52,13 @@
5353 'surveys-special-delete-failed' => 'Failed to delete survey.',
5454
5555 // Special:TakeSurvey
56 - 'surveys-takesurvey-loading' => 'Loading survey.'
 56+ 'surveys-takesurvey-loading' => 'Loading survey.',
 57+
 58+ // Special:Survey
 59+ 'surveys-special-unknown-name' => 'There is no survey with the requested name.',
 60+ 'survey-special-label-name' => 'Survey name',
 61+ 'survey-special-label-enabled' => 'Survey enabled',
 62+ 'survey-special-label-question' => 'Question text',
 63+ 'survey-special-label-button' => 'Add question',
 64+ 'survey-special-label-add' => 'New question name',
5765 );
Index: trunk/extensions/Survey/specials/SpecialSurveyPage.php
@@ -77,16 +77,7 @@
7878 * @param string|array $modules
7979 */
8080 public function addModules( $modules ) {
81 - $out = $this->getOutput();
82 - $modules = (array)$modules;
83 -
84 - // For backward compatibility with MW < 1.17.
85 - if ( is_callable( array( $out, 'addModules' ) ) ) {
86 - $out->addModules( $modules );
87 - }
88 - else {
89 - SurveyCompat::addResourceModules( $out, $modules );
90 - }
 81+ $this->getOutput()->addModules( $modules );
9182 }
9283
9384 }
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -32,6 +32,146 @@
3333 public function execute( $subPage ) {
3434 parent::execute( $subPage );
3535
 36+ $survey = Survey::newFromName( $subPage, true );
 37+
 38+ if ( $survey === false ) {
 39+ $this->showNameError();
 40+ }
 41+ else {
 42+ $this->showSurvey( $survey );
 43+ }
3644 }
3745
 46+ /**
 47+ * Show error when requesting a non-existing survey.
 48+ *
 49+ * @since 0.1
 50+ */
 51+ protected function showNameError() {
 52+ $this->getOutput()->addHTML(
 53+ '<p class="errorbox">' . wfMsgHtml( 'surveys-special-unknown-name' ) . '</p>'
 54+ );
 55+ }
 56+
 57+ /**
 58+ * Show the survey.
 59+ *
 60+ * @since 0.1
 61+ *
 62+ * @param Survey $survey
 63+ */
 64+ protected function showSurvey( Survey $survey ) {
 65+ $fields = array();
 66+
 67+ $fields[] = array(
 68+ 'type' => 'text',
 69+ //'options' => array(),
 70+ 'default' => 'ohi',
 71+ 'label-message' => 'survey-special-label-name',
 72+ 'required' => true
 73+ );
 74+
 75+ $fields[] = array(
 76+ 'type' => 'check',
 77+ //'options' => array(),
 78+ 'default' => 'there',
 79+ 'label-message' => 'survey-special-label-enabled',
 80+ 'required' => true
 81+ );
 82+
 83+ foreach ( $survey->getQuestions() as $question ) {
 84+ $fields[] = array(
 85+ 'class' => 'SurveyQuestionField',
 86+ 'options' => array(
 87+ 'required' => $question->isRequired(),
 88+ 'text' => $question->getText(),
 89+ 'type' => $question->getType()
 90+ )
 91+ );
 92+ }
 93+
 94+ $fields[] = array(
 95+ 'class' => 'SurveyAddQuestionField',
 96+ 'default' => 'foo',
 97+ 'label-message' => 'survey-special-label-add',
 98+ 'id' => 'survey-add-question-text'
 99+ );
 100+
 101+ // getContext was added in 1.18 and since that version is
 102+ // the second argument for the HTMLForm constructor.
 103+ if ( is_callable( array( $this, 'getContext' ) ) ) {
 104+ $form = new HTMLForm( $fields, $this->getContext() );
 105+ }
 106+ else {
 107+ $form = new HTMLForm( $fields );
 108+ }
 109+
 110+// $q = new SurveyQuestion( null, 5, 'foo bar', 0, false, array(), false );
 111+// var_dump($q->toUrlData());exit;
 112+ $form->displayForm( '' );
 113+ }
 114+
38115 }
 116+
 117+class SurveyAddQuestionField extends HTMLTextField {
 118+
 119+ /*
 120+ $fields[] = array(
 121+ 'type' => 'text',
 122+ //'options' => array(),
 123+ 'default' => $question->getText(),
 124+ 'label-message' => 'survey-special-label-question',
 125+ 'required' => $question->isRequired()
 126+ );
 127+
 128+ $fields[] = array(
 129+ 'type' => 'select',
 130+ 'options' => array(
 131+
 132+ ),
 133+ 'label-message' => 'survey-special-label-type',
 134+ 'required' => $question->isRequired()
 135+ );
 136+
 137+ $fields[] = array(
 138+ 'class' => 'SurveyQuestionValuesField',
 139+ //'options' => array(),
 140+ 'default' => '',
 141+ 'label-message' => 'survey-special-label-required',
 142+ 'required' => true
 143+ );
 144+ */
 145+
 146+ public function getInputHTML( $value ) {
 147+ return parent::getInputHTML( $value ) . '&#160;' . Html::element(
 148+ 'button',
 149+ array(),
 150+ wfMsg( 'survey-special-label-button' )
 151+ );
 152+ }
 153+
 154+}
 155+
 156+class SurveyQuestionField extends HTMLFormField {
 157+
 158+ public function getInputHTML( $value ) {
 159+ $attribs = array();
 160+
 161+ foreach ( $this->mParams['options'] as $name => $value ) {
 162+ if ( is_bool( $value ) ) {
 163+ $value = $value ? '1' : '0';
 164+ }
 165+ elseif( is_object( $value ) || is_array( $value ) ) {
 166+ $value = FormatJson::encode( $value );
 167+ }
 168+
 169+ $attribs['data-' . $name] = $value;
 170+ }
 171+
 172+ return Html::element(
 173+ 'div',
 174+ $attribs
 175+ );
 176+ }
 177+
 178+}
\ No newline at end of file
Index: trunk/extensions/Survey/specials/SpecialSurveys.php
@@ -67,7 +67,7 @@
6868 $this->displaySurveysTable( $surveys );
6969 }
7070
71 - $this->addModules( 'ext.survey.special' );
 71+ $this->addModules( 'ext.survey.special.surveys' );
7272 }
7373
7474 /**
Index: trunk/extensions/Survey/INSTALL
@@ -8,7 +8,7 @@
99
1010 Survey requires:
1111
12 -* MediaWiki 1.16 or above
 12+* MediaWiki 1.17 or above
1313 * PHP 5.2 or above
1414
1515 == Download ==
Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -66,15 +66,7 @@
6767 static $loadedJs = false;
6868
6969 if ( !$loadedJs ) {
70 - $po /* ParserOutput */ = $parser->getOutput();
71 -
72 - // For backward compatibility with MW < 1.17.
73 -// if ( is_callable( array( $po, 'addModules' ) ) ) {
74 -// $po->addModules( 'ext.survey.jquery' );
75 -// }
76 -// else {
77 - SurveyCompat::addResourceModules( $po, 'ext.survey.jquery' );
78 -// }
 70+ $parser->getOutput()->addModules( 'ext.survey.jquery' );
7971 }
8072
8173 return Html::element(
Index: trunk/extensions/Survey/includes/SurveyCompat.php
@@ -15,66 +15,6 @@
1616
1717 class SurveyCompat {
1818
19 - /**
20 - * Add the resources for a resource loader module.
21 - * This is needed for MediaWiki < 1.17.
22 - *
23 - * @since 0.1
24 - *
25 - * @param OuputPage|ParserOutput $out
26 - * @param array|string $modules
27 - */
28 - public static function addResourceModules( $out, $modules ) {
29 - global $egSurveyScriptPath, $egSurveyMessages;
30 - static $addedJsMessages = false, $addedBaseJs = false;
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 -
43 - foreach ( $modules as $module ) {
44 - switch ( $module ) {
45 - case 'ext.survey.special':
46 - $out->addHeadItem(
47 - Html::linkedScript( $egSurveyScriptPath . '/ext.survey.special.survey.js' ),
48 - $module
49 - );
50 - break;
51 - case 'ext.survey.jquery':
52 - $out->addHeadItem(
53 - Html::linkedStyle( $egSurveyScriptPath . '/fancybox/jquery.fancybox-1.3.4.css' ) .
54 - Html::linkedScript( $egSurveyScriptPath . '/fancybox/jquery.fancybox-1.3.4.js' ) .
55 - Html::linkedScript( $egSurveyScriptPath . '/jquery.survey.js' ),
56 - $module
57 - );
58 - break;
59 - }
60 -
61 - if ( array_key_exists( $module, $egSurveyMessages ) ) {
62 - if ( !$addedJsMessages ) {
63 - $out->addHeadItem( Html::inlineScript( 'var wgSurveyMessages = [];' ), uniqid() );
64 -
65 - $addedJsMessages = true;
66 - }
67 -
68 - $messages = array();
69 -
70 - foreach ( $egSurveyMessages[$module] as $msg ) {
71 - $messages[$msg] = wfMsgNoTrans( $msg );
72 - }
73 -
74 - $out->addHeadItem( Html::inlineScript(
75 - 'Array.push.apply( wgSurveyMessages, ' . FormatJson::encode( $messages ) . ');'
76 - ), uniqid() );
77 - }
78 - }
79 - }
8019
 20+
8121 }
\ No newline at end of file
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -88,7 +88,7 @@
8989 $this->surveyId = (int)$surveyId;
9090 $this->text = $text;
9191 $this->type = $type;
92 - $this->required = $required;
 92+ $this->required = (boolean)$required;
9393 $this->answers = $answers;
9494 $this->removed = $removed;
9595 }
@@ -120,6 +120,7 @@
121121 public static function newFromArray( array $args ) {
122122 return new self(
123123 array_key_exists( 'id', $args ) ? $args['id'] : null,
 124+ array_key_exists( 'surveyId', $args ) ? $args['surveyId'] : null,
124125 $args['text'],
125126 $args['type'],
126127 $args['required'],
@@ -150,6 +151,7 @@
151152 */
152153 public function toArray() {
153154 $args = array(
 155+ 'surveyId' => $this->surveyId,
154156 'text' => $this->text,
155157 'type' => $this->type,
156158 'required' => $this->required,
@@ -255,4 +257,37 @@
256258 );
257259 }
258260
 261+ /**
 262+ * Gets the question text.
 263+ *
 264+ * @since 0.1
 265+ *
 266+ * @return string
 267+ */
 268+ public function getText() {
 269+ return $this->text;
 270+ }
 271+
 272+ /**
 273+ * Gets the questions type.
 274+ *
 275+ * @since 0.1
 276+ *
 277+ * @return string
 278+ */
 279+ public function getType() {
 280+ return $this->type;
 281+ }
 282+
 283+ /**
 284+ * Gets if the question is required.
 285+ *
 286+ * @since 0.1
 287+ *
 288+ * @return boolean
 289+ */
 290+ public function isRequired() {
 291+ return $this->required;
 292+ }
 293+
259294 }
Index: trunk/extensions/Survey/resources/ext.survey.special.survey.js
@@ -8,40 +8,6 @@
99
1010 (function( $ ) { $( document ).ready( function() {
1111
12 - function deleteSurvey( options, successCallback, failCallback ) {
13 - $.getJSON(
14 - wgScriptPath + '/api.php',
15 - {
16 - 'action': 'deletesurvey',
17 - 'format': 'json',
18 - 'ids': options.id
19 - },
20 - function( data ) {
21 - if ( data.success ) {
22 - successCallback();
23 - }
24 - else {
25 - failCallback( survey.msg( 'surveys-special-delete-failed' ) );
26 - }
27 - }
28 - );
29 - }
 12+
3013
31 - $( '.survey-delete' ).click( function() {
32 - $this = $( this );
33 -
34 - if ( confirm( survey.msg( 'surveys-special-confirm-delete' ) ) ) {
35 - deleteSurvey(
36 - { id: $this.attr( 'data-survey-id' ) },
37 - function() {
38 - $this.closest( 'tr' ).slideUp( 'slow', function() { $( this ).remove(); } );
39 - },
40 - function( error ) {
41 - alert( error );
42 - }
43 - );
44 - }
45 - return false;
46 - } );
47 -
4814 } ); })( jQuery );
\ No newline at end of file
Index: trunk/extensions/Survey/resources/ext.survey.special.surveys.js
@@ -0,0 +1,47 @@
 2+/**
 3+ * JavasSript for the Survey MediaWiki extension.
 4+ * @see https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Extension:Survey
 5+ *
 6+ * @licence GNU GPL v3 or later
 7+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 8+ */
 9+
 10+(function( $ ) { $( document ).ready( function() {
 11+
 12+ function deleteSurvey( options, successCallback, failCallback ) {
 13+ $.getJSON(
 14+ wgScriptPath + '/api.php',
 15+ {
 16+ 'action': 'deletesurvey',
 17+ 'format': 'json',
 18+ 'ids': options.id
 19+ },
 20+ function( data ) {
 21+ if ( data.success ) {
 22+ successCallback();
 23+ }
 24+ else {
 25+ failCallback( survey.msg( 'surveys-special-delete-failed' ) );
 26+ }
 27+ }
 28+ );
 29+ }
 30+
 31+ $( '.survey-delete' ).click( function() {
 32+ $this = $( this );
 33+
 34+ if ( confirm( survey.msg( 'surveys-special-confirm-delete' ) ) ) {
 35+ deleteSurvey(
 36+ { id: $this.attr( 'data-survey-id' ) },
 37+ function() {
 38+ $this.closest( 'tr' ).slideUp( 'slow', function() { $( this ).remove(); } );
 39+ },
 40+ function( error ) {
 41+ alert( error );
 42+ }
 43+ );
 44+ }
 45+ return false;
 46+ } );
 47+
 48+} ); })( jQuery );
\ No newline at end of file
Property changes on: trunk/extensions/Survey/resources/ext.survey.special.surveys.js
___________________________________________________________________
Added: svn:eol-style
149 + native

Status & tagging log