RevDelRevisionList.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup RevisionDelete
  20. */
  21. use Wikimedia\Rdbms\FakeResultWrapper;
  22. use Wikimedia\Rdbms\IDatabase;
  23. /**
  24. * List for revision table items
  25. *
  26. * This will check both the 'revision' table for live revisions and the
  27. * 'archive' table for traditionally-deleted revisions that have an
  28. * ar_rev_id saved.
  29. *
  30. * See RevDelRevisionItem and RevDelArchivedRevisionItem for items.
  31. */
  32. class RevDelRevisionList extends RevDelList {
  33. /** @var int */
  34. public $currentRevId;
  35. public function getType() {
  36. return 'revision';
  37. }
  38. public static function getRelationType() {
  39. return 'rev_id';
  40. }
  41. public static function getRestriction() {
  42. return 'deleterevision';
  43. }
  44. public static function getRevdelConstant() {
  45. return Revision::DELETED_TEXT;
  46. }
  47. public static function suggestTarget( $target, array $ids ) {
  48. $rev = Revision::newFromId( $ids[0] );
  49. return $rev ? $rev->getTitle() : $target;
  50. }
  51. /**
  52. * @param IDatabase $db
  53. * @return mixed
  54. */
  55. public function doQuery( $db ) {
  56. $ids = array_map( 'intval', $this->ids );
  57. $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] );
  58. $queryInfo = [
  59. 'tables' => $revQuery['tables'],
  60. 'fields' => $revQuery['fields'],
  61. 'conds' => [
  62. 'rev_page' => $this->title->getArticleID(),
  63. 'rev_id' => $ids,
  64. ],
  65. 'options' => [
  66. 'ORDER BY' => 'rev_id DESC',
  67. 'USE INDEX' => [ 'revision' => 'PRIMARY' ] // workaround for MySQL bug (T104313)
  68. ],
  69. 'join_conds' => $revQuery['joins'],
  70. ];
  71. ChangeTags::modifyDisplayQuery(
  72. $queryInfo['tables'],
  73. $queryInfo['fields'],
  74. $queryInfo['conds'],
  75. $queryInfo['join_conds'],
  76. $queryInfo['options'],
  77. ''
  78. );
  79. $live = $db->select(
  80. $queryInfo['tables'],
  81. $queryInfo['fields'],
  82. $queryInfo['conds'],
  83. __METHOD__,
  84. $queryInfo['options'],
  85. $queryInfo['join_conds']
  86. );
  87. if ( $live->numRows() >= count( $ids ) ) {
  88. // All requested revisions are live, keeps things simple!
  89. return $live;
  90. }
  91. $arQuery = Revision::getArchiveQueryInfo();
  92. $archiveQueryInfo = [
  93. 'tables' => $arQuery['tables'],
  94. 'fields' => $arQuery['fields'],
  95. 'conds' => [
  96. 'ar_rev_id' => $ids,
  97. ],
  98. 'options' => [ 'ORDER BY' => 'ar_rev_id DESC' ],
  99. 'join_conds' => $arQuery['joins'],
  100. ];
  101. ChangeTags::modifyDisplayQuery(
  102. $archiveQueryInfo['tables'],
  103. $archiveQueryInfo['fields'],
  104. $archiveQueryInfo['conds'],
  105. $archiveQueryInfo['join_conds'],
  106. $archiveQueryInfo['options'],
  107. ''
  108. );
  109. // Check if any requested revisions are available fully deleted.
  110. $archived = $db->select(
  111. $archiveQueryInfo['tables'],
  112. $archiveQueryInfo['fields'],
  113. $archiveQueryInfo['conds'],
  114. __METHOD__,
  115. $archiveQueryInfo['options'],
  116. $archiveQueryInfo['join_conds']
  117. );
  118. if ( $archived->numRows() == 0 ) {
  119. return $live;
  120. } elseif ( $live->numRows() == 0 ) {
  121. return $archived;
  122. } else {
  123. // Combine the two! Whee
  124. $rows = [];
  125. foreach ( $live as $row ) {
  126. $rows[$row->rev_id] = $row;
  127. }
  128. foreach ( $archived as $row ) {
  129. $rows[$row->ar_rev_id] = $row;
  130. }
  131. krsort( $rows );
  132. return new FakeResultWrapper( array_values( $rows ) );
  133. }
  134. }
  135. public function newItem( $row ) {
  136. if ( isset( $row->rev_id ) ) {
  137. return new RevDelRevisionItem( $this, $row );
  138. } elseif ( isset( $row->ar_rev_id ) ) {
  139. return new RevDelArchivedRevisionItem( $this, $row );
  140. } else {
  141. // This shouldn't happen. :)
  142. throw new MWException( 'Invalid row type in RevDelRevisionList' );
  143. }
  144. }
  145. public function getCurrent() {
  146. if ( is_null( $this->currentRevId ) ) {
  147. $dbw = wfGetDB( DB_MASTER );
  148. $this->currentRevId = $dbw->selectField(
  149. 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
  150. }
  151. return $this->currentRevId;
  152. }
  153. public function getSuppressBit() {
  154. return Revision::DELETED_RESTRICTED;
  155. }
  156. public function doPreCommitUpdates() {
  157. $this->title->invalidateCache();
  158. return Status::newGood();
  159. }
  160. public function doPostCommitUpdates( array $visibilityChangeMap ) {
  161. $this->title->purgeSquid();
  162. // Extensions that require referencing previous revisions may need this
  163. Hooks::run( 'ArticleRevisionVisibilitySet', [ $this->title, $this->ids, $visibilityChangeMap ] );
  164. return Status::newGood();
  165. }
  166. }