DatabaseLogEntry.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Contains a class for dealing with database 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.19
  24. */
  25. use Wikimedia\Rdbms\IDatabase;
  26. /**
  27. * A value class to process existing log entries. In other words, this class caches a log
  28. * entry from the database and provides an immutable object-oriented representation of it.
  29. *
  30. * @since 1.19
  31. */
  32. class DatabaseLogEntry extends LogEntryBase {
  33. /**
  34. * Returns array of information that is needed for querying
  35. * log entries. Array contains the following keys:
  36. * tables, fields, conds, options and join_conds
  37. *
  38. * @return array
  39. */
  40. public static function getSelectQueryData() {
  41. $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
  42. $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
  43. $tables = array_merge(
  44. [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
  45. );
  46. $fields = [
  47. 'log_id', 'log_type', 'log_action', 'log_timestamp',
  48. 'log_namespace', 'log_title', // unused log_page
  49. 'log_params', 'log_deleted',
  50. 'user_id', 'user_name', 'user_editcount',
  51. ] + $commentQuery['fields'] + $actorQuery['fields'];
  52. $joins = [
  53. // IPs don't have an entry in user table
  54. 'user' => [ 'LEFT JOIN', 'user_id=' . $actorQuery['fields']['log_user'] ],
  55. ] + $commentQuery['joins'] + $actorQuery['joins'];
  56. return [
  57. 'tables' => $tables,
  58. 'fields' => $fields,
  59. 'conds' => [],
  60. 'options' => [],
  61. 'join_conds' => $joins,
  62. ];
  63. }
  64. /**
  65. * Constructs new LogEntry from database result row.
  66. * Supports rows from both logging and recentchanges table.
  67. *
  68. * @param stdClass|array $row
  69. * @return DatabaseLogEntry
  70. */
  71. public static function newFromRow( $row ) {
  72. $row = (object)$row;
  73. if ( isset( $row->rc_logid ) ) {
  74. return new RCDatabaseLogEntry( $row );
  75. } else {
  76. return new self( $row );
  77. }
  78. }
  79. /**
  80. * Loads a LogEntry with the given id from database
  81. *
  82. * @param int $id
  83. * @param IDatabase $db
  84. * @return DatabaseLogEntry|null
  85. */
  86. public static function newFromId( $id, IDatabase $db ) {
  87. $queryInfo = self::getSelectQueryData();
  88. $queryInfo['conds'] += [ 'log_id' => $id ];
  89. $row = $db->selectRow(
  90. $queryInfo['tables'],
  91. $queryInfo['fields'],
  92. $queryInfo['conds'],
  93. __METHOD__,
  94. $queryInfo['options'],
  95. $queryInfo['join_conds']
  96. );
  97. if ( !$row ) {
  98. return null;
  99. }
  100. return self::newFromRow( $row );
  101. }
  102. /** @var stdClass Database result row. */
  103. protected $row;
  104. /** @var User */
  105. protected $performer;
  106. /** @var array Parameters for log entry */
  107. protected $params;
  108. /** @var int A rev id associated to the log entry */
  109. protected $revId = null;
  110. /** @var bool Whether the parameters for this log entry are stored in new or old format. */
  111. protected $legacy;
  112. protected function __construct( $row ) {
  113. $this->row = $row;
  114. }
  115. /**
  116. * Returns the unique database id.
  117. *
  118. * @return int
  119. */
  120. public function getId() {
  121. return (int)$this->row->log_id;
  122. }
  123. /**
  124. * Returns whatever is stored in the database field.
  125. *
  126. * @return string
  127. */
  128. protected function getRawParameters() {
  129. return $this->row->log_params;
  130. }
  131. public function isLegacy() {
  132. // This extracts the property
  133. $this->getParameters();
  134. return $this->legacy;
  135. }
  136. public function getType() {
  137. return $this->row->log_type;
  138. }
  139. public function getSubtype() {
  140. return $this->row->log_action;
  141. }
  142. public function getParameters() {
  143. if ( !isset( $this->params ) ) {
  144. $blob = $this->getRawParameters();
  145. Wikimedia\suppressWarnings();
  146. $params = LogEntryBase::extractParams( $blob );
  147. Wikimedia\restoreWarnings();
  148. if ( $params !== false ) {
  149. $this->params = $params;
  150. $this->legacy = false;
  151. } else {
  152. $this->params = LogPage::extractParams( $blob );
  153. $this->legacy = true;
  154. }
  155. if ( isset( $this->params['associated_rev_id'] ) ) {
  156. $this->revId = $this->params['associated_rev_id'];
  157. unset( $this->params['associated_rev_id'] );
  158. }
  159. }
  160. return $this->params;
  161. }
  162. public function getAssociatedRevId() {
  163. // This extracts the property
  164. $this->getParameters();
  165. return $this->revId;
  166. }
  167. public function getPerformer() {
  168. if ( !$this->performer ) {
  169. $actorId = isset( $this->row->log_actor ) ? (int)$this->row->log_actor : 0;
  170. $userId = (int)$this->row->log_user;
  171. if ( $userId !== 0 || $actorId !== 0 ) {
  172. // logged-in users
  173. if ( isset( $this->row->user_name ) ) {
  174. $this->performer = User::newFromRow( $this->row );
  175. } elseif ( $actorId !== 0 ) {
  176. $this->performer = User::newFromActorId( $actorId );
  177. } else {
  178. $this->performer = User::newFromId( $userId );
  179. }
  180. } else {
  181. // IP users
  182. $userText = $this->row->log_user_text;
  183. $this->performer = User::newFromName( $userText, false );
  184. }
  185. }
  186. return $this->performer;
  187. }
  188. public function getTarget() {
  189. $namespace = $this->row->log_namespace;
  190. $page = $this->row->log_title;
  191. return Title::makeTitle( $namespace, $page );
  192. }
  193. public function getTimestamp() {
  194. return wfTimestamp( TS_MW, $this->row->log_timestamp );
  195. }
  196. public function getComment() {
  197. return CommentStore::getStore()->getComment( 'log_comment', $this->row )->text;
  198. }
  199. public function getDeleted() {
  200. return $this->row->log_deleted;
  201. }
  202. }