Merge "Use a LinkBatch for the creator/last editor user (talk) pages in InfoAction"
[lhc/web/wiklou.git] / includes / EditPage.php
index ad6c98d..ce8077d 100644 (file)
@@ -577,13 +577,15 @@ class EditPage {
        }
 
        /**
-        * Does this EditPage class support section editing?
-        * This is used by EditPage subclasses to indicate their ui cannot handle section edits
+        * Returns whether section editing is supported for the current page.
+        * Subclasses may override this to replace the default behavior, which is
+        * to check ContentHandler::supportsSections.
         *
-        * @return bool
+        * @return bool true if this edit page supports sections, false otherwise.
         */
        protected function isSectionEditSupported() {
-               return true;
+               $contentHandler = ContentHandler::getForTitle( $this->mTitle );
+               return $contentHandler->supportsSections();
        }
 
        /**
@@ -597,6 +599,11 @@ class EditPage {
 
                # Section edit can come from either the form or a link
                $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
+
+               if ( $this->section !== null && $this->section !== '' && !$this->isSectionEditSupported() ) {
+                       throw new ErrorPageError( 'sectioneditnotsupported-title', 'sectioneditnotsupported-text' );
+               }
+
                $this->isNew = !$this->mTitle->exists() || $this->section == 'new';
 
                if ( $request->wasPosted() ) {
@@ -610,15 +617,18 @@ class EditPage {
                                // modified by subclasses
                                wfProfileIn( get_class( $this ) . "::importContentFormData" );
                                $textbox1 = $this->importContentFormData( $request );
-                               if ( isset( $textbox1 ) ) {
+                               if ( $textbox1 !== null ) {
                                        $this->textbox1 = $textbox1;
                                }
 
                                wfProfileOut( get_class( $this ) . "::importContentFormData" );
                        }
 
+                       # Trim spaces on user supplied text
+                       $summary = trim( $request->getText( 'wpSummary' ) );
+
                        # Truncate for whole multibyte characters
-                       $this->summary = $wgContLang->truncate( $request->getText( 'wpSummary' ), 255 );
+                       $this->summary = $wgContLang->truncate( $summary, 255 );
 
                        # If the summary consists of a heading, e.g. '==Foobar==', extract the title from the
                        # header syntax, e.g. 'Foobar'. This is mainly an issue when we are using wpSummary for
@@ -1218,6 +1228,54 @@ class EditPage {
                }
        }
 
+       /**
+        * Run hooks that can filter edits just before they get saved.
+        *
+        * @param Content $content the Content to filter.
+        * @param Status  $status for reporting the outcome to the caller
+        * @param User    $user the user performing the edit
+        *
+        * @return bool
+        */
+       protected function runPostMergeFilters( Content $content, Status $status, User $user ) {
+               // Run old style post-section-merge edit filter
+               if ( !ContentHandler::runLegacyHooks( 'EditFilterMerged',
+                       array( $this, $content, &$this->hookError, $this->summary ) ) ) {
+
+                       # Error messages etc. could be handled within the hook...
+                       $status->fatal( 'hookaborted' );
+                       $status->value = self::AS_HOOK_ERROR;
+                       return false;
+               } elseif ( $this->hookError != '' ) {
+                       # ...or the hook could be expecting us to produce an error
+                       $status->fatal( 'hookaborted' );
+                       $status->value = self::AS_HOOK_ERROR_EXPECTED;
+                       return false;
+               }
+
+               // Run new style post-section-merge edit filter
+               if ( !wfRunHooks( 'EditFilterMergedContent',
+                       array( $this->mArticle->getContext(), $content, $status, $this->summary,
+                               $user, $this->minoredit ) ) ) {
+
+                       # Error messages etc. could be handled within the hook...
+                       // XXX: $status->value may already be something informative...
+                       $this->hookError = $status->getWikiText();
+                       $status->fatal( 'hookaborted' );
+                       $status->value = self::AS_HOOK_ERROR;
+                       return false;
+               } elseif ( !$status->isOK() ) {
+                       # ...or the hook could be expecting us to produce an error
+                       // FIXME this sucks, we should just use the Status object throughout
+                       $this->hookError = $status->getWikiText();
+                       $status->fatal( 'hookaborted' );
+                       $status->value = self::AS_HOOK_ERROR_EXPECTED;
+                       return false;
+               }
+
+               return true;
+       }
+
        /**
         * Attempt submission (no UI)
         *
@@ -1235,7 +1293,7 @@ class EditPage {
 
                $status = Status::newGood();
 
-               wfProfileIn( __METHOD__  );
+               wfProfileIn( __METHOD__ );
                wfProfileIn( __METHOD__ . '-checks' );
 
                if ( !wfRunHooks( 'EditPage::attemptSave', array( $this ) ) ) {
@@ -1243,7 +1301,7 @@ class EditPage {
                        $status->fatal( 'hookaborted' );
                        $status->value = self::AS_HOOK_ERROR;
                        wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__  );
+                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1253,6 +1311,7 @@ class EditPage {
                } catch ( MWContentSerializationException $ex ) {
                        $status->fatal( 'content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );
                        $status->value = self::AS_PARSE_ERROR;
+                       wfProfileOut( __METHOD__ . '-checks' );
                        wfProfileOut( __METHOD__ );
                        return $status;
                }
@@ -1265,7 +1324,7 @@ class EditPage {
                                $status->setResult( false, $code );
 
                                wfProfileOut( __METHOD__ . '-checks' );
-                               wfProfileOut( __METHOD__  );
+                               wfProfileOut( __METHOD__ );
 
                                return $status;
                }
@@ -1386,17 +1445,7 @@ class EditPage {
                                return $status;
                        }
 
-                       // Run post-section-merge edit filter
-                       if ( !wfRunHooks( 'EditFilterMerged', array( $this, $this->textbox1, &$this->hookError, $this->summary ) ) ) {
-                               # Error messages etc. could be handled within the hook...
-                               $status->fatal( 'hookaborted' );
-                               $status->value = self::AS_HOOK_ERROR;
-                               wfProfileOut( __METHOD__ );
-                               return $status;
-                       } elseif ( $this->hookError != '' ) {
-                               # ...or the hook could be expecting us to produce an error
-                               $status->fatal( 'hookaborted' );
-                               $status->value = self::AS_HOOK_ERROR_EXPECTED;
+                       if ( !$this->runPostMergeFilters( $textbox_content, $status, $wgUser ) ) {
                                wfProfileOut( __METHOD__ );
                                return $status;
                        }
@@ -1510,20 +1559,7 @@ class EditPage {
                                return $status;
                        }
 
-                       // Run post-section-merge edit filter
-                       $hook_args = array( $this, $content, &$this->hookError, $this->summary );
-
-                       if ( !ContentHandler::runLegacyHooks( 'EditFilterMerged', $hook_args )
-                               || !wfRunHooks( 'EditFilterMergedContent', $hook_args ) ) {
-                               # Error messages etc. could be handled within the hook...
-                               $status->fatal( 'hookaborted' );
-                               $status->value = self::AS_HOOK_ERROR;
-                               wfProfileOut( __METHOD__ );
-                               return $status;
-                       } elseif ( $this->hookError != '' ) {
-                               # ...or the hook could be expecting us to produce an error
-                               $status->fatal( 'hookaborted' );
-                               $status->value = self::AS_HOOK_ERROR_EXPECTED;
+                       if ( !$this->runPostMergeFilters( $content, $status, $wgUser ) ) {
                                wfProfileOut( __METHOD__ );
                                return $status;
                        }
@@ -1624,12 +1660,7 @@ class EditPage {
                        $doEditStatus = $this->mArticle->doEditContent( $content, $this->summary, $flags,
                                                                                                                        false, null, $this->contentFormat );
 
-               if ( $doEditStatus->isOK() ) {
-                               $result['redirect'] = $content->isRedirect();
-                       $this->updateWatchlist();
-                       wfProfileOut( __METHOD__ );
-                       return $status;
-               } else {
+               if ( !$doEditStatus->isOK() ) {
                        // Failure from doEdit()
                        // Show the edit conflict page for certain recognized errors from doEdit(),
                        // but don't show it for errors from extension hooks
@@ -1644,6 +1675,11 @@ class EditPage {
                        wfProfileOut( __METHOD__ );
                        return $doEditStatus;
                }
+
+               $result['redirect'] = $content->isRedirect();
+               $this->updateWatchlist();
+               wfProfileOut( __METHOD__ );
+               return $status;
        }
 
        /**
@@ -1735,10 +1771,10 @@ class EditPage {
                        $editContent = $result;
                        wfProfileOut( __METHOD__ );
                        return true;
-               } else {
-                       wfProfileOut( __METHOD__ );
-                       return false;
                }
+
+               wfProfileOut( __METHOD__ );
+               return false;
        }
 
        /**