Index: trunk/extensions/LinkSearch/LinkSearchPage.php |
— | — | @@ -0,0 +1,113 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class LinkSearchPage extends QueryPage { |
| 5 | + |
| 6 | + function __construct( $query, $ns, $prot ) { |
| 7 | + $this->mQuery = $query; |
| 8 | + $this->mNs = $ns; |
| 9 | + $this->mProt = $prot; |
| 10 | + } |
| 11 | + |
| 12 | + function getName() { |
| 13 | + return 'LinkSearch'; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Disable RSS/Atom feeds |
| 18 | + */ |
| 19 | + function isSyndicated() { |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Return an appropriately formatted LIKE query and the clause |
| 25 | + */ |
| 26 | + static function mungeQuery( $query , $prot ) { |
| 27 | + $field = 'el_index'; |
| 28 | + $rv = LinkFilter::makeLike( $query , $prot ); |
| 29 | + if ($rv === false) { |
| 30 | + //makeLike doesn't handle wildcard in IP, so we'll have to munge here. |
| 31 | + if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) { |
| 32 | + $rv = $prot . rtrim($query, " \t*") . '%'; |
| 33 | + $field = 'el_to'; |
| 34 | + } |
| 35 | + } |
| 36 | + return array( $rv, $field ); |
| 37 | + } |
| 38 | + |
| 39 | + function linkParameters() { |
| 40 | + global $wgMiserMode; |
| 41 | + $params = array(); |
| 42 | + $params['target'] = $this->mProt . $this->mQuery; |
| 43 | + if( isset( $this->mNs ) && !$wgMiserMode ) { |
| 44 | + $params['namespace'] = $this->mNs; |
| 45 | + } |
| 46 | + return $params; |
| 47 | + } |
| 48 | + |
| 49 | + function getSQL() { |
| 50 | + global $wgMiserMode; |
| 51 | + $dbr = wfGetDB( DB_SLAVE ); |
| 52 | + $page = $dbr->tableName( 'page' ); |
| 53 | + $externallinks = $dbr->tableName( 'externallinks' ); |
| 54 | + |
| 55 | + /* strip everything past first wildcard, so that index-based-only lookup would be done */ |
| 56 | + list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt ); |
| 57 | + $stripped = substr($munged,0,strpos($munged,'%')+1); |
| 58 | + $encSearch = $dbr->addQuotes( $stripped ); |
| 59 | + |
| 60 | + $encSQL = ''; |
| 61 | + if ( isset ($this->mNs) && !$wgMiserMode ) |
| 62 | + $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs ); |
| 63 | + |
| 64 | + $use_index = $dbr->useIndexClause( $clause ); |
| 65 | + return |
| 66 | + "SELECT |
| 67 | + page_namespace AS namespace, |
| 68 | + page_title AS title, |
| 69 | + el_index AS value, |
| 70 | + el_to AS url |
| 71 | + FROM |
| 72 | + $page, |
| 73 | + $externallinks $use_index |
| 74 | + WHERE |
| 75 | + page_id=el_from |
| 76 | + AND $clause LIKE $encSearch |
| 77 | + $encSQL"; |
| 78 | + } |
| 79 | + |
| 80 | + function formatResult( $skin, $result ) { |
| 81 | + $title = Title::makeTitle( $result->namespace, $result->title ); |
| 82 | + $url = $result->url; |
| 83 | + $pageLink = $skin->makeKnownLinkObj( $title ); |
| 84 | + $urlLink = $skin->makeExternalLink( $url, $url ); |
| 85 | + |
| 86 | + return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Override to check query validity. |
| 91 | + */ |
| 92 | + function doQuery( $offset, $limit, $shownavigation=true ) { |
| 93 | + global $wgOut; |
| 94 | + list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt ); |
| 95 | + if( $this->mMungedQuery === false ) { |
| 96 | + $wgOut->addWikiText( wfMsg( 'linksearch-error' ) ); |
| 97 | + } else { |
| 98 | + // For debugging |
| 99 | + // Generates invalid xhtml with patterns that contain -- |
| 100 | + //$wgOut->addHtml( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" ); |
| 101 | + parent::doQuery( $offset, $limit, $shownavigation ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Override to squash the ORDER BY. |
| 107 | + * We do a truncated index search, so the optimizer won't trust |
| 108 | + * it as good enough for optimizing sort. The implicit ordering |
| 109 | + * from the scan will usually do well enough for our needs. |
| 110 | + */ |
| 111 | + function getOrder() { |
| 112 | + return ''; |
| 113 | + } |
| 114 | +} |
Property changes on: trunk/extensions/LinkSearch/LinkSearchPage.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 115 | + native |
Index: trunk/extensions/LinkSearch/LinkSearch_body.php |
— | — | @@ -65,115 +65,3 @@ |
66 | 66 | } |
67 | 67 | } |
68 | 68 | } |
69 | | - |
70 | | -class LinkSearchPage extends QueryPage { |
71 | | - |
72 | | - function __construct( $query, $ns, $prot ) { |
73 | | - $this->mQuery = $query; |
74 | | - $this->mNs = $ns; |
75 | | - $this->mProt = $prot; |
76 | | - } |
77 | | - |
78 | | - function getName() { |
79 | | - return 'LinkSearch'; |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Disable RSS/Atom feeds |
84 | | - */ |
85 | | - function isSyndicated() { |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Return an appropriately formatted LIKE query and the clause |
91 | | - */ |
92 | | - static function mungeQuery( $query , $prot ) { |
93 | | - $field = 'el_index'; |
94 | | - $rv = LinkFilter::makeLike( $query , $prot ); |
95 | | - if ($rv === false) { |
96 | | - //makeLike doesn't handle wildcard in IP, so we'll have to munge here. |
97 | | - if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) { |
98 | | - $rv = $prot . rtrim($query, " \t*") . '%'; |
99 | | - $field = 'el_to'; |
100 | | - } |
101 | | - } |
102 | | - return array( $rv, $field ); |
103 | | - } |
104 | | - |
105 | | - function linkParameters() { |
106 | | - global $wgMiserMode; |
107 | | - $params = array(); |
108 | | - $params['target'] = $this->mProt . $this->mQuery; |
109 | | - if( isset( $this->mNs ) && !$wgMiserMode ) { |
110 | | - $params['namespace'] = $this->mNs; |
111 | | - } |
112 | | - return $params; |
113 | | - } |
114 | | - |
115 | | - function getSQL() { |
116 | | - global $wgMiserMode; |
117 | | - $dbr = wfGetDB( DB_SLAVE ); |
118 | | - $page = $dbr->tableName( 'page' ); |
119 | | - $externallinks = $dbr->tableName( 'externallinks' ); |
120 | | - |
121 | | - /* strip everything past first wildcard, so that index-based-only lookup would be done */ |
122 | | - list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt ); |
123 | | - $stripped = substr($munged,0,strpos($munged,'%')+1); |
124 | | - $encSearch = $dbr->addQuotes( $stripped ); |
125 | | - |
126 | | - $encSQL = ''; |
127 | | - if ( isset ($this->mNs) && !$wgMiserMode ) |
128 | | - $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs ); |
129 | | - |
130 | | - $use_index = $dbr->useIndexClause( $clause ); |
131 | | - return |
132 | | - "SELECT |
133 | | - page_namespace AS namespace, |
134 | | - page_title AS title, |
135 | | - el_index AS value, |
136 | | - el_to AS url |
137 | | - FROM |
138 | | - $page, |
139 | | - $externallinks $use_index |
140 | | - WHERE |
141 | | - page_id=el_from |
142 | | - AND $clause LIKE $encSearch |
143 | | - $encSQL"; |
144 | | - } |
145 | | - |
146 | | - function formatResult( $skin, $result ) { |
147 | | - $title = Title::makeTitle( $result->namespace, $result->title ); |
148 | | - $url = $result->url; |
149 | | - $pageLink = $skin->makeKnownLinkObj( $title ); |
150 | | - $urlLink = $skin->makeExternalLink( $url, $url ); |
151 | | - |
152 | | - return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink ); |
153 | | - } |
154 | | - |
155 | | - /** |
156 | | - * Override to check query validity. |
157 | | - */ |
158 | | - function doQuery( $offset, $limit, $shownavigation=true ) { |
159 | | - global $wgOut; |
160 | | - list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt ); |
161 | | - if( $this->mMungedQuery === false ) { |
162 | | - $wgOut->addWikiText( wfMsg( 'linksearch-error' ) ); |
163 | | - } else { |
164 | | - // For debugging |
165 | | - // Generates invalid xhtml with patterns that contain -- |
166 | | - //$wgOut->addHtml( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" ); |
167 | | - parent::doQuery( $offset, $limit, $shownavigation ); |
168 | | - } |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * Override to squash the ORDER BY. |
173 | | - * We do a truncated index search, so the optimizer won't trust |
174 | | - * it as good enough for optimizing sort. The implicit ordering |
175 | | - * from the scan will usually do well enough for our needs. |
176 | | - */ |
177 | | - function getOrder() { |
178 | | - return ''; |
179 | | - } |
180 | | -} |
Index: trunk/extensions/LinkSearch/LinkSearch.php |
— | — | @@ -21,5 +21,5 @@ |
22 | 22 | |
23 | 23 | $wgSpecialPages['LinkSearch'] = 'LinkSearchSpecialPage'; |
24 | 24 | $wgSpecialPageGroups['LinkSearch'] = 'redirects'; |
25 | | -$wgAutoloadClasses['LinkSearchPage'] = $dir . 'LinkSearch_body.php'; |
| 25 | +$wgAutoloadClasses['LinkSearchPage'] = $dir . 'LinkSearchPage.php'; |
26 | 26 | $wgAutoloadClasses['LinkSearchSpecialPage'] = $dir . 'LinkSearch_body.php'; |