Fix misc phan errors by adjusting documentation (#8)
[lhc/web/wiklou.git] / includes / logging / LogEntry.php
index a154f64..c5e4a92 100644 (file)
  * @since 1.19
  */
 
+use MediaWiki\ChangeTags\Taggable;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\User\UserIdentity;
 use Wikimedia\Rdbms\IDatabase;
+use Wikimedia\Assert\Assert;
 
 /**
  * Interface for log entries. Every log entry has these methods.
@@ -98,7 +100,7 @@ interface LogEntry {
        /**
         * Get the access restriction.
         *
-        * @return string
+        * @return int
         */
        public function getDeleted();
 
@@ -436,7 +438,7 @@ class RCDatabaseLogEntry extends DatabaseLogEntry {
  *
  * @since 1.19
  */
-class ManualLogEntry extends LogEntryBase {
+class ManualLogEntry extends LogEntryBase implements Taggable {
        /** @var string Type of log entry */
        protected $type;
 
@@ -586,14 +588,30 @@ class ManualLogEntry extends LogEntryBase {
         *
         * @since 1.27
         * @param string|string[]|null $tags
+        * @deprecated since 1.33 Please use addTags() instead
         */
        public function setTags( $tags ) {
-               if ( $tags === null ) {
-                       $tags = [];
-               } elseif ( is_string( $tags ) ) {
+               if ( $this->tags ) {
+                       wfDebug( 'Overwriting existing ManualLogEntry tags' );
+               }
+               $this->tags = [];
+               if ( $tags !== null ) {
+                       $this->addTags( $tags );
+               }
+       }
+
+       /**
+        * Add change tags for the log entry
+        *
+        * @since 1.33
+        * @param string|string[] $tags Tags to apply
+        */
+       public function addTags( $tags ) {
+               if ( is_string( $tags ) ) {
                        $tags = [ $tags ];
                }
-               $this->tags = $tags;
+               Assert::parameterElementType( 'string', $tags, 'tags' );
+               $this->tags = array_unique( array_merge( $this->tags, $tags ) );
        }
 
        /**
@@ -775,10 +793,27 @@ class ManualLogEntry extends LogEntryBase {
         * @param string $to One of: rcandudp (default), rc, udp
         */
        public function publish( $newId, $to = 'rcandudp' ) {
+               $canAddTags = true;
+               // FIXME: this code should be removed once all callers properly call publish()
+               if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
+                       \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
+                               'newId and/or revId must be set when calling ManualLogEntry::publish()',
+                               [
+                                       'newId' => $newId,
+                                       'to' => $to,
+                                       'revId' => $this->getAssociatedRevId(),
+                                       // pass a new exception to register the stack trace
+                                       'exception' => new RuntimeException()
+                               ]
+                       );
+                       $canAddTags = false;
+               }
+
                DeferredUpdates::addCallableUpdate(
-                       function () use ( $newId, $to ) {
+                       function () use ( $newId, $to, $canAddTags ) {
                                $log = new LogPage( $this->getType() );
                                if ( !$log->isRestricted() ) {
+                                       Hooks::runWithoutAbort( 'ManualLogEntryBeforePublish', [ $this ] );
                                        $rc = $this->getRecentChange( $newId );
 
                                        if ( $to === 'rc' || $to === 'rcandudp' ) {
@@ -787,9 +822,14 @@ class ManualLogEntry extends LogEntryBase {
                                                $rc->save( $rc::SEND_NONE );
                                        } else {
                                                $tags = $this->getTags();
-                                               if ( $tags ) {
-                                                       $revId = $this->getAssociatedRevId(); // Use null if $revId is 0
-                                                       ChangeTags::addTags( $tags, null, $revId > 0 ? $revId : null, $newId );
+                                               if ( $tags && $canAddTags ) {
+                                                       $revId = $this->getAssociatedRevId();
+                                                       ChangeTags::addTags(
+                                                               $tags,
+                                                               null,
+                                                               $revId > 0 ? $revId : null,
+                                                               $newId > 0 ? $newId : null
+                                                       );
                                                }
                                        }