PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/revisiondelete/RevisionDeleter.php

https://github.com/daevid/MWFork
PHP | 213 lines | 120 code | 19 blank | 74 comment | 16 complexity | 281796e22f82a03388f86573454c1b40 MD5 | raw file
  1. <?php
  2. /**
  3. * Revision/log/file deletion backend
  4. *
  5. * @file
  6. */
  7. /**
  8. * Temporary b/c interface, collection of static functions.
  9. * @ingroup SpecialPage
  10. */
  11. class RevisionDeleter {
  12. /**
  13. * Checks for a change in the bitfield for a certain option and updates the
  14. * provided array accordingly.
  15. *
  16. * @param $desc String: description to add to the array if the option was
  17. * enabled / disabled.
  18. * @param $field Integer: the bitmask describing the single option.
  19. * @param $diff Integer: the xor of the old and new bitfields.
  20. * @param $new Integer: the new bitfield
  21. * @param $arr Array: the array to update.
  22. */
  23. protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
  24. if( $diff & $field ) {
  25. $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
  26. }
  27. }
  28. /**
  29. * Gets an array of message keys describing the changes made to the visibility
  30. * of the revision. If the resulting array is $arr, then $arr[0] will contain an
  31. * array of strings describing the items that were hidden, $arr[2] will contain
  32. * an array of strings describing the items that were unhidden, and $arr[3] will
  33. * contain an array with a single string, which can be one of "applied
  34. * restrictions to sysops", "removed restrictions from sysops", or null.
  35. *
  36. * @param $n Integer: the new bitfield.
  37. * @param $o Integer: the old bitfield.
  38. * @return An array as described above.
  39. */
  40. protected static function getChanges( $n, $o ) {
  41. $diff = $n ^ $o;
  42. $ret = array( 0 => array(), 1 => array(), 2 => array() );
  43. // Build bitfield changes in language
  44. self::checkItem( 'revdelete-content',
  45. Revision::DELETED_TEXT, $diff, $n, $ret );
  46. self::checkItem( 'revdelete-summary',
  47. Revision::DELETED_COMMENT, $diff, $n, $ret );
  48. self::checkItem( 'revdelete-uname',
  49. Revision::DELETED_USER, $diff, $n, $ret );
  50. // Restriction application to sysops
  51. if( $diff & Revision::DELETED_RESTRICTED ) {
  52. if( $n & Revision::DELETED_RESTRICTED )
  53. $ret[2][] = 'revdelete-restricted';
  54. else
  55. $ret[2][] = 'revdelete-unrestricted';
  56. }
  57. return $ret;
  58. }
  59. /**
  60. * Gets a log message to describe the given revision visibility change. This
  61. * message will be of the form "[hid {content, edit summary, username}];
  62. * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
  63. *
  64. * @param $count Integer: The number of effected revisions.
  65. * @param $nbitfield Integer: The new bitfield for the revision.
  66. * @param $obitfield Integer: The old bitfield for the revision.
  67. * @param $language Language object to use
  68. * @param $isForLog Boolean
  69. */
  70. public static function getLogMessage( $count, $nbitfield, $obitfield, $language, $isForLog = false ) {
  71. $changes = self::getChanges( $nbitfield, $obitfield );
  72. array_walk( $changes, array( __CLASS__, 'expandMessageArray' ), $language );
  73. $changesText = array();
  74. if( count( $changes[0] ) ) {
  75. $changesText[] = wfMsgExt( 'revdelete-hid', array( 'parsemag', 'language' => $language ), $language->commaList( $changes[0] ) );
  76. }
  77. if( count( $changes[1] ) ) {
  78. $changesText[] = wfMsgExt( 'revdelete-unhid', array( 'parsemag', 'language' => $language ), $language->commaList( $changes[1] ) );
  79. }
  80. $s = $language->semicolonList( $changesText );
  81. if( count( $changes[2] ) ) {
  82. $s .= $s ? ' (' . $changes[2][0] . ')' : ' ' . $changes[2][0];
  83. }
  84. $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
  85. return wfMsgExt( $msg, array( 'parsemag', 'language' => $language ), $s, $language->formatNum($count) );
  86. }
  87. private static function expandMessageArray( &$msg, $key, $language ) {
  88. if ( is_array ( $msg ) ) {
  89. array_walk( $msg, array( __CLASS__, 'expandMessageArray' ), $language );
  90. } else {
  91. $msg = wfMsgExt( $msg, array( 'parsemag', 'language' => $language ) );
  92. }
  93. }
  94. // Get DB field name for URL param...
  95. // Future code for other things may also track
  96. // other types of revision-specific changes.
  97. // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
  98. public static function getRelationType( $typeName ) {
  99. if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
  100. $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
  101. }
  102. if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
  103. $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
  104. return call_user_func( array( $class, 'getRelationType' ) );
  105. } else {
  106. return null;
  107. }
  108. }
  109. /**
  110. * Checks if a revision still exists in the revision table.
  111. * If it doesn't, returns the corresponding ar_timestamp field
  112. * so that this key can be used instead.
  113. *
  114. * @param $title Title
  115. * @param $revid
  116. * @return bool|mixed
  117. */
  118. public static function checkRevisionExistence( $title, $revid ) {
  119. $dbr = wfGetDB( DB_SLAVE );
  120. $exists = $dbr->selectField( 'revision', '1',
  121. array( 'rev_id' => $revid ), __METHOD__ );
  122. if ( $exists ) {
  123. return true;
  124. }
  125. $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
  126. array( 'ar_namespace' => $title->getNamespace(),
  127. 'ar_title' => $title->getDBkey(),
  128. 'ar_rev_id' => $revid ), __METHOD__ );
  129. return $timestamp;
  130. }
  131. /**
  132. * Creates utility links for log entries.
  133. *
  134. * @param $title Title
  135. * @param $paramArray Array
  136. * @param $skin Skin
  137. * @param $messages
  138. * @return String
  139. */
  140. public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
  141. global $wgLang;
  142. if ( count( $paramArray ) >= 2 ) {
  143. // Different revision types use different URL params...
  144. $originalKey = $key = $paramArray[0];
  145. // $paramArray[1] is a CSV of the IDs
  146. $Ids = explode( ',', $paramArray[1] );
  147. $revert = array();
  148. // Diff link for single rev deletions
  149. if ( count( $Ids ) == 1 ) {
  150. // Live revision diffs...
  151. if ( in_array( $key, array( 'oldid', 'revision' ) ) ) {
  152. $revert[] = $skin->link(
  153. $title,
  154. $messages['diff'],
  155. array(),
  156. array(
  157. 'diff' => intval( $Ids[0] ),
  158. 'unhide' => 1
  159. ),
  160. array( 'known', 'noclasses' )
  161. );
  162. // Deleted revision diffs...
  163. } elseif ( in_array( $key, array( 'artimestamp','archive' ) ) ) {
  164. $revert[] = $skin->link(
  165. SpecialPage::getTitleFor( 'Undelete' ),
  166. $messages['diff'],
  167. array(),
  168. array(
  169. 'target' => $title->getPrefixedDBKey(),
  170. 'diff' => 'prev',
  171. 'timestamp' => $Ids[0]
  172. ),
  173. array( 'known', 'noclasses' )
  174. );
  175. }
  176. }
  177. // View/modify link...
  178. $revert[] = $skin->link(
  179. SpecialPage::getTitleFor( 'Revisiondelete' ),
  180. $messages['revdel-restore'],
  181. array(),
  182. array(
  183. 'target' => $title->getPrefixedText(),
  184. 'type' => $key,
  185. 'ids' => implode(',', $Ids),
  186. ),
  187. array( 'known', 'noclasses' )
  188. );
  189. // Pipe links
  190. return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
  191. }
  192. return '';
  193. }
  194. }