edit.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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( 'minor', 'Minor edit', false, false, 'm' );
  36. $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
  37. $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
  38. $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
  39. $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
  40. $this->addOption( 'createonly', 'Only create new pages', false, false );
  41. $this->addArg( 'title', 'Title of article to edit' );
  42. }
  43. public function execute() {
  44. global $wgUser;
  45. $userName = $this->getOption( 'user', false );
  46. $summary = $this->getOption( 'summary', '' );
  47. $minor = $this->hasOption( 'minor' );
  48. $bot = $this->hasOption( 'bot' );
  49. $autoSummary = $this->hasOption( 'autosummary' );
  50. $noRC = $this->hasOption( 'no-rc' );
  51. if ( $userName === false ) {
  52. $wgUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
  53. } else {
  54. $wgUser = User::newFromName( $userName );
  55. }
  56. if ( !$wgUser ) {
  57. $this->error( "Invalid username", true );
  58. }
  59. if ( $wgUser->isAnon() ) {
  60. $wgUser->addToDatabase();
  61. }
  62. $title = Title::newFromText( $this->getArg() );
  63. if ( !$title ) {
  64. $this->error( "Invalid title", true );
  65. }
  66. if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
  67. $this->error( "Page does not exist", true );
  68. } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
  69. $this->error( "Page already exists", true );
  70. }
  71. $page = WikiPage::factory( $title );
  72. # Read the text
  73. $text = $this->getStdin( Maintenance::STDIN_ALL );
  74. $content = ContentHandler::makeContent( $text, $title );
  75. # Do the edit
  76. $this->output( "Saving... " );
  77. $status = $page->doEditContent( $content, $summary,
  78. ( $minor ? EDIT_MINOR : 0 ) |
  79. ( $bot ? EDIT_FORCE_BOT : 0 ) |
  80. ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
  81. ( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
  82. if ( $status->isOK() ) {
  83. $this->output( "done\n" );
  84. $exit = 0;
  85. } else {
  86. $this->output( "failed\n" );
  87. $exit = 1;
  88. }
  89. if ( !$status->isGood() ) {
  90. $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
  91. }
  92. exit( $exit );
  93. }
  94. }
  95. $maintClass = "EditCLI";
  96. require_once RUN_MAINTENANCE_IF_MAIN;