Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / revisiondelete / RevDelRevisionList.php
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
22 use Wikimedia\Rdbms\FakeResultWrapper;
23
24 /**
25 * List for revision table items
26 *
27 * This will check both the 'revision' table for live revisions and the
28 * 'archive' table for traditionally-deleted revisions that have an
29 * ar_rev_id saved.
30 *
31 * See RevDelRevisionItem and RevDelArchivedRevisionItem for items.
32 */
33 class RevDelRevisionList extends RevDelList {
34 /** @var int */
35 public $currentRevId;
36
37 public function getType() {
38 return 'revision';
39 }
40
41 public static function getRelationType() {
42 return 'rev_id';
43 }
44
45 public static function getRestriction() {
46 return 'deleterevision';
47 }
48
49 public static function getRevdelConstant() {
50 return Revision::DELETED_TEXT;
51 }
52
53 public static function suggestTarget( $target, array $ids ) {
54 $rev = Revision::newFromId( $ids[0] );
55 return $rev ? $rev->getTitle() : $target;
56 }
57
58 /**
59 * @param IDatabase $db
60 * @return mixed
61 */
62 public function doQuery( $db ) {
63 $ids = array_map( 'intval', $this->ids );
64 $queryInfo = [
65 'tables' => [ 'revision', 'page', 'user' ],
66 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
67 'conds' => [
68 'rev_page' => $this->title->getArticleID(),
69 'rev_id' => $ids,
70 ],
71 'options' => [
72 'ORDER BY' => 'rev_id DESC',
73 'USE INDEX' => [ 'revision' => 'PRIMARY' ] // workaround for MySQL bug (T104313)
74 ],
75 'join_conds' => [
76 'page' => Revision::pageJoinCond(),
77 'user' => Revision::userJoinCond(),
78 ],
79 ];
80 ChangeTags::modifyDisplayQuery(
81 $queryInfo['tables'],
82 $queryInfo['fields'],
83 $queryInfo['conds'],
84 $queryInfo['join_conds'],
85 $queryInfo['options'],
86 ''
87 );
88
89 $live = $db->select(
90 $queryInfo['tables'],
91 $queryInfo['fields'],
92 $queryInfo['conds'],
93 __METHOD__,
94 $queryInfo['options'],
95 $queryInfo['join_conds']
96 );
97 if ( $live->numRows() >= count( $ids ) ) {
98 // All requested revisions are live, keeps things simple!
99 return $live;
100 }
101
102 $archiveQueryInfo = [
103 'tables' => [ 'archive' ],
104 'fields' => Revision::selectArchiveFields(),
105 'conds' => [
106 'ar_rev_id' => $ids,
107 ],
108 'options' => [ 'ORDER BY' => 'ar_rev_id DESC' ],
109 'join_conds' => [],
110 ];
111
112 ChangeTags::modifyDisplayQuery(
113 $archiveQueryInfo['tables'],
114 $archiveQueryInfo['fields'],
115 $archiveQueryInfo['conds'],
116 $archiveQueryInfo['join_conds'],
117 $archiveQueryInfo['options'],
118 ''
119 );
120
121 // Check if any requested revisions are available fully deleted.
122 $archived = $db->select(
123 $archiveQueryInfo['tables'],
124 $archiveQueryInfo['fields'],
125 $archiveQueryInfo['conds'],
126 __METHOD__,
127 $archiveQueryInfo['options'],
128 $archiveQueryInfo['join_conds']
129 );
130
131 if ( $archived->numRows() == 0 ) {
132 return $live;
133 } elseif ( $live->numRows() == 0 ) {
134 return $archived;
135 } else {
136 // Combine the two! Whee
137 $rows = [];
138 foreach ( $live as $row ) {
139 $rows[$row->rev_id] = $row;
140 }
141 foreach ( $archived as $row ) {
142 $rows[$row->ar_rev_id] = $row;
143 }
144 krsort( $rows );
145 return new FakeResultWrapper( array_values( $rows ) );
146 }
147 }
148
149 public function newItem( $row ) {
150 if ( isset( $row->rev_id ) ) {
151 return new RevDelRevisionItem( $this, $row );
152 } elseif ( isset( $row->ar_rev_id ) ) {
153 return new RevDelArchivedRevisionItem( $this, $row );
154 } else {
155 // This shouldn't happen. :)
156 throw new MWException( 'Invalid row type in RevDelRevisionList' );
157 }
158 }
159
160 public function getCurrent() {
161 if ( is_null( $this->currentRevId ) ) {
162 $dbw = wfGetDB( DB_MASTER );
163 $this->currentRevId = $dbw->selectField(
164 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
165 }
166 return $this->currentRevId;
167 }
168
169 public function getSuppressBit() {
170 return Revision::DELETED_RESTRICTED;
171 }
172
173 public function doPreCommitUpdates() {
174 $this->title->invalidateCache();
175 return Status::newGood();
176 }
177
178 public function doPostCommitUpdates( array $visibilityChangeMap ) {
179 $this->title->purgeSquid();
180 // Extensions that require referencing previous revisions may need this
181 Hooks::run( 'ArticleRevisionVisibilitySet', [ $this->title, $this->ids, $visibilityChangeMap ] );
182 return Status::newGood();
183 }
184 }