DeleteLogFormatter.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * Formatter for delete log entries.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @author Niklas Laxström
  22. * @license GPL-2.0-or-later
  23. * @since 1.22
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. use MediaWiki\Revision\RevisionRecord;
  27. /**
  28. * This class formats delete log entries.
  29. *
  30. * @since 1.19
  31. */
  32. class DeleteLogFormatter extends LogFormatter {
  33. /** @var array|null */
  34. private $parsedParametersDeleteLog;
  35. /**
  36. * @inheritDoc
  37. */
  38. protected function getMessageKey() {
  39. $key = parent::getMessageKey();
  40. if ( in_array( $this->entry->getSubtype(), [ 'event', 'revision' ] ) ) {
  41. if ( count( $this->getMessageParameters() ) < 5 ) {
  42. // Messages: logentry-delete-event-legacy, logentry-delete-revision-legacy,
  43. // logentry-suppress-event-legacy, logentry-suppress-revision-legacy
  44. return "$key-legacy";
  45. }
  46. } elseif ( $this->entry->getSubtype() === 'restore' ) {
  47. $rawParams = $this->entry->getParameters();
  48. if ( !isset( $rawParams[':assoc:count'] ) ) {
  49. // Message: logentry-delete-restore-nocount
  50. return $key . '-nocount';
  51. }
  52. }
  53. return $key;
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. protected function getMessageParameters() {
  59. if ( $this->parsedParametersDeleteLog !== null ) {
  60. return $this->parsedParametersDeleteLog;
  61. }
  62. $params = parent::getMessageParameters();
  63. $subtype = $this->entry->getSubtype();
  64. if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
  65. // $params[3] here is 'revision' or 'archive' for page revisions, 'oldimage' or
  66. // 'filearchive' for file versions, or a comma-separated list of log_ids for log
  67. // entries. $subtype here is 'revision' for page revisions and file
  68. // versions, or 'event' for log entries.
  69. if (
  70. ( $subtype === 'event' && count( $params ) === 6 )
  71. || (
  72. $subtype === 'revision' && isset( $params[3] )
  73. && in_array( $params[3], [ 'revision', 'archive', 'oldimage', 'filearchive' ] )
  74. )
  75. ) {
  76. // See RevDelList::getLogParams()/RevDelLogList::getLogParams()
  77. $paramStart = $subtype === 'revision' ? 4 : 3;
  78. $old = $this->parseBitField( $params[$paramStart + 1] );
  79. $new = $this->parseBitField( $params[$paramStart + 2] );
  80. list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
  81. $changes = [];
  82. // messages used: revdelete-content-hid, revdelete-summary-hid, revdelete-uname-hid
  83. foreach ( $hid as $v ) {
  84. $changes[] = $this->msg( "$v-hid" )->plain();
  85. }
  86. // messages used: revdelete-content-unhid, revdelete-summary-unhid,
  87. // revdelete-uname-unhid
  88. foreach ( $unhid as $v ) {
  89. $changes[] = $this->msg( "$v-unhid" )->plain();
  90. }
  91. foreach ( $extra as $v ) {
  92. $changes[] = $this->msg( $v )->plain();
  93. }
  94. $changeText = $this->context->getLanguage()->listToText( $changes );
  95. $newParams = array_slice( $params, 0, 3 );
  96. $newParams[3] = $changeText;
  97. $ids = is_array( $params[$paramStart] )
  98. ? $params[$paramStart]
  99. : explode( ',', $params[$paramStart] );
  100. $newParams[4] = $this->context->getLanguage()->formatNum( count( $ids ) );
  101. $this->parsedParametersDeleteLog = $newParams;
  102. return $this->parsedParametersDeleteLog;
  103. } else {
  104. $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
  105. return $this->parsedParametersDeleteLog;
  106. }
  107. } elseif ( $subtype === 'restore' ) {
  108. $rawParams = $this->entry->getParameters();
  109. if ( isset( $rawParams[':assoc:count'] ) ) {
  110. $countList = [];
  111. foreach ( $rawParams[':assoc:count'] as $type => $count ) {
  112. if ( $count ) {
  113. // Messages: restore-count-revisions, restore-count-files
  114. $countList[] = $this->context->msg( 'restore-count-' . $type )
  115. ->numParams( $count )->plain();
  116. }
  117. }
  118. $params[3] = $this->context->getLanguage()->listToText( $countList );
  119. }
  120. }
  121. $this->parsedParametersDeleteLog = $params;
  122. return $this->parsedParametersDeleteLog;
  123. }
  124. protected function parseBitField( $string ) {
  125. // Input is like ofield=2134 or just the number
  126. if ( strpos( $string, 'field=' ) === 1 ) {
  127. list( , $field ) = explode( '=', $string );
  128. return (int)$field;
  129. } else {
  130. return (int)$string;
  131. }
  132. }
  133. public function getActionLinks() {
  134. $user = $this->context->getUser();
  135. $linkRenderer = $this->getLinkRenderer();
  136. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  137. if ( !$permissionManager->userHasRight( $user, 'deletedhistory' )
  138. || $this->entry->isDeleted( LogPage::DELETED_ACTION )
  139. ) {
  140. return '';
  141. }
  142. switch ( $this->entry->getSubtype() ) {
  143. case 'delete': // Show undelete link
  144. case 'delete_redir':
  145. if ( $permissionManager->userHasRight( $user, 'undelete' ) ) {
  146. $message = 'undeletelink';
  147. } else {
  148. $message = 'undeleteviewlink';
  149. }
  150. $revert = $linkRenderer->makeKnownLink(
  151. SpecialPage::getTitleFor( 'Undelete' ),
  152. $this->msg( $message )->text(),
  153. [],
  154. [ 'target' => $this->entry->getTarget()->getPrefixedDBkey() ]
  155. );
  156. return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
  157. case 'revision': // If an edit was hidden from a page give a review link to the history
  158. $params = $this->extractParameters();
  159. if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
  160. return '';
  161. }
  162. // Different revision types use different URL params...
  163. $key = $params[3];
  164. // This is a array or CSV of the IDs
  165. $ids = is_array( $params[4] )
  166. ? $params[4]
  167. : explode( ',', $params[4] );
  168. $links = [];
  169. // If there's only one item, we can show a diff link
  170. if ( count( $ids ) == 1 ) {
  171. // Live revision diffs...
  172. if ( $key == 'oldid' || $key == 'revision' ) {
  173. $links[] = $linkRenderer->makeKnownLink(
  174. $this->entry->getTarget(),
  175. $this->msg( 'diff' )->text(),
  176. [],
  177. [
  178. 'diff' => intval( $ids[0] ),
  179. 'unhide' => 1
  180. ]
  181. );
  182. // Deleted revision diffs...
  183. } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
  184. $links[] = $linkRenderer->makeKnownLink(
  185. SpecialPage::getTitleFor( 'Undelete' ),
  186. $this->msg( 'diff' )->text(),
  187. [],
  188. [
  189. 'target' => $this->entry->getTarget()->getPrefixedDBkey(),
  190. 'diff' => 'prev',
  191. 'timestamp' => $ids[0]
  192. ]
  193. );
  194. }
  195. }
  196. // View/modify link...
  197. $links[] = $linkRenderer->makeKnownLink(
  198. SpecialPage::getTitleFor( 'Revisiondelete' ),
  199. $this->msg( 'revdel-restore' )->text(),
  200. [],
  201. [
  202. 'target' => $this->entry->getTarget()->getPrefixedText(),
  203. 'type' => $key,
  204. 'ids' => implode( ',', $ids ),
  205. ]
  206. );
  207. return $this->msg( 'parentheses' )->rawParams(
  208. $this->context->getLanguage()->pipeList( $links ) )->escaped();
  209. case 'event': // Hidden log items, give review link
  210. $params = $this->extractParameters();
  211. if ( !isset( $params[3] ) ) {
  212. return '';
  213. }
  214. // This is a CSV of the IDs
  215. $query = $params[3];
  216. if ( is_array( $query ) ) {
  217. $query = implode( ',', $query );
  218. }
  219. // Link to each hidden object ID, $params[1] is the url param
  220. $revert = $linkRenderer->makeKnownLink(
  221. SpecialPage::getTitleFor( 'Revisiondelete' ),
  222. $this->msg( 'revdel-restore' )->text(),
  223. [],
  224. [
  225. 'target' => $this->entry->getTarget()->getPrefixedText(),
  226. 'type' => 'logging',
  227. 'ids' => $query
  228. ]
  229. );
  230. return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
  231. default:
  232. return '';
  233. }
  234. }
  235. protected function getParametersForApi() {
  236. $entry = $this->entry;
  237. $params = [];
  238. $subtype = $this->entry->getSubtype();
  239. if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
  240. $rawParams = $entry->getParameters();
  241. if ( $subtype === 'event' ) {
  242. array_unshift( $rawParams, 'logging' );
  243. }
  244. static $map = [
  245. '4::type',
  246. '5::ids',
  247. '6::ofield',
  248. '7::nfield',
  249. '4::ids' => '5::ids',
  250. '5::ofield' => '6::ofield',
  251. '6::nfield' => '7::nfield',
  252. ];
  253. foreach ( $map as $index => $key ) {
  254. if ( isset( $rawParams[$index] ) ) {
  255. $rawParams[$key] = $rawParams[$index];
  256. unset( $rawParams[$index] );
  257. }
  258. }
  259. if ( !is_array( $rawParams['5::ids'] ) ) {
  260. $rawParams['5::ids'] = explode( ',', $rawParams['5::ids'] );
  261. }
  262. $params = [
  263. '::type' => $rawParams['4::type'],
  264. ':array:ids' => $rawParams['5::ids'],
  265. ];
  266. static $fields = [
  267. RevisionRecord::DELETED_TEXT => 'content',
  268. RevisionRecord::DELETED_COMMENT => 'comment',
  269. RevisionRecord::DELETED_USER => 'user',
  270. RevisionRecord::DELETED_RESTRICTED => 'restricted',
  271. ];
  272. if ( isset( $rawParams['6::ofield'] ) ) {
  273. $old = $this->parseBitField( $rawParams['6::ofield'] );
  274. $params[':assoc:old'] = [ 'bitmask' => $old ];
  275. foreach ( $fields as $bit => $key ) {
  276. $params[':assoc:old'][$key] = (bool)( $old & $bit );
  277. }
  278. }
  279. if ( isset( $rawParams['7::nfield'] ) ) {
  280. $new = $this->parseBitField( $rawParams['7::nfield'] );
  281. $params[':assoc:new'] = [ 'bitmask' => $new ];
  282. foreach ( $fields as $bit => $key ) {
  283. $params[':assoc:new'][$key] = (bool)( $new & $bit );
  284. }
  285. }
  286. } elseif ( $subtype === 'restore' ) {
  287. $rawParams = $entry->getParameters();
  288. if ( isset( $rawParams[':assoc:count'] ) ) {
  289. $params[':assoc:count'] = $rawParams[':assoc:count'];
  290. }
  291. }
  292. return $params;
  293. }
  294. public function formatParametersForApi() {
  295. $ret = parent::formatParametersForApi();
  296. if ( isset( $ret['ids'] ) ) {
  297. ApiResult::setIndexedTagName( $ret['ids'], 'id' );
  298. }
  299. return $ret;
  300. }
  301. }