PatrolLog.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Specific methods for the patrol log.
  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 Rob Church <robchur@gmail.com>
  22. * @author Niklas Laxström
  23. */
  24. /**
  25. * Class containing static functions for working with
  26. * logs of patrol events
  27. */
  28. class PatrolLog {
  29. /**
  30. * Record a log event for a change being patrolled
  31. *
  32. * @param int|RecentChange $rc Change identifier or RecentChange object
  33. * @param bool $auto Was this patrol event automatic?
  34. * @param User|null $user User performing the action or null to use $wgUser
  35. * @param string|string[]|null $tags Change tags to add to the patrol log entry
  36. * ($user should be able to add the specified tags before this is called)
  37. *
  38. * @return bool
  39. */
  40. public static function record( $rc, $auto = false, User $user = null, $tags = null ) {
  41. // Do not log autopatrol actions: T184485
  42. if ( $auto ) {
  43. return false;
  44. }
  45. if ( !$rc instanceof RecentChange ) {
  46. $rc = RecentChange::newFromId( $rc );
  47. if ( !is_object( $rc ) ) {
  48. return false;
  49. }
  50. }
  51. if ( !$user ) {
  52. global $wgUser;
  53. $user = $wgUser;
  54. }
  55. $action = $auto ? 'autopatrol' : 'patrol';
  56. $entry = new ManualLogEntry( 'patrol', $action );
  57. $entry->setTarget( $rc->getTitle() );
  58. $entry->setParameters( self::buildParams( $rc, $auto ) );
  59. $entry->setPerformer( $user );
  60. $entry->addTags( $tags );
  61. $logid = $entry->insert();
  62. if ( !$auto ) {
  63. $entry->publish( $logid, 'udp' );
  64. }
  65. return true;
  66. }
  67. /**
  68. * Prepare log parameters for a patrolled change
  69. *
  70. * @param RecentChange $change RecentChange to represent
  71. * @param bool $auto Whether the patrol event was automatic
  72. * @return array
  73. */
  74. private static function buildParams( $change, $auto ) {
  75. return [
  76. '4::curid' => $change->getAttribute( 'rc_this_oldid' ),
  77. '5::previd' => $change->getAttribute( 'rc_last_oldid' ),
  78. '6::auto' => (int)$auto
  79. ];
  80. }
  81. }