Skip to content

Category::sort() might not be needed. #9208

@live627

Description

@live627

The original commit fcb140d tried to fix this by offloading sorting to PHP. However, board_order is reliably updated every time a board is changed:

SMF/Sources/Board.php

Lines 1554 to 1585 in fc87e75

/**
* Put all boards in the right order and sorts the records of the boards table.
* Used by Board::modify(), Board::delete(), Category::modify(), and Category::delete()
*/
public static function reorder(): void
{
Category::getTree();
// Set the board order for each category.
$board_order = 0;
foreach (Category::$loaded as $cat_id => $dummy) {
foreach (Category::$boardList[$cat_id] as $board_id) {
if (self::$loaded[$board_id]->order != ++$board_order) {
Db::$db->query(
'UPDATE {db_prefix}boards
SET board_order = {int:new_order}
WHERE id_board = {int:selected_board}',
[
'new_order' => $board_order,
'selected_board' => $board_id,
],
);
self::$loaded[$board_id]->order = $board_order;
}
}
}
// Empty the board order cache
CacheApi::put('board_order', null, -3600);
}

Moreover, my code here asserts that the two methods do indeed produce the same order.

	public function listBoards(int $id_board): array
	{
		$request = Db::$db->query(
			'
			SELECT id_board, b.name, child_level, c.name AS cat_name, id_cat
			FROM {db_prefix}boards AS b
				LEFT JOIN {db_prefix}categories AS c USING (id_cat)
			ORDER BY board_order',
		);
		$boards = [];

		while ($row = Db::$db->fetch_assoc($request)) {
			if (!isset($boards[$row['id_cat']])) {
				$boards[$row['id_cat']] = [
					'name' => strip_tags($row['cat_name']),
					'boards' => [],
				];
			}

			$boards[$row['id_cat']]['boards'][$row['id_board']] = [
				'name' => strip_tags($row['name']),
				'child_level' => $row['child_level'],
				'selected' => $row['id_board'] == $id_board,
			];
		}
		Db::$db->free_result($request);

		$a = $boards;

		// Find the boards/categories they can see.
		$boardListOptions = [
			'not_redirection' => true,
			'selected_board' => $id_board,
		];

		if (!empty(Config::$modSettings['recycle_enable']) && !empty(Config::$modSettings['recycle_board'])) {
			$boardListOptions['excluded_boards'] = [(int) Config::$modSettings['recycle_board']];
		}

		$boards = \SMF\Actions\MessageIndex::getBoardList($boardListOptions);

		// Make the board safe for display.
		foreach ($boards as $id_cat => $cat) {
			$boards[$id_cat]['name'] = Utils::htmlspecialcharsDecode(strip_tags($cat['name']));

			foreach ($cat['boards'] as $id_board => $board) {
				$boards[$id_cat]['boards'][$id_board]['name'] = Utils::htmlspecialcharsDecode(strip_tags($board['name']));
			}
		}

		$b = $boards;

		var_dump(array_keys($a) === array_keys($b)); // categories

		foreach ($a as $k => $cat) {
			if (array_keys($cat['boards']) !== array_keys($b[$k]['boards'])) {
				echo "Mismatch in category $k\n";
			}
		}

		return $boards;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions