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