Skip to content

Commit

Permalink
Merge pull request #18 from attogram/v0.4.12
Browse files Browse the repository at this point in the history
V0.4.12
  • Loading branch information
attogram authored Feb 4, 2020
2 parents 0ec6e4f + d44fe5f commit efa39fe
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 58 deletions.
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
$verbose = false;

$vendor = '../vendor/autoload.php';
$vendor = '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (!is_readable($vendor)) {
exit('Site down for maintenance');
}
Expand Down
2 changes: 1 addition & 1 deletion public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ a:active {
background-color:yellow;
color:black;
}
.missing {
.missing, a.missing {
color:blueviolet;
}
.head {
Expand Down
9 changes: 7 additions & 2 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class Base
{
const VERSION = '0.4.11';
const VERSION = '0.4.12';

/**
* @var bool - print verbose debug messages to STDOUT
Expand Down Expand Up @@ -160,6 +160,7 @@ protected function encodeLink($query)

/**
* set $this->topic to string from URL elements, or empty string
* @return bool
*/
protected function setTopicFromUrl()
{
Expand All @@ -179,15 +180,19 @@ protected function setTopicFromUrl()
if (!is_string($this->topic) || !strlen($this->topic)) {
$this->topic = '';

return;
return false;
}
// format query
$this->topic = trim($this->topic);
$this->topic = str_replace('_', ' ', $this->topic);
$this->topic = urldecode($this->topic);
if (!is_string($this->topic) || !strlen($this->topic)) {
$this->topic = '';

return false;
}

return true;
}

protected function initFilesystem()
Expand Down
2 changes: 1 addition & 1 deletion src/Mediawiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function links($query)
) {
$result['title'] = $query;
$result['error'] = true;
$this->error('links: 404 NOT FOUND: ' . $query);
//$this->error('links: 404 NOT FOUND: ' . $query);

return $result;
}
Expand Down
77 changes: 34 additions & 43 deletions src/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,76 @@ class Topic extends Base
private $data = []; // topic data
private $vars = []; // template variables

const ERROR_NOT_FOUND = 'Topic Not Found';

/**
* Get a topic
* @return void
*/
public function get()
{
$this->setTopicFromUrl();
if (!$this->topic) {
$this->error404('Not Found');
if (!$this->setTopicFromUrl()) { // build topic from URL
$this->error404(self::ERROR_NOT_FOUND);
}

// get topic from Cache
$this->setDataFromCache();
if ($this->data) {
if ($this->setDataFromCache()) { // get topic data from Cache
if (!empty($this->data['error'])) {
$this->error404('Topic Not Found', $this->topic);
$this->error404(self::ERROR_NOT_FOUND, $this->topic);

return;
}
$this->display(); // show cached results

return;
}

// get topic from API
$this->setDataFromApi();
if ($this->data) {
// save results to cache
$this->filesystem->set($this->topic, json_encode($this->data));
if (!empty($this->data['error'])) {
$this->error404('Topic Not Found', $this->topic);
if ($this->setDataFromApi()) { // get topic data from API
$this->filesystem->set($this->topic, json_encode($this->data)); // save results to cache
if (!empty($this->data['error'])) { // if API reported an error
$this->error404(self::ERROR_NOT_FOUND, $this->topic);

return;
}
$this->display(); // show api results

return;
}

$this->error404('Topic Not Found');
$this->error404(self::ERROR_NOT_FOUND);
}

/**
* set $this->data to array from cached file, or empty array
* @return bool
*/
private function setDataFromCache()
{
$this->initFilesystem();
$this->data = $this->filesystem->get($this->topic);
if (!is_array($this->data)) {
$this->data = [];

return false;
}

return true;
}

/**
* set $this->data to array from api response, or empty array
* @return bool
*/
private function setDataFromApi()
{
$this->initMediawiki();
$this->data = $this->mediawiki->links($this->topic);
if (!is_array($this->data)) {
$this->data = [];

return false;
}

return true;
}

private function display()
Expand Down Expand Up @@ -114,20 +125,17 @@ private function setTemplateVars()
$this->setTemplateExists();
$this->removeTemplateTopics();
foreach (array_keys($this->vars) as $index) {
// set counts
$this->template->set($index . '_count', count($this->vars[$index]));
// sort var lists alphabetically
sort($this->vars[$index]);
// set html list
$this->template->set($index . '_list', $this->listify($index));
$this->template->set($index . '_count', count($this->vars[$index])); // set counts
sort($this->vars[$index]); // sort var lists alphabetically
$this->template->set($index . '_list', $this->listify($index)); // set html list
}
}

private function initVars()
{
$namespaces = [
'main', 'talk',
'template', 'template_talk',
'main', 'talk', 'main_secondary',
'template', 'template_talk', 'template_secondary',
'portal', 'portal_talk',
'wikipedia', 'wikipedia_talk',
'help', 'help_talk',
Expand All @@ -137,8 +145,6 @@ private function initVars()
'refs',
'missing',
'exists',
'main_secondary',
'template_secondary',
];
foreach ($namespaces as $index) {
$this->vars[$index] = [];
Expand All @@ -149,11 +155,9 @@ private function setNamespaces()
{
foreach ($this->data['topics'] as $topic) {
if (!isset($topic['exists'])) {
// page does not exist
$this->vars['missing'][] = $topic['*'];
$this->vars['missing'][] = $topic['*']; // page does not exist
}
// @see https://en.wikipedia.org/wiki/Wikipedia:Namespace
switch ($topic['ns']) {
switch ($topic['ns']) { // @see https://en.wikipedia.org/wiki/Wikipedia:Namespace
case '0': // Mainspace
$this->vars['main'][] = $topic['*'];
break;
Expand Down Expand Up @@ -202,18 +206,6 @@ private function setNamespaces()
case '829': // Module_talk
$this->vars['module_talk'][] = $topic['*'];
break;
/*
case '6': // File
case '7': // File_talk
case '8': // Mediawiki
case '9': // Mediawiki_talk
case '14': // Category
case '15': // Category_talk
case '108': // Book
case '109': // Book_talk
case '710': // TimedText
case '711': // TimedText_talk
*/
default:
break;
}
Expand Down Expand Up @@ -283,8 +275,7 @@ private function removeTemplateTopics()
}
foreach ($templateData['topics'] as $exTopic) {
if ($exTopic['ns'] == '0' && in_array($exTopic['*'], $this->vars['main'])) {
// main namespace only
// remove this template topic from master topic list
// main namespace only - remove this template topic from master topic list
unset($this->vars['main'][array_search($exTopic['*'], $this->vars['main'])]);
$this->vars['main_secondary'][] = $exTopic['*'];
}
Expand Down
3 changes: 0 additions & 3 deletions templates/about.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* About Page template
* @uses $this - \Attogram\Justrefs\Template
*/

$this->include('html_head');
$this->include('header');

?><div class="body">
<h1>
About <b><?= $this->get('name') ?></b> <small>v<?= $this->get('version') ?></small>
Expand Down Expand Up @@ -42,5 +40,4 @@
&lt;<a href="https://github.com/attogram/justrefs">https://github.com/attogram/justrefs</a>&gt;
<p>
</div><?php

$this->include('footer');
2 changes: 1 addition & 1 deletion templates/footer.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Just Refs - https://github.com/attogram/justrefs
* Footer template
*
* Footer template
* @uses $this - \Attogram\Justrefs\Template
*/
?><footer>
Expand Down
3 changes: 0 additions & 3 deletions templates/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* Home Page template
* @uses $this - \Attogram\Justrefs\Template
*/

$this->include('html_head');

?><div class="body">
<h1>Just Refs</h1>
<ul>
Expand Down Expand Up @@ -42,5 +40,4 @@
</ul>
<br />
</div><?php

$this->include('footer');
3 changes: 0 additions & 3 deletions templates/topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* Topic page template
* @uses $this - \Attogram\Justrefs\Template
*/

$this->include('html_head');
$this->include('header');

?><div class="body">
<h1><?= $this->get('h1') ?></h1>
<hr />
Expand Down Expand Up @@ -97,5 +95,4 @@
</div>
<hr />
</div><?php

$this->include('footer');

0 comments on commit efa39fe

Please sign in to comment.