| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
require_once __DIR__ . '/Maintenance.php'; |
| 38 |
|
| 39 |
|
| 40 |
use MediaWiki\Title\Title; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class NukeNS extends Maintenance { |
| 49 |
public function __construct() { |
| 50 |
parent::__construct(); |
| 51 |
$this->addDescription( 'Remove pages with only 1 revision from any namespace' ); |
| 52 |
$this->addOption( 'delete', "Actually delete the page" ); |
| 53 |
$this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true ); |
| 54 |
$this->addOption( 'all', 'Delete everything regardless of revision count' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function execute() { |
| 58 |
$ns = $this->getOption( 'ns', NS_MEDIAWIKI ); |
| 59 |
$delete = $this->hasOption( 'delete' ); |
| 60 |
$all = $this->hasOption( 'all' ); |
| 61 |
$dbw = $this->getPrimaryDB(); |
| 62 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 63 |
|
| 64 |
$res = $dbw->newSelectQueryBuilder() |
| 65 |
->select( 'page_title' ) |
| 66 |
->from( 'page' ) |
| 67 |
->where( [ 'page_namespace' => $ns ] ) |
| 68 |
->caller( __METHOD__ )->fetchResultSet(); |
| 69 |
|
| 70 |
$n_deleted = 0; |
| 71 |
|
| 72 |
foreach ( $res as $row ) { |
| 73 |
|
| 74 |
$title = Title::makeTitle( $ns, $row->page_title ); |
| 75 |
$id = $title->getArticleID(); |
| 76 |
|
| 77 |
|
| 78 |
$revs = $dbw->newSelectQueryBuilder() |
| 79 |
->select( 'rev_id' ) |
| 80 |
->from( 'revision' ) |
| 81 |
->where( [ 'rev_page' => $id ] ) |
| 82 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 83 |
$count = count( $revs ); |
| 84 |
|
| 85 |
|
| 86 |
if ( $all || $count == 1 ) { |
| 87 |
|
| 88 |
$this->output( "delete: " . $title->getPrefixedText() . "\n" ); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if ( $delete ) { |
| 93 |
$dbw->newDeleteQueryBuilder() |
| 94 |
->deleteFrom( 'page' ) |
| 95 |
->where( [ 'page_id' => $id ] ) |
| 96 |
->caller( __METHOD__ )->execute(); |
| 97 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 98 |
|
| 99 |
|
| 100 |
$child = $this->runChild( NukePage::class, 'nukePage.php' ); |
| 101 |
'@phan-var NukePage $child'; |
| 102 |
$child->deleteRevisions( $revs ); |
| 103 |
$n_deleted++; |
| 104 |
} |
| 105 |
} else { |
| 106 |
$this->output( "skip: " . $title->getPrefixedText() . "\n" ); |
| 107 |
} |
| 108 |
} |
| 109 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 110 |
|
| 111 |
if ( $n_deleted > 0 ) { |
| 112 |
$this->purgeRedundantText( true ); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
$pages = $dbw->newSelectQueryBuilder() |
| 117 |
->select( 'ss_total_pages' ) |
| 118 |
->from( 'site_stats' ) |
| 119 |
->caller( __METHOD__ )->fetchField(); |
| 120 |
$pages -= $n_deleted; |
| 121 |
$dbw->newUpdateQueryBuilder() |
| 122 |
->update( 'site_stats' ) |
| 123 |
->set( [ 'ss_total_pages' => $pages ] ) |
| 124 |
->where( [ 'ss_row_id' => 1 ] ) |
| 125 |
->caller( __METHOD__ ) |
| 126 |
->execute(); |
| 127 |
} |
| 128 |
|
| 129 |
if ( !$delete ) { |
| 130 |
$this->output( "To update the database, run the script with the --delete option.\n" ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
$maintClass = NukeNS::class; |
| 137 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 138 |
|