r42793 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r42792‎ | r42793 | r42794 >
Date:00:07, 30 October 2008
Author:tparscal
Status:old
Tags:
Comment:
Refactored a bit, cleaned lots of things up.
Modified paths:
  • /branches/CentralNotice-SpecialPage-Integration/SpecialNoticeTemplate.php (modified) (history)

Diff [purge]

Index: branches/CentralNotice-SpecialPage-Integration/SpecialNoticeTemplate.php
@@ -1,204 +1,276 @@
22 <?php
3 -
 3+
44 if ( !defined( 'MEDIAWIKI' ) ) {
5 - echo "CentralNotice extension\n";
6 - exit( 1 );
 5+ echo "CentralNotice extension\n";
 6+ exit( 1 );
77 }
88
99 class SpecialNoticeTemplate extends SpecialPage {
10 -
11 - function __construct() {
12 - parent::__construct( "NoticeTemplate" );
13 - wfLoadExtensionMessages('CentralNotice');
 10+
 11+ /* Functions */
 12+
 13+ function __construct() {
 14+ // Initialize special page
 15+ parent::__construct( 'NoticeTemplate' );
 16+
 17+ // Internationalization
 18+ wfLoadExtensionMessages( 'CentralNotice' );
1419 }
15 -
 20+
1621 function execute( $sub ) {
1722 global $wgOut, $wgUser, $wgRequest;
1823
 24+ // Begin output
1925 $this->setHeaders();
 26+
 27+ // Get current skin
2028 $sk = $wgUser->getSkin();
21 -
22 - if ( !$wgUser->isAllowed( 'centralnotice_admin_rights' )) {
 29+
 30+ // Check permissions
 31+ if ( !$wgUser->isAllowed( 'centralnotice_admin_rights' ) ) {
2332 $wgOut->permissionRequired( 'centralnotice_admin_rights' );
2433 return;
2534 }
2635
27 - $wgOut->addWikiText( wfMsg( 'centralnotice-summary' ));
 36+ // Show summary
 37+ $wgOut->addWikiText( wfMsg( 'centralnotice-summary' ) );
 38+
 39+ // Show header
2840 CentralNotice::printHeader();
29 -
30 -
 41+
 42+ // Handle forms
3143 if ( $wgRequest->wasPosted() ) {
32 - $body = file_get_contents('php://input');
33 - $wgOut->addHtml("Body of post: $body");
34 -
35 - $toRemove = $wgRequest->getArray('removeTemplates');
36 - if ( isset($toRemove) ){
 44+
 45+ /*
 46+ * For debugging purposes only
 47+ */
 48+ $body = file_get_contents( 'php://input' );
 49+ $wgOut->addHtml( Xml::element( 'pre', null, $body ) );
 50+
 51+ // Build list of templates to remove
 52+ $toRemove = $wgRequest->getArray( 'removeTemplates' );
 53+ if ( isset( $toRemove ) ) {
 54+ // Remove templates in list
3755 foreach ( $toRemove as $template ) {
3856 $this->removeTemplate( $template );
3957 }
 58+
 59+ // Show a list of templates
4060 $this->listTemplates();
4161 return;
4262 }
43 -
44 - $enabledNotices = $wgRequest->getArray('enabled');
 63+
 64+ // Build a list of notices to enable
 65+ $enabledNotices = $wgRequest->getArray( 'enabled' );
4566 if ( isset( $enabledNotices ) ) {
46 - $allNotices = $this->getNoticesName();
47 -
48 - $diff_set = array_diff( $allNotices, $enabledNotices);
49 -
50 - foreach ( $enabledNotices as $notice) {
51 - $this->updateEnabled( $notice, 'Y');
 67+ // Build a list of notices to disable
 68+ $disabledNotices = array_diff( $this->getNoticesName(), $enabledNotices );
 69+
 70+ // Set enabled/disabled flag accordingly
 71+ foreach ( $enabledNotices as $notice ) {
 72+ $this->updateEnabled( $notice, 'Y' );
5273 }
53 - foreach ( $diff_set as $notice) {
54 - $this->updateEnabled( $notice, 'N');
 74+ foreach ( $disabledNotices as $notice ) {
 75+ $this->updateEnabled( $notice, 'N' );
5576 }
5677 }
5778 }
58 -
59 - $method = $wgRequest->getVal('wpMethod');
60 -
61 - if ( $method == 'addTemplate') {
62 - $templateName = $wgRequest->getVal('templateName');
63 - $templateBody = $wgRequest->getVal('templateBody');
64 - $this->addTemplate( $templateName, $templateBody);
 79+
 80+ // Handle adding
 81+ if ( $wgRequest->getVal( 'wpMethod' ) == 'addTemplate' ) {
 82+ $this->addTemplate(
 83+ $wgRequest->getVal( 'templateName' ),
 84+ $wgRequest->getVal( 'templateBody' )
 85+ );
6586 }
 87+
 88+ // If this is a sub-page, show list of templates
6689 if ( $sub == 'listTemplates' ) {
6790 $this->listTemplates();
6891 return;
6992 }
70 -
71 - $this->listTemplates();
 93+
 94+ $this->listTemplates();
7295 }
7396
74 - private function updateEnabled( $update_notice, $enabled) {
75 - $centralnotice_table = "central_notice_campaign";
76 - $dbw = wfGetDB( DB_MASTER );
77 - $res = $dbw->update($centralnotice_table, array( cnc_enabled => $enabled ), array( cnc_template => $update_notice));
 97+ private function updateEnabled( $update_notice, $enabled ) {
 98+ global $egCentralNoticeTables;
 99+
 100+ $dbw = wfGetDB( DB_MASTER );
 101+ $res = $dbw->update( 'central_notice_campaign',
 102+ array( cnc_enabled => $enabled ),
 103+ array( cnc_template => $update_notice ),
 104+ __METHOD__
 105+ );
78106 }
79107
80108 public static function previewTemplate() {
 109+ //
81110 }
82111
83 -
84112 function queryTemplates() {
85 - $centralnotice_template_table = "central_notice_templates";
86113 $dbr = wfGetDB( DB_SLAVE );
87 - $res = $dbr->select( $centralnotice_template_table, "template_name, template_id", '', '', array('ORDER BY' => 'template_id'));
88 -
 114+ $res = $dbr->select( 'central_notice_templates',
 115+ array( 'template_name', 'template_id' ),
 116+ '',
 117+ __METHOD__,
 118+ array( 'ORDER BY' => 'template_id' )
 119+ );
 120+
89121 $templates = array();
90 - while ( $row = $dbr->fetchObject( $res )) {
91 - array_push($templates, $row->template_name);
 122+ while ( $row = $dbr->fetchObject( $res ) ) {
 123+ array_push( $templates, $row->template_name );
92124 }
 125+
93126 return $templates;
94127 }
95 -
 128+
96129 function listTemplates() {
97130 $templates = $this->queryTemplates();
98131 return $this->templatesForm( $templates );
99132 }
100 -
 133+
101134 function templatesForm( $templates ) {
102135 global $wgOut, $wgTitle;
103136
104 - $table = Xml::openElement( 'form', array(
105 - 'method' => 'post',
106 - 'action' => ''));
107 - $table .= Xml::fieldset( 'Available Templates' );
108 - $table .= Xml::openElement( 'table', array ( 'cellpadding' => 9)) ;
109 - $table .= "<th>" . wfMsg ( 'centralnotice-template-name' ) . "</th>";
110 - $table .= "<th>" . wfMsg ( 'centralnotice-remove') . "</th>";
111 -
 137+ // Templates
 138+ $htmlOut = Xml::openElement( 'form',
 139+ array(
 140+ 'method' => 'post',
 141+ 'action' => ''
 142+ )
 143+ );
 144+ $htmlOut .= Xml::fieldset( 'Available Templates' );
 145+ $htmlOut .= Xml::openElement( 'table', array ( 'cellpadding' => 9 ) ) ;
 146+ $htmlOut .= Xml::element( 'th', null, wfMsg ( 'centralnotice-template-name' ) );
 147+ $htmlOut .= Xml::element( 'th', null, wfMsg ( 'centralnotice-remove' ) );
112148 $templates = $this->queryTemplates();
113149 foreach ( $templates as $templateName ) {
114 - $table .= "<tr><td>" .
115 - $templateName .
116 - "</td>";
117 - $table .= "<td>" .
118 - Xml::check( 'removeTemplates[]', false, array( 'value' => $templateName)) .
119 - "</td></tr>";
 150+ $htmlOut .= Xml::tags( 'tr', null,
 151+ Xml::element( 'td', null, $templateName ) .
 152+ Xml::tags( 'td', null,
 153+ Xml::check( 'removeTemplates[]', false,
 154+ array( 'value' => $templateName )
 155+ )
 156+ )
 157+ );
120158 }
121 - $table .= "<tr><td>" . Xml::submitButton( wfMsg( 'centralnotice-modify')) . "</td></tr>";
122 - $table .= Xml::closeElement( 'table' );
123 - $table .= XML::closeElement( 'fieldset' );
124 -
125 - $wgOut->addHtml( $table );
126 -
127 - $wgOut->addHtml(
128 - Xml::openElement( 'form', array(
129 - 'method' => 'post',
130 - 'action' => SpecialPage::getTitleFor( 'NoticeTemplate' )->getFullUrl() ) ) .
131 - '<fieldset>' .
132 - Xml::element( 'legend', array(), wfMsg( 'centralnotice-add-template' ) ) .
133 - Xml::hidden( 'wpMethod', 'addTemplate' ) .
134 - '<p>' .
135 - Xml::inputLabel( wfMsg( 'centralnotice-template-name' ),
136 - 'templateName', 'templateName', 25) .
137 - '</p>' .
138 - '<p>' .
139 - Xml::textarea( 'templateBody', '', 60, 20) .
140 - '<p>' .
141 - Xml::submitButton( wfMsg( 'centralnotice-modify' ) ) .
142 - Xml::submitButton( wfMsg( 'centralnotice-preview' ) ) .
143 - '</p>' .
144 - '</fieldset>' .
145 - '</form>' .
146 - Xml::closeElement( 'form' )
147 - );
 159+ $htmlOut .= Xml::tags( 'tr', null,
 160+ Xml::tags( 'td', null,
 161+ Xml::submitButton( wfMsg( 'centralnotice-modify' ) )
 162+ )
 163+ );
 164+ $htmlOut .= Xml::closeElement( 'table' );
 165+ $htmlOut .= XML::closeElement( 'fieldset' );
 166+
 167+ // Notices
 168+ $htmlOut .= Xml::openElement( 'form',
 169+ array(
 170+ 'method' => 'post',
 171+ 'action' => SpecialPage::getTitleFor( 'NoticeTemplate' )->getFullUrl()
 172+ )
 173+ );
 174+ $htmlOut .= Xml::openElement( 'fieldset' );
 175+ $htmlOut .= Xml::element( 'legend', null, wfMsg( 'centralnotice-add-template' ) );
 176+ $htmlOut .= Xml::hidden( 'wpMethod', 'addTemplate' );
 177+ $htmlOut .= Xml::tags( 'p', null,
 178+ Xml::inputLabel(
 179+ wfMsg( 'centralnotice-template-name' ),
 180+ 'templateName',
 181+ 'templateName',
 182+ 25
 183+ )
 184+ );
 185+ $htmlOut .= Xml::tags( 'p', null,
 186+ Xml::textarea( 'templateBody', '', 60, 20 )
 187+ );
 188+ $htmlOut .= Xml::tags( 'p', null,
 189+ Xml::submitButton( wfMsg( 'centralnotice-modify' ) ) .
 190+ Xml::submitButton( wfMsg( 'centralnotice-preview' ) )
 191+ );
 192+ $htmlOut .= Xml::closeElement( 'fieldset' );
 193+ $htmlOut .= Xml::closeElement( 'form' );
 194+
 195+ // Output HTML
 196+ $wgOut->addHtml( $htmlOut );
148197 }
149 -
 198+
150199 function listTemplateDetail ( $template ) {
151 - global $wgOut,$wgUser;
152 -
153 - $form .= "Preview";
154 - $form .= "Template";
155 - $form .= "Button";
156 - $form .= "Link";
157 -
 200+ global $wgOut, $wgUser;
 201+
 202+ /*
 203+ * What is this supposed to be?
 204+ *
 205+ $form .= 'Preview';
 206+ $form .= 'Template';
 207+ $form .= 'Button';
 208+ $form .= 'Link';
 209+ */
 210+
158211 if ( $wgUser->isAllowed( 'centralnotice-template-edit' ) ) {
159 - $form .= "<tr><td><center>" . Xml::submitButton( wfMsgHtml('centralnotice-modify'),
160 - array('id' => 'centralnoticesubmit','name' => 'centralnoticesubmit') ) . "</td>";
161 - $form .= "<tr><td>" . Xml::submitButton( wfMsgHtml('centralnotice-preview'),
162 - array('id' => 'centralnoticepreview','name' => 'centralnoticepreview') ) . "</center></td>";
163 -
 212+ $form .= Xml::tags( 'tr', null,
 213+ Xml::tags( 'td', null,
 214+ Xml::submitButton(
 215+ wfMsgHtml( 'centralnotice-modify' ),
 216+ array(
 217+ 'id' => 'centralnoticesubmit',
 218+ 'name' => 'centralnoticesubmit'
 219+ )
 220+ )
 221+ )
 222+ );
 223+ $form .= Xml::tags( 'tr', null,
 224+ Xml::tags( 'td', null,
 225+ Xml::submitButton(
 226+ wfMsgHtml( 'centralnotice-preview' ),
 227+ array(
 228+ 'id' => 'centralnoticepreview',
 229+ 'name' => 'centralnoticepreview'
 230+ )
 231+ )
 232+ )
 233+ );
164234 }
165 -
166235 $wgOut->addHTML( $form );
167236 }
 237+
 238+ function addTemplate ( $name, $body ) {
 239+ global $wgOut, $egCentralNoticeTables;
168240
169 - function addTemplate ( $templateName, $templateBody ) {
170 - global $wgOut;
171 -
172 - if ( $templateBody == '' || $templateName == '' ) {
 241+ if ( $body == '' || $name == '' ) {
173242 $wgOut->addHtml( wfMsg( 'centralnotice-null-string' ) );
174243 return;
175244 }
176 -
 245+
177246 $dbr = wfGetDB( DB_SLAVE );
178 - $centralnotice_table = 'central_notice_templates';
179 -
180 - $eTemplateName = htmlspecialchars ( $templateName );
181 -
182 - $res = $dbr->select( $centralnotice_table, 'template_name', "template_name = '$eTemplateName' " );
 247+ $res = $dbr->select( 'central_notice_templates', 'template_name',
 248+ array( 'template_name' => $name ),
 249+ __METHOD__
 250+ );
 251+
183252 if ( $dbr->numRows( $res ) > 0 ) {
184 - $wgOut->addHTML( wfMsg( 'centralnotice-template-exists' ) );
 253+ $wgOut->addHTML( wfMsg( 'centralnotice-template-exists' ) );
185254 return;
186 - }
187 - else {
188 - $dbw = wfGetDB( DB_MASTER );
189 - $res = $dbw->insert( $centralnotice_table, array( 'template_name' => "$templateName"));
190 -
191 - //perhaps these should move into the db as blob
192 - $templatePage = "Centralnotice-" . "template-" . "$templateName";
193 - $title = Title::newFromText( $templatePage, NS_MEDIAWIKI );
194 - $article = new Article( $title );
195 - $article->doEdit( $templateBody, '' );
196 - return;
 255+ } else {
 256+ $dbw = wfGetDB( DB_MASTER );
 257+ $res = $dbw->insert( 'central_notice_templates',
 258+ array( 'template_name' => $name ),
 259+ __METHOD__
 260+ );
197261
 262+ /*
 263+ * Perhaps these should move into the db as blob
 264+ */
 265+ $article = new Article(
 266+ Title::newFromText( "centralnotice-template-{$name}", NS_MEDIAWIKI )
 267+ );
 268+ $article->doEdit( $body, '' );
 269+ return;
198270 }
199271 }
200272
201273 function removeTemplate ( $templateName ) {
202 - global $wgOut;
 274+ global $wgOut, $egCentralNoticeTables;
203275
204276 if ( $templateName == '' ) {
205277 $wgOut->addHtml( wfMsg( 'centralnotice-template-doesnt-exist' ) );
@@ -206,28 +278,37 @@
207279 }
208280
209281 $templateId = $this->getTemplateId( $templateName );
210 - $centralnotice_table = 'central_notice_template_assignments';
211282 $dbr = wfGetDB( DB_SLAVE );
212 - $res = $dbr->select( $centralnotice_table, 'template_assignment_id',
213 - "template_id =" . $dbr->addQuotes( $templateId ) );
 283+ $res = $dbr->select( 'central_notice_template_assignments', 'template_assignment_id',
 284+ array( 'template_id' => $templateId ),
 285+ __METHOD__
 286+ );
 287+
214288 if ( $dbr->numRows( $res ) > 0 ) {
215289 $wgOut->addHtml( wfMsg( 'centralnotice-template-still-bound' ) );
216290 return;
217 - }
218 - else {
219 - $centralnotice_table = 'central_notice_templates';
 291+ } else {
220292 $dbw = wfGetDB( DB_MASTER );
221 - $res = $dbw->delete( $centralnotice_table, array( "template_id =" . $dbr->addQuotes($templateId)));
 293+ $res = $dbw->delete( 'central_notice_templates',
 294+ array( 'template_id' => $templateId ),
 295+ __METHOD__
 296+ );
222297 }
223298 }
224 -
 299+
225300 function getTemplateId ( $templateName ) {
226 - global $wgOut;
 301+ global $wgOut, $egCentralNoticeTables;
227302
228 - $centralnotice_table = 'central_notice_templates';
229303 $dbr = wfGetDB( DB_SLAVE );
230 - $res = $dbr->select( $centralnotice_table, 'template_id', array( "template_name =" . $dbr->addQuotes( $templateName )));
 304+ $res = $dbr->select( 'central_notice_templates', 'template_id',
 305+ array( 'template_name' => $templateName ),
 306+ __METHOD__
 307+ );
 308+
231309 $row = $dbr->fetchObject( $res );
232 - return $row->template_id;
 310+ if( $row ) {
 311+ return $row->template_id;
 312+ }
 313+ return null;
233314 }
234315 }

Status & tagging log