MediaWiki  REL1_19
nukeNS.php
Go to the documentation of this file.
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                 $this->addOption( 'all', 'Delete everything regardless of revision count' );
00045         }
00046 
00047         public function execute() {
00048                 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
00049                 $delete = $this->getOption( 'delete', false );
00050                 $all = $this->getOption( 'all', false );
00051                 $dbw = wfGetDB( DB_MASTER );
00052                 $dbw->begin();
00053 
00054                 $tbl_pag = $dbw->tableName( 'page' );
00055                 $tbl_rev = $dbw->tableName( 'revision' );
00056                 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
00057 
00058                 $n_deleted = 0;
00059 
00060                 foreach ( $res as $row ) {
00061                         // echo "$ns_name:".$row->page_title, "\n";
00062                         $title = Title::makeTitle( $ns, $row->page_title );
00063                         $id   = $title->getArticleID();
00064 
00065                         // Get corresponding revisions
00066                         $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
00067                         $revs = array();
00068 
00069                         foreach ( $res2 as $row2 ) {
00070                                 $revs[] = $row2->rev_id;
00071                         }
00072                         $count = count( $revs );
00073 
00074                         // skip anything that looks modified (i.e. multiple revs)
00075                         if ( $all || $count == 1 ) {
00076                                 # echo $title->getPrefixedText(), "\t", $count, "\n";
00077                                 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
00078 
00079                                 // as much as I hate to cut & paste this, it's a little different, and
00080                                 // I already have the id & revs
00081                                 if ( $delete ) {
00082                                         $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
00083                                         $dbw->commit();
00084                                         // Delete revisions as appropriate
00085                                         $child = $this->runChild( 'NukePage', 'nukePage.php' );
00086                                         $child->deleteRevisions( $revs );
00087                                         $this->purgeRedundantText( true );
00088                                         $n_deleted ++;
00089                                 }
00090                         } else {
00091                           $this->output( "skip: " . $title->getPrefixedText() . "\n" );
00092                         }
00093                 }
00094                 $dbw->commit();
00095 
00096                 if ( $n_deleted > 0 ) {
00097                         # update statistics - better to decrement existing count, or just count
00098                         # the page table?
00099                         $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
00100                         $pages -= $n_deleted;
00101                         $dbw->update(
00102                                 'site_stats',
00103                                 array( 'ss_total_pages' => $pages ),
00104                                 array( 'ss_row_id' => 1 ),
00105                                 __METHOD__
00106                         );
00107                 }
00108 
00109                 if ( !$delete ) {
00110                         $this->output( "To update the database, run the script with the --delete option.\n" );
00111                 }
00112         }
00113 }
00114 
00115 $maintClass = "NukeNS";
00116 require_once( RUN_MAINTENANCE_IF_MAIN );