Skip to content

Commit

Permalink
Articleinfo: Show some Wikidata bugs, add wiki edit link helper
Browse files Browse the repository at this point in the history
  • Loading branch information
MusikAnimal committed Apr 18, 2017
1 parent 047a58f commit cdaab16
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
30 changes: 22 additions & 8 deletions app/Resources/views/articleInfo/result.html.twig
Expand Up @@ -40,7 +40,7 @@
{% if assessments is defined %}
{% set sections = sections | merge(['assessments']) %}
{% endif %}
{% if checkwiki_errors is defined %}
{% if bugs is defined %}
{% set sections = sections | merge(['bugs']) %}
{% endif %}

Expand Down Expand Up @@ -249,10 +249,10 @@
</tr>
{% endif %}

{% if checkwiki_errors is defined %}
{% if bugs is defined %}
<tr>
<td class="stat-list--new-group"><a href='#bugs' class='rm-inline-margin'>{{ msg('bugs') }}</a></td>
<td class="stat-list--new-group">{{ checkwiki_errors | length }}</td>
<td class="stat-list--new-group">{{ bugs | length }}</td>
</tr>
{% endif %}
</tbody></table>
Expand Down Expand Up @@ -629,7 +629,7 @@
{% endif %}

{# ======================== BUGS ======================== #}
{% if checkwiki_errors is defined %}
{% if bugs is defined %}
{% set content %}
<table class="table table-bordered table-hover table-striped bugs-table">
<thead><tr>
Expand All @@ -641,16 +641,30 @@
</span>
</th>
{% endfor %}
<th>{{ msg('edit') | capitalize_first }}</th>
</tr></thead>
<tbody>
{% for bug in checkwiki_errors %}
{% for bug in bugs %}
<tr>
<td class="sort-entry--priority" data-value="{{ bug.prio }}">{{ bug.prio }}</td>
<td class="sort-entry--name" data-value="{{ bug.name_trans }}">{{ bug.name_trans }}</td>
<td class="sort-entry--name" data-value="{{ bug.name }}">{{ bug.name }}</td>
<td class="sort-entry--notice" data-value="{{ bug.notice }}">
<code>{{ bug.notice }}</code>
{% if bug.name == 'Wikidata' %}
{{ bug.notice | raw }}
{% else %}
<code>{{ bug.notice }}</code>
{% endif %}
</td>
<td class="sort-entry--explanation" data-value="{{ bug.explanation }}">{{ bug.explanation | raw }}</td>
<td>
{% if bug.name == 'Wikidata' %}
<a target='_blank' href='//www.wikidata.org/wiki/{{ wikidataId }}'>
{{ msg('edit') }}
</a>
{% else %}
{{ edit_link(title, project_url) }}
{% endif %}
</td>
<td class="sort-entry--explanation" data-value="{{ bug.text_trans }}">{{ bug.text_trans | raw }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
1 change: 1 addition & 0 deletions i18n/en.json
Expand Up @@ -46,6 +46,7 @@
"deleted_edits": "Deleted edits",
"diff": "Diff",
"dupes": "Dupes",
"edit": "Edit",
"editcounter": "editcounter",
"editorcount": "Number of editors",
"edits": "edits",
Expand Down
69 changes: 65 additions & 4 deletions src/AppBundle/Controller/ArticleInfoController.php
Expand Up @@ -92,6 +92,7 @@ public function resultAction(Request $request)
'project' => preg_replace('#^https?://#', '', rtrim($projectUrl, '/')),
'project_url' => $projectUrl,
'db_name' => $dbValues['dbName'],
'lang' => $dbValues['lang'],
'id' => $basicInfo['pageid'],
'namespace' => $basicInfo['ns'],
'title' => $basicInfo['title'],
Expand Down Expand Up @@ -146,9 +147,9 @@ public function resultAction(Request $request)
}
$this->setLogsEvents();

$checkWikiErrors = $this->getCheckWikiErrors();
if (!empty($checkWikiErrors)) {
$this->pageInfo['checkwiki_errors'] = $checkWikiErrors;
$bugs = array_merge($this->getCheckWikiErrors(), $this->getWikidataErrors());
if (!empty($bugs)) {
$this->pageInfo['bugs'] = $bugs;
}

$this->pageInfo['xtPage'] = 'articleinfo';
Expand Down Expand Up @@ -350,7 +351,7 @@ private function getCheckWikiErrors()
$title = $this->pageInfo['title']; // no underscores
$dbName = preg_replace('/_p$/', '', $this->pageInfo['db_name']); // remove _p if present

$query = "SELECT error, notice, found, name_trans, prio, text_trans
$query = "SELECT error, notice, found, name_trans AS name, prio, text_trans AS explanation
FROM s51080__checkwiki_p.cw_error a
JOIN s51080__checkwiki_p.cw_overview_errors b
WHERE a.project = b.project AND a.project = '$dbName'
Expand All @@ -362,6 +363,66 @@ private function getCheckWikiErrors()
return $res;
}

/**
* Get basic wikidata on the page: label and description.
* Reported as "bugs" if they are missing.
* @return array Label and description, if present
*/
private function getWikidataErrors()
{
$wikidataId = ltrim($this->pageInfo['wikidataId'], 'Q');
$lang = $this->pageInfo['lang'];

$query = "SELECT IF(term_type = 'label', 'label', 'description') AS term, term_text
FROM wikidatawiki_p.wb_entity_per_page
JOIN wikidatawiki_p.page ON epp_page_id = page_id
JOIN wikidatawiki_p.wb_terms ON term_entity_id = epp_entity_id
AND term_language = '$lang' AND term_type IN ('label', 'description')
WHERE epp_entity_id = $wikidataId
UNION
SELECT pl_title AS term, wb_terms.term_text
FROM wikidatawiki_p.pagelinks
JOIN wikidatawiki_p.wb_terms ON term_entity_id = SUBSTRING(pl_title, 2)
AND term_entity_type = (IF(SUBSTRING(pl_title, 1, 1) = 'Q', 'item', 'property'))
AND term_language = '$lang'
AND term_type = 'label'
WHERE pl_namespace IN (0,120 )
AND pl_from = (
SELECT page_id FROM page
WHERE page_namespace = 0 AND page_title = 'Q$wikidataId'
)";

$conn = $this->container->get('doctrine')->getManager('replicas')->getConnection();
$res = $conn->query($query)->fetchAll();

$terms = array_map(function ($entry) {
return $entry['term'];
}, $res);

$errors = [];

if (!in_array('label', $terms)) {
$errors[] = [
'prio' => 2,
'name' => 'Wikidata',
'notice' => "Label for language <em>$lang</em> is missing", // FIXME: i18n
'explanation' => "See: <a target='_blank' " .
"href='//www.wikidata.org/wiki/Help:Label'>Help:Label</a>",
];
}
if (!in_array('description', $terms)) {
$errors[] = [
'prio' => 3,
'name' => 'Wikidata',
'notice' => "Description for language <em>$lang</em> is missing", // FIXME: i18n
'explanation' => "See: <a target='_blank' " .
"href='//www.wikidata.org/wiki/Help:Description'>Help:Description</a>",
];
}

return $errors;
}

/**
* Get every revision of the page
* @return array The data
Expand Down
16 changes: 16 additions & 0 deletions src/AppBundle/Twig/WikiExtension.php
Expand Up @@ -32,6 +32,7 @@ public function getFunctions()
new Twig_SimpleFunction('pageviews_links', [ $this, 'pageviewsLinks' ], $options),
new Twig_SimpleFunction('diff_link', [ $this, 'diffLink' ], $options),
new Twig_SimpleFunction('perma_link', [ $this, 'permaLink' ], $options),
new Twig_SimpleFunction('edit_link', [ $this, 'editLink' ], $options),
];
}

Expand Down Expand Up @@ -202,6 +203,21 @@ public function permaLink($revId, $projectUrl, $label = '')
return "<a href='$projectUrl/wiki/Special:PermaLink/$revId' target='_blank'>$label</a>";
}

/**
* Get a permanent link to the given page at given revision
* @param string $title Title of page
* @param string $projectUrl Project domain and protocol such as https://en.wikipedia.org
* @param string [$label] The link text, defaults to 'Edit'
* @return string Markup
*/
public function editLink($title, $projectUrl, $label = null)
{
if (!isset($label)) {
$label = $this->intuitionMessage('edit');
}
return "<a href='$projectUrl/wiki/$title?action=edit' target='_blank'>$label</a>";
}

/**
* Get links to pageviews tools for the given page
* @param string $title Title of page
Expand Down

0 comments on commit cdaab16

Please sign in to comment.