Skip to content

Commit de120c1

Browse files
authored
Merge pull request #121 from VSEphpbb/cleanup
Continue patching the removal of old ideas gunk
2 parents c40e0f1 + ddd681f commit de120c1

6 files changed

Lines changed: 402 additions & 72 deletions

File tree

event/listener.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function getSubscribedEvents()
8282
{
8383
return array(
8484
'core.viewforum_get_topic_data' => 'ideas_forum_redirect',
85-
'core.viewtopic_modify_post_row' => array(array('clean_message'), array('show_post_buttons')),
85+
'core.viewtopic_modify_post_row' => 'show_post_buttons',
8686
'core.viewtopic_modify_page_title' => 'show_idea',
8787
'core.viewtopic_add_quickmod_option_before' => 'adjust_quickmod_tools',
8888
'core.viewonline_overwrite_location' => 'viewonline_ideas',
@@ -107,34 +107,6 @@ public function ideas_forum_redirect($event)
107107
}
108108
}
109109

110-
/**
111-
* Clean obsolete link-backs from idea topics
112-
*
113-
* @param \phpbb\event\data $event The event object
114-
* @return void
115-
* @access public
116-
*/
117-
public function clean_message($event)
118-
{
119-
if (!$this->is_ideas_forum($event['row']['forum_id']))
120-
{
121-
return;
122-
}
123-
124-
if ($event['topic_data']['topic_first_post_id'] == $event['row']['post_id'] && $event['topic_data']['topic_time'] < strtotime('September 1, 2017'))
125-
{
126-
$post_row = $event['post_row'];
127-
$message = $post_row['MESSAGE'];
128-
129-
// This freakish looking regex pattern should
130-
// remove the old ideas link-backs from the message.
131-
$message = preg_replace('/(<br[^>]*>\\n?)?\\1?-{10}\\1?\\1?.*]/s', '', $message);
132-
133-
$post_row['MESSAGE'] = $message;
134-
$event['post_row'] = $post_row;
135-
}
136-
}
137-
138110
/**
139111
* Show post buttons (hide delete, quote or warn user buttons)
140112
*
@@ -149,7 +121,7 @@ public function show_post_buttons($event)
149121
return;
150122
}
151123

152-
if ($event['topic_data']['topic_first_post_id'] == $event['row']['post_id'])
124+
if ($this->is_first_post($event['topic_data']['topic_first_post_id'], $event['row']['post_id']))
153125
{
154126
$event->update_subarray('post_row', 'U_DELETE', false);
155127
$event->update_subarray('post_row', 'U_WARN', false);
@@ -336,9 +308,9 @@ public function viewonline_ideas($event)
336308
public function edit_idea_title($event)
337309
{
338310
if ($event['mode'] !== 'edit' ||
339-
$event['post_data']['topic_first_post_id'] != $event['post_id'] ||
340311
!$event['update_subject'] ||
341-
!$this->is_ideas_forum($event['forum_id']))
312+
!$this->is_ideas_forum($event['forum_id']) ||
313+
!$this->is_first_post($event['post_data']['topic_first_post_id'], $event['post_id']))
342314
{
343315
return;
344316
}
@@ -358,4 +330,17 @@ protected function is_ideas_forum($forum_id)
358330
{
359331
return (int) $forum_id === (int) $this->config['ideas_forum_id'];
360332
}
333+
334+
/**
335+
* Check if a post is the first post in a topic
336+
*
337+
* @param int|string $topic_first_post_id
338+
* @param int|string $post_id
339+
* @return bool
340+
* @access protected
341+
*/
342+
protected function is_first_post($topic_first_post_id, $post_id)
343+
{
344+
return (int) $topic_first_post_id === (int) $post_id;
345+
}
361346
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\ideas\migrations;
12+
13+
class m11_reparse_old_ideas extends \phpbb\db\migration\container_aware_migration
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public static function depends_on()
19+
{
20+
return [
21+
'\phpbb\ideas\migrations\m1_initial_schema',
22+
'\phpbb\ideas\migrations\m6_migrate_old_tables',
23+
'\phpbb\ideas\migrations\m7_drop_old_tables',
24+
'\phpbb\ideas\migrations\m8_implemented_version',
25+
'\phpbb\ideas\migrations\m10_update_idea_schema',
26+
];
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function effectively_installed()
33+
{
34+
/** @var \phpbb\textreparser\manager $reparser_manager */
35+
$reparser_manager = $this->container->get('text_reparser.manager');
36+
37+
return !empty($reparser_manager->get_resume_data('phpbb.ideas.text_reparser.clean_old_ideas') || !$this->bbcode_exists('idea'));
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function update_data()
44+
{
45+
return [
46+
['custom', [[$this, 'reparse']]],
47+
];
48+
}
49+
50+
/**
51+
* Run the clean old ideas reparser
52+
*
53+
* @param int $current An idea identifier
54+
* @return bool|int An idea identifier or true if finished
55+
*/
56+
public function reparse($current = 0)
57+
{
58+
/** @var \phpbb\textreparser\manager $reparser_manager */
59+
$reparser_manager = $this->container->get('text_reparser.manager');
60+
61+
/** @var \phpbb\textformatter\s9e\utils $text_formatter_utils */
62+
$text_formatter_utils = $this->container->get('text_formatter.utils');
63+
64+
$reparser = new \phpbb\ideas\textreparser\plugins\clean_old_ideas(
65+
$this->db,
66+
$text_formatter_utils,
67+
$this->container->getParameter('tables.posts'),
68+
$this->container->getParameter('tables.topics'),
69+
$this->container->getParameter('core.table_prefix') . 'ideas_ideas'
70+
);
71+
72+
if (empty($current))
73+
{
74+
$current = $reparser->get_max_id();
75+
}
76+
77+
$limit = 50; // lets keep the reparsing conservative
78+
$start = max(1, $current + 1 - $limit);
79+
$end = max(1, $current);
80+
81+
$reparser->reparse_range($start, $end);
82+
83+
$current = $start - 1;
84+
85+
if ($current === 0)
86+
{
87+
// Prevent CLI command from running this reparser again
88+
$reparser_manager->update_resume_data('phpbb.ideas.text_reparser.clean_old_ideas', 1, 0, $limit);
89+
90+
return true;
91+
}
92+
93+
return $current;
94+
}
95+
96+
/**
97+
* Check if a bbcode exists
98+
*
99+
* @param string $tag BBCode's tag
100+
* @return bool True if bbcode exists, false if not
101+
*/
102+
public function bbcode_exists($tag)
103+
{
104+
$sql = 'SELECT bbcode_id
105+
FROM ' . $this->table_prefix . "bbcodes
106+
WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($tag)) . "'
107+
OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($tag)) . "='";
108+
$result = $this->db->sql_query_limit($sql, 1);
109+
$bbcode_id = $this->db->sql_fetchfield('bbcode_id');
110+
$this->db->sql_freeresult($result);
111+
112+
return $bbcode_id !== false;
113+
}
114+
}

tests/event/listener_test.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,46 +118,6 @@ public function test_getSubscribedEvents()
118118
), array_keys(\phpbb\ideas\event\listener::getSubscribedEvents()));
119119
}
120120

121-
/**
122-
* Data set for test_clean_message
123-
*
124-
* @return array Array of test data
125-
*/
126-
public function clean_message_data()
127-
{
128-
return array(
129-
array(1, 1, 1, 'Foo Bar', 'Foo Bar'), // Invalid forum, nothing cleaned
130-
array(2, 1, 2, 'Foo Bar', 'Foo Bar'), // Invalid post, nothing clean
131-
array(2, 1, 1, 'Foo Bar', 'Foo Bar'), // Valid post, nothing to clean
132-
array(2, 1, 1, 'Foo Bar<br />----------<br>BarFoo<br>', 'Foo Bar'), // Valid post, requires cleaning
133-
array(2, 1, 1, 'Foo Bar<br />\n\n----------<br>\nBarFoo<br>', 'Foo Bar'), // Valid post, requires cleaning
134-
array(2, 1, 1, 'Foo Bar<br />--------<br>BarFoo<br>', 'Foo Bar<br />--------<br>BarFoo<br>'), // Valid post, nothing to clean
135-
);
136-
}
137-
138-
/**
139-
* Test the clean_message event
140-
*
141-
* @dataProvider clean_message_data
142-
*/
143-
public function test_clean_message($forum_id, $post_id, $first_post_id, $message, $expected)
144-
{
145-
$listener = $this->get_listener();
146-
147-
$event = new \phpbb\event\data(array(
148-
'row' => array(
149-
'forum_id' => $forum_id,
150-
'post_id' => $post_id,
151-
),
152-
'post_row' => array('MESSAGE' => $message),
153-
'topic_data' => array('topic_first_post_id' => $first_post_id),
154-
));
155-
156-
$listener->clean_message($event);
157-
158-
$this->assertContains($expected, $event['post_row']['MESSAGE']);
159-
}
160-
161121
/**
162122
* Data set for show_post_buttons
163123
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\ideas\tests\textreparser;
12+
13+
include_once __DIR__ . '/../../../../../../tests/text_reparser/plugins/test_row_based_plugin.php';
14+
15+
class clean_old_ideas_test extends \phpbb_textreparser_test_row_based_plugin
16+
{
17+
protected static function setup_extensions()
18+
{
19+
return array('phpbb/ideas');
20+
}
21+
22+
public function getDataSet()
23+
{
24+
return $this->createXMLDataSet(__DIR__ . '/fixtures/ideas.xml');
25+
}
26+
27+
protected function get_reparser()
28+
{
29+
return new \phpbb\ideas\textreparser\plugins\clean_old_ideas(
30+
$this->db,
31+
new \phpbb\textformatter\s9e\utils(),
32+
'phpbb_posts',
33+
'phpbb_topics',
34+
'phpbb_ideas_ideas'
35+
);
36+
}
37+
38+
public function get_reparse_tests()
39+
{
40+
return [
41+
[
42+
1,
43+
5,
44+
[
45+
[
46+
'id' => '100',
47+
'text' => 'This post has nothing to change',
48+
],
49+
[
50+
'id' => '200',
51+
'text' => '<r>This post is too new to be cleaned<IDEA idea="1000"><s>[idea=1000]</s>Linked Idea<e>[/idea]</e></IDEA><USER user="1000"><s>[user=1000]</s>testuser<e>[/user]</e></USER></r>',
52+
],
53+
[
54+
'id' => '300',
55+
'text' => '<r>This post should be cleaned</r>',
56+
],
57+
[
58+
'id' => '400',
59+
'text' => '<r>This post is a reply and should not be changed<IDEA idea="1000"><s>[idea=1000]</s>Linked Idea<e>[/idea]</e></IDEA><USER user="1000"><s>[user=1000]</s>testuser<e>[/user]</e></USER></r>',
60+
],
61+
[
62+
'id' => '101010',
63+
'text' => '<r>This post is out of range<IDEA idea="1000"><s>[idea=1000]</s>Linked Idea<e>[/idea]</e></IDEA><USER user="1000"><s>[user=1000]</s>testuser<e>[/user]</e></USER></r>',
64+
],
65+
]
66+
],
67+
];
68+
}
69+
}

0 commit comments

Comments
 (0)