| 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 <[email protected]> |
| 22 | * @author Niklas Laxström |
| 23 | */ |
| 24 | |
| 25 | use MediaWiki\Page\PageReferenceValue; |
| 26 | use MediaWiki\User\UserIdentity; |
| 27 | |
| 28 | /** |
| 29 | * Class containing static functions for working with |
| 30 | * logs of patrol events |
| 31 | */ |
| 32 | class PatrolLog { |
| 33 | |
| 34 | /** |
| 35 | * Record a log event for a change being patrolled |
| 36 | * |
| 37 | * @param int|RecentChange $rc Change identifier or RecentChange object |
| 38 | * @param bool $auto Was this patrol event automatic? |
| 39 | * @param UserIdentity $user User performing the action |
| 40 | * @param string|string[]|null $tags Change tags to add to the patrol log entry |
| 41 | * ($user should be able to add the specified tags before this is called) |
| 42 | * |
| 43 | * @return bool |
| 44 | */ |
| 45 | public static function record( $rc, $auto, UserIdentity $user, $tags = null ) { |
| 46 | // Do not log autopatrol actions: T184485 |
| 47 | if ( $auto ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if ( !$rc instanceof RecentChange ) { |
| 52 | $rc = RecentChange::newFromId( $rc ); |
| 53 | if ( !is_object( $rc ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | $entry = new ManualLogEntry( 'patrol', 'patrol' ); |
| 59 | |
| 60 | // B/C: ->getPage() on RC will return a page reference or null, reconcile this in |
| 61 | // $entry->setTarget() call so we don't throw. |
| 62 | $page = $rc->getPage() ?? PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle' ); |
| 63 | $entry->setTarget( $page ); |
| 64 | $entry->setParameters( self::buildParams( $rc ) ); |
| 65 | $entry->setPerformer( $user ); |
| 66 | $entry->addTags( $tags ); |
| 67 | $logid = $entry->insert(); |
| 68 | $entry->publish( $logid, 'udp' ); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Prepare log parameters for a patrolled change |
| 75 | * |
| 76 | * @param RecentChange $change RecentChange to represent |
| 77 | * @return array |
| 78 | */ |
| 79 | private static function buildParams( $change ) { |
| 80 | return [ |
| 81 | '4::curid' => $change->getAttribute( 'rc_this_oldid' ), |
| 82 | '5::previd' => $change->getAttribute( 'rc_last_oldid' ), |
| 83 | '6::auto' => 0 |
| 84 | ]; |
| 85 | } |
| 86 | } |