stwalkerster /
waca
| 1 | <?php |
||
| 2 | /****************************************************************************** |
||
| 3 | * Wikipedia Account Creation Assistance tool * |
||
| 4 | * * |
||
| 5 | * All code in this file is released into the public domain by the ACC * |
||
| 6 | * Development Team. Please see team.json for a list of contributors. * |
||
| 7 | ******************************************************************************/ |
||
| 8 | |||
| 9 | namespace Waca\Pages; |
||
| 10 | |||
| 11 | use Exception; |
||
| 12 | use Waca\DataObjects\User; |
||
| 13 | use Waca\DataObjects\WelcomeTemplate; |
||
| 14 | use Waca\Exceptions\ApplicationLogicException; |
||
| 15 | use Waca\Helpers\Logger; |
||
| 16 | use Waca\Helpers\MediaWikiHelper; |
||
| 17 | use Waca\Helpers\OAuthUserHelper; |
||
| 18 | use Waca\SessionAlert; |
||
| 19 | use Waca\Tasks\InternalPageBase; |
||
| 20 | use Waca\WebRequest; |
||
| 21 | |||
| 22 | class PageWelcomeTemplateManagement extends InternalPageBase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Main function for this page, when no specific actions are called. |
||
| 26 | * @return void |
||
| 27 | */ |
||
| 28 | protected function main() |
||
| 29 | { |
||
| 30 | $templateList = WelcomeTemplate::getAll($this->getDatabase()); |
||
| 31 | |||
| 32 | $this->setHtmlTitle('Welcome Templates'); |
||
| 33 | |||
| 34 | $this->assignCSRFToken(); |
||
| 35 | |||
| 36 | $user = User::getCurrent($this->getDatabase()); |
||
| 37 | $this->assign('canEdit', $this->barrierTest('edit', $user)); |
||
| 38 | $this->assign('canAdd', $this->barrierTest('add', $user)); |
||
| 39 | $this->assign('canSelect', $this->barrierTest('select', $user)); |
||
| 40 | |||
| 41 | $this->assign('templateList', $templateList); |
||
| 42 | $this->setTemplate('welcome-template/list.tpl'); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Handles the requests for selecting a template to use. |
||
| 47 | * |
||
| 48 | * @throws ApplicationLogicException |
||
| 49 | */ |
||
| 50 | protected function select() |
||
| 51 | { |
||
| 52 | // get rid of GETs |
||
| 53 | if (!WebRequest::wasPosted()) { |
||
| 54 | $this->redirect('welcomeTemplates'); |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->validateCSRFToken(); |
||
| 58 | |||
| 59 | $user = User::getCurrent($this->getDatabase()); |
||
| 60 | |||
| 61 | if (WebRequest::postBoolean('disable')) { |
||
| 62 | $user->setWelcomeTemplate(null); |
||
| 63 | $user->save(); |
||
| 64 | |||
| 65 | SessionAlert::success('Disabled automatic user welcoming.'); |
||
| 66 | $this->redirect('welcomeTemplates'); |
||
| 67 | |||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | $database = $this->getDatabase(); |
||
| 72 | |||
| 73 | $templateId = WebRequest::postInt('template'); |
||
| 74 | /** @var false|WelcomeTemplate $template */ |
||
| 75 | $template = WelcomeTemplate::getById($templateId, $database); |
||
| 76 | |||
| 77 | if ($template === false || $template->isDeleted()) { |
||
| 78 | throw new ApplicationLogicException('Unknown template'); |
||
| 79 | } |
||
| 80 | |||
| 81 | $user->setWelcomeTemplate($template->getId()); |
||
| 82 | $user->save(); |
||
| 83 | |||
| 84 | SessionAlert::success("Updated selected welcome template for automatic welcoming."); |
||
| 85 | |||
| 86 | $this->redirect('welcomeTemplates'); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Handles the requests for viewing a template. |
||
| 91 | * |
||
| 92 | * @throws ApplicationLogicException |
||
| 93 | */ |
||
| 94 | protected function view() |
||
| 95 | { |
||
| 96 | $this->setHtmlTitle('View Welcome Template'); |
||
| 97 | |||
| 98 | $database = $this->getDatabase(); |
||
| 99 | |||
| 100 | $templateId = WebRequest::getInt('template'); |
||
| 101 | |||
| 102 | /** @var false|WelcomeTemplate $template */ |
||
| 103 | $template = WelcomeTemplate::getById($templateId, $database); |
||
| 104 | |||
| 105 | if ($template === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 106 | throw new ApplicationLogicException('Cannot find requested template'); |
||
| 107 | } |
||
| 108 | |||
| 109 | $currentUser = User::getCurrent($database); |
||
| 110 | |||
| 111 | // This includes a section header, because we use the "new section" API call. |
||
| 112 | $wikiText = "== " . $template->getSectionHeader() . "==\n" . $template->getBotCodeForWikiSave('Example User', $currentUser->getOnWikiName()); |
||
| 113 | |||
| 114 | $oauth = new OAuthUserHelper($currentUser, $database, $this->getOauthProtocolHelper(), |
||
| 115 | $this->getSiteConfiguration()); |
||
| 116 | $mediaWikiHelper = new MediaWikiHelper($oauth, $this->getSiteConfiguration()); |
||
| 117 | |||
| 118 | $templateHtml = $mediaWikiHelper->getHtmlForWikiText($wikiText); |
||
| 119 | |||
| 120 | // Add site to relevant links, since the MediaWiki parser returns, eg, `/wiki/Help:Introduction` |
||
| 121 | // and we want to link to <https://en.wikipedia.org/wiki/Help:Introduction> rather than |
||
| 122 | // <https://accounts.wmflabs.org/wiki/Help:Introduction> |
||
| 123 | // The code currently assumes that the template was parsed for enwiki, and will need to be |
||
| 124 | // updated once other wikis are supported. |
||
| 125 | $templateHtml = preg_replace('/(<a href=")(\/wiki\/)/', '$1//en.wikipedia.org$2', $templateHtml); |
||
| 126 | |||
| 127 | $this->assign('templateHtml', $templateHtml); |
||
| 128 | $this->assign('template', $template); |
||
| 129 | $this->setTemplate('welcome-template/view.tpl'); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Handler for the add action to create a new welcome template |
||
| 134 | * |
||
| 135 | * @throws Exception |
||
| 136 | */ |
||
| 137 | protected function add() |
||
| 138 | { |
||
| 139 | $this->assign('createmode', true); |
||
| 140 | |||
| 141 | if (WebRequest::wasPosted()) { |
||
| 142 | $this->validateCSRFToken(); |
||
| 143 | $database = $this->getDatabase(); |
||
| 144 | |||
| 145 | $userCode = WebRequest::postString('usercode'); |
||
| 146 | $botCode = WebRequest::postString('botcode'); |
||
| 147 | |||
| 148 | $this->validate($userCode, $botCode); |
||
| 149 | |||
| 150 | $template = new WelcomeTemplate(); |
||
| 151 | $template->setDatabase($database); |
||
| 152 | $template->setUserCode($userCode); |
||
| 153 | $template->setBotCode($botCode); |
||
| 154 | $template->save(); |
||
| 155 | |||
| 156 | Logger::welcomeTemplateCreated($database, $template); |
||
| 157 | |||
| 158 | $this->getNotificationHelper()->welcomeTemplateCreated($template); |
||
| 159 | |||
| 160 | SessionAlert::success("Template successfully created."); |
||
| 161 | |||
| 162 | $this->redirect('welcomeTemplates'); |
||
| 163 | } |
||
| 164 | else { |
||
| 165 | $this->assignCSRFToken(); |
||
| 166 | $this->assign('template', new WelcomeTemplate()); |
||
| 167 | $this->setTemplate("welcome-template/edit.tpl"); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Handler for editing templates |
||
| 173 | */ |
||
| 174 | protected function edit() |
||
| 175 | { |
||
| 176 | $database = $this->getDatabase(); |
||
| 177 | |||
| 178 | $templateId = WebRequest::getInt('template'); |
||
| 179 | |||
| 180 | /** @var false|WelcomeTemplate $template */ |
||
| 181 | $template = WelcomeTemplate::getById($templateId, $database); |
||
| 182 | |||
| 183 | if ($template === false) { |
||
|
0 ignored issues
–
show
|
|||
| 184 | throw new ApplicationLogicException('Cannot find requested template'); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($template->isDeleted()) { |
||
| 188 | throw new ApplicationLogicException('The specified template has been deleted'); |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->assign('createmode', false); |
||
| 192 | |||
| 193 | if (WebRequest::wasPosted()) { |
||
| 194 | $this->validateCSRFToken(); |
||
| 195 | |||
| 196 | $userCode = WebRequest::postString('usercode'); |
||
| 197 | $botCode = WebRequest::postString('botcode'); |
||
| 198 | |||
| 199 | $this->validate($userCode, $botCode); |
||
| 200 | |||
| 201 | $template->setUserCode($userCode); |
||
| 202 | $template->setBotCode($botCode); |
||
| 203 | $template->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 204 | $template->save(); |
||
| 205 | |||
| 206 | Logger::welcomeTemplateEdited($database, $template); |
||
| 207 | |||
| 208 | SessionAlert::success("Template updated."); |
||
| 209 | |||
| 210 | $this->getNotificationHelper()->welcomeTemplateEdited($template); |
||
| 211 | |||
| 212 | $this->redirect('welcomeTemplates'); |
||
| 213 | } |
||
| 214 | else { |
||
| 215 | $this->assignCSRFToken(); |
||
| 216 | $this->assign('template', $template); |
||
| 217 | $this->setTemplate('welcome-template/edit.tpl'); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function delete() |
||
| 222 | { |
||
| 223 | $this->redirect('welcomeTemplates'); |
||
| 224 | |||
| 225 | if (!WebRequest::wasPosted()) { |
||
| 226 | return; |
||
| 227 | } |
||
| 228 | |||
| 229 | $this->validateCSRFToken(); |
||
| 230 | |||
| 231 | $database = $this->getDatabase(); |
||
| 232 | |||
| 233 | $templateId = WebRequest::postInt('template'); |
||
| 234 | $updateVersion = WebRequest::postInt('updateversion'); |
||
| 235 | |||
| 236 | /** @var false|WelcomeTemplate $template */ |
||
| 237 | $template = WelcomeTemplate::getById($templateId, $database); |
||
| 238 | |||
| 239 | if ($template === false || $template->isDeleted()) { |
||
| 240 | throw new ApplicationLogicException('Cannot find requested template'); |
||
| 241 | } |
||
| 242 | |||
| 243 | // set the update version to the version sent by the client (optimisticly lock from initial page load) |
||
| 244 | $template->setUpdateVersion($updateVersion); |
||
| 245 | |||
| 246 | $database |
||
| 247 | ->prepare("UPDATE user SET welcome_template = NULL WHERE welcome_template = :id;") |
||
| 248 | ->execute(array(":id" => $templateId)); |
||
| 249 | |||
| 250 | Logger::welcomeTemplateDeleted($database, $template); |
||
| 251 | |||
| 252 | $template->delete(); |
||
| 253 | |||
| 254 | SessionAlert::success( |
||
| 255 | "Template deleted. Any users who were using this template have had automatic welcoming disabled."); |
||
| 256 | $this->getNotificationHelper()->welcomeTemplateDeleted($templateId); |
||
| 257 | } |
||
| 258 | |||
| 259 | private function validate($userCode, $botCode) |
||
| 260 | { |
||
| 261 | if ($userCode === null) { |
||
| 262 | throw new ApplicationLogicException('User code cannot be null'); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($botCode === null) { |
||
| 266 | throw new ApplicationLogicException('Bot code cannot be null'); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 |