Merge "Clarify docs for wgCookieExpiration"
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
index 4e4b2cc..2ca93f5 100644 (file)
@@ -66,6 +66,11 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        $fld_token = false;
                }
 
+               // If user can't undelete, no tokens
+               if ( !$user->isAllowed( 'undelete' ) ) {
+                       $fld_token = false;
+               }
+
                $result = $this->getResult();
                $pageSet = $this->getPageSet();
                $titles = $pageSet->getTitles();
@@ -101,8 +106,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                }
 
                $this->addTables( 'archive' );
-               $this->addWhere( 'ar_deleted = 0' );
-               $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
+               $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ) );
 
                $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
                $this->addFieldsIf( 'ar_rev_id', $fld_revid );
@@ -131,11 +135,13 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
 
                if ( $fld_content ) {
                        $this->addTables( 'text' );
+                       $this->addJoinConds(
+                               array( 'text' => array( 'INNER JOIN', array( 'ar_text_id=old_id' ) ) )
+                       );
                        $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
-                       $this->addWhere( 'ar_text_id = old_id' );
 
                        // This also means stricter restrictions
-                       if ( !$user->isAllowed( 'undelete' ) ) {
+                       if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
                                $this->dieUsage(
                                        'You don\'t have permission to view deleted revision content',
                                        'permissiondenied'
@@ -170,8 +176,12 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                } elseif ( $mode == 'all' ) {
                        $this->addWhereFld( 'ar_namespace', $params['namespace'] );
 
-                       $from = $params['from'] === null ? null : $this->titlePartToKey( $params['from'], $params['namespace'] );
-                       $to = $params['to'] === null ? null : $this->titlePartToKey( $params['to'], $params['namespace'] );
+                       $from = $params['from'] === null
+                               ? null
+                               : $this->titlePartToKey( $params['from'], $params['namespace'] );
+                       $to = $params['to'] === null
+                               ? null
+                               : $this->titlePartToKey( $params['to'], $params['namespace'] );
                        $this->addWhereRange( 'ar_title', $dir, $from, $to );
 
                        if ( isset( $params['prefix'] ) ) {
@@ -188,19 +198,49 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                $db->addQuotes( $params['excludeuser'] ) );
                }
 
-               if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) {
+               if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
+                       // Paranoia: avoid brute force searches (bug 17342)
+                       // (shouldn't be able to get here without 'deletedhistory', but
+                       // check it again just in case)
+                       if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                               $bitmask = Revision::DELETED_USER;
+                       } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
+                               $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
+                       } else {
+                               $bitmask = 0;
+                       }
+                       if ( $bitmask ) {
+                               $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
+                       }
+               }
+
+               if ( !is_null( $params['continue'] ) ) {
                        $cont = explode( '|', $params['continue'] );
-                       $this->dieContinueUsageIf( count( $cont ) != 3 );
-                       $ns = intval( $cont[0] );
-                       $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
-                       $title = $db->addQuotes( $cont[1] );
-                       $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
                        $op = ( $dir == 'newer' ? '>' : '<' );
-                       $this->addWhere( "ar_namespace $op $ns OR " .
-                               "(ar_namespace = $ns AND " .
-                               "(ar_title $op $title OR " .
-                               "(ar_title = $title AND " .
-                               "ar_timestamp $op= $ts)))" );
+                       if ( $mode == 'all' || $mode == 'revs' ) {
+                               $this->dieContinueUsageIf( count( $cont ) != 4 );
+                               $ns = intval( $cont[0] );
+                               $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
+                               $title = $db->addQuotes( $cont[1] );
+                               $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
+                               $ar_id = (int)$cont[3];
+                               $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
+                               $this->addWhere( "ar_namespace $op $ns OR " .
+                                       "(ar_namespace = $ns AND " .
+                                       "(ar_title $op $title OR " .
+                                       "(ar_title = $title AND " .
+                                       "(ar_timestamp $op $ts OR " .
+                                       "(ar_timestamp = $ts AND " .
+                                       "ar_id $op= $ar_id)))))" );
+                       } else {
+                               $this->dieContinueUsageIf( count( $cont ) != 2 );
+                               $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
+                               $ar_id = (int)$cont[1];
+                               $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
+                               $this->addWhere( "ar_timestamp $op $ts OR " .
+                                       "(ar_timestamp = $ts AND " .
+                                       "ar_id $op= $ar_id)" );
+                       }
                }
 
                $this->addOption( 'LIMIT', $limit + 1 );
@@ -210,12 +250,14 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                );
                if ( $mode == 'all' ) {
                        if ( $params['unique'] ) {
+                               // @todo Does this work on non-MySQL?
                                $this->addOption( 'GROUP BY', 'ar_title' );
                        } else {
                                $sort = ( $dir == 'newer' ? '' : ' DESC' );
                                $this->addOption( 'ORDER BY', array(
                                        'ar_title' . $sort,
-                                       'ar_timestamp' . $sort
+                                       'ar_timestamp' . $sort,
+                                       'ar_id' . $sort,
                                ) );
                        }
                } else {
@@ -225,6 +267,8 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                $this->addWhereRange( 'ar_title', $dir, null, null );
                        }
                        $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
+                       // Include in ORDER BY for uniqueness
+                       $this->addWhereRange( 'ar_id', $dir, null, null );
                }
                $res = $this->select( __METHOD__ );
                $pageMap = array(); // Maps ns&title to (fake) pageid
@@ -234,15 +278,18 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        if ( ++$count > $limit ) {
                                // We've had enough
                                if ( $mode == 'all' || $mode == 'revs' ) {
-                                       $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
-                                               $row->ar_title . '|' . $row->ar_timestamp );
+                                       $this->setContinueEnumParameter( 'continue',
+                                               "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
+                                       );
                                } else {
-                                       $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
+                                       $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
                                }
                                break;
                        }
 
                        $rev = array();
+                       $anyHidden = false;
+
                        $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
                        if ( $fld_revid ) {
                                $rev['revid'] = intval( $row->ar_rev_id );
@@ -250,21 +297,37 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
                                $rev['parentid'] = intval( $row->ar_parent_id );
                        }
-                       if ( $fld_user ) {
-                               $rev['user'] = $row->ar_user_text;
-                       }
-                       if ( $fld_userid ) {
-                               $rev['userid'] = $row->ar_user;
-                       }
-                       if ( $fld_comment ) {
-                               $rev['comment'] = $row->ar_comment;
+                       if ( $fld_user || $fld_userid ) {
+                               if ( $row->ar_deleted & Revision::DELETED_USER ) {
+                                       $rev['userhidden'] = '';
+                                       $anyHidden = true;
+                               }
+                               if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) {
+                                       if ( $fld_user ) {
+                                               $rev['user'] = $row->ar_user_text;
+                                       }
+                                       if ( $fld_userid ) {
+                                               $rev['userid'] = $row->ar_user;
+                                       }
+                               }
                        }
 
-                       $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
-
-                       if ( $fld_parsedcomment ) {
-                               $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title );
+                       if ( $fld_comment || $fld_parsedcomment ) {
+                               if ( $row->ar_deleted & Revision::DELETED_COMMENT ) {
+                                       $rev['commenthidden'] = '';
+                                       $anyHidden = true;
+                               }
+                               if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) {
+                                       if ( $fld_comment ) {
+                                               $rev['comment'] = $row->ar_comment;
+                                       }
+                                       if ( $fld_parsedcomment ) {
+                                               $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
+                                               $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title );
+                                       }
+                               }
                        }
+
                        if ( $fld_minor && $row->ar_minor_edit == 1 ) {
                                $rev['minor'] = '';
                        }
@@ -272,14 +335,26 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                $rev['len'] = $row->ar_len;
                        }
                        if ( $fld_sha1 ) {
-                               if ( $row->ar_sha1 != '' ) {
-                                       $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
-                               } else {
-                                       $rev['sha1'] = '';
+                               if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
+                                       $rev['sha1hidden'] = '';
+                                       $anyHidden = true;
+                               }
+                               if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
+                                       if ( $row->ar_sha1 != '' ) {
+                                               $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
+                                       } else {
+                                               $rev['sha1'] = '';
+                                       }
                                }
                        }
                        if ( $fld_content ) {
-                               ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
+                               if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
+                                       $rev['texthidden'] = '';
+                                       $anyHidden = true;
+                               }
+                               if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
+                                       ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
+                               }
                        }
 
                        if ( $fld_tags ) {
@@ -292,11 +367,16 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                }
                        }
 
+                       if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) {
+                               $rev['suppressed'] = '';
+                       }
+
                        if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
                                $pageID = $newPageID++;
                                $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
                                $a['revisions'] = array( $rev );
                                $result->setIndexedTagName( $a['revisions'], 'rev' );
+                               $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
                                ApiQueryBase::addTitleInfo( $a, $title );
                                if ( $fld_token ) {
                                        $a['token'] = $token;
@@ -310,10 +390,11 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        }
                        if ( !$fit ) {
                                if ( $mode == 'all' || $mode == 'revs' ) {
-                                       $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
-                                               $row->ar_title . '|' . $row->ar_timestamp );
+                                       $this->setContinueEnumParameter( 'continue',
+                                               "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
+                                       );
                                } else {
-                                       $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
+                                       $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
                                }
                                break;
                        }
@@ -407,7 +488,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        'namespace' => 'Only list pages in this namespace (3)',
                        'user' => 'Only list revisions by this user',
                        'excludeuser' => 'Don\'t list revisions by this user',
-                       'continue' => 'When more results are available, use this to continue (1, 3)',
+                       'continue' => 'When more results are available, use this to continue',
                        'unique' => 'List only one revision for each page (3)',
                        'tag' => 'Only list revisions tagged with this tag',
                );
@@ -431,12 +512,12 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                return array(
                        'List deleted revisions.',
                        'Operates in three modes:',
-                       ' 1) List deleted revisions for the given title(s), sorted by timestamp',
-                       ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
-                       " 3) List all deleted revisions in the given namespace, sorted by title and timestamp',
-                       '    (no titles specified, {$p}user not set)",
+                       ' 1) List deleted revisions for the given title(s), sorted by timestamp.',
+                       ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified).',
+                       ' 3) List all deleted revisions in the given namespace, sorted by title and timestamp',
+                       "    (no titles specified, {$p}user not set).",
                        'Certain parameters only apply to some modes and are ignored in others.',
-                       'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
+                       'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
                );
        }