Index: branches/CentralNotice-SpecialPage-Integration/SpecialNoticeTemplate.php |
— | — | @@ -1,204 +1,276 @@ |
2 | 2 | <?php |
3 | | - |
| 3 | + |
4 | 4 | if ( !defined( 'MEDIAWIKI' ) ) { |
5 | | - echo "CentralNotice extension\n"; |
6 | | - exit( 1 ); |
| 5 | + echo "CentralNotice extension\n"; |
| 6 | + exit( 1 ); |
7 | 7 | } |
8 | 8 | |
9 | 9 | 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' ); |
14 | 19 | } |
15 | | - |
| 20 | + |
16 | 21 | function execute( $sub ) { |
17 | 22 | global $wgOut, $wgUser, $wgRequest; |
18 | 23 | |
| 24 | + // Begin output |
19 | 25 | $this->setHeaders(); |
| 26 | + |
| 27 | + // Get current skin |
20 | 28 | $sk = $wgUser->getSkin(); |
21 | | - |
22 | | - if ( !$wgUser->isAllowed( 'centralnotice_admin_rights' )) { |
| 29 | + |
| 30 | + // Check permissions |
| 31 | + if ( !$wgUser->isAllowed( 'centralnotice_admin_rights' ) ) { |
23 | 32 | $wgOut->permissionRequired( 'centralnotice_admin_rights' ); |
24 | 33 | return; |
25 | 34 | } |
26 | 35 | |
27 | | - $wgOut->addWikiText( wfMsg( 'centralnotice-summary' )); |
| 36 | + // Show summary |
| 37 | + $wgOut->addWikiText( wfMsg( 'centralnotice-summary' ) ); |
| 38 | + |
| 39 | + // Show header |
28 | 40 | CentralNotice::printHeader(); |
29 | | - |
30 | | - |
| 41 | + |
| 42 | + // Handle forms |
31 | 43 | 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 |
37 | 55 | foreach ( $toRemove as $template ) { |
38 | 56 | $this->removeTemplate( $template ); |
39 | 57 | } |
| 58 | + |
| 59 | + // Show a list of templates |
40 | 60 | $this->listTemplates(); |
41 | 61 | return; |
42 | 62 | } |
43 | | - |
44 | | - $enabledNotices = $wgRequest->getArray('enabled'); |
| 63 | + |
| 64 | + // Build a list of notices to enable |
| 65 | + $enabledNotices = $wgRequest->getArray( 'enabled' ); |
45 | 66 | 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' ); |
52 | 73 | } |
53 | | - foreach ( $diff_set as $notice) { |
54 | | - $this->updateEnabled( $notice, 'N'); |
| 74 | + foreach ( $disabledNotices as $notice ) { |
| 75 | + $this->updateEnabled( $notice, 'N' ); |
55 | 76 | } |
56 | 77 | } |
57 | 78 | } |
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 | + ); |
65 | 86 | } |
| 87 | + |
| 88 | + // If this is a sub-page, show list of templates |
66 | 89 | if ( $sub == 'listTemplates' ) { |
67 | 90 | $this->listTemplates(); |
68 | 91 | return; |
69 | 92 | } |
70 | | - |
71 | | - $this->listTemplates(); |
| 93 | + |
| 94 | + $this->listTemplates(); |
72 | 95 | } |
73 | 96 | |
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 | + ); |
78 | 106 | } |
79 | 107 | |
80 | 108 | public static function previewTemplate() { |
| 109 | + // |
81 | 110 | } |
82 | 111 | |
83 | | - |
84 | 112 | function queryTemplates() { |
85 | | - $centralnotice_template_table = "central_notice_templates"; |
86 | 113 | $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 | + |
89 | 121 | $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 ); |
92 | 124 | } |
| 125 | + |
93 | 126 | return $templates; |
94 | 127 | } |
95 | | - |
| 128 | + |
96 | 129 | function listTemplates() { |
97 | 130 | $templates = $this->queryTemplates(); |
98 | 131 | return $this->templatesForm( $templates ); |
99 | 132 | } |
100 | | - |
| 133 | + |
101 | 134 | function templatesForm( $templates ) { |
102 | 135 | global $wgOut, $wgTitle; |
103 | 136 | |
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' ) ); |
112 | 148 | $templates = $this->queryTemplates(); |
113 | 149 | 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 | + ); |
120 | 158 | } |
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 ); |
148 | 197 | } |
149 | | - |
| 198 | + |
150 | 199 | 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 | + |
158 | 211 | 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 | + ); |
164 | 234 | } |
165 | | - |
166 | 235 | $wgOut->addHTML( $form ); |
167 | 236 | } |
| 237 | + |
| 238 | + function addTemplate ( $name, $body ) { |
| 239 | + global $wgOut, $egCentralNoticeTables; |
168 | 240 | |
169 | | - function addTemplate ( $templateName, $templateBody ) { |
170 | | - global $wgOut; |
171 | | - |
172 | | - if ( $templateBody == '' || $templateName == '' ) { |
| 241 | + if ( $body == '' || $name == '' ) { |
173 | 242 | $wgOut->addHtml( wfMsg( 'centralnotice-null-string' ) ); |
174 | 243 | return; |
175 | 244 | } |
176 | | - |
| 245 | + |
177 | 246 | $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 | + |
183 | 252 | if ( $dbr->numRows( $res ) > 0 ) { |
184 | | - $wgOut->addHTML( wfMsg( 'centralnotice-template-exists' ) ); |
| 253 | + $wgOut->addHTML( wfMsg( 'centralnotice-template-exists' ) ); |
185 | 254 | 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 | + ); |
197 | 261 | |
| 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; |
198 | 270 | } |
199 | 271 | } |
200 | 272 | |
201 | 273 | function removeTemplate ( $templateName ) { |
202 | | - global $wgOut; |
| 274 | + global $wgOut, $egCentralNoticeTables; |
203 | 275 | |
204 | 276 | if ( $templateName == '' ) { |
205 | 277 | $wgOut->addHtml( wfMsg( 'centralnotice-template-doesnt-exist' ) ); |
— | — | @@ -206,28 +278,37 @@ |
207 | 279 | } |
208 | 280 | |
209 | 281 | $templateId = $this->getTemplateId( $templateName ); |
210 | | - $centralnotice_table = 'central_notice_template_assignments'; |
211 | 282 | $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 | + |
214 | 288 | if ( $dbr->numRows( $res ) > 0 ) { |
215 | 289 | $wgOut->addHtml( wfMsg( 'centralnotice-template-still-bound' ) ); |
216 | 290 | return; |
217 | | - } |
218 | | - else { |
219 | | - $centralnotice_table = 'central_notice_templates'; |
| 291 | + } else { |
220 | 292 | $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 | + ); |
222 | 297 | } |
223 | 298 | } |
224 | | - |
| 299 | + |
225 | 300 | function getTemplateId ( $templateName ) { |
226 | | - global $wgOut; |
| 301 | + global $wgOut, $egCentralNoticeTables; |
227 | 302 | |
228 | | - $centralnotice_table = 'central_notice_templates'; |
229 | 303 | $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 | + |
231 | 309 | $row = $dbr->fetchObject( $res ); |
232 | | - return $row->template_id; |
| 310 | + if( $row ) { |
| 311 | + return $row->template_id; |
| 312 | + } |
| 313 | + return null; |
233 | 314 | } |
234 | 315 | } |