edit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Make a page edit.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to make a page edit.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class EditCLI extends Maintenance {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Edit an article from the command line, text is from stdin' );
  33. $this->addOption( 'user', 'Username', false, true, 'u' );
  34. $this->addOption( 'summary', 'Edit summary', false, true, 's' );
  35. $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
  36. $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
  37. $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
  38. $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
  39. $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
  40. $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
  41. $this->addOption( 'createonly', 'Only create new pages', false, false );
  42. $this->addOption( 'slot', 'Slot role name', false, true );
  43. $this->addArg( 'title', 'Title of article to edit' );
  44. }
  45. public function execute() {
  46. global $wgUser;
  47. $userName = $this->getOption( 'user', false );
  48. $summary = $this->getOption( 'summary', '' );
  49. $remove = $this->hasOption( 'remove' );
  50. $minor = $this->hasOption( 'minor' );
  51. $bot = $this->hasOption( 'bot' );
  52. $autoSummary = $this->hasOption( 'autosummary' );
  53. $noRC = $this->hasOption( 'no-rc' );
  54. $slot = $this->getOption( 'slot', 'main' );
  55. if ( $userName === false ) {
  56. $wgUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
  57. } else {
  58. $wgUser = User::newFromName( $userName );
  59. }
  60. if ( !$wgUser ) {
  61. $this->fatalError( "Invalid username" );
  62. }
  63. if ( $wgUser->isAnon() ) {
  64. $wgUser->addToDatabase();
  65. }
  66. $title = Title::newFromText( $this->getArg() );
  67. if ( !$title ) {
  68. $this->fatalError( "Invalid title" );
  69. }
  70. if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
  71. $this->fatalError( "Page does not exist" );
  72. } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
  73. $this->fatalError( "Page already exists" );
  74. }
  75. $page = WikiPage::factory( $title );
  76. if ( $remove ) {
  77. if ( $slot === 'main' ) {
  78. $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
  79. }
  80. $content = false;
  81. } else {
  82. # Read the text
  83. $text = $this->getStdin( Maintenance::STDIN_ALL );
  84. $content = ContentHandler::makeContent( $text, $title );
  85. }
  86. # Do the edit
  87. $this->output( "Saving... " );
  88. $updater = $page->newPageUpdater( $wgUser );
  89. $flags = ( $minor ? EDIT_MINOR : 0 ) |
  90. ( $bot ? EDIT_FORCE_BOT : 0 ) |
  91. ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
  92. ( $noRC ? EDIT_SUPPRESS_RC : 0 );
  93. if ( $content === false ) {
  94. $updater->removeSlot( $slot );
  95. } else {
  96. $updater->setContent( $slot, $content );
  97. }
  98. $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
  99. $status = $updater->getStatus();
  100. if ( $status->isOK() ) {
  101. $this->output( "done\n" );
  102. $exit = 0;
  103. } else {
  104. $this->output( "failed\n" );
  105. $exit = 1;
  106. }
  107. if ( !$status->isGood() ) {
  108. $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
  109. }
  110. exit( $exit );
  111. }
  112. }
  113. $maintClass = EditCLI::class;
  114. require_once RUN_MAINTENANCE_IF_MAIN;