* Respect read-only mode on block removals
[lhc/web/wiklou.git] / includes / Article.php
index 1e7f443..62f3129 100644 (file)
@@ -320,7 +320,7 @@ class Article {
                                # the rest of the page we're on.
                                if( $globalTitle ) {
                                        global $wgOut;
-                                       if ( $rt->getInterwiki() != '' ) {
+                                       if ( $rt->getInterwiki() != '' && $rt->isLocal() ) {
                                                $wgOut->redirect( $rt->getFullURL() ) ;
                                                return false;
                                        }
@@ -675,7 +675,7 @@ class Article {
                $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->getID() != 0 &&
+               if ( $wgUseRCPatrol && !is_null ( $rcid ) && $rcid != 0 && $wgUser->isLoggedIn() &&
                     ( $wgUser->isAllowed('patrol') || !$wgOnlySysopsCanPatrol ) )
                {
                        $wgOut->addHTML( wfMsg ( 'markaspatrolledlink',
@@ -718,10 +718,9 @@ class Article {
                else { $redir = 0; }
 
                $now = wfTimestampNow();
-               $won = wfInvertTimestamp( $now );
                wfSeedRandom();
                $rand = wfRandom();
-               $isminor = ( $isminor && $wgUser->getID() ) ? 1 : 0;
+               $isminor = ( $isminor && $wgUser->isLoggedIn() ) ? 1 : 0;
                
                $mungedText = $text;
                $flags = Revision::compressRevisionText( $mungedText );
@@ -759,7 +758,6 @@ class Article {
                        'rev_timestamp' => $dbw->timestamp($now),
                        'rev_minor_edit' => $isminor,
                        'rev_user_text' => $wgUser->getName(),
-                       'inverse_timestamp' => $won,
                ), $fname );
 
                $this->mTitle->resetArticleID( $newid );
@@ -915,7 +913,7 @@ class Article {
                $good = true;
 
                if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
-               if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
+               if ( $minor && $wgUser->isLoggedIn() ) { $me2 = 1; } else { $me2 = 0; }
                if ( $this->isRedirect( $text ) ) {
                        # Remove all content but redirect
                        # This could be done by reconstructing the redirect from a title given by 
@@ -948,7 +946,6 @@ class Article {
                        $this->mCountAdjustment = $this->isCountable( $text )
                          - $this->isCountable( $oldtext );
                        $now = wfTimestampNow();
-                       $won = wfInvertTimestamp( $now );
 
                        $mungedText = $text;
                        $flags = Revision::compressRevisionText( $mungedText );
@@ -977,7 +974,6 @@ class Article {
                                        'rev_user' => $wgUser->getID(),
                                        'rev_user_text' => $wgUser->getName(),
                                        'rev_timestamp' => $dbw->timestamp( $now ),
-                                       'inverse_timestamp' => $won
                                ), $fname
                        );
                        
@@ -1085,27 +1081,6 @@ class Article {
                $wgEnotif->NotifyOnPageChange( $wgUser->getID(), $this->mTitle->getDBkey(), $this->mTitle->getNamespace(),$now, $summary, $me2, $oldid );
        }
 
-       /**
-        * Validate article
-        * @todo document this function a bit more
-        */
-       function validate () {
-               global $wgOut, $wgUseValidation;
-               if( $wgUseValidation ) {
-                       require_once ( 'SpecialValidate.php' ) ;
-                       $wgOut->setPagetitle( wfMsg( 'validate' ) . ': ' . $this->mTitle->getPrefixedText() );
-                       $wgOut->setRobotpolicy( 'noindex,follow' );
-                       if( $this->mTitle->getNamespace() != 0 ) {
-                               $wgOut->addHTML( wfMsg( 'val_validate_article_namespace_only' ) );
-                               return;
-                       }
-                       $v = new Validation;
-                       $v->validate_form( $this->mTitle->getDBkey() );
-               } else {
-                       $wgOut->errorpage( 'nosuchaction', 'nosuchactiontext' );
-               }
-       }
-
        /**
         * Mark this particular edit as patrolled
         */
@@ -1118,7 +1093,7 @@ class Article {
                        $wgOut->errorpage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
                        return;
                }
-               if ( $wgUser->getID() == 0 )
+               if ( $wgUser->isAnon() )
                {
                        $wgOut->loginToUse();
                        return;
@@ -1153,7 +1128,7 @@ class Article {
                
                global $wgUser, $wgOut;
 
-               if ( 0 == $wgUser->getID() ) {
+               if ( $wgUser->isAnon() ) {
                        $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
                        return;
                }
@@ -1188,7 +1163,7 @@ class Article {
 
                global $wgUser, $wgOut;
 
-               if ( 0 == $wgUser->getID() ) {
+               if ( $wgUser->isAnon() ) {
                        $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
                        return;
                }
@@ -1235,7 +1210,9 @@ class Article {
                        return;
                }
 
-               $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
+               $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) &&
+                       $wgRequest->wasPosted() &&
+                       $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
                $moveonly = $wgRequest->getBool( 'wpMoveOnly' );
                $reason = $wgRequest->getText( 'wpReasonProtect' );
 
@@ -1287,7 +1264,7 @@ class Article {
         * Output protection confirmation dialog
         */
        function confirmProtect( $par, $reason, $limit = 'sysop'  ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                wfDebug( "Article::confirmProtect\n" );
 
@@ -1316,6 +1293,7 @@ class Article {
                }
 
                $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
+               $token = htmlspecialchars( $wgUser->editToken() );
 
                $wgOut->addHTML( "
 <form id='protectconfirm' method='post' action=\"{$formaction}\">
@@ -1358,6 +1336,7 @@ class Article {
                        </td>
                </tr>
        </table>
+       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
 </form>\n" );
 
                $wgOut->returnToMain( false );
@@ -1376,7 +1355,9 @@ class Article {
        function delete() {
                global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
                $fname = 'Article::delete';
-               $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
+               $confirm = $wgRequest->getBool( 'wpConfirm' ) &&
+                       $wgRequest->wasPosted() &&
+                       $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
                $reason = $wgRequest->getText( 'wpReason' );
 
                # This code desperately needs to be totally rewritten
@@ -1416,7 +1397,7 @@ class Article {
                                'page_namespace' => $ns,
                                'page_title' => $title,
                                'rev_page = page_id'
-                       ), $fname, $this->getSelectOptions( array( 'ORDER BY' => 'inverse_timestamp' ) )
+                       ), $fname, $this->getSelectOptions( array( 'ORDER BY' => 'rev_timestamp DESC' ) )
                );
 
                if( $dbr->numRows( $revisions ) > 1 && !$confirm ) {
@@ -1485,7 +1466,7 @@ class Article {
         * Output deletion confirmation dialog
         */
        function confirmDelete( $par, $reason ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                wfDebug( "Article::confirmDelete\n" );
 
@@ -1499,6 +1480,7 @@ class Article {
                $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
                $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
                $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
+               $token = htmlspecialchars( $wgUser->editToken() );
 
                $wgOut->addHTML( "
 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
@@ -1529,6 +1511,7 @@ class Article {
                        </td>
                </tr>
        </table>
+       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
 </form>\n" );
 
                $wgOut->returnToMain( false );
@@ -1684,6 +1667,13 @@ class Article {
                        $wgOut->readOnlyPage( $this->getContent( true ) );
                        return;
                }
+               if( !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ),
+                       array( $this->mTitle->getPrefixedText(),
+                               $wgRequest->getVal( 'from' ) )  ) ) {
+                       $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
+                       $wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
+                       return;
+               }
                $dbw =& wfGetDB( DB_MASTER );
 
                # Enhanced rollback, marks edits rc_bot=1
@@ -1908,7 +1898,7 @@ class Article {
                return $wgUseFileCache
                        and (!$wgShowIPinHeader)
                        and ($this->getID() != 0)
-                       and ($wgUser->getId() == 0)
+                       and ($wgUser->isAnon())
                        and (!$wgUser->getNewtalk())
                        and ($this->mTitle->getNamespace() != NS_SPECIAL )
                        and (empty( $action ) || $action == 'view')
@@ -1956,7 +1946,7 @@ class Article {
                $ns = $this->mTitle->getNamespace();
                $dbkey = $this->mTitle->getDBkey();
                $encDbKey = $dbw->strencode( $dbkey );
-               $timestamp = wfTimestampNow();
+               $timestamp = $dbw->timestamp();
                # insert new text
                $dbw->insert( 'text', array(
                                'old_text' => $text,
@@ -1981,7 +1971,6 @@ class Article {
                        'rev_user' => $wgUser->getID(),
                        'rev_user_text' => $wgUser->getName(),
                        'rev_timestamp' => $timestamp,
-                       'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
                        'rev_minor_edit' => intval($minor) ),
                        $fname );
                wfProfileOut( $fname );
@@ -2083,79 +2072,92 @@ class Article {
 
        /**
         * Info about this page
+        * Called for ?action=info when $wgAllowPageInfo is on.
+        *
+        * @access public
         */
        function info() {
-               global $wgUser, $wgTitle, $wgOut, $wgAllowPageInfo;
+               global $wgLang, $wgOut, $wgAllowPageInfo;
                $fname = 'Article::info';
 
-wfDebugDieBacktrace( 'This function is apparently not called by any other PHP file and might be obsolete.' );
-
                if ( !$wgAllowPageInfo ) {
                        $wgOut->errorpage( 'nosuchaction', 'nosuchactiontext' );
                        return;
                }
 
-               $dbr =& $this->getDB();
-
-               $basenamespace = $wgTitle->getNamespace() & (~1);
-               $cur_clause = array( 'cur_title' => $wgTitle->getDBkey(), 'cur_namespace' => $basenamespace );
-               $old_clause = array( 'old_title' => $wgTitle->getDBkey(), 'old_namespace' => $basenamespace );
-               $wl_clause  = array( 'wl_title' => $wgTitle->getDBkey(), 'wl_namespace' => $basenamespace );
-               $fullTitle = $wgTitle->makeName($basenamespace, $wgTitle->getDBKey());
-               $wgOut->setPagetitle(  $fullTitle );
+               $page = $this->mTitle->getSubjectPage();
+               
+               $wgOut->setPagetitle( $page->getPrefixedText() );
                $wgOut->setSubtitle( wfMsg( 'infosubtitle' ));
 
                # first, see if the page exists at all.
-               $exists = $dbr->selectField( 'cur', 'COUNT(*)', $cur_clause, $fname, $this->getSelectOptions() );
-               if ($exists < 1) {
+               $exists = $page->getArticleId() != 0;
+               if( !$exists ) {
                        $wgOut->addHTML( wfMsg('noarticletext') );
                } else {
-                       $numwatchers = $dbr->selectField( 'watchlist', 'COUNT(*)', $wl_clause, $fname,
+                       $dbr =& $this->getDB( DB_SLAVE );       
+                       $wl_clause = array(
+                               'wl_title'     => $page->getDBkey(),
+                               'wl_namespace' => $page->getNamespace() );
+                       $numwatchers = $dbr->selectField(
+                               'watchlist',
+                               'COUNT(*)',
+                               $wl_clause,
+                               $fname,
                                $this->getSelectOptions() );
-                       $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers", $numwatchers) . '</li>' );
-                       $old = $dbr->selectField( 'old', 'COUNT(*)', $old_clause, $fname, $this->getSelectOptions() );
-                       $wgOut->addHTML( "<li>" . wfMsg('numedits', $old + 1) . '</li>');
 
-                       # to find number of distinct authors, we need to do some
-                       # funny stuff because of the cur/old table split:
-                       # - first, find the name of the 'cur' author
-                       # - then, find the number of *other* authors in 'old'
-
-                       # find 'cur' author
-                       $cur_author = $dbr->selectField( 'cur', 'cur_user_text', $cur_clause, $fname,
-                               $this->getSelectOptions() );
-
-                       # find number of 'old' authors excluding 'cur' author
-                       $authors = $dbr->selectField( 'old', 'COUNT(DISTINCT old_user_text)',
-                               $old_clause + array( 'old_user_text<>' . $dbr->addQuotes( $cur_author ) ), $fname,
-                               $this->getSelectOptions() ) + 1;
-
-                       # now for the Talk page ...
-                       $cur_clause = array( 'cur_title' => $wgTitle->getDBkey(), 'cur_namespace' => $basenamespace+1 );
-                       $old_clause = array( 'old_title' => $wgTitle->getDBkey(), 'old_namespace' => $basenamespace+1 );
-
-                       # does it exist?
-                       $exists = $dbr->selectField( 'cur', 'COUNT(*)', $cur_clause, $fname, $this->getSelectOptions() );
-
-                       # number of edits
-                       if ($exists > 0) {
-                               $old = $dbr->selectField( 'old', 'COUNT(*)', $old_clause, $fname, $this->getSelectOptions() );
-                               $wgOut->addHTML( '<li>' . wfMsg("numtalkedits", $old + 1) . '</li>');
+                       $pageInfo = $this->pageCountInfo( $page );
+                       $talkInfo = $this->pageCountInfo( $page->getTalkPage() );
+                       
+                       $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers", $wgLang->formatNum( $numwatchers ) ) . '</li>' );
+                       $wgOut->addHTML( "<li>" . wfMsg('numedits', $wgLang->formatNum( $pageInfo['edits'] ) ) . '</li>');
+                       if( $talkInfo ) {
+                               $wgOut->addHTML( '<li>' . wfMsg("numtalkedits", $wgLang->formatNum( $talkInfo['edits'] ) ) . '</li>');
                        }
-                       $wgOut->addHTML( '<li>' . wfMsg("numauthors", $authors) . '</li>' );
-
-                       # number of authors
-                       if ($exists > 0) {
-                               $cur_author = $dbr->selectField( 'cur', 'cur_user_text', $cur_clause, $fname,
-                                       $this->getSelectOptions() );
-                               $authors = $dbr->selectField( 'cur', 'COUNT(DISTINCT old_user_text)',
-                                       $old_clause + array( 'old_user_text<>' . $dbr->addQuotes( $cur_author ) ),
-                                       $fname, $this->getSelectOptions() );
-
-                               $wgOut->addHTML( '<li>' . wfMsg('numtalkauthors', $authors) . '</li></ul>' );
+                       $wgOut->addHTML( '<li>' . wfMsg("numauthors", $wgLang->formatNum( $pageInfo['authors'] ) ) . '</li>' );
+                       if( $talkInfo ) {
+                               $wgOut->addHTML( '<li>' . wfMsg('numtalkauthors', $wgLang->formatNum( $talkInfo['authors'] ) ) . '</li>' );
                        }
+                       $wgOut->addHTML( '</ul>' );
+
                }
        }
+       
+       /**
+        * Return the total number of edits and number of unique editors
+        * on a given page. If page does not exist, returns false.
+        *
+        * @param Title $title
+        * @return array
+        * @access private
+        */
+       function pageCountInfo( $title ) {
+               $id = $title->getArticleId();
+               if( $id == 0 ) {
+                       return false;
+               }
+               
+               $dbr =& $this->getDB( DB_SLAVE );       
+
+               $rev_clause = array( 'rev_page' => $id );
+               $fname = 'Article::pageCountInfo';
+
+               $edits = $dbr->selectField(
+                       'revision',
+                       'COUNT(rev_page)',
+                       $rev_clause,
+                       $fname,
+                       $this->getSelectOptions() );
+
+               $authors = $dbr->selectField(
+                       'revision',
+                       'COUNT(DISTINCT rev_user_text)',
+                       $rev_clause,
+                       $fname,
+                       $this->getSelectOptions() );
+               
+               return array( 'edits' => $edits, 'authors' => $authors );
+       }
 }
 
 /**