Code housekeeping stuff (and barring any stuff-ups on my behalf, there should be...
[lhc/web/wiklou.git] / includes / Article.php
index c3ef0fb..d85a24b 100644 (file)
@@ -48,7 +48,7 @@ class Article {
                $this->mOldId = $oldId;
                $this->clear();
        }
-       
+
        /**
         * Tell the page view functions that this view was redirected
         * from another page on the wiki.
@@ -79,7 +79,7 @@ class Article {
                                }
                        } else {
                                if( $rt->getNamespace() == NS_SPECIAL ) {
-                                       // Gotta hand redirects to special pages differently:
+                                       // Gotta handle redirects to special pages differently:
                                        // Fill the HTTP response "Location" header and ignore
                                        // the rest of the page we're on.
                                        //
@@ -139,7 +139,7 @@ class Article {
         * @return Return the text of this revision
        */
        function getContent() {
-               global $wgRequest, $wgUser, $wgOut;
+               global $wgUser, $wgOut;
 
                wfProfileIn( __METHOD__ );
 
@@ -236,9 +236,6 @@ class Article {
 
                # Pre-fill content with error message so that if something
                # fails we'll have something telling us what we intended.
-
-               $t = $this->mTitle->getPrefixedText();
-
                $this->mOldId = $oldid;
                $this->fetchContent( $oldid );
        }
@@ -575,13 +572,10 @@ class Article {
        function getContributors($limit = 0, $offset = 0) {
                # XXX: this is expensive; cache this info somewhere.
 
-               $title = $this->mTitle;
                $contribs = array();
                $dbr =& wfGetDB( DB_SLAVE );
                $revTable = $dbr->tableName( 'revision' );
                $userTable = $dbr->tableName( 'user' );
-               $encDBkey = $dbr->addQuotes( $title->getDBkey() );
-               $ns = $title->getNamespace();
                $user = $this->getUser();
                $pageId = $this->getId();
 
@@ -784,9 +778,6 @@ class Article {
                                if( !$wasRedirected && $this->isCurrent() ) {
                                        $wgOut->setSubtitle( wfMsgHtml( 'redirectpagesub' ) );
                                }
-                               $targetUrl = $rt->escapeLocalURL();
-                               # fixme unused $titleText :
-                               $titleText = htmlspecialchars( $rt->getPrefixedText() );
                                $link = $sk->makeLinkObj( $rt );
 
                                $wgOut->addHTML( '<img src="'.$imageUrl.'" alt="#REDIRECT" />' .
@@ -1154,7 +1145,7 @@ class Article {
         * @deprecated use Article::doEdit()
         */
        function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false, $comment=false ) {
-               $flags = EDIT_NEW | EDIT_DEFER_UPDATES |
+               $flags = EDIT_NEW | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
                        ( $isminor ? EDIT_MINOR : 0 ) |
                        ( $suppressRC ? EDIT_SUPPRESS_RC : 0 );
 
@@ -1186,7 +1177,7 @@ class Article {
         * @deprecated use Article::doEdit()
         */
        function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' ) {
-               $flags = EDIT_UPDATE | EDIT_DEFER_UPDATES |
+               $flags = EDIT_UPDATE | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
                        ( $minor ? EDIT_MINOR : 0 ) |
                        ( $forceBot ? EDIT_FORCE_BOT : 0 );
 
@@ -1235,6 +1226,8 @@ class Article {
         *          Mark the edit a "bot" edit regardless of user rights
         *      EDIT_DEFER_UPDATES
         *          Defer some of the updates until the end of index.php
+        *      EDIT_AUTOSUMMARY
+        *          Fill in blank summaries with generated text where possible
         * 
         * If neither EDIT_NEW nor EDIT_UPDATE is specified, the status of the article will be detected. 
         * If EDIT_UPDATE is specified and the article doesn't exist, the function will return false. If 
@@ -1272,6 +1265,14 @@ class Article {
                $isminor = ( $flags & EDIT_MINOR ) && $wgUser->isAllowed('minoredit');
                $bot = $wgUser->isAllowed( 'bot' ) || ( $flags & EDIT_FORCE_BOT );
 
+               $oldtext = $this->getContent();
+               $oldsize = strlen( $oldtext );
+               $newsize = strlen( $text );
+
+               # Provide autosummaries if one is not provided.
+               if ($flags & EDIT_AUTOSUMMARY && $summary == '')
+                       $summary = $this->getAutosummary( $oldtext, $text, $flags );
+
                $text = $this->preSaveTransform( $text );
 
                $dbw =& wfGetDB( DB_MASTER );
@@ -1285,9 +1286,6 @@ class Article {
                                $userAbort = ignore_user_abort( true );
                        }
 
-                       $oldtext = $this->getContent();
-                       $oldsize = strlen( $oldtext );
-                       $newsize = strlen( $text );
                        $lastRevision = 0;
                        $revisionId = 0;
 
@@ -1917,7 +1915,7 @@ class Article {
         */
        function doDeleteArticle( $reason ) {
                global $wgUseSquid, $wgDeferredUpdateList;
-               global $wgPostCommitUpdateList, $wgUseTrackbacks;
+               global $wgUseTrackbacks;
 
                wfDebug( __METHOD__."\n" );
 
@@ -2034,8 +2032,6 @@ class Article {
                $bot = $wgRequest->getBool( 'bot' );
 
                # Replace all this user's current edits with the next one down
-               $tt = $this->mTitle->getDBKey();
-               $n = $this->mTitle->getNamespace();
 
                # Get the last editor
                $current = Revision::newFromTitle( $this->mTitle );
@@ -2262,6 +2258,9 @@ class Article {
                $lnk = $current
                        ? wfMsg( 'currentrevisionlink' )
                        : $lnk = $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'currentrevisionlink' ) );
+               $curdiff = $current
+                       ? wfMsg( 'diff' )
+                       : $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'diff' ), 'diff=cur&oldid='.$oldid );
                $prev = $this->mTitle->getPreviousRevisionID( $oldid ) ;
                $prevlink = $prev
                        ? $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'previousrevision' ), 'direction=prev&oldid='.$oldid )
@@ -2279,7 +2278,8 @@ class Article {
                $userlinks = $sk->userLink( $revision->getUser(), $revision->getUserText() )
                                                . $sk->userToolLinks( $revision->getUser(), $revision->getUserText() );
                
-               $r = wfMsg( 'old-revision-navigation', $td, $lnk, $prevlink, $nextlink, $userlinks, $prevdiff, $nextdiff );
+               $r = "\n\t\t\t\t<div id=\"mw-revision-info\">" . wfMsg( 'revision-info', $td, $userlinks ) . "</div>\n" .
+                    "\n\t\t\t\t<div id=\"mw-revision-nav\">" . wfMsg( 'revision-nav', $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>\n\t\t\t";
                $wgOut->setSubtitle( $r );
        }
 
@@ -2398,8 +2398,7 @@ class Article {
                        'comment'    => $comment,
                        'minor_edit' => $minor ? 1 : 0,
                        ) );
-               # fixme : $revisionId never used
-               $revisionId = $revision->insertOn( $dbw );
+               $revision->insertOn( $dbw );
                $this->updateRevisionOn( $dbw, $revision );
                $dbw->commit();
 
@@ -2448,7 +2447,7 @@ class Article {
                        if ($wgDBtype == 'mysql')
                                $dbw->query("LOCK TABLES $hitcounterTable WRITE");
                        $tabletype = $wgDBtype == 'mysql' ? "ENGINE=HEAP " : '';
-                       $dbw->query("CREATE TEMPORARY TABLE $acchitsTable $tabletype".
+                       $dbw->query("CREATE TEMPORARY TABLE $acchitsTable $tabletype AS".
                                "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable ".
                                'GROUP BY hc_id');
                        $dbw->query("DELETE FROM $hitcounterTable");
@@ -2513,8 +2512,6 @@ class Article {
        static function onArticleEdit( $title ) {
                global $wgDeferredUpdateList, $wgUseFileCache;
 
-               $urls = array();
-
                // Invalidate caches of articles which include this page
                $update = new HTMLCacheUpdate( $title, 'templatelinks' );
                $wgDeferredUpdateList[] = $update;
@@ -2650,6 +2647,85 @@ class Article {
                $dbr->freeResult( $res );
                return $result;
        }
+       
+       /**
+        * Return an auto-generated summary if the text provided is a redirect.
+        *
+        * @param  string $text The wikitext to check
+        * @return string '' or an appropriate summary
+        */
+       public static function getRedirectAutosummary( $text ) {
+               $rt = Title::newFromRedirect( $text );
+               if( is_object( $rt ) )
+                       return wfMsgForContent( 'autoredircomment', $rt->getPrefixedText() );
+               else
+                       return '';
+       }
+
+       /**
+        * Return an auto-generated summary if the new text is much shorter than
+        * the old text.
+        *
+        * @param  string $oldtext The previous text of the page
+        * @param  string $text    The submitted text of the page
+        * @return string An appropriate autosummary, or an empty string.
+        */
+       public static function getBlankingAutosummary( $oldtext, $text ) {
+               if ($oldtext!='' && $text=='') {
+                       return wfMsgForContent('autosumm-blank');
+               } elseif (strlen($oldtext) > 10 * strlen($text) && strlen($text) < 500) {
+                       #Removing more than 90% of the article
+                       global $wgContLang;
+                       $truncatedtext = $wgContLang->truncate($text, max(0, 200 - strlen(wfMsgForContent('autosumm-replace'))), '...');
+                       return wfMsgForContent('autosumm-replace', $truncatedtext);
+               } else {
+                       return '';
+               }
+       }
+
+       /**
+       * Return an applicable autosummary if one exists for the given edit.
+       * @param string $oldtext The previous text of the page.
+       * @param string $newtext The submitted text of the page.
+       * @param bitmask $flags A bitmask of flags submitted for the edit.
+       * @return string An appropriate autosummary, or an empty string.
+       */
+       public static function getAutosummary( $oldtext, $newtext, $flags ) {
+
+               # This code is UGLY UGLY UGLY.
+               # Somebody PLEASE come up with a more elegant way to do it.
+
+               $summary = '';
+
+               #Blanking autosummaries
+               if (!($flags & EDIT_NEW))
+                       $summary = self::getBlankingAutosummary( $oldtext, $newtext );
+
+               if ($summary)
+                       return $summary;
+
+               #New redirect autosummaries.
+               if ( $flags & EDIT_NEW ) {
+                       $summary = self::getRedirectAutosummary( $newtext );
+               }
+
+               if ($summary)
+                       return $summary;
+
+               #New page autosummaries
+               if ($flags & EDIT_NEW && strlen($newtext) <= 500) {
+                       #If they're making a new short article, give its text in the summary.
+                       global $wgContLang;
+                       $truncatedtext = $wgContLang->truncate( $newtext, max( 0, 200 - 
+                               strlen( wfMsgForContent( 'autosumm-shortnew') ) ), '...' );
+                       $summary = wfMsgForContent( 'autosumm-shortnew', $truncatedtext );
+               }
+
+               if ($summary)
+                       return $summary;
+
+               return $summary;
+       }
 }
 
 ?>