MediaWiki  1.23.8
orphans.php
Go to the documentation of this file.
1 <?php
31 require_once __DIR__ . '/Maintenance.php';
32 
39 class Orphans extends Maintenance {
40  public function __construct() {
41  parent::__construct();
42  $this->mDescription = "Look for 'orphan' revisions hooked to pages which don't exist\n" .
43  "and 'childless' pages with no revisions\n" .
44  "Then, kill the poor widows and orphans\n" .
45  "Man this is depressing";
46  $this->addOption( 'fix', 'Actually fix broken entries' );
47  }
48 
49  public function execute() {
50  $this->checkOrphans( $this->hasOption( 'fix' ) );
51  $this->checkSeparation( $this->hasOption( 'fix' ) );
52  # Does not work yet, do not use
53  # $this->checkWidows( $this->hasOption( 'fix' ) );
54  }
55 
61  private function lockTables( $db, $extraTable = array() ) {
62  $tbls = array( 'page', 'revision', 'redirect' );
63  if ( $extraTable ) {
64  $tbls = array_merge( $tbls, $extraTable );
65  }
66  $db->lockTables( array(), $tbls, __METHOD__, false );
67  }
68 
73  private function checkOrphans( $fix ) {
74  $dbw = wfGetDB( DB_MASTER );
75  $page = $dbw->tableName( 'page' );
76  $revision = $dbw->tableName( 'revision' );
77 
78  if ( $fix ) {
79  $this->lockTables( $dbw );
80  }
81 
82  $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
83  $result = $dbw->query( "
84  SELECT *
85  FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
86  WHERE page_id IS NULL
87  " );
88  $orphans = $result->numRows();
89  if ( $orphans > 0 ) {
91  $this->output( "$orphans orphan revisions...\n" );
92  $this->output( sprintf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' ) );
93  foreach ( $result as $row ) {
94  $comment = ( $row->rev_comment == '' )
95  ? ''
96  : '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
97  $this->output( sprintf( "%10d %10d %14s %20s %s\n",
98  $row->rev_id,
99  $row->rev_page,
100  $row->rev_timestamp,
101  $wgContLang->truncate( $row->rev_user_text, 17 ),
102  $comment ) );
103  if ( $fix ) {
104  $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
105  }
106  }
107  if ( !$fix ) {
108  $this->output( "Run again with --fix to remove these entries automatically.\n" );
109  }
110  } else {
111  $this->output( "No orphans! Yay!\n" );
112  }
113 
114  if ( $fix ) {
115  $dbw->unlockTables( __METHOD__ );
116  }
117  }
118 
125  private function checkWidows( $fix ) {
126  $dbw = wfGetDB( DB_MASTER );
127  $page = $dbw->tableName( 'page' );
128  $revision = $dbw->tableName( 'revision' );
129 
130  if ( $fix ) {
131  $this->lockTables( $dbw );
132  }
133 
134  $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
135  $result = $dbw->query( "
136  SELECT *
137  FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
138  WHERE rev_id IS NULL
139  " );
140  $widows = $result->numRows();
141  if ( $widows > 0 ) {
142  $this->output( "$widows childless pages...\n" );
143  $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
144  foreach ( $result as $row ) {
145  printf( "%10d %11d %2d %s\n",
146  $row->page_id,
147  $row->page_latest,
148  $row->page_namespace,
149  $row->page_title );
150  if ( $fix ) {
151  $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
152  }
153  }
154  if ( !$fix ) {
155  $this->output( "Run again with --fix to remove these entries automatically.\n" );
156  }
157  } else {
158  $this->output( "No childless pages! Yay!\n" );
159  }
160 
161  if ( $fix ) {
162  $dbw->unlockTables( __METHOD__ );
163  }
164  }
165 
170  private function checkSeparation( $fix ) {
171  $dbw = wfGetDB( DB_MASTER );
172  $page = $dbw->tableName( 'page' );
173  $revision = $dbw->tableName( 'revision' );
174 
175  if ( $fix ) {
176  $this->lockTables( $dbw, array( 'user', 'text' ) );
177  }
178 
179  $this->output( "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n" );
180  $result = $dbw->query( "
181  SELECT *
182  FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
183  " );
184  $found = 0;
185  foreach ( $result as $row ) {
186  $result2 = $dbw->query( "
187  SELECT MAX(rev_timestamp) as max_timestamp
188  FROM $revision
189  WHERE rev_page=$row->page_id
190  " );
191  $row2 = $dbw->fetchObject( $result2 );
192  if ( $row2 ) {
193  if ( $row->rev_timestamp != $row2->max_timestamp ) {
194  if ( $found == 0 ) {
195  $this->output( sprintf( "%10s %10s %14s %14s\n",
196  'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
197  }
198  ++$found;
199  $this->output( sprintf( "%10d %10d %14s %14s\n",
200  $row->page_id,
201  $row->page_latest,
202  $row->rev_timestamp,
203  $row2->max_timestamp ) );
204  if ( $fix ) {
205  # ...
206  $maxId = $dbw->selectField(
207  'revision',
208  'rev_id',
209  array(
210  'rev_page' => $row->page_id,
211  'rev_timestamp' => $row2->max_timestamp ) );
212  $this->output( "... updating to revision $maxId\n" );
213  $maxRev = Revision::newFromId( $maxId );
214  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
216  $article->updateRevisionOn( $dbw, $maxRev );
217  }
218  }
219  } else {
220  $this->output( "wtf\n" );
221  }
222  }
223 
224  if ( $found ) {
225  $this->output( "Found $found pages with incorrect latest revision.\n" );
226  } else {
227  $this->output( "No pages with incorrect latest revision. Yay!\n" );
228  }
229  if ( !$fix && $found > 0 ) {
230  $this->output( "Run again with --fix to remove these entries automatically.\n" );
231  }
232 
233  if ( $fix ) {
234  $dbw->unlockTables( __METHOD__ );
235  }
236  }
237 }
238 
239 $maintClass = "Orphans";
240 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
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
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
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:88
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
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
Orphans\checkWidows
checkWidows( $fix)
Definition: orphans.php:125
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
Orphans\lockTables
lockTables( $db, $extraTable=array())
Lock the appropriate tables for the script.
Definition: orphans.php:61
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
Orphans\__construct
__construct()
Default constructor.
Definition: orphans.php:40
Orphans\checkSeparation
checkSeparation( $fix)
Check for pages where page_latest is wrong.
Definition: orphans.php:170
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$comment
$comment
Definition: importImages.php:107
Orphans\checkOrphans
checkOrphans( $fix)
Check for orphan revisions.
Definition: orphans.php:73
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
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
Orphans
Maintenance script that looks for 'orphan' revisions hooked to pages which don't exist and 'childless...
Definition: orphans.php:39
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Orphans\execute
execute()
Do the actual work.
Definition: orphans.php:49
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
$article
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:78
$maintClass
$maintClass
Definition: orphans.php:239