MediaWiki  1.23.2
nukeNS.php
Go to the documentation of this file.
1 <?php
36 require_once __DIR__ . '/Maintenance.php';
37 
44 class NukeNS extends Maintenance {
45  public function __construct() {
46  parent::__construct();
47  $this->mDescription = "Remove pages with only 1 revision from any namespace";
48  $this->addOption( 'delete', "Actually delete the page" );
49  $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
50  $this->addOption( 'all', 'Delete everything regardless of revision count' );
51  }
52 
53  public function execute() {
54  $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
55  $delete = $this->getOption( 'delete', false );
56  $all = $this->getOption( 'all', false );
57  $dbw = wfGetDB( DB_MASTER );
58  $dbw->begin( __METHOD__ );
59 
60  $tbl_pag = $dbw->tableName( 'page' );
61  $tbl_rev = $dbw->tableName( 'revision' );
62  $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
63 
64  $n_deleted = 0;
65 
66  foreach ( $res as $row ) {
67  // echo "$ns_name:".$row->page_title, "\n";
68  $title = Title::makeTitle( $ns, $row->page_title );
69  $id = $title->getArticleID();
70 
71  // Get corresponding revisions
72  $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
73  $revs = array();
74 
75  foreach ( $res2 as $row2 ) {
76  $revs[] = $row2->rev_id;
77  }
78  $count = count( $revs );
79 
80  // skip anything that looks modified (i.e. multiple revs)
81  if ( $all || $count == 1 ) {
82  # echo $title->getPrefixedText(), "\t", $count, "\n";
83  $this->output( "delete: " . $title->getPrefixedText() . "\n" );
84 
85  // as much as I hate to cut & paste this, it's a little different, and
86  // I already have the id & revs
87  if ( $delete ) {
88  $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
89  $dbw->commit( __METHOD__ );
90  // Delete revisions as appropriate
91  $child = $this->runChild( 'NukePage', 'nukePage.php' );
92  $child->deleteRevisions( $revs );
93  $this->purgeRedundantText( true );
94  $n_deleted ++;
95  }
96  } else {
97  $this->output( "skip: " . $title->getPrefixedText() . "\n" );
98  }
99  }
100  $dbw->commit( __METHOD__ );
101 
102  if ( $n_deleted > 0 ) {
103  # update statistics - better to decrement existing count, or just count
104  # the page table?
105  $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
106  $pages -= $n_deleted;
107  $dbw->update(
108  'site_stats',
109  array( 'ss_total_pages' => $pages ),
110  array( 'ss_row_id' => 1 ),
111  __METHOD__
112  );
113  }
114 
115  if ( !$delete ) {
116  $this->output( "To update the database, run the script with the --delete option.\n" );
117  }
118  }
119 }
120 
121 $maintClass = "NukeNS";
122 require_once RUN_MAINTENANCE_IF_MAIN;
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$maintClass
$maintClass
Definition: nukeNS.php:121
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
Maintenance\runChild
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
Definition: Maintenance.php:444
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
NukeNS\execute
execute()
Do the actual work.
Definition: nukeNS.php:53
NukeNS\__construct
__construct()
Default constructor.
Definition: nukeNS.php:45
Maintenance\purgeRedundantText
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
Definition: Maintenance.php:943
$count
$count
Definition: UtfNormalTest2.php:96
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:87
$res
$res
Definition: database.txt:21
NukeNS
Maintenance script that removes pages with only one revision from the MediaWiki namespace.
Definition: nukeNS.php:44