orphans.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Look for 'orphan' revisions hooked to pages which don't exist and
  4. * 'childless' pages with no revisions.
  5. * Then, kill the poor widows and orphans.
  6. * Man this is depressing.
  7. *
  8. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  9. * https://www.mediawiki.org/
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. * http://www.gnu.org/copyleft/gpl.html
  25. *
  26. * @file
  27. * @author <brion@pobox.com>
  28. * @ingroup Maintenance
  29. */
  30. require_once __DIR__ . '/Maintenance.php';
  31. use MediaWiki\MediaWikiServices;
  32. use Wikimedia\Rdbms\IMaintainableDatabase;
  33. /**
  34. * Maintenance script that looks for 'orphan' revisions hooked to pages which
  35. * don't exist and 'childless' pages with no revisions.
  36. *
  37. * @ingroup Maintenance
  38. */
  39. class Orphans extends Maintenance {
  40. public function __construct() {
  41. parent::__construct();
  42. $this->addDescription( "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. );
  47. $this->addOption( 'fix', 'Actually fix broken entries' );
  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. /**
  56. * Lock the appropriate tables for the script
  57. * @param IMaintainableDatabase $db
  58. * @param string[] $extraTable The name of any extra tables to lock (eg: text)
  59. */
  60. private function lockTables( $db, $extraTable = [] ) {
  61. $tbls = [ 'page', 'revision', 'redirect' ];
  62. if ( $extraTable ) {
  63. $tbls = array_merge( $tbls, $extraTable );
  64. }
  65. $db->lockTables( [], $tbls, __METHOD__, false );
  66. }
  67. /**
  68. * Check for orphan revisions
  69. * @param bool $fix Whether to fix broken revisions when found
  70. */
  71. private function checkOrphans( $fix ) {
  72. $dbw = $this->getDB( DB_MASTER );
  73. $commentStore = CommentStore::getStore();
  74. if ( $fix ) {
  75. $this->lockTables( $dbw );
  76. }
  77. $commentQuery = $commentStore->getJoin( 'rev_comment' );
  78. $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
  79. $this->output( "Checking for orphan revision table entries... "
  80. . "(this may take a while on a large wiki)\n" );
  81. $result = $dbw->select(
  82. [ 'revision', 'page' ] + $commentQuery['tables'] + $actorQuery['tables'],
  83. [ 'rev_id', 'rev_page', 'rev_timestamp' ] + $commentQuery['fields'] + $actorQuery['fields'],
  84. [ 'page_id' => null ],
  85. __METHOD__,
  86. [],
  87. [ 'page' => [ 'LEFT JOIN', [ 'rev_page=page_id' ] ] ] + $commentQuery['joins']
  88. + $actorQuery['joins']
  89. );
  90. $orphans = $result->numRows();
  91. if ( $orphans > 0 ) {
  92. $this->output( "$orphans orphan revisions...\n" );
  93. $this->output( sprintf(
  94. "%10s %10s %14s %20s %s\n",
  95. 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment'
  96. ) );
  97. $contLang = MediaWikiServices::getInstance()->getContentLanguage();
  98. foreach ( $result as $row ) {
  99. $comment = $commentStore->getComment( 'rev_comment', $row )->text;
  100. if ( $comment !== '' ) {
  101. $comment = '(' . $contLang->truncateForVisual( $comment, 40 ) . ')';
  102. }
  103. $rev_user_text = $contLang->truncateForVisual( $row->rev_user_text, 20 );
  104. # pad $rev_user_text to 20 characters. Note that this may
  105. # yield poor results if $rev_user_text contains combining
  106. # or half-width characters. Alas.
  107. if ( mb_strlen( $rev_user_text ) < 20 ) {
  108. $rev_user_text = str_repeat( ' ', 20 - mb_strlen( $rev_user_text ) );
  109. }
  110. $this->output( sprintf( "%10d %10d %14s %s %s\n",
  111. $row->rev_id,
  112. $row->rev_page,
  113. $row->rev_timestamp,
  114. $rev_user_text,
  115. $comment ) );
  116. if ( $fix ) {
  117. $dbw->delete( 'revision', [ 'rev_id' => $row->rev_id ] );
  118. }
  119. }
  120. if ( !$fix ) {
  121. $this->output( "Run again with --fix to remove these entries automatically.\n" );
  122. }
  123. } else {
  124. $this->output( "No orphans! Yay!\n" );
  125. }
  126. if ( $fix ) {
  127. $dbw->unlockTables( __METHOD__ );
  128. }
  129. }
  130. /**
  131. * @param bool $fix
  132. * @todo DON'T USE THIS YET! It will remove entries which have children,
  133. * but which aren't properly attached (eg if page_latest is bogus
  134. * but valid revisions do exist)
  135. */
  136. private function checkWidows( $fix ) {
  137. $dbw = $this->getDB( DB_MASTER );
  138. $page = $dbw->tableName( 'page' );
  139. $revision = $dbw->tableName( 'revision' );
  140. if ( $fix ) {
  141. $this->lockTables( $dbw );
  142. }
  143. $this->output( "\nChecking for childless page table entries... "
  144. . "(this may take a while on a large wiki)\n" );
  145. $result = $dbw->query( "
  146. SELECT *
  147. FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
  148. WHERE rev_id IS NULL
  149. " );
  150. $widows = $result->numRows();
  151. if ( $widows > 0 ) {
  152. $this->output( "$widows childless pages...\n" );
  153. $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
  154. foreach ( $result as $row ) {
  155. printf( "%10d %11d %2d %s\n",
  156. $row->page_id,
  157. $row->page_latest,
  158. $row->page_namespace,
  159. $row->page_title );
  160. if ( $fix ) {
  161. $dbw->delete( 'page', [ 'page_id' => $row->page_id ] );
  162. }
  163. }
  164. if ( !$fix ) {
  165. $this->output( "Run again with --fix to remove these entries automatically.\n" );
  166. }
  167. } else {
  168. $this->output( "No childless pages! Yay!\n" );
  169. }
  170. if ( $fix ) {
  171. $dbw->unlockTables( __METHOD__ );
  172. }
  173. }
  174. /**
  175. * Check for pages where page_latest is wrong
  176. * @param bool $fix Whether to fix broken entries
  177. */
  178. private function checkSeparation( $fix ) {
  179. $dbw = $this->getDB( DB_MASTER );
  180. $page = $dbw->tableName( 'page' );
  181. $revision = $dbw->tableName( 'revision' );
  182. if ( $fix ) {
  183. $this->lockTables( $dbw, [ 'user', 'text' ] );
  184. }
  185. $this->output( "\nChecking for pages whose page_latest links are incorrect... "
  186. . "(this may take a while on a large wiki)\n" );
  187. $result = $dbw->query( "
  188. SELECT *
  189. FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
  190. " );
  191. $found = 0;
  192. foreach ( $result as $row ) {
  193. $result2 = $dbw->query( "
  194. SELECT MAX(rev_timestamp) as max_timestamp
  195. FROM $revision
  196. WHERE rev_page=" . (int)( $row->page_id )
  197. );
  198. $row2 = $dbw->fetchObject( $result2 );
  199. if ( $row2 ) {
  200. if ( $row->rev_timestamp != $row2->max_timestamp ) {
  201. if ( $found == 0 ) {
  202. $this->output( sprintf( "%10s %10s %14s %14s\n",
  203. 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
  204. }
  205. ++$found;
  206. $this->output( sprintf( "%10d %10d %14s %14s\n",
  207. $row->page_id,
  208. $row->page_latest,
  209. $row->rev_timestamp,
  210. $row2->max_timestamp ) );
  211. if ( $fix ) {
  212. # ...
  213. $maxId = $dbw->selectField(
  214. 'revision',
  215. 'rev_id',
  216. [
  217. 'rev_page' => $row->page_id,
  218. 'rev_timestamp' => $row2->max_timestamp ] );
  219. $this->output( "... updating to revision $maxId\n" );
  220. $maxRev = Revision::newFromId( $maxId );
  221. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  222. $article = WikiPage::factory( $title );
  223. $article->updateRevisionOn( $dbw, $maxRev );
  224. }
  225. }
  226. } else {
  227. $this->output( "wtf\n" );
  228. }
  229. }
  230. if ( $found ) {
  231. $this->output( "Found $found pages with incorrect latest revision.\n" );
  232. } else {
  233. $this->output( "No pages with incorrect latest revision. Yay!\n" );
  234. }
  235. if ( !$fix && $found > 0 ) {
  236. $this->output( "Run again with --fix to remove these entries automatically.\n" );
  237. }
  238. if ( $fix ) {
  239. $dbw->unlockTables( __METHOD__ );
  240. }
  241. }
  242. }
  243. $maintClass = Orphans::class;
  244. require_once RUN_MAINTENANCE_IF_MAIN;