Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
61 / 61 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| NukePage | |
100.00% |
61 / 61 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
50 / 50 |
|
100.00% |
1 / 1 |
8 | |||
| deleteRevisions | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Erase a page record from the database |
| 4 | * Irreversible (can't use standard undelete) and does not update link tables |
| 5 | * |
| 6 | * @license GPL-2.0-or-later |
| 7 | * @file |
| 8 | * @ingroup Maintenance |
| 9 | * @author Rob Church <robchur@gmail.com> |
| 10 | */ |
| 11 | |
| 12 | // @codeCoverageIgnoreStart |
| 13 | require_once __DIR__ . '/Maintenance.php'; |
| 14 | // @codeCoverageIgnoreEnd |
| 15 | |
| 16 | use MediaWiki\Deferred\SiteStatsUpdate; |
| 17 | use MediaWiki\Maintenance\Maintenance; |
| 18 | use MediaWiki\Title\Title; |
| 19 | |
| 20 | /** |
| 21 | * Maintenance script that erases a page record from the database. |
| 22 | * |
| 23 | * @ingroup Maintenance |
| 24 | */ |
| 25 | class NukePage extends Maintenance { |
| 26 | public function __construct() { |
| 27 | parent::__construct(); |
| 28 | $this->addDescription( 'Remove a page record from the database' ); |
| 29 | $this->addOption( 'delete', "Actually delete the page" ); |
| 30 | $this->addArg( 'title', 'Title to delete' ); |
| 31 | } |
| 32 | |
| 33 | public function execute() { |
| 34 | $name = $this->getArg( 0 ); |
| 35 | $delete = $this->hasOption( 'delete' ); |
| 36 | |
| 37 | $dbw = $this->getPrimaryDB(); |
| 38 | $this->beginTransaction( $dbw, __METHOD__ ); |
| 39 | |
| 40 | # Get page ID |
| 41 | $this->output( "Searching for \"$name\"..." ); |
| 42 | $title = Title::newFromText( $name ); |
| 43 | if ( $title && $title->exists() ) { |
| 44 | $id = $title->getArticleID(); |
| 45 | $real = $title->getPrefixedText(); |
| 46 | $isGoodArticle = $title->isContentPage(); |
| 47 | $this->output( "found \"$real\" with ID $id.\n" ); |
| 48 | |
| 49 | # Get corresponding revisions |
| 50 | $this->output( "Searching for revisions..." ); |
| 51 | |
| 52 | $revs = $dbw->newSelectQueryBuilder() |
| 53 | ->select( 'rev_id' ) |
| 54 | ->from( 'revision' ) |
| 55 | ->where( [ 'rev_page' => $id ] ) |
| 56 | ->caller( __METHOD__ )->fetchFieldValues(); |
| 57 | $count = count( $revs ); |
| 58 | $this->output( "found $count.\n" ); |
| 59 | |
| 60 | # Delete the page record and associated recent changes entries |
| 61 | if ( $delete ) { |
| 62 | $this->output( "Deleting page record..." ); |
| 63 | $dbw->newDeleteQueryBuilder() |
| 64 | ->deleteFrom( 'page' ) |
| 65 | ->where( [ 'page_id' => $id ] ) |
| 66 | ->caller( __METHOD__ )->execute(); |
| 67 | $this->output( "done.\n" ); |
| 68 | $this->output( "Cleaning up recent changes..." ); |
| 69 | $dbw->newDeleteQueryBuilder() |
| 70 | ->deleteFrom( 'recentchanges' ) |
| 71 | ->where( [ 'rc_cur_id' => $id ] ) |
| 72 | ->caller( __METHOD__ )->execute(); |
| 73 | $this->output( "done.\n" ); |
| 74 | } |
| 75 | |
| 76 | $this->commitTransaction( $dbw, __METHOD__ ); |
| 77 | |
| 78 | # Delete revisions as appropriate |
| 79 | if ( $delete && $count ) { |
| 80 | $this->output( "Deleting revisions..." ); |
| 81 | $this->deleteRevisions( $revs ); |
| 82 | $this->output( "done.\n" ); |
| 83 | $this->purgeRedundantText( true ); |
| 84 | } |
| 85 | |
| 86 | # Update stats as appropriate |
| 87 | if ( $delete ) { |
| 88 | $this->output( "Updating site stats..." ); |
| 89 | // if it was good, decrement that too |
| 90 | $ga = $isGoodArticle ? -1 : 0; |
| 91 | $stats = SiteStatsUpdate::factory( [ |
| 92 | 'edits' => -$count, |
| 93 | 'articles' => $ga, |
| 94 | 'pages' => -1 |
| 95 | ] ); |
| 96 | $stats->doUpdate(); |
| 97 | $this->output( "done.\n" ); |
| 98 | } |
| 99 | } else { |
| 100 | $this->output( "not found in database.\n" ); |
| 101 | $this->commitTransaction( $dbw, __METHOD__ ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public function deleteRevisions( array $ids ) { |
| 106 | $dbw = $this->getPrimaryDB(); |
| 107 | $this->beginTransaction( $dbw, __METHOD__ ); |
| 108 | |
| 109 | $dbw->newDeleteQueryBuilder() |
| 110 | ->deleteFrom( 'revision' ) |
| 111 | ->where( [ 'rev_id' => $ids ] ) |
| 112 | ->caller( __METHOD__ )->execute(); |
| 113 | |
| 114 | $this->commitTransaction( $dbw, __METHOD__ ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // @codeCoverageIgnoreStart |
| 119 | $maintClass = NukePage::class; |
| 120 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 121 | // @codeCoverageIgnoreEnd |