MachineReadableRCFeedFormatter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. */
  20. /**
  21. * Abstract class so there can be multiple formatters outputting the same data
  22. *
  23. * @since 1.23
  24. */
  25. abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter {
  26. /**
  27. * Take the packet and return the formatted string
  28. * @param array $packet
  29. * @return string
  30. */
  31. abstract protected function formatArray( array $packet );
  32. /**
  33. * Generates a notification that can be easily interpreted by a machine.
  34. * @see RCFeedFormatter::getLine
  35. * @param array $feed
  36. * @param RecentChange $rc
  37. * @param string|null $actionComment
  38. * @return string|null
  39. */
  40. public function getLine( array $feed, RecentChange $rc, $actionComment ) {
  41. global $wgCanonicalServer, $wgServerName, $wgScriptPath;
  42. $packet = [
  43. // Usually, RC ID is exposed only for patrolling purposes,
  44. // but there is no real reason not to expose it in other cases,
  45. // and I can see how this may be potentially useful for clients.
  46. 'id' => $rc->getAttribute( 'rc_id' ),
  47. 'type' => RecentChange::parseFromRCType( $rc->getAttribute( 'rc_type' ) ),
  48. 'namespace' => $rc->getTitle()->getNamespace(),
  49. 'title' => $rc->getTitle()->getPrefixedText(),
  50. 'comment' => $rc->getAttribute( 'rc_comment' ),
  51. 'timestamp' => (int)wfTimestamp( TS_UNIX, $rc->getAttribute( 'rc_timestamp' ) ),
  52. 'user' => $rc->getAttribute( 'rc_user_text' ),
  53. 'bot' => (bool)$rc->getAttribute( 'rc_bot' ),
  54. ];
  55. if ( isset( $feed['channel'] ) ) {
  56. $packet['channel'] = $feed['channel'];
  57. }
  58. $type = $rc->getAttribute( 'rc_type' );
  59. if ( $type == RC_EDIT || $type == RC_NEW ) {
  60. global $wgUseRCPatrol, $wgUseNPPatrol;
  61. $packet['minor'] = (bool)$rc->getAttribute( 'rc_minor' );
  62. if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) {
  63. $packet['patrolled'] = (bool)$rc->getAttribute( 'rc_patrolled' );
  64. }
  65. }
  66. switch ( $type ) {
  67. case RC_EDIT:
  68. $packet['length'] = [
  69. 'old' => $rc->getAttribute( 'rc_old_len' ),
  70. 'new' => $rc->getAttribute( 'rc_new_len' )
  71. ];
  72. $packet['revision'] = [
  73. 'old' => $rc->getAttribute( 'rc_last_oldid' ),
  74. 'new' => $rc->getAttribute( 'rc_this_oldid' )
  75. ];
  76. break;
  77. case RC_NEW:
  78. $packet['length'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_new_len' ) ];
  79. $packet['revision'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_this_oldid' ) ];
  80. break;
  81. case RC_LOG:
  82. $packet['log_id'] = $rc->getAttribute( 'rc_logid' );
  83. $packet['log_type'] = $rc->getAttribute( 'rc_log_type' );
  84. $packet['log_action'] = $rc->getAttribute( 'rc_log_action' );
  85. if ( $rc->getAttribute( 'rc_params' ) ) {
  86. $params = $rc->parseParams();
  87. if (
  88. // If it's an actual serialised false...
  89. $rc->getAttribute( 'rc_params' ) == serialize( false ) ||
  90. // Or if we did not get false back when trying to unserialise
  91. $params !== false
  92. ) {
  93. // From ApiQueryLogEvents::addLogParams
  94. $logParams = [];
  95. // Keys like "4::paramname" can't be used for output so we change them to "paramname"
  96. foreach ( $params as $key => $value ) {
  97. if ( strpos( $key, ':' ) === false ) {
  98. $logParams[$key] = $value;
  99. continue;
  100. }
  101. $logParam = explode( ':', $key, 3 );
  102. $logParams[$logParam[2]] = $value;
  103. }
  104. $packet['log_params'] = $logParams;
  105. } else {
  106. $packet['log_params'] = explode( "\n", $rc->getAttribute( 'rc_params' ) );
  107. }
  108. }
  109. $packet['log_action_comment'] = $actionComment;
  110. break;
  111. }
  112. $packet['server_url'] = $wgCanonicalServer;
  113. $packet['server_name'] = $wgServerName;
  114. $packet['server_script_path'] = $wgScriptPath ?: '/';
  115. $packet['wiki'] = WikiMap::getWikiIdFromDbDomain( WikiMap::getCurrentWikiDbDomain() );
  116. return $this->formatArray( $packet );
  117. }
  118. }