Skip to content

Commit

Permalink
EditCounter: show files uploaded/renamed on local wiki and Commons
Browse files Browse the repository at this point in the history
EditCounter: don't show file uploads/renames for Commons when Commons is
the requested project.

Bug: T177903
Bug: T226228
  • Loading branch information
MusikAnimal committed Jul 4, 2019
1 parent 4e5b74b commit 9a5c5dd
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
2 changes: 2 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
"feedback": "Feedback",
"file": "File",
"files": "Files",
"files-moved": "Files moved",
"files-moved-commons": "Files moved (Commons)",
"files-uploaded": "Files uploaded",
"files-uploaded-commons": "Files uploaded (Commons)",
"first-edit": "First edit",
Expand Down
4 changes: 3 additions & 1 deletion i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@
"feedback": "Link to feedback in header\n{{Identical|Feedback}}",
"file": "label for file\n{{Identical|File}}",
"files": "Heading for section showing stats about files.\n{{Identical|File}}",
"files-moved": "Label for the number of files a user has moved.",
"files-moved-commons": "Label for the number of files a user has moved on Wikimedia Commons.",
"files-uploaded": "Label for the number of files a user has uploaded.",
"files-uploaded-commons": "Label for the number of files a user has uploaded to Wikimedia Commons.",
"files-uploaded-commons": "Label for the number of files a user has uploaded on Wikimedia Commons.",
"first-edit": "Label for the date of a users first edit.",
"former-admin": "Text indicating a user was formerly an administrator of a wiki.",
"former-bot": "Text indicating that a bot is no longer a bot.",
Expand Down
22 changes: 20 additions & 2 deletions src/AppBundle/Model/EditCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,26 @@ public function countFilesUploaded(): int
*/
public function countFilesUploadedCommons(): int
{
$logCounts = $this->getLogCounts();
return $logCounts['files_uploaded_commons'] ?: 0;
$fileCounts = $this->getRepository()->getFileCounts($this->project, $this->user);
return $fileCounts['files_uploaded_commons'] ?? 0;
}

/**
* Get the total number of files that were renamed (including those now deleted).
*/
public function countFilesMoved(): int
{
$fileCounts = $this->getRepository()->getFileCounts($this->project, $this->user);
return $fileCounts['files_moved'] ?? 0;
}

/**
* Get the total number of files that were renamed on Commons (including those now deleted).
*/
public function countFilesMovedCommons(): int
{
$fileCounts = $this->getRepository()->getFileCounts($this->project, $this->user);
return $fileCounts['files_moved_commons'] ?? 0;
}

/**
Expand Down
78 changes: 63 additions & 15 deletions src/AppBundle/Repository/EditCounterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public function getPairData(Project $project, User $user): array
SELECT 'created-deleted' AS `key`, COUNT(DISTINCT ar_page_id) AS `val`
FROM $archiveTable
WHERE ar_actor = :actorId AND ar_parent_id = 0
)
";
)";

$resultQuery = $this->executeProjectsQuery($sql, [
'actorId' => $user->getActorId($project),
Expand Down Expand Up @@ -160,22 +159,71 @@ public function getLogCounts(Project $project, User $user): array
}
}

// Add Commons upload count, if applicable.
$logCounts['files_uploaded_commons'] = 0;
if ($this->isLabs() && !$user->isAnon()) {
$sql = "SELECT COUNT(log_id)
FROM commonswiki_p.logging_userindex
JOIN commonswiki_p.actor ON actor_id = log_actor
WHERE log_type = 'upload' AND log_action = 'upload'
AND actor_name = :username";
$resultQuery = $this->executeProjectsQuery($sql, [
'username' => $user->getUsername(),
]);
$logCounts['files_uploaded_commons'] = (int)$resultQuery->fetchColumn();
// Cache and return.
return $this->setCache($cacheKey, $logCounts);
}

/**
* Get counts of files moved, and files moved/uploaded on Commons.
* Local file uploads are counted in getLogCounts() since we're querying the same rows anyway.
* @param Project $project
* @param User $user
* @return array
*/
public function getFileCounts(Project $project, User $user): array
{
// Anons can't upload or move files.
if ($user->isAnon()) {
return [];
}

// Set up cache.
$cacheKey = $this->getCacheKey(func_get_args(), 'ec_filecounts');
if ($this->cache->hasItem($cacheKey)) {
return $this->cache->getItem($cacheKey)->get();
}

$loggingTable = $project->getTableName('logging');

$sqlParts = [
"SELECT 'files_moved' AS `key`, COUNT(log_id) AS `val`
FROM $loggingTable
WHERE log_actor = :actorId
AND log_type = 'move'
AND log_action = 'move'
AND log_namespace = 6",
];

$bindings = ['actorId' => $user->getActorId($project)];

if ($this->isLabs() && 'commons.wikimedia.org' !== $project->getDomain()) {
$commonsProject = ProjectRepository::getProject('commonswiki', $this->container);
$loggingTableCommons = $commonsProject->getTableName('logging');
$sqlParts[] = "SELECT 'files_moved_commons' AS `key`, COUNT(log_id) AS `val`
FROM $loggingTableCommons
WHERE log_actor = :actorId2 AND log_type = 'move'
AND log_action = 'move' AND log_namespace = 6";
$sqlParts[] = "SELECT 'files_uploaded_commons' AS `key`, COUNT(log_id) AS `val`
FROM $loggingTableCommons
WHERE log_actor = :actorId2 AND log_type = 'upload' AND log_action = 'upload'";
$bindings['actorId2'] = $user->getActorId($commonsProject);
}

$sql = '('.implode("\n) UNION (\n", $sqlParts).')';

$results = $this->executeProjectsQuery($sql, $bindings)->fetchAll();

$counts = array_combine(
array_map(function ($e) {
return $e['key'];
}, $results),
array_map(function ($e) {
return (int)$e['val'];
}, $results)
);

// Cache and return.
return $this->setCache($cacheKey, $logCounts);
return $this->setCache($cacheKey, $counts);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions templates/editCounter/general_stats.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,26 @@
<tr>
<td>{{ msg('files-uploaded') }}</td>
<td>
<a href="{{ wiki.pageUrlRaw('Special:ListFiles', project) }}/{{ user.username }}">{{ ec.countFilesUploaded|num_format }}</a>
<a target="_blank" href="{{ wiki.pageUrlRaw('Special:ListFiles', project) }}/{{ user.username }}">{{ ec.countFilesUploaded|num_format }}</a>
</td>
</tr>
{% if isWMFLabs() %}
<tr>
<td>{{ msg('files-moved') }}</td>
<td>
{{ ec.countFilesMoved }}
</td>
</tr>
{% if isWMFLabs() and project.domain != 'commons.wikimedia.org' %}
<tr>
<td>{{ msg('files-uploaded-commons') }}</td>
<td>
<a href="https://commons.wikimedia.org/wiki/Special:ListFiles/{{ user.username }}" >{{ ec.countFilesUploadedCommons|num_format }}</a>
<a target="_blank" href="https://commons.wikimedia.org/wiki/Special:ListFiles/{{ user.username }}">{{ ec.countFilesUploadedCommons|num_format }}</a>
</td>
</tr>
<tr>
<td>{{ msg('files-moved-commons') }}</td>
<td>
{{ ec.countFilesMovedCommons }}
</td>
</tr>
{% endif %}
Expand Down

0 comments on commit 9a5c5dd

Please sign in to comment.