1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SiteWideSearch\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Core\Environment; |
7
|
|
|
use SilverStripe\Dev\BuildTask; |
8
|
|
|
use SilverStripe\ORM\DB; |
9
|
|
|
use Sunnysideup\SiteWideSearch\Api\SearchApi; |
10
|
|
|
|
11
|
|
|
class SiteWideSearch extends BuildTask |
12
|
|
|
{ |
13
|
|
|
protected $title = 'Search the whole site for a word or phrase'; |
14
|
|
|
|
15
|
|
|
protected $description = 'Search the whole site and get a list of links to the matching records'; |
16
|
|
|
|
17
|
|
|
protected $enabled = false; |
18
|
|
|
|
19
|
|
|
private static $segment = 'search-and-replace'; |
20
|
|
|
|
21
|
|
|
public function run($request) |
22
|
|
|
{ |
23
|
|
|
Environment::increaseTimeLimitTo(300); |
24
|
|
|
Environment::setMemoryLimitMax(-1); |
25
|
|
|
Environment::increaseMemoryLimitTo(-1); |
26
|
|
|
$debug = $request->postVar('debug') ? 'checked="checked"' : ''; |
27
|
|
|
$word = $request->requestVar('word'); |
28
|
|
|
if (! is_string($word)) { |
29
|
|
|
$word = ''; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$replace = trim($request->requestVar('replace')); |
33
|
|
|
if (! is_string($replace)) { |
|
|
|
|
34
|
|
|
$replace = ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$html = ' |
38
|
|
|
<form methd="post" action=""> |
39
|
|
|
<h2>Enter Search Word(s):</h2> |
40
|
|
|
<h3>Find</h3> |
41
|
|
|
<input name="word" value="' . Convert::raw2att($word) . '" style="width: 500px; padding: 5px;" /> |
42
|
|
|
<h3>Replace (optional)</h3> |
43
|
|
|
<input name="replace" value="' . Convert::raw2att($replace) . '" style="width: 500px; padding: 5px;" /> |
44
|
|
|
<h3>Do it now ... (careful)</h3> |
45
|
|
|
<input type="submit" value="search OR search and replace" style="width: 250px; padding: 5px;" /> |
46
|
|
|
<br /> |
47
|
|
|
<br />debug: <input name="debug" type="checkbox" ' . $debug . ' /> |
48
|
|
|
</form> |
49
|
|
|
'; |
50
|
|
|
echo $html; |
51
|
|
|
$api = SearchApi::create(); |
52
|
|
|
if ($debug !== '' && $debug !== '0') { |
53
|
|
|
$api->setDebug(true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$api->setWordsAsString($word); |
57
|
|
|
$links = $api->getLinks(); |
58
|
|
|
echo '<h2>results</h2>'; |
59
|
|
|
foreach ($links as $item) { |
60
|
|
|
$title = $item->Title . ' (' . $item->SingularName . ')'; |
61
|
|
|
if ($debug !== '' && $debug !== '0') { |
62
|
|
|
$title .= ' Class: ' . $item->ClassName . ', ID: ' . $item->ID . ', Sort Value: ' . $item->SiteWideSearchSortValue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$cmsEditLink = $item->HasCMSEditLink ? '<a href="' . $item->CMSEditLink . '">✎</a> ...' : 'x ...'; |
66
|
|
|
if ($item->HasLink) { |
67
|
|
|
DB::alteration_message($cmsEditLink . '<a href="' . $item->Link . '">' . $title . '</a> - ', 'created'); |
68
|
|
|
} else { |
69
|
|
|
DB::alteration_message($cmsEditLink . $title, 'obsolete'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($replace !== '' && $replace !== '0') { |
74
|
|
|
$api->setDebug(true); |
75
|
|
|
$api->doReplacement($word, $replace); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|