Topic on Project:Support desk

API: Section Number From Name?

3
Summary by Johnywhy

Solution works on 1.30. Some issues on 1.31.

Make a template that converts section-name to section-id number:

  1. Install Extension:External_Data.
  2. Create a page called Template:SectionID.
  3. Paste in the following code:
    Params 
    
    |1=PageName
    
    |2=SectionName
    
    <includeonly><onlyinclude>{{#get_web_data: 
    url={{SERVER}}/api.php?format=json&action=parse&page={{{1}}}&prop=sections
    |format=JSON
    |data=index=index
    |filters=line={{{2}}}
    }}{{#external_value:index}}</onlyinclude></includeonly>
    
  4. Call it with the following transclusion:
    {{SectionID|PageName|SectionName}}
    Eg.,
    {{SectionID|About Bears|Mating Habits}}
    

This will return the integer index number of the section.

Johnywhy (talkcontribs)

is there a way to obtain the section number of a section, if I know it's name?

My goal is to use API edit action on an existing section, but the edit action requires a section number.

API:Edit#Editing pages

Section name is allowed only for new sections. (i wish name was allowed for existing sections too).

Alternatively, is there a method to edit a section by name directly?

Might be relevant: https://m.mediawiki.org/wiki/Special:MyLanguage/API:Query#/talk/21

Bawolff (talkcontribs)
Johnywhy (talkcontribs)

Thanks for that. Then, i'd have to parse the returned data-- search for the section-name i seek, then reference its section number.

Could be done with #get_web_data, and the Extension:External_Data. But native functionality is preferred over using an extension.

Is there a filter available for the parse action? Doesn't appear to be (should be :)

Or is there an API function to query a single value from json?