PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/includes/EditPage.php

https://bitbucket.org/ghostfreeman/freeside-wiki
PHP | 3281 lines | 2055 code | 395 blank | 831 comment | 470 complexity | d4e1586f44beac127ba969d76901c161 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Page edition user interface.
  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. */
  22. /**
  23. * The edit page/HTML interface (split from Article)
  24. * The actual database and text munging is still in Article,
  25. * but it should get easier to call those from alternate
  26. * interfaces.
  27. *
  28. * EditPage cares about two distinct titles:
  29. * $this->mContextTitle is the page that forms submit to, links point to,
  30. * redirects go to, etc. $this->mTitle (as well as $mArticle) is the
  31. * page in the database that is actually being edited. These are
  32. * usually the same, but they are now allowed to be different.
  33. *
  34. * Surgeon General's Warning: prolonged exposure to this class is known to cause
  35. * headaches, which may be fatal.
  36. */
  37. class EditPage {
  38. /**
  39. * Status: Article successfully updated
  40. */
  41. const AS_SUCCESS_UPDATE = 200;
  42. /**
  43. * Status: Article successfully created
  44. */
  45. const AS_SUCCESS_NEW_ARTICLE = 201;
  46. /**
  47. * Status: Article update aborted by a hook function
  48. */
  49. const AS_HOOK_ERROR = 210;
  50. /**
  51. * Status: A hook function returned an error
  52. */
  53. const AS_HOOK_ERROR_EXPECTED = 212;
  54. /**
  55. * Status: User is blocked from editting this page
  56. */
  57. const AS_BLOCKED_PAGE_FOR_USER = 215;
  58. /**
  59. * Status: Content too big (> $wgMaxArticleSize)
  60. */
  61. const AS_CONTENT_TOO_BIG = 216;
  62. /**
  63. * Status: User cannot edit? (not used)
  64. */
  65. const AS_USER_CANNOT_EDIT = 217;
  66. /**
  67. * Status: this anonymous user is not allowed to edit this page
  68. */
  69. const AS_READ_ONLY_PAGE_ANON = 218;
  70. /**
  71. * Status: this logged in user is not allowed to edit this page
  72. */
  73. const AS_READ_ONLY_PAGE_LOGGED = 219;
  74. /**
  75. * Status: wiki is in readonly mode (wfReadOnly() == true)
  76. */
  77. const AS_READ_ONLY_PAGE = 220;
  78. /**
  79. * Status: rate limiter for action 'edit' was tripped
  80. */
  81. const AS_RATE_LIMITED = 221;
  82. /**
  83. * Status: article was deleted while editting and param wpRecreate == false or form
  84. * was not posted
  85. */
  86. const AS_ARTICLE_WAS_DELETED = 222;
  87. /**
  88. * Status: user tried to create this page, but is not allowed to do that
  89. * ( Title->usercan('create') == false )
  90. */
  91. const AS_NO_CREATE_PERMISSION = 223;
  92. /**
  93. * Status: user tried to create a blank page
  94. */
  95. const AS_BLANK_ARTICLE = 224;
  96. /**
  97. * Status: (non-resolvable) edit conflict
  98. */
  99. const AS_CONFLICT_DETECTED = 225;
  100. /**
  101. * Status: no edit summary given and the user has forceeditsummary set and the user is not
  102. * editting in his own userspace or talkspace and wpIgnoreBlankSummary == false
  103. */
  104. const AS_SUMMARY_NEEDED = 226;
  105. /**
  106. * Status: user tried to create a new section without content
  107. */
  108. const AS_TEXTBOX_EMPTY = 228;
  109. /**
  110. * Status: article is too big (> $wgMaxArticleSize), after merging in the new section
  111. */
  112. const AS_MAX_ARTICLE_SIZE_EXCEEDED = 229;
  113. /**
  114. * not used
  115. */
  116. const AS_OK = 230;
  117. /**
  118. * Status: WikiPage::doEdit() was unsuccessfull
  119. */
  120. const AS_END = 231;
  121. /**
  122. * Status: summary contained spam according to one of the regexes in $wgSummarySpamRegex
  123. */
  124. const AS_SPAM_ERROR = 232;
  125. /**
  126. * Status: anonymous user is not allowed to upload (User::isAllowed('upload') == false)
  127. */
  128. const AS_IMAGE_REDIRECT_ANON = 233;
  129. /**
  130. * Status: logged in user is not allowed to upload (User::isAllowed('upload') == false)
  131. */
  132. const AS_IMAGE_REDIRECT_LOGGED = 234;
  133. /**
  134. * HTML id and name for the beginning of the edit form.
  135. */
  136. const EDITFORM_ID = 'editform';
  137. /**
  138. * @var Article
  139. */
  140. var $mArticle;
  141. /**
  142. * @var Title
  143. */
  144. var $mTitle;
  145. private $mContextTitle = null;
  146. var $action = 'submit';
  147. var $isConflict = false;
  148. var $isCssJsSubpage = false;
  149. var $isCssSubpage = false;
  150. var $isJsSubpage = false;
  151. var $isWrongCaseCssJsPage = false;
  152. var $isNew = false; // new page or new section
  153. var $deletedSinceEdit;
  154. var $formtype;
  155. var $firsttime;
  156. var $lastDelete;
  157. var $mTokenOk = false;
  158. var $mTokenOkExceptSuffix = false;
  159. var $mTriedSave = false;
  160. var $incompleteForm = false;
  161. var $tooBig = false;
  162. var $kblength = false;
  163. var $missingComment = false;
  164. var $missingSummary = false;
  165. var $allowBlankSummary = false;
  166. var $autoSumm = '';
  167. var $hookError = '';
  168. #var $mPreviewTemplates;
  169. /**
  170. * @var ParserOutput
  171. */
  172. var $mParserOutput;
  173. /**
  174. * Has a summary been preset using GET parameter &summary= ?
  175. * @var Bool
  176. */
  177. var $hasPresetSummary = false;
  178. var $mBaseRevision = false;
  179. var $mShowSummaryField = true;
  180. # Form values
  181. var $save = false, $preview = false, $diff = false;
  182. var $minoredit = false, $watchthis = false, $recreate = false;
  183. var $textbox1 = '', $textbox2 = '', $summary = '', $nosummary = false;
  184. var $edittime = '', $section = '', $sectiontitle = '', $starttime = '';
  185. var $oldid = 0, $editintro = '', $scrolltop = null, $bot = true;
  186. # Placeholders for text injection by hooks (must be HTML)
  187. # extensions should take care to _append_ to the present value
  188. public $editFormPageTop = ''; // Before even the preview
  189. public $editFormTextTop = '';
  190. public $editFormTextBeforeContent = '';
  191. public $editFormTextAfterWarn = '';
  192. public $editFormTextAfterTools = '';
  193. public $editFormTextBottom = '';
  194. public $editFormTextAfterContent = '';
  195. public $previewTextAfterContent = '';
  196. public $mPreloadText = '';
  197. /* $didSave should be set to true whenever an article was succesfully altered. */
  198. public $didSave = false;
  199. public $undidRev = 0;
  200. public $suppressIntro = false;
  201. /**
  202. * @param $article Article
  203. */
  204. public function __construct( Article $article ) {
  205. $this->mArticle = $article;
  206. $this->mTitle = $article->getTitle();
  207. }
  208. /**
  209. * @return Article
  210. */
  211. public function getArticle() {
  212. return $this->mArticle;
  213. }
  214. /**
  215. * @since 1.19
  216. * @return Title
  217. */
  218. public function getTitle() {
  219. return $this->mTitle;
  220. }
  221. /**
  222. * Set the context Title object
  223. *
  224. * @param $title Title object or null
  225. */
  226. public function setContextTitle( $title ) {
  227. $this->mContextTitle = $title;
  228. }
  229. /**
  230. * Get the context title object.
  231. * If not set, $wgTitle will be returned. This behavior might changed in
  232. * the future to return $this->mTitle instead.
  233. *
  234. * @return Title object
  235. */
  236. public function getContextTitle() {
  237. if ( is_null( $this->mContextTitle ) ) {
  238. global $wgTitle;
  239. return $wgTitle;
  240. } else {
  241. return $this->mContextTitle;
  242. }
  243. }
  244. function submit() {
  245. $this->edit();
  246. }
  247. /**
  248. * This is the function that gets called for "action=edit". It
  249. * sets up various member variables, then passes execution to
  250. * another function, usually showEditForm()
  251. *
  252. * The edit form is self-submitting, so that when things like
  253. * preview and edit conflicts occur, we get the same form back
  254. * with the extra stuff added. Only when the final submission
  255. * is made and all is well do we actually save and redirect to
  256. * the newly-edited page.
  257. */
  258. function edit() {
  259. global $wgOut, $wgRequest, $wgUser;
  260. // Allow extensions to modify/prevent this form or submission
  261. if ( !wfRunHooks( 'AlternateEdit', array( $this ) ) ) {
  262. return;
  263. }
  264. wfProfileIn( __METHOD__ );
  265. wfDebug( __METHOD__ . ": enter\n" );
  266. // If they used redlink=1 and the page exists, redirect to the main article
  267. if ( $wgRequest->getBool( 'redlink' ) && $this->mTitle->exists() ) {
  268. $wgOut->redirect( $this->mTitle->getFullURL() );
  269. wfProfileOut( __METHOD__ );
  270. return;
  271. }
  272. $this->importFormData( $wgRequest );
  273. $this->firsttime = false;
  274. if ( $this->live ) {
  275. $this->livePreview();
  276. wfProfileOut( __METHOD__ );
  277. return;
  278. }
  279. if ( wfReadOnly() && $this->save ) {
  280. // Force preview
  281. $this->save = false;
  282. $this->preview = true;
  283. }
  284. if ( $this->save ) {
  285. $this->formtype = 'save';
  286. } elseif ( $this->preview ) {
  287. $this->formtype = 'preview';
  288. } elseif ( $this->diff ) {
  289. $this->formtype = 'diff';
  290. } else { # First time through
  291. $this->firsttime = true;
  292. if ( $this->previewOnOpen() ) {
  293. $this->formtype = 'preview';
  294. } else {
  295. $this->formtype = 'initial';
  296. }
  297. }
  298. $permErrors = $this->getEditPermissionErrors();
  299. if ( $permErrors ) {
  300. wfDebug( __METHOD__ . ": User can't edit\n" );
  301. // Auto-block user's IP if the account was "hard" blocked
  302. $wgUser->spreadAnyEditBlock();
  303. $this->displayPermissionsError( $permErrors );
  304. wfProfileOut( __METHOD__ );
  305. return;
  306. }
  307. wfProfileIn( __METHOD__ . "-business-end" );
  308. $this->isConflict = false;
  309. // css / js subpages of user pages get a special treatment
  310. $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage();
  311. $this->isCssSubpage = $this->mTitle->isCssSubpage();
  312. $this->isJsSubpage = $this->mTitle->isJsSubpage();
  313. $this->isWrongCaseCssJsPage = $this->isWrongCaseCssJsPage();
  314. $this->isNew = !$this->mTitle->exists() || $this->section == 'new';
  315. # Show applicable editing introductions
  316. if ( $this->formtype == 'initial' || $this->firsttime ) {
  317. $this->showIntro();
  318. }
  319. # Attempt submission here. This will check for edit conflicts,
  320. # and redundantly check for locked database, blocked IPs, etc.
  321. # that edit() already checked just in case someone tries to sneak
  322. # in the back door with a hand-edited submission URL.
  323. if ( 'save' == $this->formtype ) {
  324. if ( !$this->attemptSave() ) {
  325. wfProfileOut( __METHOD__ . "-business-end" );
  326. wfProfileOut( __METHOD__ );
  327. return;
  328. }
  329. }
  330. # First time through: get contents, set time for conflict
  331. # checking, etc.
  332. if ( 'initial' == $this->formtype || $this->firsttime ) {
  333. if ( $this->initialiseForm() === false ) {
  334. $this->noSuchSectionPage();
  335. wfProfileOut( __METHOD__ . "-business-end" );
  336. wfProfileOut( __METHOD__ );
  337. return;
  338. }
  339. if ( !$this->mTitle->getArticleID() )
  340. wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) );
  341. else
  342. wfRunHooks( 'EditFormInitialText', array( $this ) );
  343. }
  344. $this->showEditForm();
  345. wfProfileOut( __METHOD__ . "-business-end" );
  346. wfProfileOut( __METHOD__ );
  347. }
  348. /**
  349. * @return array
  350. */
  351. protected function getEditPermissionErrors() {
  352. global $wgUser;
  353. $permErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser );
  354. # Can this title be created?
  355. if ( !$this->mTitle->exists() ) {
  356. $permErrors = array_merge( $permErrors,
  357. wfArrayDiff2( $this->mTitle->getUserPermissionsErrors( 'create', $wgUser ), $permErrors ) );
  358. }
  359. # Ignore some permissions errors when a user is just previewing/viewing diffs
  360. $remove = array();
  361. foreach ( $permErrors as $error ) {
  362. if ( ( $this->preview || $this->diff ) &&
  363. ( $error[0] == 'blockedtext' || $error[0] == 'autoblockedtext' ) )
  364. {
  365. $remove[] = $error;
  366. }
  367. }
  368. $permErrors = wfArrayDiff2( $permErrors, $remove );
  369. return $permErrors;
  370. }
  371. /**
  372. * Display a permissions error page, like OutputPage::showPermissionsErrorPage(),
  373. * but with the following differences:
  374. * - If redlink=1, the user will be redirected to the page
  375. * - If there is content to display or the error occurs while either saving,
  376. * previewing or showing the difference, it will be a
  377. * "View source for ..." page displaying the source code after the error message.
  378. *
  379. * @since 1.19
  380. * @param $permErrors Array of permissions errors, as returned by
  381. * Title::getUserPermissionsErrors().
  382. */
  383. protected function displayPermissionsError( array $permErrors ) {
  384. global $wgRequest, $wgOut;
  385. if ( $wgRequest->getBool( 'redlink' ) ) {
  386. // The edit page was reached via a red link.
  387. // Redirect to the article page and let them click the edit tab if
  388. // they really want a permission error.
  389. $wgOut->redirect( $this->mTitle->getFullUrl() );
  390. return;
  391. }
  392. $content = $this->getContent();
  393. # Use the normal message if there's nothing to display
  394. if ( $this->firsttime && $content === '' ) {
  395. $action = $this->mTitle->exists() ? 'edit' :
  396. ( $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage' );
  397. throw new PermissionsError( $action, $permErrors );
  398. }
  399. $wgOut->setPageTitle( wfMessage( 'viewsource-title', $this->getContextTitle()->getPrefixedText() ) );
  400. $wgOut->addBacklinkSubtitle( $this->getContextTitle() );
  401. $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) );
  402. $wgOut->addHTML( "<hr />\n" );
  403. # If the user made changes, preserve them when showing the markup
  404. # (This happens when a user is blocked during edit, for instance)
  405. if ( !$this->firsttime ) {
  406. $content = $this->textbox1;
  407. $wgOut->addWikiMsg( 'viewyourtext' );
  408. } else {
  409. $wgOut->addWikiMsg( 'viewsourcetext' );
  410. }
  411. $this->showTextbox( $content, 'wpTextbox1', array( 'readonly' ) );
  412. $wgOut->addHTML( Html::rawElement( 'div', array( 'class' => 'templatesUsed' ),
  413. Linker::formatTemplates( $this->getTemplates() ) ) );
  414. if ( $this->mTitle->exists() ) {
  415. $wgOut->returnToMain( null, $this->mTitle );
  416. }
  417. }
  418. /**
  419. * Show a read-only error
  420. * Parameters are the same as OutputPage:readOnlyPage()
  421. * Redirect to the article page if redlink=1
  422. * @deprecated in 1.19; use displayPermissionsError() instead
  423. */
  424. function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) {
  425. wfDeprecated( __METHOD__, '1.19' );
  426. global $wgRequest, $wgOut;
  427. if ( $wgRequest->getBool( 'redlink' ) ) {
  428. // The edit page was reached via a red link.
  429. // Redirect to the article page and let them click the edit tab if
  430. // they really want a permission error.
  431. $wgOut->redirect( $this->mTitle->getFullUrl() );
  432. } else {
  433. $wgOut->readOnlyPage( $source, $protected, $reasons, $action );
  434. }
  435. }
  436. /**
  437. * Should we show a preview when the edit form is first shown?
  438. *
  439. * @return bool
  440. */
  441. protected function previewOnOpen() {
  442. global $wgRequest, $wgUser, $wgPreviewOnOpenNamespaces;
  443. if ( $wgRequest->getVal( 'preview' ) == 'yes' ) {
  444. // Explicit override from request
  445. return true;
  446. } elseif ( $wgRequest->getVal( 'preview' ) == 'no' ) {
  447. // Explicit override from request
  448. return false;
  449. } elseif ( $this->section == 'new' ) {
  450. // Nothing *to* preview for new sections
  451. return false;
  452. } elseif ( ( $wgRequest->getVal( 'preload' ) !== null || $this->mTitle->exists() ) && $wgUser->getOption( 'previewonfirst' ) ) {
  453. // Standard preference behaviour
  454. return true;
  455. } elseif ( !$this->mTitle->exists() &&
  456. isset( $wgPreviewOnOpenNamespaces[$this->mTitle->getNamespace()] ) &&
  457. $wgPreviewOnOpenNamespaces[$this->mTitle->getNamespace()] )
  458. {
  459. // Categories are special
  460. return true;
  461. } else {
  462. return false;
  463. }
  464. }
  465. /**
  466. * Checks whether the user entered a skin name in uppercase,
  467. * e.g. "User:Example/Monobook.css" instead of "monobook.css"
  468. *
  469. * @return bool
  470. */
  471. protected function isWrongCaseCssJsPage() {
  472. if ( $this->mTitle->isCssJsSubpage() ) {
  473. $name = $this->mTitle->getSkinFromCssJsSubpage();
  474. $skins = array_merge(
  475. array_keys( Skin::getSkinNames() ),
  476. array( 'common' )
  477. );
  478. return !in_array( $name, $skins )
  479. && in_array( strtolower( $name ), $skins );
  480. } else {
  481. return false;
  482. }
  483. }
  484. /**
  485. * Does this EditPage class support section editing?
  486. * This is used by EditPage subclasses to indicate their ui cannot handle section edits
  487. *
  488. * @return bool
  489. */
  490. protected function isSectionEditSupported() {
  491. return true;
  492. }
  493. /**
  494. * This function collects the form data and uses it to populate various member variables.
  495. * @param $request WebRequest
  496. */
  497. function importFormData( &$request ) {
  498. global $wgLang, $wgUser;
  499. wfProfileIn( __METHOD__ );
  500. # Section edit can come from either the form or a link
  501. $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
  502. if ( $request->wasPosted() ) {
  503. # These fields need to be checked for encoding.
  504. # Also remove trailing whitespace, but don't remove _initial_
  505. # whitespace from the text boxes. This may be significant formatting.
  506. $this->textbox1 = $this->safeUnicodeInput( $request, 'wpTextbox1' );
  507. if ( !$request->getCheck( 'wpTextbox2' ) ) {
  508. // Skip this if wpTextbox2 has input, it indicates that we came
  509. // from a conflict page with raw page text, not a custom form
  510. // modified by subclasses
  511. wfProfileIn( get_class( $this ) . "::importContentFormData" );
  512. $textbox1 = $this->importContentFormData( $request );
  513. if ( isset( $textbox1 ) )
  514. $this->textbox1 = $textbox1;
  515. wfProfileOut( get_class( $this ) . "::importContentFormData" );
  516. }
  517. # Truncate for whole multibyte characters
  518. $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 255 );
  519. # If the summary consists of a heading, e.g. '==Foobar==', extract the title from the
  520. # header syntax, e.g. 'Foobar'. This is mainly an issue when we are using wpSummary for
  521. # section titles.
  522. $this->summary = preg_replace( '/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->summary );
  523. # Treat sectiontitle the same way as summary.
  524. # Note that wpSectionTitle is not yet a part of the actual edit form, as wpSummary is
  525. # currently doing double duty as both edit summary and section title. Right now this
  526. # is just to allow API edits to work around this limitation, but this should be
  527. # incorporated into the actual edit form when EditPage is rewritten (Bugs 18654, 26312).
  528. $this->sectiontitle = $wgLang->truncate( $request->getText( 'wpSectionTitle' ), 255 );
  529. $this->sectiontitle = preg_replace( '/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->sectiontitle );
  530. $this->edittime = $request->getVal( 'wpEdittime' );
  531. $this->starttime = $request->getVal( 'wpStarttime' );
  532. $this->scrolltop = $request->getIntOrNull( 'wpScrolltop' );
  533. if ( $this->textbox1 === '' && $request->getVal( 'wpTextbox1' ) === null ) {
  534. // wpTextbox1 field is missing, possibly due to being "too big"
  535. // according to some filter rules such as Suhosin's setting for
  536. // suhosin.request.max_value_length (d'oh)
  537. $this->incompleteForm = true;
  538. } else {
  539. // edittime should be one of our last fields; if it's missing,
  540. // the submission probably broke somewhere in the middle.
  541. $this->incompleteForm = is_null( $this->edittime );
  542. }
  543. if ( $this->incompleteForm ) {
  544. # If the form is incomplete, force to preview.
  545. wfDebug( __METHOD__ . ": Form data appears to be incomplete\n" );
  546. wfDebug( "POST DATA: " . var_export( $_POST, true ) . "\n" );
  547. $this->preview = true;
  548. } else {
  549. /* Fallback for live preview */
  550. $this->preview = $request->getCheck( 'wpPreview' ) || $request->getCheck( 'wpLivePreview' );
  551. $this->diff = $request->getCheck( 'wpDiff' );
  552. // Remember whether a save was requested, so we can indicate
  553. // if we forced preview due to session failure.
  554. $this->mTriedSave = !$this->preview;
  555. if ( $this->tokenOk( $request ) ) {
  556. # Some browsers will not report any submit button
  557. # if the user hits enter in the comment box.
  558. # The unmarked state will be assumed to be a save,
  559. # if the form seems otherwise complete.
  560. wfDebug( __METHOD__ . ": Passed token check.\n" );
  561. } elseif ( $this->diff ) {
  562. # Failed token check, but only requested "Show Changes".
  563. wfDebug( __METHOD__ . ": Failed token check; Show Changes requested.\n" );
  564. } else {
  565. # Page might be a hack attempt posted from
  566. # an external site. Preview instead of saving.
  567. wfDebug( __METHOD__ . ": Failed token check; forcing preview\n" );
  568. $this->preview = true;
  569. }
  570. }
  571. $this->save = !$this->preview && !$this->diff;
  572. if ( !preg_match( '/^\d{14}$/', $this->edittime ) ) {
  573. $this->edittime = null;
  574. }
  575. if ( !preg_match( '/^\d{14}$/', $this->starttime ) ) {
  576. $this->starttime = null;
  577. }
  578. $this->recreate = $request->getCheck( 'wpRecreate' );
  579. $this->minoredit = $request->getCheck( 'wpMinoredit' );
  580. $this->watchthis = $request->getCheck( 'wpWatchthis' );
  581. # Don't force edit summaries when a user is editing their own user or talk page
  582. if ( ( $this->mTitle->mNamespace == NS_USER || $this->mTitle->mNamespace == NS_USER_TALK ) &&
  583. $this->mTitle->getText() == $wgUser->getName() )
  584. {
  585. $this->allowBlankSummary = true;
  586. } else {
  587. $this->allowBlankSummary = $request->getBool( 'wpIgnoreBlankSummary' ) || !$wgUser->getOption( 'forceeditsummary' );
  588. }
  589. $this->autoSumm = $request->getText( 'wpAutoSummary' );
  590. } else {
  591. # Not a posted form? Start with nothing.
  592. wfDebug( __METHOD__ . ": Not a posted form.\n" );
  593. $this->textbox1 = '';
  594. $this->summary = '';
  595. $this->sectiontitle = '';
  596. $this->edittime = '';
  597. $this->starttime = wfTimestampNow();
  598. $this->edit = false;
  599. $this->preview = false;
  600. $this->save = false;
  601. $this->diff = false;
  602. $this->minoredit = false;
  603. $this->watchthis = $request->getBool( 'watchthis', false ); // Watch may be overriden by request parameters
  604. $this->recreate = false;
  605. // When creating a new section, we can preload a section title by passing it as the
  606. // preloadtitle parameter in the URL (Bug 13100)
  607. if ( $this->section == 'new' && $request->getVal( 'preloadtitle' ) ) {
  608. $this->sectiontitle = $request->getVal( 'preloadtitle' );
  609. // Once wpSummary isn't being use for setting section titles, we should delete this.
  610. $this->summary = $request->getVal( 'preloadtitle' );
  611. }
  612. elseif ( $this->section != 'new' && $request->getVal( 'summary' ) ) {
  613. $this->summary = $request->getText( 'summary' );
  614. if ( $this->summary !== '' ) {
  615. $this->hasPresetSummary = true;
  616. }
  617. }
  618. if ( $request->getVal( 'minor' ) ) {
  619. $this->minoredit = true;
  620. }
  621. }
  622. $this->bot = $request->getBool( 'bot', true );
  623. $this->nosummary = $request->getBool( 'nosummary' );
  624. $this->oldid = $request->getInt( 'oldid' );
  625. $this->live = $request->getCheck( 'live' );
  626. $this->editintro = $request->getText( 'editintro',
  627. // Custom edit intro for new sections
  628. $this->section === 'new' ? 'MediaWiki:addsection-editintro' : '' );
  629. // Allow extensions to modify form data
  630. wfRunHooks( 'EditPage::importFormData', array( $this, $request ) );
  631. wfProfileOut( __METHOD__ );
  632. }
  633. /**
  634. * Subpage overridable method for extracting the page content data from the
  635. * posted form to be placed in $this->textbox1, if using customized input
  636. * this method should be overrided and return the page text that will be used
  637. * for saving, preview parsing and so on...
  638. *
  639. * @param $request WebRequest
  640. */
  641. protected function importContentFormData( &$request ) {
  642. return; // Don't do anything, EditPage already extracted wpTextbox1
  643. }
  644. /**
  645. * Initialise form fields in the object
  646. * Called on the first invocation, e.g. when a user clicks an edit link
  647. * @return bool -- if the requested section is valid
  648. */
  649. function initialiseForm() {
  650. global $wgUser;
  651. $this->edittime = $this->mArticle->getTimestamp();
  652. $this->textbox1 = $this->getContent( false );
  653. // activate checkboxes if user wants them to be always active
  654. # Sort out the "watch" checkbox
  655. if ( $wgUser->getOption( 'watchdefault' ) ) {
  656. # Watch all edits
  657. $this->watchthis = true;
  658. } elseif ( $wgUser->getOption( 'watchcreations' ) && !$this->mTitle->exists() ) {
  659. # Watch creations
  660. $this->watchthis = true;
  661. } elseif ( $wgUser->isWatched( $this->mTitle ) ) {
  662. # Already watched
  663. $this->watchthis = true;
  664. }
  665. if ( $wgUser->getOption( 'minordefault' ) && !$this->isNew ) {
  666. $this->minoredit = true;
  667. }
  668. if ( $this->textbox1 === false ) {
  669. return false;
  670. }
  671. wfProxyCheck();
  672. return true;
  673. }
  674. /**
  675. * Fetch initial editing page content.
  676. *
  677. * @param $def_text string
  678. * @return mixed string on success, $def_text for invalid sections
  679. * @private
  680. */
  681. function getContent( $def_text = '' ) {
  682. global $wgOut, $wgRequest, $wgParser;
  683. wfProfileIn( __METHOD__ );
  684. $text = false;
  685. // For message page not locally set, use the i18n message.
  686. // For other non-existent articles, use preload text if any.
  687. if ( !$this->mTitle->exists() || $this->section == 'new' ) {
  688. if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && $this->section != 'new' ) {
  689. # If this is a system message, get the default text.
  690. $text = $this->mTitle->getDefaultMessageText();
  691. }
  692. if ( $text === false ) {
  693. # If requested, preload some text.
  694. $preload = $wgRequest->getVal( 'preload',
  695. // Custom preload text for new sections
  696. $this->section === 'new' ? 'MediaWiki:addsection-preload' : '' );
  697. $text = $this->getPreloadedText( $preload );
  698. }
  699. // For existing pages, get text based on "undo" or section parameters.
  700. } else {
  701. if ( $this->section != '' ) {
  702. // Get section edit text (returns $def_text for invalid sections)
  703. $text = $wgParser->getSection( $this->getOriginalContent(), $this->section, $def_text );
  704. } else {
  705. $undoafter = $wgRequest->getInt( 'undoafter' );
  706. $undo = $wgRequest->getInt( 'undo' );
  707. if ( $undo > 0 && $undoafter > 0 ) {
  708. if ( $undo < $undoafter ) {
  709. # If they got undoafter and undo round the wrong way, switch them
  710. list( $undo, $undoafter ) = array( $undoafter, $undo );
  711. }
  712. $undorev = Revision::newFromId( $undo );
  713. $oldrev = Revision::newFromId( $undoafter );
  714. # Sanity check, make sure it's the right page,
  715. # the revisions exist and they were not deleted.
  716. # Otherwise, $text will be left as-is.
  717. if ( !is_null( $undorev ) && !is_null( $oldrev ) &&
  718. $undorev->getPage() == $oldrev->getPage() &&
  719. $undorev->getPage() == $this->mTitle->getArticleID() &&
  720. !$undorev->isDeleted( Revision::DELETED_TEXT ) &&
  721. !$oldrev->isDeleted( Revision::DELETED_TEXT ) ) {
  722. $text = $this->mArticle->getUndoText( $undorev, $oldrev );
  723. if ( $text === false ) {
  724. # Warn the user that something went wrong
  725. $undoMsg = 'failure';
  726. } else {
  727. # Inform the user of our success and set an automatic edit summary
  728. $undoMsg = 'success';
  729. # If we just undid one rev, use an autosummary
  730. $firstrev = $oldrev->getNext();
  731. if ( $firstrev && $firstrev->getId() == $undo ) {
  732. $undoSummary = wfMessage( 'undo-summary', $undo, $undorev->getUserText() )->inContentLanguage()->text();
  733. if ( $this->summary === '' ) {
  734. $this->summary = $undoSummary;
  735. } else {
  736. $this->summary = $undoSummary . wfMessage( 'colon-separator' )
  737. ->inContentLanguage()->text() . $this->summary;
  738. }
  739. $this->undidRev = $undo;
  740. }
  741. $this->formtype = 'diff';
  742. }
  743. } else {
  744. // Failed basic sanity checks.
  745. // Older revisions may have been removed since the link
  746. // was created, or we may simply have got bogus input.
  747. $undoMsg = 'norev';
  748. }
  749. $class = ( $undoMsg == 'success' ? '' : 'error ' ) . "mw-undo-{$undoMsg}";
  750. $this->editFormPageTop .= $wgOut->parse( "<div class=\"{$class}\">" .
  751. wfMessage( 'undo-' . $undoMsg )->plain() . '</div>', true, /* interface */true );
  752. }
  753. if ( $text === false ) {
  754. $text = $this->getOriginalContent();
  755. }
  756. }
  757. }
  758. wfProfileOut( __METHOD__ );
  759. return $text;
  760. }
  761. /**
  762. * Get the content of the wanted revision, without section extraction.
  763. *
  764. * The result of this function can be used to compare user's input with
  765. * section replaced in its context (using WikiPage::replaceSection())
  766. * to the original text of the edit.
  767. *
  768. * This difers from Article::getContent() that when a missing revision is
  769. * encountered the result will be an empty string and not the
  770. * 'missing-revision' message.
  771. *
  772. * @since 1.19
  773. * @return string
  774. */
  775. private function getOriginalContent() {
  776. if ( $this->section == 'new' ) {
  777. return $this->getCurrentText();
  778. }
  779. $revision = $this->mArticle->getRevisionFetched();
  780. if ( $revision === null ) {
  781. return '';
  782. }
  783. return $this->mArticle->getContent();
  784. }
  785. /**
  786. * Get the actual text of the page. This is basically similar to
  787. * WikiPage::getRawText() except that when the page doesn't exist an empty
  788. * string is returned instead of false.
  789. *
  790. * @since 1.19
  791. * @return string
  792. */
  793. private function getCurrentText() {
  794. $text = $this->mArticle->getRawText();
  795. if ( $text === false ) {
  796. return '';
  797. } else {
  798. return $text;
  799. }
  800. }
  801. /**
  802. * Use this method before edit() to preload some text into the edit box
  803. *
  804. * @param $text string
  805. */
  806. public function setPreloadedText( $text ) {
  807. $this->mPreloadText = $text;
  808. }
  809. /**
  810. * Get the contents to be preloaded into the box, either set by
  811. * an earlier setPreloadText() or by loading the given page.
  812. *
  813. * @param $preload String: representing the title to preload from.
  814. * @return String
  815. */
  816. protected function getPreloadedText( $preload ) {
  817. global $wgUser, $wgParser;
  818. if ( !empty( $this->mPreloadText ) ) {
  819. return $this->mPreloadText;
  820. }
  821. if ( $preload === '' ) {
  822. return '';
  823. }
  824. $title = Title::newFromText( $preload );
  825. # Check for existence to avoid getting MediaWiki:Noarticletext
  826. if ( $title === null || !$title->exists() || !$title->userCan( 'read' ) ) {
  827. return '';
  828. }
  829. $page = WikiPage::factory( $title );
  830. if ( $page->isRedirect() ) {
  831. $title = $page->getRedirectTarget();
  832. # Same as before
  833. if ( $title === null || !$title->exists() || !$title->userCan( 'read' ) ) {
  834. return '';
  835. }
  836. $page = WikiPage::factory( $title );
  837. }
  838. $parserOptions = ParserOptions::newFromUser( $wgUser );
  839. return $wgParser->getPreloadText( $page->getRawText(), $title, $parserOptions );
  840. }
  841. /**
  842. * Make sure the form isn't faking a user's credentials.
  843. *
  844. * @param $request WebRequest
  845. * @return bool
  846. * @private
  847. */
  848. function tokenOk( &$request ) {
  849. global $wgUser;
  850. $token = $request->getVal( 'wpEditToken' );
  851. $this->mTokenOk = $wgUser->matchEditToken( $token );
  852. $this->mTokenOkExceptSuffix = $wgUser->matchEditTokenNoSuffix( $token );
  853. return $this->mTokenOk;
  854. }
  855. /**
  856. * Attempt submission
  857. * @return bool false if output is done, true if the rest of the form should be displayed
  858. */
  859. function attemptSave() {
  860. global $wgUser, $wgOut;
  861. $resultDetails = false;
  862. # Allow bots to exempt some edits from bot flagging
  863. $bot = $wgUser->isAllowed( 'bot' ) && $this->bot;
  864. $status = $this->internalAttemptSave( $resultDetails, $bot );
  865. // FIXME: once the interface for internalAttemptSave() is made nicer, this should use the message in $status
  866. if ( $status->value == self::AS_SUCCESS_UPDATE || $status->value == self::AS_SUCCESS_NEW_ARTICLE ) {
  867. $this->didSave = true;
  868. }
  869. switch ( $status->value ) {
  870. case self::AS_HOOK_ERROR_EXPECTED:
  871. case self::AS_CONTENT_TOO_BIG:
  872. case self::AS_ARTICLE_WAS_DELETED:
  873. case self::AS_CONFLICT_DETECTED:
  874. case self::AS_SUMMARY_NEEDED:
  875. case self::AS_TEXTBOX_EMPTY:
  876. case self::AS_MAX_ARTICLE_SIZE_EXCEEDED:
  877. case self::AS_END:
  878. return true;
  879. case self::AS_HOOK_ERROR:
  880. return false;
  881. case self::AS_SUCCESS_NEW_ARTICLE:
  882. $query = $resultDetails['redirect'] ? 'redirect=no' : '';
  883. $anchor = isset ( $resultDetails['sectionanchor'] ) ? $resultDetails['sectionanchor'] : '';
  884. $wgOut->redirect( $this->mTitle->getFullURL( $query ) . $anchor );
  885. return false;
  886. case self::AS_SUCCESS_UPDATE:
  887. $extraQuery = '';
  888. $sectionanchor = $resultDetails['sectionanchor'];
  889. // Give extensions a chance to modify URL query on update
  890. wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this->mArticle, &$sectionanchor, &$extraQuery ) );
  891. if ( $resultDetails['redirect'] ) {
  892. if ( $extraQuery == '' ) {
  893. $extraQuery = 'redirect=no';
  894. } else {
  895. $extraQuery = 'redirect=no&' . $extraQuery;
  896. }
  897. }
  898. $wgOut->redirect( $this->mTitle->getFullURL( $extraQuery ) . $sectionanchor );
  899. return false;
  900. case self::AS_BLANK_ARTICLE:
  901. $wgOut->redirect( $this->getContextTitle()->getFullURL() );
  902. return false;
  903. case self::AS_SPAM_ERROR:
  904. $this->spamPageWithContent( $resultDetails['spam'] );
  905. return false;
  906. case self::AS_BLOCKED_PAGE_FOR_USER:
  907. throw new UserBlockedError( $wgUser->getBlock() );
  908. case self::AS_IMAGE_REDIRECT_ANON:
  909. case self::AS_IMAGE_REDIRECT_LOGGED:
  910. throw new PermissionsError( 'upload' );
  911. case self::AS_READ_ONLY_PAGE_ANON:
  912. case self::AS_READ_ONLY_PAGE_LOGGED:
  913. throw new PermissionsError( 'edit' );
  914. case self::AS_READ_ONLY_PAGE:
  915. throw new ReadOnlyError;
  916. case self::AS_RATE_LIMITED:
  917. throw new ThrottledError();
  918. case self::AS_NO_CREATE_PERMISSION:
  919. $permission = $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage';
  920. throw new PermissionsError( $permission );
  921. default:
  922. // We don't recognize $status->value. The only way that can happen
  923. // is if an extension hook aborted from inside ArticleSave.
  924. // Render the status object into $this->hookError
  925. // FIXME this sucks, we should just use the Status object throughout
  926. $this->hookError = '<div class="error">' . $status->getWikitext() .
  927. '</div>';
  928. return true;
  929. }
  930. }
  931. /**
  932. * Attempt submission (no UI)
  933. *
  934. * @param $result
  935. * @param $bot bool
  936. *
  937. * @return Status object, possibly with a message, but always with one of the AS_* constants in $status->value,
  938. *
  939. * FIXME: This interface is TERRIBLE, but hard to get rid of due to various error display idiosyncrasies. There are
  940. * also lots of cases where error metadata is set in the object and retrieved later instead of being returned, e.g.
  941. * AS_CONTENT_TOO_BIG and AS_BLOCKED_PAGE_FOR_USER. All that stuff needs to be cleaned up some time.
  942. */
  943. function internalAttemptSave( &$result, $bot = false ) {
  944. global $wgUser, $wgRequest, $wgParser, $wgMaxArticleSize;
  945. $status = Status::newGood();
  946. wfProfileIn( __METHOD__ );
  947. wfProfileIn( __METHOD__ . '-checks' );
  948. if ( !wfRunHooks( 'EditPage::attemptSave', array( $this ) ) ) {
  949. wfDebug( "Hook 'EditPage::attemptSave' aborted article saving\n" );
  950. $status->fatal( 'hookaborted' );
  951. $status->value = self::AS_HOOK_ERROR;
  952. wfProfileOut( __METHOD__ . '-checks' );
  953. wfProfileOut( __METHOD__ );
  954. return $status;
  955. }
  956. # Check image redirect
  957. if ( $this->mTitle->getNamespace() == NS_FILE &&
  958. Title::newFromRedirect( $this->textbox1 ) instanceof Title &&
  959. !$wgUser->isAllowed( 'upload' ) ) {
  960. $code = $wgUser->isAnon() ? self::AS_IMAGE_REDIRECT_ANON : self::AS_IMAGE_REDIRECT_LOGGED;
  961. $status->setResult( false, $code );
  962. wfProfileOut( __METHOD__ . '-checks' );
  963. wfProfileOut( __METHOD__ );
  964. return $status;
  965. }
  966. # Check for spam
  967. $match = self::matchSummarySpamRegex( $this->summary );
  968. if ( $match === false ) {
  969. $match = self::matchSpamRegex( $this->textbox1 );
  970. }
  971. if ( $match !== false ) {
  972. $result['spam'] = $match;
  973. $ip = $wgRequest->getIP();
  974. $pdbk = $this->mTitle->getPrefixedDBkey();
  975. $match = str_replace( "\n", '', $match );
  976. wfDebugLog( 'SpamRegex', "$ip spam regex hit [[$pdbk]]: \"$match\"" );
  977. $status->fatal( 'spamprotectionmatch', $match );
  978. $status->value = self::AS_SPAM_ERROR;
  979. wfProfileOut( __METHOD__ . '-checks' );
  980. wfProfileOut( __METHOD__ );
  981. return $status;
  982. }
  983. if ( !wfRunHooks( 'EditFilter', array( $this, $this->textbox1, $this->section, &$this->hookError, $this->summary ) ) ) {
  984. # Error messages etc. could be handled within the hook...
  985. $status->fatal( 'hookaborted' );
  986. $status->value = self::AS_HOOK_ERROR;
  987. wfProfileOut( __METHOD__ . '-checks' );
  988. wfProfileOut( __METHOD__ );
  989. return $status;
  990. } elseif ( $this->hookError != '' ) {
  991. # ...or the hook could be expecting us to produce an error
  992. $status->fatal( 'hookaborted' );
  993. $status->value = self::AS_HOOK_ERROR_EXPECTED;
  994. wfProfileOut( __METHOD__ . '-checks' );
  995. wfProfileOut( __METHOD__ );
  996. return $status;
  997. }
  998. if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
  999. // Auto-block user's IP if the account was "hard" blocked
  1000. $wgUser->spreadAnyEditBlock();
  1001. # Check block state against master, thus 'false'.
  1002. $status->setResult( false, self::AS_BLOCKED_PAGE_FOR_USER );
  1003. wfProfileOut( __METHOD__ . '-checks' );
  1004. wfProfileOut( __METHOD__ );
  1005. return $status;
  1006. }
  1007. $this->kblength = (int)( strlen( $this->textbox1 ) / 1024 );
  1008. if ( $this->kblength > $wgMaxArticleSize ) {
  1009. // Error will be displayed by showEditForm()
  1010. $this->tooBig = true;
  1011. $status->setResult( false, self::AS_CONTENT_TOO_BIG );
  1012. wfProfileOut( __METHOD__ . '-checks' );
  1013. wfProfileOut( __METHOD__ );
  1014. return $status;
  1015. }
  1016. if ( !$wgUser->isAllowed( 'edit' ) ) {
  1017. if ( $wgUser->isAnon() ) {
  1018. $status->setResult( false, self::AS_READ_ONLY_PAGE_ANON );
  1019. wfProfileOut( __METHOD__ . '-checks' );
  1020. wfProfileOut( __METHOD__ );
  1021. return $status;
  1022. } else {
  1023. $status->fatal( 'readonlytext' );
  1024. $status->value = self::AS_READ_ONLY_PAGE_LOGGED;
  1025. wfProfileOut( __METHOD__ . '-checks' );
  1026. wfProfileOut( __METHOD__ );
  1027. return $status;
  1028. }
  1029. }
  1030. if ( wfReadOnly() ) {
  1031. $status->fatal( 'readonlytext' );
  1032. $status->value = self::AS_READ_ONLY_PAGE;
  1033. wfProfileOut( __METHOD__ . '-checks' );
  1034. wfProfileOut( __METHOD__ );
  1035. return $status;
  1036. }
  1037. if ( $wgUser->pingLimiter() ) {
  1038. $status->fatal( 'actionthrottledtext' );
  1039. $status->value = self::AS_RATE_LIMITED;
  1040. wfProfileOut( __METHOD__ . '-checks' );
  1041. wfProfileOut( __METHOD__ );
  1042. return $status;
  1043. }
  1044. # If the article has been deleted while editing, don't save it without
  1045. # confirmation
  1046. if ( $this->wasDeletedSinceLastEdit() && !$this->recreate ) {
  1047. $status->setResult( false, self::AS_ARTICLE_WAS_DELETED );
  1048. wfProfileOut( __METHOD__ . '-checks' );
  1049. wfProfileOut( __METHOD__ );
  1050. return $status;
  1051. }
  1052. wfProfileOut( __METHOD__ . '-checks' );
  1053. # Load the page data from the master. If anything changes in the meantime,
  1054. # we detect it by using page_latest like a token in a 1 try compare-and-swap.
  1055. $this->mArticle->loadPageData( 'fromdbmaster' );
  1056. $new = !$this->mArticle->exists();
  1057. if ( $new ) {
  1058. // Late check for create permission, just in case *PARANOIA*
  1059. if ( !$this->mTitle->userCan( 'create' ) ) {
  1060. $status->fatal( 'nocreatetext' );
  1061. $status->value = self::AS_NO_CREATE_PERMISSION;
  1062. wfDebug( __METHOD__ . ": no create permission\n" );
  1063. wfProfileOut( __METHOD__ );
  1064. return $status;
  1065. }
  1066. # Don't save a new article if it's blank.
  1067. if ( $this->textbox1 == '' ) {
  1068. $status->setResult( false, self::AS_BLANK_ARTICLE );
  1069. wfProfileOut( __METHOD__ );
  1070. return $status;
  1071. }
  1072. // Run post-section-merge edit filter
  1073. if ( !wfRunHooks( 'EditFilterMerged', array( $this, $this->textbox1, &$this->hookError, $this->summary ) ) ) {
  1074. # Error messages etc. could be handled within the hook...
  1075. $status->fatal( 'hookaborted' );
  1076. $status->value = self::AS_HOOK_ERROR;
  1077. wfProfileOut( __METHOD__ );
  1078. return $status;
  1079. } elseif ( $this->hookError != '' ) {
  1080. # ...or the hook could be expecting us to produce an error
  1081. $status->fatal( 'hookaborted' );
  1082. $status->value = self::AS_HOOK_ERROR_EXPECTED;
  1083. wfProfileOut( __METHOD__ );
  1084. return $status;
  1085. }
  1086. $text = $this->textbox1;
  1087. $result['sectionanchor'] = '';
  1088. if ( $this->section == 'new' ) {
  1089. if ( $this->sectiontitle !== '' ) {
  1090. // Insert the section title above the content.
  1091. $text = wfMessage( 'newsectionheaderdefaultlevel', $this->sectiontitle )
  1092. ->inContentLanguage()->text() . "\n\n" . $text;
  1093. // Jump to the new section
  1094. $result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->sectiontitle );
  1095. // If no edit summary was specified, create one automatically from the section
  1096. // title and have it link to the new section. Otherwise, respect the summary as
  1097. // passed.
  1098. if ( $this->summary === '' ) {
  1099. $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
  1100. $this->summary = wfMessage( 'newsectionsummary' )
  1101. ->rawParams( $cleanSectionTitle )->inContentLanguage()->text();
  1102. }
  1103. } elseif ( $this->summary !== '' ) {
  1104. // Insert the section title above the content.
  1105. $text = wfMessage( 'newsectionheaderdefaultlevel', $this->summary )
  1106. ->inContentLanguage()->text() . "\n\n" . $text;
  1107. // Jump to the new section
  1108. $result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
  1109. // Create a link to the new section from the edit summary.
  1110. $cleanSummary = $wgParser->stripSectionName( $this->summary );
  1111. $this->summary = wfMessage( 'newsectionsummary' )
  1112. ->rawParams( $cleanSummary )->inContentLanguage()->text();
  1113. }
  1114. }
  1115. $status->value = self::AS_SUCCESS_NEW_ARTICLE;
  1116. } else {
  1117. # Article exists. Check for edit conflict.
  1118. $timestamp = $this->mArticle->getTimestamp();
  1119. wfDebug( "timestamp: {$timestamp}, edittime: {$this->edittime}\n" );
  1120. if ( $timestamp != $this->edittime ) {
  1121. $this->isConflict = true;
  1122. if ( $this->section == 'new' ) {
  1123. if ( $this->mArticle->getUserText() == $wgUser->getName() &&
  1124. $this->mArticle->getComment() == $this->summary ) {
  1125. // Probably a duplicate submission of a new comment.
  1126. // This can happen when squid resends a request after
  1127. // a timeout but the first one actually went through.
  1128. wfDebug( __METHOD__ . ": duplicate new section submission; trigger edit conflict!\n" );
  1129. } else {
  1130. // New comment; suppress conflict.
  1131. $this->isConflict = false;
  1132. wfDebug( __METHOD__ . ": conflict suppressed; new section\n" );
  1133. }
  1134. } elseif ( $this->section == '' && Revision::userWasLastToEdit( DB_MASTER, $this->mTitle->getArticleID(), $wgUser->getId(), $this->edittime ) ) {
  1135. # Suppress edit conflict with self, except for section edits where merging is required.
  1136. wfDebug( __METHOD__ . ": Suppressing edit conflict, same user.\n" );
  1137. $this->isConflict = false;
  1138. }
  1139. }
  1140. // If sectiontitle is set, use it, otherwise use the summary as the section title (for
  1141. // backwards compatibility with old forms/bots).
  1142. if ( $this->sectiontitle !== '' ) {
  1143. $sectionTitle = $this->sectiontitle;
  1144. } else {
  1145. $sectionTitle = $this->summary;
  1146. }
  1147. if ( $this->isConflict ) {
  1148. wfDebug( __METHOD__ . ": conflict! getting section '$this->section' for time '$this->edittime' (article time '{$timestamp}')\n" );
  1149. $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $sectionTitle, $this->edittime );
  1150. } else {
  1151. wfDebug( __METHOD__ . ": getting section '$this->section'\n" );
  1152. $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $sectionTitle );
  1153. }
  1154. if ( is_null( $text ) ) {
  1155. wfDebug( __METHOD__ . ": activating conflict; section replace failed.\n" );
  1156. $this->isConflict = true;
  1157. $text = $this->textbox1; // do not try to merge here!
  1158. } elseif ( $this->isConflict ) {
  1159. # Attempt merge
  1160. if ( $this->mergeChangesInto( $text ) ) {
  1161. // Successful merge! Maybe we should tell the user the good news?
  1162. $this->isConflict = false;
  1163. wfDebug( __METHOD__ . ": Suppressing edit conflict, successful merge.\n" );
  1164. } else {
  1165. $this->section = '';
  1166. $this->textbox1 = $text;
  1167. wfDebug( __METHOD__ . ": Keeping edit conflict, failed merge.\n" );
  1168. }
  1169. }
  1170. if ( $this->isConflict ) {
  1171. $status->setResult( false, self::AS_CONFLICT_DETECTED );
  1172. wfProfileOut( __METHOD__ );
  1173. return $status;
  1174. }
  1175. // Run post-section-merge edit filter
  1176. if ( !wfRunHooks( 'EditFilterMerged', array( $this, $text, &$this->hookError, $this->summary ) ) ) {
  1177. # Error messages etc. could be handled within the hook...
  1178. $status->fatal( 'hookaborted' );
  1179. $status->value = self::AS_HOOK_ERROR;
  1180. wfProfileOut( __METHOD__ );
  1181. return $status;
  1182. } elseif ( $this->hookError != '' ) {
  1183. # ...or the hook could be expecting us to produce an error
  1184. $status->fatal( 'hookaborted' );
  1185. $status->value = self::AS_HOOK_ERROR_EXPECTED;
  1186. wfProfileOut( __METHOD__ );
  1187. return $status;
  1188. }
  1189. # Handle the user preference to force summaries here, but not for null edits
  1190. if ( $this->section != 'new' && !$this->allowBlankSummary
  1191. && $this->getOriginalContent() != $text
  1192. && !Title::newFromRedirect( $text ) ) # check if it's not a redirect
  1193. {
  1194. if ( md5( $this->summary ) == $this->autoSumm ) {
  1195. $this->missingSummary = true;
  1196. $status->fatal( 'missingsummary' );
  1197. $status->value = self::AS_SUMMARY_NEEDED;
  1198. wfProfileOut( __METHOD__ );
  1199. return $status;
  1200. }
  1201. }
  1202. # And a similar thing for new sections
  1203. if ( $this->section == 'new' && !$this->allowBlankSummary ) {
  1204. if ( trim( $this->summary ) == '' ) {
  1205. $this->missingSummary = true;
  1206. $status->fatal( 'missingsummary' ); // or 'missingcommentheader' if $section == 'new'. Blegh
  1207. $status->value = self::AS_SUMMARY_NEEDED;
  1208. wfProfileOut( __METHOD__ );
  1209. return $status;
  1210. }
  1211. }
  1212. # All's well
  1213. wfProfileIn( __METHOD__ . '-sectionanchor' );
  1214. $sectionanchor = '';
  1215. if ( $this->section == 'new' ) {
  1216. if ( $this->textbox1 == '' ) {
  1217. $this->missingComment = true;
  1218. $status->fatal( 'missingcommenttext' );
  1219. $status->value = self::AS_TEXTBOX_EMPTY;
  1220. wfProfileOut( __METHOD__ . '-sectionanchor' );
  1221. wfProfileOut( __METHOD__ );
  1222. return $status;
  1223. }
  1224. if ( $this->sectiontitle !== '' ) {
  1225. $sectionanchor = $wgParser->guessLegacySectionNameFromWikiText( $this->sectiontitle );
  1226. // If no edit summary was specified, create one automatically from the section
  1227. // title and have it link to the new section. Otherwise, respect the summary as
  1228. // passed.
  1229. if ( $this->summary === '' ) {
  1230. $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
  1231. $this->summary = wfMessage( 'newsectionsummary' )
  1232. ->rawParams( $cleanSectionTitle )->inContentLanguage()->text();
  1233. }
  1234. } elseif ( $this->summary !== '' ) {
  1235. $sectionanchor = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
  1236. # This is a new section, so create a link to the new section
  1237. # in the revision summary.
  1238. $cleanSummary = $wgParser->stripSectionName( $this->summary );
  1239. $this->summary = wfMessage( 'newsectionsummary' )
  1240. ->rawParams( $cleanSummary )->inContentLanguage()->text();
  1241. }
  1242. } elseif ( $this->section != '' ) {
  1243. # Try to get a section anchor from the section source, redirect to edited section if header found
  1244. # XXX: might be better to integrate this into Article::replaceSection
  1245. # for duplicate heading checking and maybe parsing
  1246. $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
  1247. # we can't deal with anchors, includes, html etc in the header for now,
  1248. # headline would need to be parsed to improve this
  1249. if ( $hasmatch && strlen( $matches[2] ) > 0 ) {
  1250. $sectionanchor = $wgParser->guessLegacySectionNameFromWikiText( $matches[2] );
  1251. }
  1252. }
  1253. $result['sectionanchor'] = $sectionanchor;
  1254. wfProfileOut( __METHOD__ . '-sectionanchor' );
  1255. // Save errors may fall down to the edit form, but we've now
  1256. // merged the section into full text. Clear the section field
  1257. // so that later submission of conflict forms won't try to
  1258. // replace that into a duplicated mess.
  1259. $this->textbox1 = $text;
  1260. $this->section = '';
  1261. $status->value = self::AS_SUCCESS_UPDATE;
  1262. }
  1263. // Check for length errors again now that the section is merged in
  1264. $this->kblength = (int)( strlen( $text ) / 1024 );
  1265. if ( $this->kblength > $wgMaxArticleSize ) {
  1266. $this->tooBig = true;
  1267. $status->setResult( false, self::AS_MAX_ARTICLE_SIZE_EXCEEDED );
  1268. wfProfileOut( __METHOD__ );
  1269. return $status;
  1270. }
  1271. $flags = EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
  1272. ( $new ? EDIT_NEW : EDIT_UPDATE ) |
  1273. ( ( $this->minoredit && !$this->isNew ) ? EDIT_MINOR : 0 ) |
  1274. ( $bot ? EDIT_FORCE_BOT : 0 );
  1275. $doEditStatus = $this->mArticle->doEdit( $text, $this->summary, $flags );
  1276. if ( $doEditStatus->isOK() ) {
  1277. $result['redirect'] = Title::newFromRedirect( $text ) !== null;
  1278. $this->commitWatch();
  1279. wfProfileOut( __METHOD__ );
  1280. return $status;
  1281. } else {
  1282. // Failure from doEdit()
  1283. // Show the edit conflict page for certain recognized errors from doEdit(),
  1284. // but don't show it for errors from extension hooks
  1285. $errors = $doEditStatus->getErrorsArray();
  1286. if ( in_array( $errors[0][0], array( 'edit-gone-missing', 'edit-conflict',
  1287. 'edit-already-exists' ) ) )
  1288. {
  1289. $this->isConflict = true;
  1290. // Destroys data doEdit() put in $status->value but who cares
  1291. $doEditStatus->value = self::AS_END;
  1292. }
  1293. wfProfileOut( __METHOD__ );
  1294. return $doEditStatus;
  1295. }
  1296. }
  1297. /**
  1298. * Commit the change of watch status
  1299. */
  1300. protected function commitWatch() {
  1301. global $wgUser;
  1302. if ( $wgUser->isLoggedIn() && $this->watchthis != $wgUser->isWatched( $this->mTitle ) ) {
  1303. $dbw = wfGetDB( DB_MASTER );
  1304. $dbw->begin( __METHOD__ );
  1305. if ( $this->watchthis ) {
  1306. WatchAction::doWatch( $this->mTitle, $wgUser );
  1307. } else {
  1308. WatchAction::doUnwatch( $this->mTitle, $wgUser );
  1309. }
  1310. $dbw->commit( __METHOD__ );
  1311. }
  1312. }
  1313. /**
  1314. * @private
  1315. * @todo document
  1316. *
  1317. * @param $editText string
  1318. *
  1319. * @return bool
  1320. */
  1321. function mergeChangesInto( &$editText ) {
  1322. wfProfileIn( __METHOD__ );
  1323. $db = wfGetDB( DB_MASTER );
  1324. // This is the revision the edito…

Large files files are truncated, but you can click here to view the full file