* (bug 2151) The delete summary now includes editor name, if only one has edited...
[lhc/web/wiklou.git] / includes / Article.php
index 2d20807..5f872f2 100644 (file)
@@ -14,9 +14,9 @@ $wgArticleCurContentFields = false;
 $wgArticleOldContentFields = false;
 
 /**
- * Class representing a Wikipedia article and history.
+ * Class representing a MediaWiki article and history.
  *
- * See design.doc for an overview.
+ * See design.txt for an overview.
  * Note: edit user interface and cache support functions have been
  * moved to separate EditPage and CacheManager classes.
  *
@@ -34,6 +34,7 @@ class Article {
        var $mId, $mTable;
        var $mForUpdate;
        var $mOldId;
+       var $mRevIdFetched;
        /**#@-*/
 
        /**
@@ -68,6 +69,7 @@ class Article {
                $this->mTouched = '19700101000000';
                $this->mForUpdate = false;
                $this->mIsRedirect = false;
+               $this->mRevIdFetched = 0;
        }
 
        /**
@@ -78,7 +80,7 @@ class Article {
         * @return Return the text of this revision
        */
        function getContent( $noredir ) {
-               global $wgRequest;
+               global $wgRequest, $wgUser;
 
                # Get variables from query string :P
                $action = $wgRequest->getText( 'action', 'view' );
@@ -98,7 +100,7 @@ class Article {
                        $this->loadContent( $noredir );
                        # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
                        if ( $this->mTitle->getNamespace() == NS_USER_TALK &&
-                         preg_match('/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/',$this->mTitle->getText()) &&
+                         $wgUser->isIP($this->mTitle->getText()) &&
                          $action=='view'
                        ) {
                                wfProfileOut( $fname );
@@ -426,7 +428,9 @@ class Article {
                $this->mComment   = $revision->getComment();
                $this->mTimestamp = wfTimestamp( TS_MW, $revision->getTimestamp() );
                
+               $this->mRevIdFetched = $revision->getID();
                $this->mContentLoaded = true;
+               
                return $this->mContent;
        }
 
@@ -519,7 +523,7 @@ class Article {
        function isRedirect( $text = false ) {
                if ( $text === false ) {
                        $this->loadContent();
-                       $titleObj = Title::newFromRedirect( $this->mText );
+                       $titleObj = Title::newFromRedirect( $this->fetchRevisionText() );
                } else {
                        $titleObj = Title::newFromRedirect( $text );
                }
@@ -575,6 +579,11 @@ class Article {
                $this->loadLastEdit();
                return $this->mMinorEdit;
        }
+       
+       function getRevIdFetched() {
+               $this->loadLastEdit();
+               return $this->mRevIdFetched;
+       }
 
        function getContributors($limit = 0, $offset = 0) {
                $fname = 'Article::getContributors';
@@ -637,8 +646,12 @@ class Article {
                if ( !is_null( $diff ) ) {
                        require_once( 'DifferenceEngine.php' );
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+                       
                        $de = new DifferenceEngine( $oldid, $diff, $rcid );
+                       // DifferenceEngine directly fetched the revision:
+                       $this->mRevIdFetched = $de->mNewid;
                        $de->showDiffPage();
+
                        if( $diff == 0 ) {
                                # Run view updates for current revision only
                                $this->viewUpdates();
@@ -659,7 +672,7 @@ class Article {
                        }
                }
                # Should the parser cache be used?
-               if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
+               if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) && $this->getID() ) {
                        $pcache = true;
                } else {
                        $pcache = false;
@@ -737,16 +750,27 @@ class Article {
                                $wgOut->addWikiText( $text );
                        }
                }
-               $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+               /* title may have been set from the cache */
+               $t = $wgOut->getPageTitle();    
+               if( empty( $t ) ) {
+                       $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+               }
+
                # If we have been passed an &rcid= parameter, we want to give the user a
                # chance to mark this new article as patrolled.
-               if ( $wgUseRCPatrol && !is_null ( $rcid ) && $rcid != 0 && $wgUser->isLoggedIn() &&
-                    ( $wgUser->isAllowed('patrol') || !$wgOnlySysopsCanPatrol ) )
+               if ( $wgUseRCPatrol
+                       && !is_null($rcid)
+                       && $rcid != 0
+                       && $wgUser->isLoggedIn()
+                       && ( $wgUser->isAllowed('patrol') || !$wgOnlySysopsCanPatrol ) )
                {
-                       $wgOut->addHTML( wfMsg ( 'markaspatrolledlink',
-                               $sk->makeKnownLinkObj ( $this->mTitle, wfMsg ( 'markaspatrolledtext' ),
-                                       'action=markpatrolled&rcid='.$rcid )
-                        ) );
+                       $wgOut->addHTML(
+                               "<div class='patrollink'>" .
+                                       wfMsg ( 'markaspatrolledlink',
+                                       $sk->makeKnownLinkObj( $this->mTitle, wfMsg('markaspatrolledtext'), "action=markpatrolled&rcid=$rcid" )
+                                       ) .
+                               '</div>'
+                        );
                }
 
                # Put link titles into the link cache
@@ -875,7 +899,7 @@ class Article {
         * errors at some point.
         * @private
         */
-       function insertNewArticle( $text, $summary, $isminor, $watchthis ) {
+       function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false ) {
                global $wgOut, $wgUser;
                global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
 
@@ -887,6 +911,7 @@ class Article {
                $ttl = $this->mTitle->getDBkey();
                $text = $this->preSaveTransform( $text );
                $isminor = ( $isminor && $wgUser->isLoggedIn() ) ? 1 : 0;
+               $now = wfTimestampNow();
 
                $dbw =& wfGetDB( DB_MASTER );
 
@@ -908,7 +933,9 @@ class Article {
                $this->updateRevisionOn( $dbw, $revision, 0 );
 
                Article::onArticleCreate( $this->mTitle );
-               RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
+               if(!$suppressRC) {
+                       RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
+               }
 
                if ($watchthis) {
                        if(!$this->mTitle->userIsWatching()) $this->watch();
@@ -936,6 +963,8 @@ class Article {
        /**
         * Fetch and uncompress the text for a given revision.
         * Can ask by rev_id number or timestamp (set $field)
+        * FIXME: This function is broken. Eliminate all uses and remove.
+        * Use Revision class in place.
         */
        function fetchRevisionText( $revId = null, $field = 'rev_id' ) {
                $fname = 'Article::fetchRevisionText';
@@ -965,12 +994,15 @@ class Article {
        
        function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = '', $edittime = NULL) {
                $fname = 'Article::getTextOfLastEditWithSectionReplacedOrAdded';
-               if( is_null( $edittime ) ) {
-                       $oldtext = $this->fetchRevisionText();
-               } else {
-                       $oldtext = $this->fetchRevisionText( $edittime, 'rev_timestamp' );
-               }
                if ($section != '') {
+                       if( is_null( $edittime ) ) {
+                               $rev = Revision::newFromTitle( $this->mTitle );
+                       } else {
+                               $dbw =& wfGetDB( DB_MASTER );
+                               $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
+                       }
+                       $oldtext = $rev->getText();
+                       
                        if($section=='new') {
                                if($summary) $subject="== {$summary} ==\n\n";
                                $text=$oldtext."\n\n".$subject.$text;
@@ -1072,19 +1104,19 @@ class Article {
 
                $text = $this->preSaveTransform( $text );
                $dbw =& wfGetDB( DB_MASTER );
+               $now = wfTimestampNow();
 
                # Update article, but only if changed.
 
                # It's important that we either rollback or complete, otherwise an attacker could
                # overwrite cur entries by sending precisely timed user aborts. Random bored users
                # could conceivably have the same effect, especially if cur is locked for long periods.
-               if( $wgDBtransactions ) {
-                       $dbw->query( 'BEGIN', $fname );
-               } else {
+               if( !$wgDBtransactions ) {
                        $userAbort = ignore_user_abort( true );
                }
 
                $oldtext = $this->getContent( true );
+               $lastRevision = 0;
 
                if ( 0 != strcmp( $text, $oldtext ) ) {
                        $this->mCountAdjustment = $this->isCountable( $text )
@@ -1111,15 +1143,13 @@ class Article {
                        } else {
                                # Update recentchanges and purge cache and whatnot
                                $bot = (int)($wgUser->isBot() || $forceBot);
-                               RecentChange::notifyEdit( $now, $this->mTitle, $me2, $wgUser, $summary,
+                               RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $wgUser, $summary,
                                        $lastRevision, $this->getTimestamp(), $bot );
                                Article::onArticleEdit( $this->mTitle );
                        }
                }
 
-               if( $wgDBtransactions ) {
-                       $dbw->query( 'COMMIT', $fname );
-               } else {
+               if( !$wgDBtransactions ) {
                        ignore_user_abort( $userAbort );
                }
 
@@ -1155,7 +1185,7 @@ class Article {
                                array_push( $wgPostCommitUpdateList, $u );
                        }
        
-                       $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor, $me2, $now, $summary, $lastRevision );
+                       $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor, $isminor, $now, $summary, $lastRevision );
                }
                return $good;
        }
@@ -1193,7 +1223,7 @@ class Article {
 
                include_once( "UserMailer.php" );
                $wgEnotif = new EmailNotification ();
-               $wgEnotif->NotifyOnPageChange( $wgUser->getID(), $this->mTitle->getDBkey(), $this->mTitle->getNamespace(),$now, $summary, $me2, $oldid );
+               $wgEnotif->notifyOnPageChange( $this->mTitle, $now, $summary, $me2, $oldid );
        }
 
        /**
@@ -1442,9 +1472,6 @@ class Article {
                        <td align='left'>
                                <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
                        </td>
-               </tr>
-               <tr>
-                       <td>&nbsp;</td>
                </tr>" );
                if($moveonly != '') {
                        $wgOut->AddHTML( "
@@ -1452,7 +1479,7 @@ class Article {
                        <td align='right'>
                                <input type='checkbox' name='wpMoveOnly' value='1' id='wpMoveOnly' />
                        </td>
-                       <td>
+                       <td align='left'>
                                <label for='wpMoveOnly'>{$moveonly}</label>
                        </td>
                </tr> " );
@@ -1466,7 +1493,7 @@ class Article {
                </tr>
        </table>
        <input type='hidden' name='wpEditToken' value=\"{$token}\" />
-</form>\n" );
+</form>" );
 
                $wgOut->returnToMain( false );
        }
@@ -1520,7 +1547,7 @@ class Article {
                $ns = $this->mTitle->getNamespace();
                $title = $this->mTitle->getDBkey();
                $revisions = $dbr->select( array( 'page', 'revision' ),
-                       array( 'rev_id' ),
+                       array( 'rev_id', 'rev_user_text' ),
                        array(
                                'page_namespace' => $ns,
                                'page_title' => $title,
@@ -1535,28 +1562,32 @@ class Article {
                }
 
                # Fetch cur_text
-               $s = $dbr->selectRow( array( 'page', 'text' ),
-                       array( 'old_text' ),
-                       array(
-                               'page_namespace' => $ns,
-                               'page_title' => $title,
-                               'page_latest = old_id'
-                       ), $fname, $this->getSelectOptions()
-               );
-
-               if( $s !== false ) {
+               $rev =& Revision::newFromTitle( $this->mTitle );
+               
+               # Fetch name(s) of contributors
+               $rev_name = '';
+               $all_same_user = true;
+               while ( $row = $dbr->fetchObject( $revisions ) ) {
+                       if ( $rev_name != '' && $rev_name != $row->rev_user_text ) {
+                               $all_same_user = false;
+                       } else {
+                               $rev_name = $row->rev_user_text;
+                       }
+               }
+               
+               if( !is_null( $rev ) ) {
                        # if this is a mini-text, we can paste part of it into the deletion reason
+                       $text = $rev->getText();
 
                        #if this is empty, an earlier revision may contain "useful" text
                        $blanked = false;
-                       if($s->old_text != '') {
-                               $text=$s->old_text;
-                       } else {
-                               if($old) { # TODO
-                                       $text = Revision::getRevisionText( $old );
+                       if($text == '') {
+                               $prevId = $this->mTitle->getPreviousRevisionID( $rev->getId() );
+                               $rev = Revision::newFromId( $prevId );
+                               if ( $rev ) { 
+                                       $text = $rev->getText();
                                        $blanked = true;
                                }
-
                        }
 
                        $length=strlen($text);
@@ -1577,13 +1608,18 @@ class Article {
                                $text=preg_replace('/\</','&lt;',$text);
                                $text=preg_replace('/\>/','&gt;',$text);
                                $text=preg_replace("/[\n\r]/",'',$text);
+                               
+                               if( $length > 150 ) { $text .= '...'; } # we've only pasted part of the text
+                               
                                if(!$blanked) {
-                                       $reason=wfMsg('excontent'). " '".$text;
+                                       if(!$all_same_user) {
+                                               $reason = wfMsg ( 'excontent', $text );
+                                       } else {
+                                               $reason = wfMsg ( 'excontentauthor', $text, $rev_name );
+                                       }
                                } else {
-                                       $reason=wfMsg('exbeforeblank') . " '".$text;
+                                       $reason = wfMsg ( 'exbeforeblank', $text );
                                }
-                               if($length>150) { $reason .= '...'; } # we've only pasted part of the text
-                               $reason.="'";
                        }
                }
 
@@ -1620,9 +1656,6 @@ class Article {
                                <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
                        </td>
                </tr>
-               <tr>
-                       <td>&nbsp;</td>
-               </tr>
                <tr>
                        <td>&nbsp;</td>
                        <td>
@@ -1653,8 +1686,8 @@ class Article {
                                $wgOut->setRobotpolicy( 'noindex,nofollow' );
                                
                                $sk = $wgUser->getSkin();
-                               $loglink = $sk->makeKnownLink( $wgContLang->getNsText( NS_PROJECT ) .
-                                                                                          ':' . wfMsgForContent( 'dellogpage' ),
+                               $loglink = $sk->makeKnownLink( $wgContLang->getNsText( NS_SPECIAL ) .
+                                                                                          ':Log/delete',
                                                                                           wfMsg( 'deletionlog' ) );
                                
                                $text = wfMsg( 'deletedtext', $deleted, $loglink );
@@ -1700,11 +1733,8 @@ class Article {
                                $this->mTitle->getInternalURL(),
                                $this->mTitle->getInternalURL( 'history' )
                        );
-                       foreach ( $linksTo as $linkTo ) {
-                               $urls[] = $linkTo->getInternalURL();
-                       }
 
-                       $u = new SquidUpdate( $urls );
+                       $u = SquidUpdate::newFromTitles( $linksTo, $urls );
                        array_push( $wgPostCommitUpdateList, $u );
 
                }
@@ -1712,33 +1742,39 @@ class Article {
                # Client and file cache invalidation
                Title::touchArray( $linksTo );
 
-               # Move article and history to the "archive" table
 
-               $dbw->insertSelect( 'archive', array( 'page','revision', 'text' ),
+               // For now, shunt the revision data into the archive table.
+               // Text is *not* removed from the text table; bulk storage
+               // is left intact to avoid breaking block-compression or
+               // immutable storage schemes.
+               //
+               // For backwards compatibility, note that some older archive
+               // table entries will have ar_text and ar_flags fields still.
+               //
+               // In the future, we may keep revisions and mark them with
+               // the rev_deleted field, which is reserved for this purpose.
+               $dbw->insertSelect( 'archive', array( 'page', 'revision' ),
                        array(
                                'ar_namespace'  => 'page_namespace',
                                'ar_title'      => 'page_title',
-                               'ar_text'       => 'old_text',
                                'ar_comment'    => 'rev_comment',
                                'ar_user'       => 'rev_user',
                                'ar_user_text'  => 'rev_user_text',
                                'ar_timestamp'  => 'rev_timestamp',
                                'ar_minor_edit' => 'rev_minor_edit',
-                               'ar_flags'      => 'old_flags',
                                'ar_rev_id'     => 'rev_id',
+                               'ar_text_id'    => 'rev_text_id',
                        ), array(
-                               'page_namespace' => $ns,
-                               'page_title' => $t,
-                               'page_id = rev_page AND old_id = rev_id'
+                               'page_id' => $id,
+                               'page_id = rev_page'
                        ), $fname
                );
-
+               
                # Now that it's safely backed up, delete it
-
-               $dbw->deleteJoin( 'text', 'revision', 'old_id', 'rev_id', array( "rev_page = {$id}" ), $fname );
                $dbw->delete( 'revision', array( 'rev_page' => $id ), $fname );
                $dbw->delete( 'page', array( 'page_id' => $id ), $fname);
+               
+               # Clean up recentchanges entries...
                $dbw->delete( 'recentchanges', array( 'rc_namespace' => $ns, 'rc_title' => $t ), $fname );
 
                # Finally, clean up the link tables
@@ -1899,7 +1935,8 @@ class Article {
 
                global $wgUser;
                if ($this->mTitle->getNamespace() == NS_USER_TALK &&
-                       $this->mTitle->getText() == $wgUser->getName()) {
+                       $this->mTitle->getText() == $wgUser->getName()) 
+               {
                        require_once( 'UserTalkUpdate.php' );
                        $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(), $this->mTitle->getDBkey(), false, false, false );
                } else {
@@ -2127,8 +2164,8 @@ class Article {
                                'GROUP BY hc_id');
                        $dbw->query("DELETE FROM $hitcounterTable");
                        $dbw->query('UNLOCK TABLES');
-                       $dbw->query("UPDATE $curTable,$acchitsTable SET cur_counter=cur_counter + hc_n ".
-                               'WHERE cur_id = hc_id');
+                       $dbw->query("UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n ".
+                               'WHERE page_id = hc_id');
                        $dbw->query("DROP TABLE $acchitsTable");
 
                        ignore_user_abort( $old_user_abort );
@@ -2264,6 +2301,35 @@ class Article {
                
                return array( 'edits' => $edits, 'authors' => $authors );
        }
+
+       /**
+        * Return a list of templates used by this article.
+        * Uses the links table to find the templates
+        *
+        * @return array
+        */
+       function getUsedTemplates() {
+               $result = array();
+               $id = $this->mTitle->getArticleID();
+
+               $db =& wfGetDB( DB_SLAVE );
+               $page = $db->tableName( 'page' );
+               $links = $db->tableName( 'links' );
+               $sql = "SELECT page_title ".
+                       "FROM $page,$links WHERE l_to=page_id AND l_from={$id} and page_namespace=".NS_TEMPLATE;
+               $res = $db->query( $sql, "Article:getUsedTemplates" );
+               if ( false !== $res ) {
+                       if ( $db->numRows( $res ) ) {
+                               while ( $row = $db->fetchObject( $res ) ) {
+                                       $result[] = $row->page_title;
+                               }
+                       }
+               }
+               $db->freeResult( $res );
+               return $result;
+       }
+
+
 }