Skip to content

Commit

Permalink
Misc/Cleanup
Browse files Browse the repository at this point in the history
 * moving commonly used strings to defines
 * moving commonly reused/similar page generation functions to the parent
 * generally using consistent return types, more type hints and less strings
 * prevent browser context menu when right clicking on UI elements with their own context menus
 * fixed menu path for icons
  • Loading branch information
Sarjuuk committed May 24, 2020
1 parent b044488 commit 6cabfd3
Show file tree
Hide file tree
Showing 38 changed files with 411 additions and 611 deletions.
2 changes: 1 addition & 1 deletion includes/ajaxHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AjaxHandler
protected $params = [];
protected $handler;

protected $contentType = 'application/x-javascript; charset=utf-8';
protected $contentType = MIME_TYPE_JSON;

protected $_post = [];
protected $_get = [];
Expand Down
6 changes: 3 additions & 3 deletions includes/ajaxHandler/comment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function handleCommentSticky() : void

protected function handleCommentOutOfDate() : string
{
$this->contentType = 'text/plain';
$this->contentType = MIME_TYPE_TEXT;

if (!$this->_post['id'])
{
Expand Down Expand Up @@ -320,7 +320,7 @@ protected function handleCommentShowReplies() : string

protected function handleReplyAdd() : string
{
$this->contentType = 'text/plain';
$this->contentType = MIME_TYPE_TEXT;

if (!User::canComment())
return Lang::main('cannotComment');
Expand All @@ -343,7 +343,7 @@ protected function handleReplyAdd() : string

protected function handleReplyEdit() : string
{
$this->contentType = 'text/plain';
$this->contentType = MIME_TYPE_TEXT;

if (!User::canComment())
return Lang::main('cannotComment');
Expand Down
2 changes: 1 addition & 1 deletion includes/ajaxHandler/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected function handleAvatar() : void // image
return;
}

$this->contentType = 'image/'.$matches[2];
$this->contentType = $matches[2] == 'png' ? MIME_TYPE_PNG : MIME_TYPE_JPEG;

$id = $matches[1];
$dest = imageCreateTruecolor($sizes[$s], $sizes[$s]);
Expand Down
7 changes: 5 additions & 2 deletions includes/basetype.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ trait spawnHelper

private function createShortSpawns() // [zoneId, floor, [[x1, y1], [x2, y2], ..]] as tooltip2 if enabled by <a rel="map" ...> or anchor #map (one area, one floor, one creature, no survivors)
{
$this->spawnResult[SPAWNINFO_SHORT] = new StdClass;

// first get zone/floor with the most spawns
if ($res = DB::Aowow()->selectRow('SELECT areaId, floor FROM ?_spawns WHERE type = ?d && typeId = ?d GROUP BY areaId, floor ORDER BY count(1) DESC LIMIT 1', self::$type, $this->id))
{
Expand All @@ -566,7 +568,8 @@ private function createShortSpawns() // [zoneId, floor, [[x1,
foreach ($points as $p)
$spawns[] = [$p['posX'], $p['posY']];

$this->spawnResult[SPAWNINFO_SHORT] = [$res['areaId'], $res['floor'], $spawns];
$this->spawnResult[SPAWNINFO_SHORT]->zone = $res['areaId'];
$this->spawnResult[SPAWNINFO_SHORT]->coords = [$res['floor'] => $spawns];
}
}

Expand Down Expand Up @@ -736,7 +739,7 @@ public function getSpawns($mode)
switch ($mode)
{
case SPAWNINFO_SHORT:
if (empty($this->spawnResult[SPAWNINFO_SHORT]))
if ($this->spawnResult[SPAWNINFO_SHORT] === null)
$this->createShortSpawns();

return $this->spawnResult[SPAWNINFO_SHORT];
Expand Down
10 changes: 9 additions & 1 deletion includes/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
* Page
*/

define('E_AOWOW', E_ALL & ~(E_DEPRECATED | E_USER_DEPRECATED | E_STRICT));
define('E_AOWOW', E_ALL & ~(E_DEPRECATED | E_USER_DEPRECATED | E_STRICT));
define('JSON_AOWOW_POWER', JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

define('MIME_TYPE_TEXT', 'Content-Type: text/plain; charset=utf-8');
define('MIME_TYPE_XML', 'Content-Type: text/xml; charset=utf-8');
define('MIME_TYPE_JSON', 'Content-Type: application/x-javascript; charset=utf-8');
define('MIME_TYPE_RSS', 'Content-Type: application/rss+xml; charset=utf-8');
define('MIME_TYPE_JPEG', 'Content-Type: image/jpeg');
define('MIME_TYPE_PNG', 'Content-Type: image/png');

// TypeIds
define('TYPE_NPC', 1);
Expand Down
4 changes: 2 additions & 2 deletions includes/kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
);

if (!CLI)
(new GenericPage(null))->error();
(new GenericPage())->error();
else
echo 'Exception - '.$ex->getMessage()."\n ".$ex->getFile(). '('.$ex->getLine().")\n".$ex->getTraceAsString()."\n";
});
Expand Down Expand Up @@ -244,7 +244,7 @@
$str = explode('&', mb_strtolower($_SERVER['QUERY_STRING']), 2)[0];
$_ = explode('=', $str, 2);
$pageCall = $_[0];
$pageParam = isset($_[1]) ? $_[1] : null;
$pageParam = isset($_[1]) ? $_[1] : '';

Util::$wowheadLink = 'http://'.Util::$subDomains[User::$localeId].'.wowhead.com/'.$str;
}
Expand Down
7 changes: 5 additions & 2 deletions includes/types/areatrigger.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct($conditions)
$_curTpl['name'] = 'Unnamed Areatrigger #' . $id;
}

public function getListviewData()
public function getListviewData() : array
{
$data = [];

Expand All @@ -46,7 +46,10 @@ public function getListviewData()
return $data;
}

public function getJSGlobals($addMask = GLOBALINFO_ANY) { }
public function getJSGlobals($addMask = GLOBALINFO_ANY)
{
return [];
}

public function renderTooltip() { }
}
Expand Down
5 changes: 3 additions & 2 deletions includes/types/item.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,9 @@ public function renderTooltip($interactive = false, $subOf = 0, $enhance = [])
if ($dur = $this->curTpl['durability'])
$x .= sprintf(Lang::item('durability'), $dur, $dur).'<br />';

$jsg = [];
// required classes
if ($classes = Lang::getClassString($this->curTpl['requiredClass'], $jsg, $__))
if ($classes = Lang::getClassString($this->curTpl['requiredClass'], $jsg))
{
foreach ($jsg as $js)
if (empty($this->jsGlobals[TYPE_CLASS][$js]))
Expand All @@ -863,7 +864,7 @@ public function renderTooltip($interactive = false, $subOf = 0, $enhance = [])
}

// required races
if ($races = Lang::getRaceString($this->curTpl['requiredRace'], $jsg, $__))
if ($races = Lang::getRaceString($this->curTpl['requiredRace'], $jsg))
{
foreach ($jsg as $js)
if (empty($this->jsGlobals[TYPE_RACE][$js]))
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
header('Location: '.$out, true, 302);
else
{
header('Content-type: '.$ajax->getContentType());
header($ajax->getContentType());
die($out);
}
}
Expand Down
6 changes: 2 additions & 4 deletions localization/lang.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static function getMagicSchools($schoolMask)
return implode(', ', $tmp);
}

public static function getClassString($classMask, &$ids = [], &$n = 0, $asHTML = true)
public static function getClassString(int $classMask, array &$ids = [], bool $asHTML = true) : string
{
$classMask &= CLASS_MASK_ALL; // clamp to available classes..

Expand All @@ -359,13 +359,12 @@ public static function getClassString($classMask, &$ids = [], &$n = 0, $asHTML =
$i++;
}

$n = count($tmp);
$ids = array_keys($tmp);

return implode(', ', $tmp);
}

public static function getRaceString($raceMask, &$ids = [], &$n = 0, $asHTML = true)
public static function getRaceString(int $raceMask, array &$ids = [], bool $asHTML = true) : string
{
$raceMask &= RACE_MASK_ALL; // clamp to available races..

Expand Down Expand Up @@ -396,7 +395,6 @@ public static function getRaceString($raceMask, &$ids = [], &$n = 0, $asHTML = t
$i++;
}

$n = count($tmp);
$ids = array_keys($tmp);

return implode(', ', $tmp);
Expand Down
44 changes: 10 additions & 34 deletions pages/achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class AchievementPage extends GenericPage
protected $tabId = 0;
protected $mode = CACHE_TYPE_PAGE;

private $powerTpl = '$WowheadPower.registerAchievement(%d, %d, %s);';

public function __construct($pageCall, $id)
{
parent::__construct($pageCall, $id);
Expand All @@ -44,7 +46,7 @@ public function __construct($pageCall, $id)

$this->subject = new AchievementList(array(['id', $this->typeId]));
if ($this->subject->error)
$this->notFound();
$this->notFound(Lang::game('achievement'), Lang::achievement('notFound'));

$this->extendGlobalData($this->subject->getJSGlobals(GLOBALINFO_REWARDS));

Expand Down Expand Up @@ -527,43 +529,17 @@ protected function generateContent()
}
}

protected function generateTooltip($asError = false)
{
if ($asError)
return '$WowheadPower.registerAchievement('.$this->typeId.', '.User::$localeId.', {});';

$x = '$WowheadPower.registerAchievement('.$this->typeId.', '.User::$localeId.",{\n";
$x .= "\tname_".User::$localeString.": '".Util::jsEscape($this->subject->getField('name', true))."',\n";
$x .= "\ticon: '".rawurlencode($this->subject->getField('iconString', true, true))."',\n";
$x .= "\ttooltip_".User::$localeString.": '".$this->subject->renderTooltip()."'\n";
$x .= "});";

return $x;
}

public function display($override = '')
protected function generateTooltip()
{
if ($this->mode != CACHE_TYPE_TOOLTIP)
return parent::display($override);

if (!$this->loadCache($tt))
$power = new StdClass();
if (!$this->subject->error)
{
$tt = $this->generateTooltip();
$this->saveCache($tt);
$power->{'name_'.User::$localeString} = $this->subject->getField('name', true);
$power->icon = rawurlencode($this->subject->getField('iconString', true, true));
$power->{'tooltip_'.User::$localeString} = $this->subject->renderTooltip();
}

header('Content-type: application/x-javascript; charset=utf-8');
die($tt);
}

public function notFound($title = '', $msg = '')
{
if ($this->mode != CACHE_TYPE_TOOLTIP)
return parent::notFound($title ?: Lang::game('achievement'), $msg ?: Lang::achievement('notFound'));

header('Content-type: application/x-javascript; charset=utf-8');
echo $this->generateTooltip(true);
exit();
return sprintf($this->powerTpl, $this->typeId, User::$localeId, Util::toJSON($power, JSON_AOWOW_POWER));
}

private function createMail(&$reqCss = false)
Expand Down
7 changes: 3 additions & 4 deletions pages/areatrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($pageCall, $id)

$this->subject = new AreaTriggerList(array(['id', $this->typeId]));
if ($this->subject->error)
$this->notFound(Util::ucFirst(Lang::game('areatrigger')), Lang::areatrigger('notFound'));
$this->notFound(Lang::game('areatrigger'), Lang::areatrigger('notFound'));

$this->name = $this->subject->getField('name') ?: 'AT #'.$this->typeId;
}
Expand Down Expand Up @@ -99,14 +99,13 @@ protected function generateContent()
{
$sai = new SmartAI(SAI_SRC_TYPE_AREATRIGGER, $this->typeId, ['name' => $this->name, 'teleportA' => $this->subject->getField('teleportA')]);
if ($sai->prepare())
foreach ($sai->getJSGlobals() as $type => $typeIds)
$this->extendGlobalIds($type, $typeIds);
$this->extendGlobalData($sai->getJSGlobals());
}


$this->map = $map;
$this->infobox = false;
$this->extraText = $sai ? $sai->getMarkdown() : null;
$this->smartAI = $sai ? $sai->getMarkdown() : null;
$this->redButtons = array(
BUTTON_LINKS => false,
BUTTON_WOWHEAD => false
Expand Down
4 changes: 2 additions & 2 deletions pages/arenateam.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ protected function generateContent()
}
}

public function notFound($title = '', $msg = '')
public function notFound(string $title = '', string $msg = '') : void
{
return parent::notFound($title ?: Util::ucFirst(Lang::profiler('profiler')), $msg ?: Lang::profiler('notFound', 'arenateam'));
parent::notFound($title ?: Util::ucFirst(Lang::profiler('profiler')), $msg ?: Lang::profiler('notFound', 'arenateam'));
}

private function handleIncompleteData($teamGuid)
Expand Down
44 changes: 10 additions & 34 deletions pages/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class CurrencyPage extends GenericPage
protected $tabId = 0;
protected $mode = CACHE_TYPE_PAGE;

private $powerTpl = '$WowheadPower.registerCurrency(%d, %d, %s);';

public function __construct($pageCall, $id)
{
parent::__construct($pageCall, $id);
Expand All @@ -29,7 +31,7 @@ public function __construct($pageCall, $id)

$this->subject = new CurrencyList(array(['id', $this->typeId]));
if ($this->subject->error)
$this->notFound();
$this->notFound(Lang::game('currency'), Lang::currency('notFound'));

$this->name = $this->subject->getField('name', true);
}
Expand Down Expand Up @@ -220,43 +222,17 @@ protected function generateContent()
}
}

protected function generateTooltip($asError = false)
{
if ($asError)
return '$WowheadPower.registerCurrency('.$this->typeId.', '.User::$localeId.', {});';

$x = '$WowheadPower.registerCurrency('.$this->typeId.', '.User::$localeId.", {\n";
$x .= "\tname_".User::$localeString.": '".Util::jsEscape($this->subject->getField('name', true))."',\n";
$x .= "\ticon: '".rawurlencode($this->subject->getField('iconString', true, true))."',\n";
$x .= "\ttooltip_".User::$localeString.": '".$this->subject->renderTooltip()."'\n";
$x .= "});";

return $x;
}

public function display($override = '')
protected function generateTooltip()
{
if ($this->mode != CACHE_TYPE_TOOLTIP)
return parent::display($override);

if (!$this->loadCache($tt))
$power = new StdClass();
if (!$this->subject->error)
{
$tt = $this->generateTooltip();
$this->saveCache($tt);
$power->{'name_'.User::$localeString} = $this->subject->getField('name', true);
$power->icon = rawurlencode($this->subject->getField('iconString', true, true));
$power->{'tooltip_'.User::$localeString} = $this->subject->renderTooltip();
}

header('Content-type: application/x-javascript; charset=utf-8');
die($tt);
}

public function notFound($title = '', $msg = '')
{
if ($this->mode != CACHE_TYPE_TOOLTIP)
return parent::notFound($title ?: Lang::game('currency'), $msg ?: Lang::currency('notFound'));

header('Content-type: application/x-javascript; charset=utf-8');
echo $this->generateTooltip(true);
exit();
return sprintf($this->powerTpl, $this->typeId, User::$localeId, Util::toJSON($power, JSON_AOWOW_POWER));
}
}

Expand Down
2 changes: 1 addition & 1 deletion pages/emote.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($pageCall, $id)

$this->subject = new EmoteList(array(['id', $this->typeId]));
if ($this->subject->error)
$this->notFound(Util::ucFirst(Lang::game('emote')), Lang::emote('notFound'));
$this->notFound(Lang::game('emote'), Lang::emote('notFound'));

$this->name = Util::ucFirst($this->subject->getField('cmd'));
}
Expand Down
2 changes: 1 addition & 1 deletion pages/enchantment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($pageCall, $id)

$this->subject = new EnchantmentList(array(['id', $this->typeId]));
if ($this->subject->error)
$this->notFound(Util::ucFirst(Lang::game('enchantment')), Lang::enchantment('notFound'));
$this->notFound(Lang::game('enchantment'), Lang::enchantment('notFound'));

$this->extendGlobalData($this->subject->getJSGlobals());

Expand Down
Loading

0 comments on commit 6cabfd3

Please sign in to comment.