00001 <?php
00002
00036 require_once( dirname(__FILE__) . '/Maintenance.php' );
00037
00038 class NukeNS extends Maintenance {
00039 public function __construct() {
00040 parent::__construct();
00041 $this->mDescription = "Remove pages with only 1 revision from any namespace";
00042 $this->addOption( 'delete', "Actually delete the page" );
00043 $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
00044 }
00045
00046 public function execute() {
00047 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
00048 $delete = $this->getOption( 'delete', false );
00049 $dbw = wfGetDB( DB_MASTER );
00050 $dbw->begin();
00051
00052 $tbl_pag = $dbw->tableName( 'page' );
00053 $tbl_rev = $dbw->tableName( 'revision' );
00054 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
00055
00056 $n_deleted = 0;
00057
00058 foreach( $res as $row ) {
00059
00060 $title = Title::makeTitle( $ns, $row->page_title );
00061 $id = $title->getArticleID();
00062
00063
00064 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
00065 $revs = array();
00066
00067 foreach( $res2 as $row2 ) {
00068 $revs[] = $row2->rev_id;
00069 }
00070 $count = count( $revs );
00071
00072
00073 if ( $count == 1 ) {
00074 #echo $title->getPrefixedText(), "\t", $count, "\n";
00075 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
00076
00077
00078
00079 if( $delete ) {
00080 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
00081 $dbw->commit();
00082
00083 $child = $this->runChild( 'NukePage', 'NukePage.php' );
00084 $child->deleteRevisions( $revs );
00085 $this->purgeRedundantText( true );
00086 $n_deleted ++;
00087 }
00088 } else {
00089 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
00090 }
00091 }
00092 $dbw->commit();
00093
00094 if ( $n_deleted > 0 ) {
00095 #update statistics - better to decrement existing count, or just count
00096 #the page table?
00097 $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
00098 $pages -= $n_deleted;
00099 $dbw->update(
00100 'site_stats',
00101 array( 'ss_total_pages' => $pages ),
00102 array( 'ss_row_id' => 1 ),
00103 __METHOD__
00104 );
00105 }
00106
00107 if ( !$delete ) {
00108 $this->output( "To update the database, run the script with the --delete option.\n" );
00109 }
00110 }
00111 }
00112
00113 $maintClass = "NukeNS";
00114 require_once( DO_MAINTENANCE );