Skip to content

Commit

Permalink
Meta: move usage API endpoint out of API namespace
Browse files Browse the repository at this point in the history
Move /usage to /meta/usage, don't query on index pages
  • Loading branch information
MusikAnimal committed Oct 5, 2017
1 parent 7c77259 commit 4cc8fe7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@
<span class='hidden loader'>
<img src="{{ asset('static/images/loader.gif') }}" /> <span class="sr-only">{{ msg('loading') }}&hellip;</span>
</span>
{% if xtPage is defined and project.domain is defined %}
{% if xtPage is defined and project.domain is defined and 'indexAction' not in app.request.attributes.get('_controller') %}
<script>
$.ajax({
url: "{{ path('homepage') }}api/usage/{{ xtPage }}/{{ project.domain }}/{{ csrf_token('intention') }}",
url: "{{ path('homepage') }}meta/usage/{{ xtPage }}/{{ project.domain }}/{{ csrf_token('intention') }}",
method: 'PUT'
});
</script>
Expand Down
75 changes: 0 additions & 75 deletions src/AppBundle/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,79 +305,4 @@ public function articleInfo(Request $request, $project, $article)

return $view;
}

/**
* Record usage of a particular XTools tool. This is called automatically
* in base.html.twig via JavaScript so that it is done asynchronously
* @Rest\Put("/api/usage/{tool}/{project}/{token}")
* @param string $tool Internal name of tool
* @param string $project Project domain such as en.wikipedia.org
* @param string $token Unique token for this request, so we don't have people
* meddling with these statistics
* @return View
*/
public function recordUsage($tool, $project, $token)
{
// Validate token
if (!$this->isCsrfTokenValid('intention', $token)) {
return new View(
[],
Response::HTTP_FORBIDDEN
);
}

// Don't update counts for tools that aren't enabled
if (!$this->container->getParameter("enable.$tool")) {
return new View(
[
'error' => 'This tool is disabled'
],
Response::HTTP_FORBIDDEN
);
}

$conn = $this->getDoctrine()->getManager('default')->getConnection();
$date = date('Y-m-d');

// Increment count in timeline
$existsSql = "SELECT 1 FROM usage_timeline
WHERE date = '$date'
AND tool = '$tool'";

if (count($conn->query($existsSql)->fetchAll()) === 0) {
$createSql = "INSERT INTO usage_timeline
VALUES(NULL, '$date', '$tool', 1)";
$conn->query($createSql);
} else {
$updateSql = "UPDATE usage_timeline
SET count = count + 1
WHERE tool = '$tool'
AND date = '$date'";
$conn->query($updateSql);
}

// Update per-project usage, if applicable
if (!$this->container->getParameter('app.single_wiki')) {
$existsSql = "SELECT 1 FROM usage_projects
WHERE tool = '$tool'
AND project = '$project'";

if (count($conn->query($existsSql)->fetchAll()) === 0) {
$createSql = "INSERT INTO usage_projects
VALUES(NULL, '$tool', '$project', 1)";
$conn->query($createSql);
} else {
$updateSql = "UPDATE usage_projects
SET count = count + 1
WHERE tool = '$tool'
AND project = '$project'";
$conn->query($updateSql);
}
}

return new View(
[],
Response::HTTP_NO_CONTENT
);
}
}
75 changes: 75 additions & 0 deletions src/AppBundle/Controller/MetaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,79 @@ public function resultAction($start, $end, $legacy = false)
'timeline' => $timeline,
]);
}

/**
* Record usage of a particular XTools tool. This is called automatically
* in base.html.twig via JavaScript so that it is done asynchronously
* @Route("/meta/usage/{tool}/{project}/{token}")
* @param $request Request
* @param string $tool Internal name of tool
* @param string $project Project domain such as en.wikipedia.org
* @param string $token Unique token for this request, so we don't have people
* meddling with these statistics
* @return Response
*/
public function recordUsage(Request $request, $tool, $project, $token)
{
// Validate method and token.
if ($request->getMethod() !== 'PUT' || !$this->isCsrfTokenValid('intention', $token)) {
throw $this->createAccessDeniedException('This endpoint is for internal use only.');
}

// Ready the response object.
$response = new Response();
$response->headers->set('Content-Type', 'application/json');

// Don't update counts for tools that aren't enabled
if (!$this->container->getParameter("enable.$tool")) {
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->setContent(json_encode([
'error' => 'This tool is disabled'
]));
return $response;
}

$conn = $this->container->get('doctrine')->getManager('default')->getConnection();
$date = date('Y-m-d');

// Increment count in timeline
$existsSql = "SELECT 1 FROM usage_timeline
WHERE date = '$date'
AND tool = '$tool'";

if (count($conn->query($existsSql)->fetchAll()) === 0) {
$createSql = "INSERT INTO usage_timeline
VALUES(NULL, '$date', '$tool', 1)";
$conn->query($createSql);
} else {
$updateSql = "UPDATE usage_timeline
SET count = count + 1
WHERE tool = '$tool'
AND date = '$date'";
$conn->query($updateSql);
}

// Update per-project usage, if applicable
if (!$this->container->getParameter('app.single_wiki')) {
$existsSql = "SELECT 1 FROM usage_projects
WHERE tool = '$tool'
AND project = '$project'";

if (count($conn->query($existsSql)->fetchAll()) === 0) {
$createSql = "INSERT INTO usage_projects
VALUES(NULL, '$tool', '$project', 1)";
$conn->query($createSql);
} else {
$updateSql = "UPDATE usage_projects
SET count = count + 1
WHERE tool = '$tool'
AND project = '$project'";
$conn->query($updateSql);
}
}

$response->setStatusCode(Response::HTTP_NO_CONTENT);
$response->setContent(json_encode([]));
return $response;
}
}

0 comments on commit 4cc8fe7

Please sign in to comment.