Passed
Push — master ( 15dbed...23823a )
by Nicolaas
17:52 queued 06:19
created

SiteWideSearch   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 3
Metric Value
eloc 39
c 11
b 0
f 3
dl 0
loc 66
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
D run() 0 56 13
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 = true;
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 = $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 $link) {
60
            $item = $link->Object;
61
            $title = $item->getTitle() . ' (' . $item->i18n_singular_name() . ')';
62
            if ($debug !== '' && $debug !== '0') {
63
                $title .= ' Class: ' . $item->ClassName . ', ID: ' . $item->ID . ', Sort Value: ' . $link->SiteWideSearchSortValue;
64
            }
65
66
            $cmsEditLink = $link->HasCMSEditLink ? '<a href="' . $link->CMSEditLink . '">✎</a> ...' : 'x  ...';
67
            if ($link->HasLink) {
68
                DB::alteration_message($cmsEditLink . '<a href="' . $link->Link . '">' . $title . '</a> - ', 'created');
69
            } else {
70
                DB::alteration_message($cmsEditLink . $title, 'obsolete');
71
            }
72
        }
73
74
        if ($replace !== '' && $replace !== '0') {
75
            $api->doReplacement($word, $replace);
76
            $api->setDebug(true);
77
        }
78
    }
79
}
80