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