00001 <?php
00016 class SpecialPageTranslationDeletePage extends UnlistedSpecialPage {
00017
00018 protected $text;
00019
00023 protected $title;
00024
00025
00027 protected $subaction;
00028
00030 protected $reason;
00031
00033 protected $doSubpages = false;
00034
00038 protected $page;
00039
00041 protected $code;
00042
00043 protected $sectionPages;
00044
00045 protected $translationPages;
00046
00047 public function __construct() {
00048 parent::__construct( 'PageTranslationDeletePage' );
00049 }
00050
00051 public function execute( $par ) {
00052 $request = $this->getRequest();
00053
00054
00055 $this->text = $request->getVal( 'wpTitle', $par );
00056 $this->title = Title::newFromText( $this->text );
00057 $this->reason = $request->getText( 'reason' );
00058
00059 $this->doSubpages = $request->getBool( 'subpages', !$request->wasPosted() );
00060
00061 $user = $this->getUser();
00062
00063 if ( $this->doBasicChecks() !== true ) {
00064 return;
00065 }
00066
00067 $out = $this->getOutput();
00068
00069
00070 if ( TranslatablePage::isSourcePage( $this->title ) ) {
00071 $title = $this->msg( 'pt-deletepage-full-title', $this->title->getPrefixedText() );
00072 $out->setPagetitle( $title );
00073
00074 $this->code = '';
00075 $this->page = TranslatablePage::newFromTitle( $this->title );
00076 } else {
00077 $page = TranslatablePage::isTranslationPage( $this->title );
00078 if ( $page ) {
00079 $title = $this->msg( 'pt-deletepage-lang-title', $this->title->getPrefixedText() );
00080 $out->setPagetitle( $title );
00081
00082 list( , $this->code ) = TranslateUtils::figureMessage( $this->title->getText() );
00083 $this->page = $page;
00084 } else {
00085 throw new ErrorPageError(
00086 'pt-deletepage-invalid-title',
00087 'pt-deletepage-invalid-text'
00088 );
00089 }
00090 }
00091
00092 if ( !$user->isAllowed( 'pagetranslation' ) ) {
00093 throw new PermissionsError( 'pagetranslation' );
00094 }
00095
00096
00097 $subactionText = $request->getText( 'subaction' );
00098 switch ( $subactionText ) {
00099 case $this->msg( 'pt-deletepage-action-check' )->text():
00100 $subaction = 'check';
00101 break;
00102 case $this->msg( 'pt-deletepage-action-perform' )->text():
00103 $subaction = 'perform';
00104 break;
00105 case $this->msg( 'pt-deletepage-action-other' )->text():
00106 $subaction = '';
00107 break;
00108 default:
00109 $subaction = '';
00110 }
00111
00112 if ( $subaction === 'check' && $this->checkToken() && $request->wasPosted() ) {
00113 $this->showConfirmation();
00114 } elseif ( $subaction === 'perform' && $this->checkToken() && $request->wasPosted() ) {
00115 $this->performAction();
00116 } else {
00117 $this->showForm();
00118 }
00119 }
00120
00127 protected function doBasicChecks() {
00128 # Check for database lock
00129 if ( wfReadOnly() ) {
00130 throw new ReadOnlyError;
00131 }
00132
00133 if ( $this->title === null ) {
00134 throw new ErrorPageError( 'notargettitle', 'notargettext' );
00135 }
00136
00137 if ( !$this->title->exists() ) {
00138 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
00139 }
00140
00141 # Check rights
00142 $permErrors = $this->title->getUserPermissionsErrors( 'delete', $this->getUser() );
00143 if ( !empty( $permErrors ) ) {
00144 throw new PermissionsError( 'delete', $permErrors );
00145 }
00146
00147
00148 return true;
00149 }
00150
00156 protected function checkToken() {
00157 return $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'wpEditToken' ) );
00158 }
00159
00163 protected function showForm() {
00164 $this->getOutput()->addWikiMsg( 'pt-deletepage-intro' );
00165
00166 $subaction = array( 'name' => 'subaction' );
00167 $formParams = array(
00168 'method' => 'post',
00169 'action' => $this->getTitle( $this->text )->getLocalURL()
00170 );
00171
00172 $form = array();
00173 $form[] = Xml::fieldset( $this->msg( 'pt-deletepage-any-legend' )->text() );
00174 $form[] = Html::openElement( 'form', $formParams );
00175 $form[] = Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
00176 $this->addInputLabel(
00177 $form,
00178 $this->msg( 'pt-deletepage-current' )->text(),
00179 'wpTitle',
00180 30,
00181 $this->text
00182 );
00183 $this->addInputLabel(
00184 $form,
00185 $this->msg( 'pt-deletepage-reason' )->text(),
00186 'reason',
00187 60,
00188 $this->reason
00189 );
00190 $form[] = Xml::submitButton(
00191 $this->msg( 'pt-deletepage-action-check' )->text(),
00192 $subaction
00193 );
00194 $form[] = Xml::closeElement( 'form' );
00195 $form[] = Xml::closeElement( 'fieldset' );
00196 $this->getOutput()->addHTML( implode( "\n", $form ) );
00197 }
00198
00210 protected function addInputLabel( &$form, $label, $name, $size = false, $text = false,
00211 $attribs = array()
00212 ) {
00213 $br = Html::element( 'br' );
00214 list( $label, $input ) = Xml::inputLabelSep( $label, $name, $name, $size, $text, $attribs );
00215 $form[] = $label . $br;
00216 $form[] = $input . $br;
00217 }
00218
00223 protected function showConfirmation() {
00224 $out = $this->getOutput();
00225
00226 $out->addWikiMsg( 'pt-deletepage-intro' );
00227
00228 $count = 1;
00229
00230 $out->wrapWikiMsg( '== $1 ==', 'pt-deletepage-list-pages' );
00231 if ( !$this->singleLanguage() ) {
00232 $this->printChangeLine( $this->title );
00233 }
00234
00235 $out->wrapWikiMsg( '=== $1 ===', 'pt-deletepage-list-translation' );
00236 $translationPages = $this->getTranslationPages();
00237 foreach ( $translationPages as $old ) {
00238 $count++;
00239 $this->printChangeLine( $old );
00240 }
00241
00242 $out->wrapWikiMsg( '=== $1 ===', 'pt-deletepage-list-section' );
00243 $sectionPages = $this->getSectionPages();
00244 foreach ( $sectionPages as $old ) {
00245 $count++;
00246 $this->printChangeLine( $old );
00247 }
00248
00249 $out->wrapWikiMsg( '=== $1 ===', 'pt-deletepage-list-other' );
00250 $subpages = $this->getSubpages();
00251 foreach ( $subpages as $old ) {
00252 if ( TranslatablePage::isTranslationPage( $old ) ) {
00253 continue;
00254 }
00255
00256 if ( $this->doSubpages ) {
00257 $count++;
00258 }
00259
00260 $this->printChangeLine( $old, $this->doSubpages );
00261 }
00262
00263 $out->addWikiText( "----\n" );
00264 $out->addWikiMsg( 'pt-deletepage-list-count', $this->getLanguage()->formatNum( $count ) );
00265
00266 $br = Html::element( 'br' );
00267 $readonly = array( 'readonly' => 'readonly' );
00268
00269 $subaction = array( 'name' => 'subaction' );
00270 $formParams = array(
00271 'method' => 'post',
00272 'action' => $this->getTitle( $this->text )->getLocalURL()
00273 );
00274
00275 $form = array();
00276 if ( $this->singleLanguage() ) {
00277 $form[] = Xml::fieldset( $this->msg( 'pt-deletepage-lang-legend' )->text() );
00278 } else {
00279 $form[] = Xml::fieldset( $this->msg( 'pt-deletepage-full-legend' )->text() );
00280 }
00281 $form[] = Html::openElement( 'form', $formParams );
00282 $form[] = Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
00283 $this->addInputLabel(
00284 $form,
00285 $this->msg( 'pt-deletepage-current' )->text(),
00286 'wpTitle',
00287 30,
00288 $this->text, $readonly );
00289 $this->addInputLabel(
00290 $form,
00291 $this->msg( 'pt-deletepage-reason' )->text(),
00292 'reason',
00293 60,
00294 $this->reason );
00295 $form[] = Xml::checkLabel(
00296 $this->msg( 'pt-deletepage-subpages' )->text(),
00297 'subpages',
00298 'mw-subpages',
00299 $this->doSubpages,
00300 $readonly
00301 ) . $br;
00302 $form[] = Xml::submitButton(
00303 $this->msg( 'pt-deletepage-action-perform' )->text(),
00304 $subaction
00305 );
00306 $form[] = Xml::submitButton(
00307 $this->msg( 'pt-deletepage-action-other' )->text(),
00308 $subaction
00309 );
00310 $form[] = Xml::closeElement( 'form' );
00311 $form[] = Xml::closeElement( 'fieldset' );
00312 $out->addHTML( implode( "\n", $form ) );
00313 }
00314
00319 protected function printChangeLine( $title, $enabled = true ) {
00320 if ( $enabled ) {
00321 $this->getOutput()->addWikiText( '* ' . $title->getPrefixedText() );
00322 } else {
00323 $this->getOutput()->addWikiText( '* <s>' . $title->getPrefixedText() . '</s>' );
00324 }
00325 }
00326
00327 protected function performAction() {
00328 $jobs = array();
00329 $target = $this->title;
00330 $base = $this->title->getPrefixedText();
00331
00332 $translationPages = $this->getTranslationPages();
00333 $user = $this->getUser();
00334 foreach ( $translationPages as $old ) {
00335 $jobs[$old->getPrefixedText()] = TranslateDeleteJob::newJob(
00336 $old,
00337 $base,
00338 !$this->singleLanguage(),
00339 $user
00340 );
00341 }
00342
00343 $sectionPages = $this->getSectionPages();
00344 foreach ( $sectionPages as $old ) {
00345 $jobs[$old->getPrefixedText()] = TranslateDeleteJob::newJob(
00346 $old,
00347 $base,
00348 !$this->singleLanguage(),
00349 $user
00350 );
00351 }
00352
00353 if ( !$this->doSubpages ) {
00354 $subpages = $this->getSubpages();
00355 foreach ( $subpages as $old ) {
00356 if ( TranslatablePage::isTranslationPage( $old ) ) {
00357 continue;
00358 }
00359
00360 $jobs[$old->getPrefixedText()] = TranslateDeleteJob::newJob(
00361 $old,
00362 $base,
00363 !$this->singleLanguage(),
00364 $user
00365 );
00366 }
00367 }
00368
00369 Job::batchInsert( $jobs );
00370
00371 $cache = wfGetCache( CACHE_DB );
00372 $cache->set(
00373 wfMemcKey( 'pt-base', $target->getPrefixedText() ),
00374 array_keys( $jobs ),
00375 60 * 60 * 6
00376 );
00377
00378 if ( !$this->singleLanguage() ) {
00379 $this->page->unmarkTranslatablePage();
00380 }
00381
00382 $this->clearMetadata();
00383 MessageGroups::clearCache();
00384 MessageIndexRebuildJob::newJob()->insert();
00385
00386 $this->getOutput()->addWikiMsg( 'pt-deletepage-started' );
00387 }
00388
00389 protected function clearMetadata() {
00390
00391 $groupId = $this->page->getMessageGroupId();
00392 TranslateMetadata::set( $groupId, 'prioritylangs', false );
00393 TranslateMetadata::set( $groupId, 'priorityforce', false );
00394 TranslateMetadata::set( $groupId, 'priorityreason', false );
00395
00396 $groups = MessageGroups::getAllGroups();
00397 foreach ( $groups as $group ) {
00398 if ( $group instanceof AggregateMessageGroup ) {
00399 $subgroups = TranslateMetadata::get( $group->getId(), 'subgroups' );
00400 if ( $subgroups !== false ) {
00401 $subgroups = explode( ',', $subgroups );
00402 $subgroups = array_flip( $subgroups );
00403 if ( isset( $subgroups[$groupId] ) ) {
00404 unset( $subgroups[$groupId] );
00405 $subgroups = array_flip( $subgroups );
00406 TranslateMetadata::set(
00407 $group->getId(),
00408 'subgroups',
00409 implode( ',', $subgroups )
00410 );
00411 }
00412 }
00413 }
00414 }
00415 }
00416
00421 protected function getSectionPages() {
00422 $code = $this->singleLanguage() ? $this->code : false;
00423
00424 return $this->page->getTranslationUnitPages( 'all', $code );
00425 }
00426
00431 protected function getTranslationPages() {
00432 if ( $this->singleLanguage() ) {
00433 return array( $this->title );
00434 }
00435
00436 if ( !isset( $this->translationPages ) ) {
00437 $this->translationPages = $this->page->getTranslationPages();
00438 }
00439
00440 return $this->translationPages;
00441 }
00442
00447 protected function getSubpages() {
00448 return $this->title->getSubpages();
00449 }
00450
00454 protected function singleLanguage() {
00455 return $this->code !== '';
00456 }
00457 }