Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / content / ContentHandler.php
index 0509e29..edfc81c 100644 (file)
@@ -754,82 +754,190 @@ abstract class ContentHandler {
                return false;
        }
 
+       /**
+        * Return type of change if one exists for the given edit.
+        *
+        * @since 1.31
+        *
+        * @param Content|null $oldContent The previous text of the page.
+        * @param Content|null $newContent The submitted text of the page.
+        * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
+        *
+        * @return string|null String key representing type of change, or null.
+        */
+       private function getChangeType(
+               Content $oldContent = null,
+               Content $newContent = null,
+               $flags = 0
+       ) {
+               $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null;
+               $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null;
+
+               // We check for the type of change in the given edit, and return string key accordingly
+
+               // Blanking of a page
+               if ( $oldContent && $oldContent->getSize() > 0 &&
+                       $newContent && $newContent->getSize() === 0
+               ) {
+                       return 'blank';
+               }
+
+               // Redirects
+               if ( $newTarget ) {
+                       if ( !$oldTarget ) {
+                               // New redirect page (by creating new page or by changing content page)
+                               return 'new-redirect';
+                       } elseif ( !$newTarget->equals( $oldTarget ) ||
+                               $oldTarget->getFragment() !== $newTarget->getFragment()
+                       ) {
+                               // Redirect target changed
+                               return 'changed-redirect-target';
+                       }
+               } elseif ( $oldTarget ) {
+                       // Changing an existing redirect into a non-redirect
+                       return 'removed-redirect';
+               }
+
+               // New page created
+               if ( $flags & EDIT_NEW && $newContent ) {
+                       if ( $newContent->getSize() === 0 ) {
+                               // New blank page
+                               return 'newblank';
+                       } else {
+                               return 'newpage';
+                       }
+               }
+
+               // Removing more than 90% of the page
+               if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
+                       return 'replace';
+               }
+
+               // Content model changed
+               if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
+                       return 'contentmodelchange';
+               }
+
+               return null;
+       }
+
        /**
         * Return an applicable auto-summary if one exists for the given edit.
         *
         * @since 1.21
         *
-        * @param Content $oldContent The previous text of the page.
-        * @param Content $newContent The submitted text of the page.
+        * @param Content|null $oldContent The previous text of the page.
+        * @param Content|null $newContent The submitted text of the page.
         * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
         *
         * @return string An appropriate auto-summary, or an empty string.
         */
-       public function getAutosummary( Content $oldContent = null, Content $newContent = null,
-               $flags ) {
-               // Decide what kind of auto-summary is needed.
-
-               // Redirect auto-summaries
-
-               /**
-                * @var $ot Title
-                * @var $rt Title
-                */
+       public function getAutosummary(
+               Content $oldContent = null,
+               Content $newContent = null,
+               $flags = 0
+       ) {
+               $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
 
-               $ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
-               $rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
+               // There's no applicable auto-summary for our case, so our auto-summary is empty.
+               if ( !$changeType ) {
+                       return '';
+               }
 
-               if ( is_object( $rt ) ) {
-                       if ( !is_object( $ot )
-                               || !$rt->equals( $ot )
-                               || $ot->getFragment() != $rt->getFragment()
-                       ) {
+               // Decide what kind of auto-summary is needed.
+               switch ( $changeType ) {
+                       case 'new-redirect':
+                               $newTarget = $newContent->getRedirectTarget();
                                $truncatedtext = $newContent->getTextForSummary(
                                        250
                                        - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
-                                       - strlen( $rt->getFullText() ) );
+                                       - strlen( $newTarget->getFullText() )
+                               );
 
-                               return wfMessage( 'autoredircomment', $rt->getFullText() )
-                                       ->rawParams( $truncatedtext )->inContentLanguage()->text();
-                       }
-               }
+                               return wfMessage( 'autoredircomment', $newTarget->getFullText() )
+                                       ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
+                       case 'changed-redirect-target':
+                               $oldTarget = $oldContent->getRedirectTarget();
+                               $newTarget = $newContent->getRedirectTarget();
 
-               // New page auto-summaries
-               if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
-                       // If they're making a new article, give its text, truncated, in
-                       // the summary.
+                               $truncatedtext = $newContent->getTextForSummary(
+                                       250
+                                       - strlen( wfMessage( 'autosumm-changed-redirect-target' )
+                                               ->inContentLanguage()->text() )
+                                       - strlen( $oldTarget->getFullText() )
+                                       - strlen( $newTarget->getFullText() )
+                               );
+
+                               return wfMessage( 'autosumm-changed-redirect-target',
+                                               $oldTarget->getFullText(),
+                                               $newTarget->getFullText() )
+                                       ->rawParams( $truncatedtext )->inContentLanguage()->text();
+                       case 'removed-redirect':
+                               $oldTarget = $oldContent->getRedirectTarget();
+                               $truncatedtext = $newContent->getTextForSummary(
+                                       250
+                                       - strlen( wfMessage( 'autosumm-removed-redirect' )
+                                               ->inContentLanguage()->text() )
+                                       - strlen( $oldTarget->getFullText() ) );
 
-                       $truncatedtext = $newContent->getTextForSummary(
-                               200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
+                               return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
+                                       ->rawParams( $truncatedtext )->inContentLanguage()->text();
+                       case 'newpage':
+                               // If they're making a new article, give its text, truncated, in the summary.
+                               $truncatedtext = $newContent->getTextForSummary(
+                                       200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
 
-                       return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
-                               ->inContentLanguage()->text();
+                               return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
+                                       ->inContentLanguage()->text();
+                       case 'blank':
+                               return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
+                       case 'replace':
+                               $truncatedtext = $newContent->getTextForSummary(
+                                       200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
+
+                               return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
+                                       ->inContentLanguage()->text();
+                       case 'newblank':
+                               return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
+                       default:
+                               return '';
                }
+       }
 
-               // Blanking auto-summaries
-               if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
-                       return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
-               } elseif ( !empty( $oldContent )
-                       && $oldContent->getSize() > 10 * $newContent->getSize()
-                       && $newContent->getSize() < 500
-               ) {
-                       // Removing more than 90% of the article
-
-                       $truncatedtext = $newContent->getTextForSummary(
-                               200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
+       /**
+        * Return an applicable tag if one exists for the given edit or return null.
+        *
+        * @since 1.31
+        *
+        * @param Content|null $oldContent The previous text of the page.
+        * @param Content|null $newContent The submitted text of the page.
+        * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
+        *
+        * @return string|null An appropriate tag, or null.
+        */
+       public function getChangeTag(
+               Content $oldContent = null,
+               Content $newContent = null,
+               $flags = 0
+       ) {
+               $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
 
-                       return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
-                               ->inContentLanguage()->text();
+               // There's no applicable tag for this change.
+               if ( !$changeType ) {
+                       return null;
                }
 
-               // New blank article auto-summary
-               if ( $flags & EDIT_NEW && $newContent->isEmpty() ) {
-                       return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
+               // Core tags use the same keys as ones returned from $this->getChangeType()
+               // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
+               // if this type of change should be tagged
+               $tag = 'mw-' . $changeType;
+
+               // Not all change types are tagged, so we check against the list of defined tags.
+               if ( in_array( $tag, ChangeTags::getSoftwareTags() ) ) {
+                       return $tag;
                }
 
-               // If we reach this point, there's no applicable auto-summary for our
-               // case, so our auto-summary is empty.
-               return '';
+               return null;
        }
 
        /**